Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 63 64 [65] 66 67 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100624 times)

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #960 on: November 30, 2012, 11:20:55 am »

I see how that might work, since it looks nested. Will try tomorrow.
(I fully expect another set of template-related madness to occur, so stay tuned, folks! I think so because before I changed it to <,> from <><>, there was a big set of strange and alien errors xD
I feel templates are ruining my productivity @_@ I don't even expect to need to be compatiable with multiple types ....)
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #961 on: November 30, 2012, 11:56:20 am »

IMHO, you should only use templates in C++ so you can learn to avoid using them unless forced. I haven't written a templated class or function in 5 years.
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #962 on: November 30, 2012, 12:58:01 pm »

Templates are used heavily in boost and the C++ standard library, so it's important you are comfortable using them. They're also invaluable for writing any sort of container class or generic algorithm. Other than that, try not to overgeneralize your code, especially if you're only programming for yourself.
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #963 on: November 30, 2012, 02:22:16 pm »

I agree, especially in c++ generalization only leads to messy code and dynamic_casts, and even the libraries are against you.

like the c++ iterators: you don't have one generic iterator, because the c++ idea is that you should think and decide your data structure beforehand. Java starts from different premises, and should be used in different ways, but c++....
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #964 on: December 02, 2012, 12:47:11 am »

So C++ 'kinda' has a generic iterator through operator overloading. For example, std::for_each is implemented through templates by using the !=, ++(prefix), and *(prefix) operators of the iterator which you put in. Notice that pointers and the iterators in the standard library are all implemented in this way. Notice that you call ++iterator to increment an iterator or ++pointer to increment a pointer, you check iterator != container.end() or pointer != arrayEnd to check for bounds, and use *iterator or *pointer to dereference what you're pointing at.

Because of these conventions C++ has a "generic" iterator. An important thing to note is that templates in C++ are instantiated at compile-time, and can be thought of as shorthand for writing code for a different class for every template type that you use. So, list<int> and list<double>, when you use them in your code, are actually compiled as literal instructions for a list class that handles ints and a list class that handles doubles.

Note that C libraries are not designed with templates in mind (for the most part), so there can be some kludging when you're using C libraries. However, well-designed C++ libraries tend to be very good at using templates (again, see the boost libraries for an example).
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #965 on: December 02, 2012, 02:20:42 am »

Forgot to post the solution for my problem. If anyone is interested, it was because I implemented the overloaded operators incorrectly. I (i) didn't follow the rule of three, (ii) put the operator in the wrong scope(class?), and most importantly (iii) accidentally defined a binary operator like a unary one xD
I tidied it all up now, and fixed a pesky divide-by-zero problem, so I'm all ready to go implement Part Two of this project, making a graphic user interface for all this. The first issue I noticed is that SDL can't draw lines (gotta use a line algorithm @_@) and the second is SDL don't do text either (SDL_tt or something should help).
Also, the program has to have Korean, so now I must learn how to unicode Dx
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: Programming Help Thread (For Dummies)
« Reply #966 on: December 02, 2012, 07:31:32 am »

C++11 introduces a build in foreach construct, which looks a lot like Java's (I personally prefer C#s way but whatever)

Example:
Code: [Select]
std::vector<int> collection = { 15, 23, 542 };
for(int i : collection)
    std::cout << i << std::endl;

I've never been in a situation where I've needed dynamic_cast, and only ever use the typeid aspect of RTTI behind templated functions that completely hide the RTTI from the users of those functions and classes. Though that does introduce a few static_pointer_cast from std::shared_ptr<void> to some other class (which I can reasonably be sure to be safe thanks to the afore-mentioned RTTI).
« Last Edit: December 03, 2012, 12:59:54 pm by MorleyDev »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #967 on: December 02, 2012, 07:27:23 pm »

So C++ 'kinda' has a generic iterator through operator overloading.

Note that you still have to recompile every client if you change the implementation, so template can pick up the change.

btw, found an interesting essay on c++ and generic functions:
http://www.artima.com/cppsource/type_erasure.html
Logged

Johuotar

  • Bay Watcher
    • View Profile
    • Some game projects
Re: Programming Help Thread (For Dummies)
« Reply #968 on: December 08, 2012, 12:39:21 pm »

Code: [Select]
        float[] values = {3.7f, 4.2f, 5.0f, 2.3f, 0.0f, 4.1f, 1.5f, 0.5f, 3.5f, 2.8f,
                            0.0f, 1.4f, 3.3f, 0.4f, 2.6f, 4.5f, 4.8f, 2.9f, 3.0f, 3.4f};
I need to get the mean average of these when button is pressed for C# school program, but all the stuff I googled for seemed unhelpful.  ???
Logged
[img height=x width=y]http://LINK TO IMAGE HERE[/img]
The Toad hops in mysterious ways.
This pure mountain spring water is indispensable. Literally. I'm out of paper cups.

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #969 on: December 08, 2012, 02:44:56 pm »

What exactly is the problem? The GUI, the calculation, or how to interface the two?
Logged

Johuotar

  • Bay Watcher
    • View Profile
    • Some game projects
Re: Programming Help Thread (For Dummies)
« Reply #970 on: December 08, 2012, 04:01:25 pm »

The calculation itself, Im using visual studio so no problem with GUI.
Logged
[img height=x width=y]http://LINK TO IMAGE HERE[/img]
The Toad hops in mysterious ways.
This pure mountain spring water is indispensable. Literally. I'm out of paper cups.

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #971 on: December 08, 2012, 09:57:55 pm »

Not sure how much pointing you need[1], so I'll just stick to some generic pseudocode for you to convert how you wish.

Code: [Select]
sub CalculateAverage(query:type array) {
  //either//
  length=arraylength(query); total=0;
  foreach element in (query) do {
    total+=element;
  } // END foreach (or any other form of loop through) for the query array
  return total/length;

  //or//
  length=0; total=0
  while (arraylength(query)>0) {
    element = popelement(query) // or shiftelement() or otherwise remove an element
    if isvalidnumber(element) { // optional check that you might want to make
      total+=element; length++;
   } // END optional check
  } // END while query array has elements (i.e. specifically a value-shaving loop)
  return total/length;
}

I reckon you can have fun working out what C# gives you to accomplish that (to be honest, I'm not entirely sure myself, been a while since I've touched that, although I'm sure there's a maths #include-type thing that just gives you an Average function, but obviously you're being asked to implement it yourself), and perhaps you can even find a neater (but still longhand) method.  Note that I've given two looping-methods and two length-finding methods, and only didn't give multiple totalling methods because I couldn't think of any more exciting ways of doing it that would have made much sense.  However, you can mix and match what I've given to your satisfaction, I'm sure.


[1] I'm assuming you already know what the 'mean' is,
Logged

Knight of Fools

  • Bay Watcher
  • From Start to Beginning
    • View Profile
    • Knight of Fools
Re: Programming Help Thread (For Dummies)
« Reply #972 on: December 09, 2012, 09:15:53 am »

Hokay, I'm having a heck of a time with what's supposedly a simple stopwatch Java applet.

Here's what I've got so far, but it seems intent on running a "java.lang.reflect.InvocationTargetException" every time I try to run it. I have no clue what that means or how to fix it. I just want the dang time to start when the user clicks, and to stop when the user clicks again.

Code: [Select]
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Stopwatch extends JLabel
{
private long startTime;
private boolean running;
private Timer timer;

public void init()
{
addMouseListener(new MyMouseListener());
}

public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.drawString("Click to start the timer!", 200, 200);
}

public void actionPerformed(ActionEvent event)
{
long time = (System.currentTimeMillis() - startTime) / 1000;
setText(time + " Seconds have Passed");
}

private class MyMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
if (running == false)
{
running = true;
startTime = e.getWhen();
setText("0 Seconds have Passed");
}
else
{
timer.stop();
running = false;
long endTime = e.getWhen();
double seconds = (endTime - startTime) / 1000;
setText(seconds + "Seconds Passed");
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

}
}

I'm still fiddling with it to see if I can figure it out, but it seems to happen whether or not I comment out everything under the mouseClicked and actionPerformed events. I'm really starting to dislike working with Applets.
Logged
Proud Member of the Zombie Horse Executioner Squad. "This Horse ain't quite dead yet."

I don't have a British accent, but I still did a YouTube.

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #973 on: December 09, 2012, 09:42:44 am »

What is the InvocationTargetException cause?

I guess the applet misses some permissions, and it should say in the full stack trace
Logged

Knight of Fools

  • Bay Watcher
  • From Start to Beginning
    • View Profile
    • Knight of Fools
Re: Programming Help Thread (For Dummies)
« Reply #974 on: December 09, 2012, 10:50:20 am »

It's a runtime error. It compiles just fine, but when I go into the browser to run it, that's when it gives me the InvocationTargetException. Clicking on "details" gives me even less information. I'm too newb to even know what to do at this point.
Logged
Proud Member of the Zombie Horse Executioner Squad. "This Horse ain't quite dead yet."

I don't have a British accent, but I still did a YouTube.
Pages: 1 ... 63 64 [65] 66 67 ... 91