File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Button number game problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Game Development
Bookmark "Button number game problem" Watch "Button number game problem" New topic
Author

Button number game problem

Damir Cedic
Greenhorn

Joined: Oct 16, 2012
Posts: 6
I'm trying to make a click game. In the game, player must click at random placed numbers from 1 to 36(table 6x6). If player get wrong game will restart, if it's presed ok number will become X. On 1st click = No1, this click start the timer and when player click No36 stop the timer. This is the 1st frame, on second frame game display the time from 1 to 36 click. I tryed to made this game but I have a problem with aray of numbers. How to define that player must click from 1 to 36 by arow, and if get wrong restart game.
For now I have a frame with 36 buttons, timer, action listener, but I don't have an idea how to solve the problem.
Thanks in advance
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

honestly...my advice would be to start over.

you should never write a line of code until you have thoroughly analyzed the problem, understand how to do everything, and thought through the best ways to do it. Writing code before figuring out all that will cause you no end of trouble.

So...forget about java for a while. figure out, in English (or any natural language you prefer) how YOU would do it. better yet, write down the steps as if you were telling a 8-yr old child how to do it. Once you have that done, writing the code usually follows pretty easy. If it doesn't, odds are you haven't refined your steps well enough to really explain what to do.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Damir Cedic
Greenhorn

Joined: Oct 16, 2012
Posts: 6
Thanks, for answer. I'll try again. I am newbie in Java, and I'm trying to understand possibilities in resolving this problem.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

don't get me wrong...writing code is the best way to learn. Fred Brooks wrote in his book "The Mythical Man-Month" that you should "plan on writing one to throw away".

What he meant was that no matter how much planning you do ahead of time, when you really get down into it, when you actually start writing the code, you'll find all kinds of things you forgot to consider before you began, and will be hard to shoe-horn into what you have written to that point.

So don't delete what you wrote...just move it aside, reference it as you write you new code, and learn from what you've already done.
Damir Cedic
Greenhorn

Joined: Oct 16, 2012
Posts: 6
Please Help. I made some changes in game. Now player have a frame with shuffled 36 numbers. Player must click on all 36 buttons from 1 to 36, and if get wrong buttons shuffled again. If player do as it must after the 36th click joptionpane give a massage of time between 1 and 36 click, that's all. After many corrections I made on game I am again on begining. Here is my code where I was stuck.



I can't get good shuffle collection for buttons with string on it, and than use action listener for string on button, then make array for if else, and that array compare with array of 1-36 and put else with shuffle again.
Can somebody give me a good example I have no time anymore. Must send for a 2 days.
Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

I am not a GUI expert...but...i THINK your problem in in lines 35-40.

you create a new button with a label on 35, and assign it to your "button" reference. So button is pointing to a single object.

You then shuffle that one object, and then add it to your pane.


I think what you need to do is something like

Damir Cedic
Greenhorn

Joined: Oct 16, 2012
Posts: 6
Thanks Fred, but that doesn't help me. I'm stil looking for solution, in the first line about shuffle. Then everything else.

In this solution NetBeans says me this: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 36

Thanks,

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

Where is "the first line about the shuffle"? the code you posted compiles and runs.

An ArrayOutOfBounds exception is just what you'd think it is, based on its name. Java is telling you that you are trying to access the 37th element (index 36), but your array doesn't have that many elements.

I can't see where that would be happening, based on your code. Further, the error message tells you EXACTLY what line the error is occurring on.

So, without the actual code you are trying to run, and without the FULL error message, there is nothing I can do.
Damir Cedic
Greenhorn

Joined: Oct 16, 2012
Posts: 6
Here is my code, and finally it works. I got random numbers, and action listener can recognize it. Now I need a help about actionlistener. How define the aray from 1-36, and if else restart JFrame? I looked for some sort of button++ solution, but I am green in Java for that. If someone know this kinde of solution I'll appreciate that.
Thanks in advance
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Here is my code, and finally it works.

it's a bit rough, but it does display what you want.

seems this may be a learning exercise for others, so here's a working alternate version.
it's also a bit rough, but it might handle your latest query



[edit] typo fix
Damir Cedic
Greenhorn

Joined: Oct 16, 2012
Posts: 6
Thank you very much, Michael. I got a clear view, now.
 
GeeCON Prague 2014
 
subject: Button number game problem