RalphOrDie RalphOrDie - 1 year ago 52
Java Question

Can't figure out how to reset an int so I can use my loop properly making the game of Pig

When it is the computer's turn I need it to keep going until it reaches 35 points for the current turn but I am not sure how to reset it back to 0 for a new turn so every time after the computer reaches 35 points it will only go once.

import java.util.Scanner;
import java.util.Random;

public class PigGamev1
{
public static void main(String[] args)
{
int humanScore = 0;
int computerScore = 0;
int win = 100;
int die1 = 0;
int die2 = 0;
int total = 0;
boolean gameOver = false;
boolean turnOver = false;
char repeat = 'r';
String input;
Scanner keyboard = new Scanner(System.in);
Random randomGenerator = new Random();


do
{

//human rolls dice
do
{
die1 = randomGenerator.nextInt(6) + 1;
die2 = randomGenerator.nextInt(6) + 1;
total = die1 + die2;

if(total == 2)
{
humanScore += 0;
System.out.println("You rolled a: " + total);
System.out.println("Turn over.");
System.out.println("Your current score is " + humanScore);
turnOver = true;
}
else if(humanScore <= win && total != 2)
{
humanScore += total;
System.out.println("You rolled a: " + total);
if(humanScore >= win)
{
System.out.println("Your score is " + humanScore + " and my score is "
+ computerScore);
System.out.println("You win!");
turnOver = true;
gameOver = true;
System.exit(0);
}
System.out.println("Your current score is " + humanScore);
System.out.println("Enter r to roll again or h to hold.");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 'r')
{
turnOver=false;
}
else if(repeat == 'h')
{
System.out.println("Turn over.");
System.out.println("Your current score is " + humanScore +
" total computer score is " + computerScore + "\n");
turnOver = true;
}
}
}while(turnOver == false);

//computer rolls dice
System.out.println("My turn! \n");

do
{
turnOver = false;
die1 = randomGenerator.nextInt(6) + 1;
die2 = randomGenerator.nextInt(6) + 1;
total = die1 + die2;
System.out.println("I rolled a: " + total);

if(total == 2)
{
computerScore += 0;
System.out.println("Turn over.");
System.out.println("My current score is " + computerScore + "\n");
turnOver = true;
}

else
{
computerScore += total;
System.out.println("My current score is " + computerScore + "\n");
if(computerScore >= 35)
{
System.out.println("I hold.");
System.out.println("Your turn!\n");
System.out.println("My current score is " + computerScore +
" your score is " + humanScore + "\n");
turnOver = true;
}
}

if(computerScore >= win)
{
System.out.println("I win!");
turnOver = true;
gameOver = true;
System.exit(0);
}


}while(turnOver == false);

}while(gameOver == false);
keyboard.close();
}
}

Answer Source

Use different variables to keep track of the total score for the computer and the score it has on their current turn. For instance, you could define int computerTurn; with the rest of your variables, and use it as such in your program, remembering to reset it to 0 when the turn ends:

//some code omitted if not relevant to your problem
do
{
    if(total == 2)
    {
        computerScore += computerTurn;  //adds current turn to running total
        System.out.println("Turn over.");
        System.out.println("My current score is " + computerScore + "\n");
        turnOver = true;
    }

    else
    {
        computerTurn+= total;
        System.out.println("My current score is " + computerScore + "\n");
        if(computerTurn >= 35) //checks against current turn instead of total
        {
            computerScore += computerTurn; //adds current turn to running total
            System.out.println("I hold.");
            System.out.println("Your turn!\n");
            System.out.println("My current score is " + computerScore + 
                " your score is " + humanScore + "\n");
            turnOver = true;
        }
    }
}while(turnOver == false);
computerTurn = 0;