Results 1 to 10 of 10
  1. #1
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default Generate Random Number Loop

    Hello, thank you for visiting this thread..

    I have a problem on my board game..

    First I want to generate 2 random numbers, those 2 random numbers will become the coordinates of the board and then after that It will check if the string on the button is not enemy, if the button is not enemy it will generate random numbers again and checked again..

    this is the piece of the code..

    Java Code:
    do{
    
    int randomIntX = randomX.nextInt(7);
     int randomIntY = randomY.nextInt(8);
                
     }
    while(Board[randomIntX][randomIntY].getText()!= "enemy");
    the [randomIntX][randomIntY] have errors too.. I dont know how to fix it.

    And how do i check if the string on the button is not "enemy" those code give me errors..

    Could you please help me.. thanks!
    Last edited by saber210; 08-11-2011 at 05:43 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    What are the errors?

    And don't use == or != on String Objects. Use the .equals() method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    abhilasha is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by saber210 View Post
    Hello, thank you for visiting this thread..

    I have a problem on my board game..

    First I want to generate 2 random numbers, those 2 random numbers will become the coordinates of the board and then after that It will check if the string on the button is not enemy, if the button is not enemy it will generate random numbers again and checked again..

    this is the piece of the code..

    Java Code:
    do{
    
    int randomIntX = randomX.nextInt(7);
     int randomIntY = randomY.nextInt(8);
                
     }
    while(Board[randomIntX][randomIntY].getText()!= "enemy");
    the [randomIntX][randomIntY] have errors too.. I dont know how to fix it.

    And how do i check if the string on the button is not "enemy" those code give me errors..

    Could you please help me.. thanks!

    since it appears to me tht ur comparing strings

    instead of
    while(Board[randomIntX][randomIntY].getText()!= "enemy")


    use
    while(! Board[randomIntX][randomIntY].getText().equals("enemy"))
    Last edited by abhilasha; 08-11-2011 at 06:21 PM.

  4. #4
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    the first error is cannot find the variable randomIntX and randomIntY..


    if(!YourTurn)
    {
    do{

    int randomIntX = randomX.nextInt(100);
    int randomIntY = randomY.nextInt(100);

    }
    while(!Board[randomIntX][randomIntY].getText("enemy"));

    }
    Basically, I want to generate 2 random numbers when not users turn.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your problem is that you have declared the variables inside the loop and trying to use them outside the loop. The scope of the variables is limited to the enclosing braces { }.

  6. #6
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    If i put it outside how can i fix that it will still produce random numbers while the condition on the loop are met?

    Thanks!

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You declare the variables outside the loop but still generate and assign the random numbers to the variables inside the loop.

  8. #8
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    I got it now,, Thanks.. I use return.. to return the if the condition is met..

    Another question.. I have strings and integers on my button.. How can i check if there is integer or string on the button?
    Last edited by saber210; 08-12-2011 at 02:33 AM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Are you serious?
    Java Code:
    declare variables
    loop {
         assign values to variables
    }

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    Quote Originally Posted by saber210 View Post
    I got it now,, Thanks.. I use return.. to return the if the condition is met..

    Another question.. I have strings and integers on my button.. How can i check if there is integer or string on the button?
    Have you looked at the API for String, Character, or Integer? They all contain useful functions for you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: 10-24-2010, 10:24 PM
  2. Replies: 3
    Last Post: 04-16-2009, 01:42 AM
  3. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 PM

Posting Permissions

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