GameContinue() not functioning. should loop back to beginning of main() - java

I am currently creating a pig game, and have most functions working.
I need the code to work where if player 1 decides to stop rolling, and player two decides to stop rolling, the game will loop back until one player reaches 20 points. Currently, the code ends after 1 run through.
import java.util.Random;
import java.util.Scanner;
public class Project3_Part1_Submit {
static int playerScore = 0;
static int playerTotal = 0;
static int dice = 0;
static final int FINAL_SCORE = 20;
static int playerTwoScore = 0;
static int playerTwoTotal = 0;
static boolean gameOver = false;
static boolean turnOver = false;
static char repeat;
static String input;
static Scanner key = new Scanner(System.in);
static Random rand = new Random();
public static void main(String[] args) {
PlayerOneTurn();
PlayerTwoTurn();
GameContinue();
if (playerTotal >= FINAL_SCORE) {
System.out.print("Current Score: Player 1 has " + playerTotal);
System.out.print(", Player 2 has " + playerTwoTotal);
System.out.println("");
System.out.println("Player 1 wins!");
}
if (playerTwoTotal >= FINAL_SCORE) {
System.out.print("Current Score: Player 1 has " + playerTotal);
System.out.print(", Player 2 has " + playerTwoTotal);
System.out.println("");
System.out.println("Player 2 wins!");
}
}
public static void PlayerOneTurn() {
while (turnOver == false) {
do {
System.out.print("Player 1 turn total is " + playerTotal); // welcome line
System.out.print(". Enter (r)oll or (s)top: ");
System.out.println("");
input = key.next(); // input = next key entered
repeat = input.charAt(0);
if (repeat == 'r') // if input letter = r
{
dice = rand.nextInt(3) + 1; // set dice to number between 1-6
System.out.println("Player 1 rolled: " + dice); // display number, signifying the amount rolled
if (dice == 1) // if the number rolled happens to be 1...
{
playerScore = 0; // reset score to 0
System.out.print("Player 1 lost their turn! ");
System.out.print("Player 1 total is " + playerTotal);
System.out.println("");
return;
}
else
{
playerScore += dice; // add dice amount to player score
System.out.print("Player 1 turn total is " + playerScore);// print out total amount earned
System.out.print(" Enter (r)oll or (s)top: "); // repeat question
System.out.println("");
input = key.next();
repeat = input.charAt(0);
if (playerScore >= 20) {
playerTotal = playerScore;
gameOver = true;
return;
}
}
}
else if (repeat == 's') // if neither option is called
{
return;
}
else
{
System.out.println("Incorrect entry, please try again"); // prompt retry
System.out.println("");
input = key.next();
}
}while(turnOver == false || dice != 1);
playerTotal += playerScore;
playerScore = 0;
if (playerTotal >= FINAL_SCORE) {
// System.out.println("YOU WIN!");
gameOver = true;
while (playerTotal >= FINAL_SCORE)
;
break;
}
}
}
public static void PlayerTwoTurn() {
System.out.println("success");
while (turnOver == false) {
do {
System.out.print("Player 2 turn total is " + playerTwoTotal); // welcome line
System.out.print(". Enter (r)oll or (s)top: ");
System.out.println("");
input = key.next(); // input = next key entered
repeat = input.charAt(0);
if (repeat == 'r') // if input letter = r
{
dice = rand.nextInt(3) + 1; // set dice to number between 1-6
System.out.println("Player 2 rolled: " + dice); // display number, signifying the amount rolled
System.out.println("");
if (dice == 1) // if the number rolled happens to be 1...
{
playerTwoScore = 0; // reset score to 0
System.out.print("Player 2 lost their turn! ");
System.out.print("Player 2 total is " + playerTwoTotal);
System.out.println("");
return;
}
else
{
playerTwoScore += dice; // add dice amount to player score
System.out.print("Player 2 turn total is " + playerTwoScore);// print out total amount earned
System.out.print(" Enter (r)oll or (s)top: "); // repeat question.
System.out.println("");
input = key.next();
repeat = input.charAt(0);
if (playerTwoScore >= 20) {
playerTwoTotal = playerTwoScore;
gameOver = true;
return;
}
}
}
else if (repeat == 's') // if neither option is called
{
turnOver = true;
return;
}
else
{
System.out.println("Incorrect entry, please try again"); // prompt retry
System.out.println("");
input = key.next();
}
}while(turnOver == false || dice != 1);
playerTwoTotal += playerTwoScore;
playerTwoScore = 0;
if (playerTwoTotal >= FINAL_SCORE) {
// System.out.println("YOU WIN!");
gameOver = true;
while (playerTwoTotal >= FINAL_SCORE)
;
break;
}
}
}
public static void GameContinue() {
if (playerTotal < 20 && playerTwoTotal < 20) { //if neither players totals or 20 or greater
PlayerOneTurn(); //go back to playerOne
}
else //if either one is 20 or greater
{
return; //go to main method and finish
}
}
}
My GameContinue() function is what i created to go loop back to PlayerOne.
public static void GameContinue() {
if (playerTotal < 20 && playerTwoTotal < 20) { //if neither players totals or 20 or greater
gameOver = false;
PlayerOneTurn(); //go back to playerOne
}
else //if either one is 20 or greater
{
return; //go to main method and finish
}
}
I know calling PlayerOneTurn(); will not loop it back to the start of the main method. I've tried calling all three methods again like this.
public static void GameContinue() {
if (playerTotal < 20 && playerTwoTotal < 20) { //if neither players totals or 20 or greater
gameOver = false;
PlayerOneTurn(); //go back to playerOne
PlayerTwoTurn();
GameContinue();
}
else //if either one is 20 or greater
{
return; //go to main method and finish
}
}
Obviously that just caused a butload of errors. How should i go about having my code continue to loop back until the if statement in GameContinue() is met?

As Ecto has commented, you could use a while loop in the main function. It is shown here:
public static void main(String[] args) {
while (GameContinue()) {
PlayerOneTurn();
PlayerTwoTurn();
}
if (playerTotal >= FINAL_SCORE) {
System.out.print("Current Score: Player 1 has " + playerTotal);
System.out.print(", Player 2 has " + playerTwoTotal);
System.out.println("");
System.out.println("Player 1 wins!");
}
if (playerTwoTotal >= FINAL_SCORE) {
System.out.print("Current Score: Player 1 has " + playerTotal);
System.out.print(", Player 2 has " + playerTwoTotal);
System.out.println("");
System.out.println("Player 2 wins!");
}
}
Now obviously this would be an error as GameContinue() is a void method. To combat this you can change GameContinue() to this:
public static boolean GameContinue() {
if (playerTotal < 20 && playerTwoTotal < 20) {
gameOver = false;
return true;
} else {
return false;
}
}
This would solve your problems and your code is minimally changed.

Related

When the loop comes to a continue statement at the end it doesn't restart from the top

Ok, so I have created the following program. It is not yet complete but when the code comes to the end (y or n part) and the user decides to try again they don't get the option to enter a new bet it just uses the one entered from the first time.
(Please comment if you need help about understanding the code or thinking it might be hard for other people to understand)
import java.util.*;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; //variable to cancel whole loop
char yesNo; //if user wants to continue playing or not
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
do { //loop to test if the bet is legit
if(money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while(bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n)");
yesNo = kin.next().charAt(0);
int loopBreak = 0; //to break do while loop bellow
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while(loopBreak == 0);
if(play == 0) {
break;
}
}
}
}
}
Its all about nested while loop don't stop when it needed. Its like this
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
...
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while(loopBreak == 0);
if(play == 0) {
break;
}
}
}
After you get answer 'y' you don't break the nested while and program never get the
while(money > 0 && play == 1) {
--> System.out.print("Please Enter The Amount You Want To Bet: ");
--> double bet = kin.nextDouble();
//because below while loop continues to loop
while((bet <= money || bet > 0)) {
...
}
}
part. Anyways corrected code is here(i tried every possibility and it worked)
import java.util.*;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; //variable to cancel whole loop
char yesNo; //if user wants to continue playing or not
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
do { //loop to test if the bet is legit
if(money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while(bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n): ");
yesNo = kin.next().charAt(0);
int loopBreak = 0; //to break do while loop bellow
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak++;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play--; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n': ");
}
} while(loopBreak == 0);
if(play == 0 || loopBreak == 1) {
break;
}
}
}
}
}
Have a good day!
Another solution is to use labeled breaks/continues https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
However it should be used sparingly (or not at all). You probably can simplify it in some other way.
package test.test;
import java.util.Scanner;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; // variable to cancel whole loop
char yesNo; // if user wants to continue playing or not
//Labeled <-----------------
start: while (money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while ((bet <= money || bet > 0)) {
do { // loop to test if the bet is legit
if (money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while (bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n)");
yesNo = kin.next().charAt(0);
int loopBreak = 0; // to break do while loop bellow
do {
if (yesNo == 'y') { // take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
//Labeled continue <-----------------
continue start;
} else if (yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; // to cancel whole program
break;
} else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while (loopBreak == 0);
if (play == 0) {
break;
}
}
}
}
}

why is my loop iterating and how do i resolve this?

My code is not stopping the iteration where i use the do while loop, i wanted to only iterate when the secondThrow is not the same as the firstThrow and not a number 7????? help
import java.util.Scanner;
public class Craps_callee
{
private Die die1;
private Die die2;
public Craps_callee() //
{
die1 = new Die ();
die2 = new Die ();
}
private void rollDice()
{
die1.roll();
die2.roll();
}
private int getSum()
{
int sum = die1.getNumber()+die2.getNumber();
return sum;
}
public void playRound()
{
rollDice();
int firstThrow = getSum();
int secondThrow = getSum();
//for (int i; i )
if(firstThrow == 7 || firstThrow == 11)
{
System.out.println("You rolled a " +firstThrow+ " You Win Congratulations!");
}
else if (firstThrow == 2 || firstThrow == 3 || firstThrow == 12)
{
System.out.println("You rolled a "+firstThrow+" You Lose! ");
}
else
{
System.out.println("You rolled a: "+firstThrow);
System.out.println(" !! Establish Point !!");
System.out.println(" you need to roll another "+firstThrow+" to win before 7 is rolled");
do
{
rollDice();
secondThrow = getSum();
System.out.println("SeconndThrow is: "+secondThrow);
System.out.println("FirstThrow is: "+firstThrow);
if (secondThrow == firstThrow)
{
System.out.println("You rolled a " +secondThrow+ " Twice. You Win Congratulations!");
}
else if (secondThrow == 7)
{
System.out.println("You rolled a 7 You Lose! ");
}
}
while (secondThrow != firstThrow || secondThrow != 7);
}
}
public static void main(String[] args)
{
String prompt_from_user;
Scanner scan = new Scanner(System.in);
System.out.println("Input the amount of "+scan);
prompt_from_user = scan.nextLine();
if (prompt_from_user == "YES" || prompt_from_user == "yes");
{
}
This is the part that's wrong
do
{
rollDice();
secondThrow = getSum();
System.out.println("SeconndThrow is: "+secondThrow);
System.out.println("FirstThrow is: "+firstThrow);
if (secondThrow == firstThrow)
{
System.out.println("You rolled a " +secondThrow+ " Twice. You Win Congratulations!");
}
else if (secondThrow == 7)
{
System.out.println("You rolled a 7 You Lose! ");
}
}
while (secondThrow != firstThrow || secondThrow != 7);
I wanted to only iterate when the > secondThrow is not the same as > > the firstThrow and not a number 7
Read the sentence and realise then that your condition should implement an AND instead of an OR

NimGame issue please assist

I am having an issue with my Nim Game running infinitely on the third option of my method. The code runs the game and presents the winner but it runs infinitely. The third method is used in this code to run the game many different times and record how many times each computer has won.
import java.util.Scanner;
public class NimGame
{
public static void main(String[] args)
{
int computerMove = 0;
int computer2Move = 0;
int userMove = 0;
int elementsRemaining = 0;
int take = 0;
int mode;
int times;
int initial = 0;
int comp1wins = 0;
int comp2wins = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Which mode would you like to run the game?");
System.out.println("1) Player Mode ");
System.out.println("2) AI vs AI Single Mode");
System.out.println("3) AI vs AI building mode");
mode = sc.nextInt();
if(mode==1)
{
System.out.println();
System.out.println();
System.out.println("Enter number of elements to start.");
elementsRemaining = sc.nextInt();
System.out.println("How many elements can you take at a time?");
take = sc.nextInt();
while(elementsRemaining > 0)
{
computerMove = getComputerMove(elementsRemaining, take);
System.out.println("Computer takes "+computerMove);
elementsRemaining -= computerMove;
System.out.println("Now there are "+elementsRemaining+" elements remaning.");
System.out.println();
if(elementsRemaining <= 0)
{
System.out.println("Computer wins!");
return;
}
System.out.println("How many elements do you want to remove? (1 to " + take + ")");
userMove = sc.nextInt();
while((userMove <1) || (userMove > take))
{
System.out.println("Taking " + userMove + " elements is not allowed, please choose from 1 to " + take + ".");
System.out.println();
userMove = sc.nextInt();
}
elementsRemaining -= userMove;
System.out.println("Now there are " + elementsRemaining + " elements remaining.");
System.out.println();
if(elementsRemaining <= 0)
{
System.out.println("You win!");
System.out.println();
return;
}
}
}
else if(mode==2)
{
System.out.println();
System.out.println();
System.out.println("Enter number of elements to start.");
elementsRemaining = sc.nextInt();
System.out.println("How many elements can you take at a time?");
take = sc.nextInt();
while(elementsRemaining > 0)
{
computerMove = getComputerMove(elementsRemaining, take);
System.out.println("Computer takes "+computerMove);
elementsRemaining -= computerMove;
System.out.println("Now there are "+elementsRemaining+" elements remaning.");
System.out.println();
if(elementsRemaining <= 0)
{
System.out.println("Computer 1 wins!");
return;
}
computer2Move = getComputer2Move(elementsRemaining, take);
System.out.println("Computer takes "+computerMove);
elementsRemaining -= computerMove;
System.out.println("Now there are "+elementsRemaining+" elements remaning.");
System.out.println();
if(elementsRemaining <= 0)
{
System.out.println("Computer 2 wins!");
return;
}
}
}
else if(mode==3)
{
System.out.println("How many times would you like to run the game?");
times = sc.nextInt();
System.out.println();
System.out.println();
System.out.println("Enter number of elements to start.");
initial = sc.nextInt();
System.out.println("How many elements can you take at a time?");
take = sc.nextInt();
elementsRemaining = initial;
for(int i=0; i<times; i++)
{
while(elementsRemaining > 0)
{
computerMove = getComputerMove(elementsRemaining, take);
System.out.println("Computer takes "+computerMove);
elementsRemaining -= computerMove;
System.out.println("Now there are "+elementsRemaining+" elements remaning.");
System.out.println();
if(elementsRemaining <= 0)
{
System.out.println("Computer 1 wins!");
comp1wins++;
elementsRemaining = initial;
}
computer2Move = getComputer2Move(elementsRemaining, take);
System.out.println("Computer takes "+computer2Move);
elementsRemaining -= computer2Move;
System.out.println("Now there are "+elementsRemaining+" elements remaning.");
System.out.println();
if(elementsRemaining <= 0)
{
System.out.println("Computer 2 wins!");
comp2wins++;
elementsRemaining = initial;
}
}
}
System.out.println("Computer 1 wins: " + comp1wins);
System.out.println("Computer 2 wins: " + comp2wins);
return;
}
else
{
}
}
public static int getComputerMove(int left, int take)
{
if(left == 1)
{
return left;
}
else
{
return (int)(Math.random()*take)+1;
}
}
public static int getComputer2Move(int left, int take)
{
if(left == 1)
{
return left;
}
else
{
return (int)(Math.random()*take)+1;
}
}
}
If I had to make a guess on the issue, I would have to say there is a problem with the nested while loop in my for loop. Any help or suggested changes to make would be greatly appreciated.
ou are right. In case of elementsRemaining is lower the zero you set it to the initial value. So it is every time grater then zero at the beginning of the while loop. You should take a boolean variable to decides, if your run the while loop again or not.
In the last one else you write twice
if(elementsRemaining <= 0)
{
....
elementsRemaining = initial;
}
but the elementsRemaining <=0 is exactly the condition that is needed to exit the internal loop!

Encountering Error with Pig Code

My variable names are pretty stupid near the bottom but that's not the point. Line 110 says that I'm making an else statement without the if and I'm nearly certain the if is there. Please help in any way you can.
Thank you
import java.util.*;
public class hw7
{
public static void main(String[] args)
{
int turnScores = 0;
int totalScores = 0;
int turnScores2 = 0;
int totalScores2 = 0;
int dice;
int dice2;
String input = "r";
char repeat;
boolean peppers;
peppers = true;
Scanner keyboard = new Scanner(System.in);
Random randomNumbers = new Random();
System.out.println("Welcome to the game of Pig!\n");
while(totalScores < 20 || totalScores2 < 20)
{
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScores = 0;
System.out.print("Your lose your turn!");
System.out.println("Your Total is " + totalScores);
break;
}
else
{
turnScores += dice;
System.out.print("Your turn score is " + turnScores);
System.out.println(" and your total scores is " + totalScores);
System.out.println("If you hold, you will have " + turnScores
+ " points.");
System.out.println("Enter 'r' to roll again, 'h' to hold.");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 'h')
{
break;
}
}
}
while(input.equalsIgnoreCase("r") || dice != 1);
totalScores += turnScores;
peppers = peppers(turnScores, turnScores2);
System.out.println("Your scores is " + totalScores);
turnScores = 0;
System.out.println();
System.out.println("It is the computer's turn.");
do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("The computer rolled: " + dice2);
if(dice2 == 1)
{
turnScores2 = 0;
System.out.print("The computer lost its turn!");
System.out.println(" Computer total is " + totalScores2);
break;
}
else
{
turnScores2 += dice2;
if(turnScores2 >= 20 || (totalScores2 + turnScores2) >= 20 )
{
System.out.println("The computer holds");
break;
}
}
}
while(dice2 != 1 || turnScores2 < 20);
totalScores2 += turnScores2;
System.out.println("The computer's scores is " + totalScores2 + "\n");
turnScores2 = 0;
}
}
public static boolean peppers(int chili, int ghost)
{
boolean done;
done = true;
if(chili >= 20);
{
done = false;
System.out.println(" YOU WIN!!!");
return done;
}
else (ghost >= 20);
{
done = false;
System.out.println(" COMPUTER WINS");
return done;
}
/*else
{
return done;
}
*/
}
}
Try removing the semicolons in if(chili >= 20); and else (ghost >= 20);.
public static boolean peppers(int chili, int ghost)
{
boolean done;
done = true;
if(chili >= 20)
{
done = false;
System.out.println(" YOU WIN!!!");
}
else if (ghost >= 20)
{
System.out.println(" COMPUTER WINS");
}
return done;
}
replace
else (ghost >= 20);
with
else if (ghost >= 20)
and remove the semicolons after the if-statement.

Game of Pig-java

So I made quite a few changes to my code and now it complies, but I get the wrong totals and it always thinks Player 2 wins, even before it hits "20". For some reason it isn't reading player 1 totalScore until after player 2 has rolled and then it does not calc player 2 turnTotal. When I made changes before, one thing would start working, but another would stop, so I took it back to where I began to have problems once it would compile.
import java.util.*;
public class Proj3Part1
{
public static void main(String[] args)
{
int turnScore = 0;
int totalScore = 0;
int turnScore2 = 0;
int totalScore2 = 0;
final int WIN = 20;
int dice = 0;
int dice2 = 0;
String input = "r";
String input2 = "r";
char repeat;
Scanner keyboard = new Scanner(System.in);
Scanner s = new Scanner (System.in);
Random randomNumbers = new Random();
while(totalScore < WIN && totalScore2 < WIN)
{
//Player 1's turn
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScore = 0;
System.out.println("Turn over.");
System.out.println("Player 1 total is " + totalScore);
break;
}
else
{
turnScore += dice;
System.out.print("Player 1 turn total is " + turnScore + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over.");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input.equalsIgnoreCase("r") || dice != 1);
{
totalScore += turnScore;
}
if(totalScore >= WIN)
{
System.out.println("Your total Score is " + totalScore);
System.out.println("Player 1 wins!");
}
//player2's turn
System.out.println();
System.out.println("It is Player 2's turn.");
{ do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("Player 2 rolled: " + dice2);
if(dice2 == 1)
{
turnScore2 = 0;
System.out.print("Turn over");
System.out.println("Player 2 total is " + totalScore2);
break;
}
else
{
turnScore2 += dice2;
System.out.print("Player 2 total is " +turnScore2 + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input2.equalsIgnoreCase("r") && dice != 1); {
totalScore2 += turnScore2;
}
}
if(totalScore2 >= WIN);
{
System.out.println("Player 2 score is " + totalScore2 + "\n");
System.out.println("Player 2 wins");
break;
}
}
}
}
There is an error in the loop for player2/computer's turn. It's in the first if-else loop, located in the else portion.
input keyboard.nextLine();
should be
input = keyboard.nextLine();
It works fine after correcting that error.
Also, pay close attention to the compilation errors, they will point you towards which lines are generating said error.
Revision:
I think this works the way you intended it to.
import java.util.*;
public class Proj3Part1
{
public static void main(String[] args)
{
int turnScore = 0;
int totalScore = 0;
int turnScore2 = 0;
int totalScore2 = 0;
final int WIN = 20;
int dice = 0;
int dice2 = 0;
String input = "r";
String input2 = "r";
char repeat;
Scanner keyboard = new Scanner(System.in);
Scanner s = new Scanner (System.in);
Random randomNumbers = new Random();
while(totalScore < WIN && totalScore2 < WIN)
{
//Player 1's turn
do
{
dice = randomNumbers.nextInt(6) + 1;
System.out.println();
System.out.println("You rolled: " + dice);
if(dice == 1)
{
turnScore = 0;
System.out.println("Turn over.");
System.out.println("Player 1 total is " + totalScore);
break;
}
else
{
turnScore = dice; //removed +=??? think it's only the value of dice roll?
//either way it's used to compute total, which would be redundant if not
totalScore +=turnScore; //added to compute totalScore before turn is over
System.out.print("Player 1 turn total is " + totalScore + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over.");
System.out.print("Current score: Player 1 has " + totalScore); //previously total wasn't computed
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}while(input.equalsIgnoreCase("r"));
//totalScore += turnScore; was removed + curly braces that seemed to attach it to the above while loop
//it isn't needed due to totalScore now being calculated after dice is rolled when !=1(else portion)
if(totalScore >= WIN)
{
System.out.println("Your total Score is " + totalScore);
System.out.println("Player 1 wins!");
break; //added to break the loop if player 1 wins
}
//player2's turn
System.out.println();
System.out.println("It is Player 2's turn.");
do
{
dice2 = randomNumbers.nextInt(6) + 1;
System.out.println("Player 2 rolled: " + dice2);
if(dice2 == 1)
{
turnScore2 = 0;
System.out.print("Turn over");
System.out.println("Player 2 total is " + totalScore2);
break;
}
else
{
turnScore2 = dice2; //removed += ... same as for player 1's turn
totalScore2 += turnScore2; //added totalScore2 calculations.
System.out.print("Player 2 total is " +totalScore2 + " ");
System.out.print("Enter (r)oll or (s)top: ");
input = keyboard.nextLine();
repeat = input.charAt(0);
if(repeat == 's')
{
System.out.println("Turn over");
System.out.print("Current score: Player 1 has " + totalScore);
System.out.println(", Player 2 has " + totalScore2);
break;
}
}
}
while(input2.equalsIgnoreCase("r")); //{ <- incorrect brace + fixed loop for dice2 !=1, then removed it :P since you already did a check inside the do-while loop
//totalScore2 += turnScore2; removed, no longer is needed
//}
//} <- not needed nor is the brace that was infront of the do while loop.
if(totalScore2 >= WIN) //removed semicolon since it ended the if statement before it's body
{
System.out.println("Player 2 score is " + totalScore2 + "\n");
System.out.println("Player 2 wins");
break;
}
if(totalScore>totalScore2) //added loops to check which score is higher and terminate
{
System.out.println("Player 1 Wins!");
break;
}else if(totalScore==totalScore2){
System.out.println("It's a Tie!");
break;
}else if(totalScore<totalScore2){
System.out.println("Player 2 Wins!");
break;
}
}
}
}
I also recommend installing an IDE such as Netbeans or Eclipse. An IDE would make your life much easier, especially with formatting and syntax errors.

Categories