This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Button number game problem

 
Damir Cedic
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damir Cedic
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Michael. I got a clear view, now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic