Results 1 to 6 of 6
  1. #1
    Benaka is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Smile how do i make a restart button for my game? pls help im confused

    Java Code:
    private int getUniqueRandomNumber()
       {
          Random generator = new Random();
       
          int randomNumber;
          
    
          // generate random numbers until unused flag is found
          do
          {
             // generate a number between 0-9
             randomNumber = generator.nextInt( 10 );
          }
          while ( Usednumber[ randomNumber ] == true );
          
          // indicate that flag has been used
          Usednumber[ randomNumber ] = true;
             
          return randomNumber;
          
       } // end method getUniqueRandomNumber
    
    private void resetgetUniqueRandomNumber()
    {
      /**how do i reset getUniqueRandomNumber() to its original state,from here?, im trying to make a restart button so that everytime i clicked that button it would reset everything and starts a new game*/
    }
    
     // restarts the game
       private void restartJButtonActionPerformed( ActionEvent event)
       	{
                what do i place here exactly?
       	}
    Last edited by Fubarable; 09-26-2010 at 12:42 PM. Reason: Moderator Edit: Code tags added

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    The cheapest way would be to reset your Usednumber array to a new array of booleans somewhere (please don't start variable names with an upper case letter).

    kind regards,

    Jos

  3. #3
    Benaka is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    okay... but how do you restart the array to a new array of booleans. i can picture the possibilities but im not so sure. im thinking it like this. sorry for bad English


    usednumberarray()

    resetusednumberarray()
    restart usednumberarray;

    newgameJbutton()
    {
    usednumberarray()
    }



    restartJbutton()
    {
    resetusednumberarray();
    }

    is this valid?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Benaka View Post
    okay... but how do you restart the array to a new array of booleans. i can picture the possibilities but im not so sure. im thinking it like this. sorry for bad English


    usednumberarray()

    resetusednumberarray()
    restart usednumberarray;

    newgameJbutton()
    {
    usednumberarray()
    }



    restartJbutton()
    {
    resetusednumberarray();
    }

    is this valid?
    Why no simply do:

    Java Code:
    usednumberarray= new boolean[10];
    The new array has all its elements set to false.

    kind regards,

    Jos

  5. #5
    Benaka is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    thank you very much. im half way finished with my code.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Benaka View Post
    thank you very much. im half way finished with my code.
    Good; here's a little tip: set up your game class in a modular way; e.g.

    Java Code:
    public class Game {
       public Game( ... ) {
          initGame( ... );
          initGui( ... );
       }
       public void playGame() {
          ...
       }
       private void initGame( ... ) {
          // init your array here ...
       }
       ...
    }
    From your actionPerformed( ... ) method all you have to do is call the initGame( ... ) method that initializes your array (and probably other things too). Better create a lot of small methods instead of one big mess of a single 'god method'.

    kind regards,

    Jos

Similar Threads

  1. Help to make a game ???
    By Peter20 in forum Java Applets
    Replies: 3
    Last Post: 05-10-2010, 05:36 AM
  2. Level Restart Button
    By Curtiz in forum AWT / Swing
    Replies: 4
    Last Post: 04-08-2010, 09:04 PM
  3. Replies: 6
    Last Post: 11-04-2009, 09:41 PM
  4. Make application restart itself
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 03-22-2009, 02:49 AM
  5. The words *game* in java programming :confused:
    By ibmzz in forum Advanced Java
    Replies: 1
    Last Post: 01-23-2008, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •