dimanche 1 janvier 2017

Java Game on Random Number [on hold]

I need assistance from anyone who can fix my code. Basically its a program to simulate two computer players playing a simple game of sticks.

This is how the program run:

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.

But this is how my program run: 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.

This my code: Main

public class StickGameApp {

/**
 * @param args the command line arguments
 */
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 ");
        }
    }

}

}

And this is 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;
}

}




Aucun commentaire:

Enregistrer un commentaire