• Post Reply Bookmark Topic Watch Topic
  • New Topic

tortoise and hare race java applet not working properly?  RSS feed

 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble simulating a race between two competitors. This is your typical race program where you use a random number generator to determine what "moves" the competitors use. As seen in my code below, the track is composed of 50 rectangles, and the filled in rectangle shows the location of each competitor on the track. Some "moves" make the competitor jump 9 squares to the right, or 2 squares back, for example. When I run the applet, only the initial starting position is displayed; the applet doesn't work. I realize it's a lot of code, but what do I need to do to fix my problem? I'm really stuck at this point. Any help is appreciated.

 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should try the display on its own. Write a little class which simply displays the two squares for their locations, and run that with constant speed, ie each of them moving one square per second. Get that working, and the rest will be easier
Also, you haven't convinced me that you are using object-oriented programming. I think you should have a Hare object and a Tortoise object, each maintaining its own location, and implementing its own motion.. Something like this:that is obviously only a starting-point, which will guarantee a dead heat every time. You can work from there.
 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I know my way is probably not the easiest or most effective way to do things, but I'm still curious as to why the applet does not run with the tools I've used in my code. Is there a problem with the way I'm trying to display things? If so, what is it? My applet is stuck in the initial starting position and I'm trying to debug that first.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, but I do know things are much easier to design and create if you go bit by bit. Don't write more than five lines without compiling and running the application. If you try to create a large application all in one fell swoop, you won't know what is happening.
 
Harold Lime
Ranch Hand
Posts: 38
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code appears to throw a NullPointerException on by line 105 (which, IMO, should be split over two lines).

Try adding some System.out.println() statements so you can see where your code gets to - I think you'll notice that something important is never happening.
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys

Im always keen on getting some new excercises, and so i tried to solve this one too. Heres
my approach, let me know what you think. I tried to get it done in a OO-design.

Class "Main"


Class "Racer"


race.jpg
[Thumbnail for race.jpg]
Screenshot of the Race-output
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!