• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Button number game problem

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Damson Chedich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Damson Chedich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Damson Chedich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Damson Chedich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Damson Chedich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Michael. I got a clear view, now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic