Hakim Bajuri
Hakim Bajuri

Reputation: 61

Java Game using Random Numbers

I have a java program where the computer will decide which player goes first and pick random number to remove from an array. As the player remove the number, the array will decrease. All of this will be done using random. But when I run my code, its not what I expect it to be. May I know where did I went wrong?

This is my code:

[The Class]

public class StickBag {

    private int numOfSticks;

    public StickBag(int numOfSticks)
    {
        this.numOfSticks = numOfSticks;
    }

    public int getNumOfSticks() {
        return numOfSticks;
    }

    public void setNumOfSticks(int numOfSticks) {
        this.numOfSticks = numOfSticks;
    }

    public int remove(int n)
    {
        numOfSticks = numOfSticks - n;
        return numOfSticks;
    }
}

[The Main]

public class StickGameApp {

    public static void main(String[] args) {

        StickBag s1 = new StickBag(25);

        System.out.println("Welcome to the game of sticks!");
        System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board.");

        int minP = 1;
        int maxP = 2;
        int randP;

        int minN = 1;
        int maxN = 10;
        int randNum;

        randP = minP + (int)(Math.random()*(maxP));
        randNum = minN + (int)(Math.random()*(maxN));

        for (int i=0; i<10; i++)
        {
            if(s1.getNumOfSticks() > randNum)
            {
            System.out.println("Computer player " + randP + " choose " + randNum + " sticks ");
            s1.remove(randNum);
            }
            else
            {
                System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to.");
                System.out.println("Computer player " + randP + " loses ");
            }
        }

    }
}

When I run the code, it display as follows:


Welcome to the game of sticks!

There are initially 25 sticks on the board.

Computer player 2 choose 6 sticks

Computer player 2 choose 6 sticks

Computer player 2 choose 6 sticks

Computer player 2 choose 6 sticks

Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses

Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses

Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses

Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses

Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses

Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses


But I want it to display like this:


Welcome to the game of sticks!

There are initially 25 sticks on the board.

Computer Player 1 chooses 5 sticks.

Computer Player 2 chooses 7 sticks.

Computer Player 1 chooses 7 sticks.

Computer Player 2 wants to choose 7 sticks but is unable to.

Computer Player 2, you lose.


May I know where did I went wrong?

Upvotes: 1

Views: 1486

Answers (4)

user7340747
user7340747

Reputation:

I'm not certain if this is the correct approach to your game but I hope you find it helpful.

import java.util.*;

public class Main {

public static void main(String[] args) {

    Random rand = new Random();
    StickBag s1 = new StickBag(25);
    int total= s1.getNumOfSticks();

    System.out.println("Welcome to the game of sticks!");
    System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board.");

    int randP = 0;
    int randNum;
    int P1=0,P2=0;


    for (int i=0; i<10; i++)
    {

          randP = rand.nextInt(2)+1;
        randNum = rand.nextInt(10)+1;


        if (s1.getNumOfSticks() - randNum > 0)
        {
        System.out.println("\n Computer player #" + randP + "  chooses " + randNum + " sticks and takes them. ");
        s1.remove(randNum);

        System.out.println(" Computer player #" + (!(randP == 2) ? 2 : 1 ) + " wants to choose " + randNum + " sticks but is unable to.");

        // Evaluate the score
        if (randP == 1)
            randP = (P1 = P1 + randNum);
        else
            randP = (P2 = P2 + randNum); 
        }
    }

    System.out.println("\n\n---------------------------------------------------------------------------");
    System.out.println("\t Player #1 has " + P1 + " sticks and Player #2 has " + P2 + " sticks. \n"  + ( (P1 > P2) ? "Player 1 wins" : "Player 2 wins"));
    System.out.println("The sticks remaining in the bag are: " + s1.getNumOfSticks());}}

Upvotes: 0

Oghli
Oghli

Reputation: 2340

your mistake is simply you put the random number generator of player and computer randP and randNum before the for loop that running the game so the random number choosing will be executed only once.

your code should be :

for (int i=0; i<10; i++)
    {
     randP = minP + (int)(Math.random()*(maxP));
     randNum = minN + (int)(Math.random()*(maxN));
        if(s1.getNumOfSticks() > randNum)
        {
        System.out.println("Computer player " + randP + " choose " + randNum + " sticks ");
        s1.remove(randNum);
        }
        else
        {
            System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to.");
            System.out.println("Computer player " + randP + " loses ");
        }
    }

}

Upvotes: 1

npinti
npinti

Reputation: 52185

You will need to move these two lines of code:

randP = minP + (int)(Math.random()*(maxP));
randNum = minN + (int)(Math.random()*(maxN));

At the beginning of your for loop. This would ensure that at each iteration, you get to pick a new player and a new array location.

Upvotes: 0

Roberto Attias
Roberto Attias

Reputation: 1903

For one thing, you're selecting the.player.outside the.loop, so whatever is selected will always run all rounds

Upvotes: 0

Related Questions