In: Computer Science
Why does the loop dont stop after Player1 wins. It stops
after player2 wins but not player1. After player1 wins it asks
player2 to play again. Can someone pls help?
import java.util.Scanner;
public class Project2 {
public static void main(String [] args) {
Scanner input= new Scanner(System.in);
int total = 15, max_stones = 5, num_stones_removed;
int pile = total; //all stones are in the pile to start
int count = total;
boolean won= false;
boolean player2=false;
boolean player1=false;
while (pile>0 && won==false) {
while (player1==false){
System.out.println("Player1- How many stones you want to remove
between 1 and " + max_stones);
num_stones_removed= input.nextInt();
System.out.print(num_stones_removed);
if (num_stones_removed>=1 &&
num_stones_removed<=max_stones &&
num_stones_removed<=pile) {
pile =pile-num_stones_removed; //update pile
player1=true;
count--;
System.out.println("You have " + pile + " stones left");
if (pile==0) {
won=true;
System.out.println("Play1 won");
break;
}
} else {
System.out.println("Invalid pick");
}// end else
}
player1=false;
while (player2==false) {
System.out.println("Player2- How many stones you want to remove
between 1 and " + max_stones);
num_stones_removed= input.nextInt();
if (num_stones_removed>=1 &&
num_stones_removed<=max_stones &&
num_stones_removed<=pile) {
pile =pile-num_stones_removed; //update pile
player2=true;
count--;
System.out.println("You have " + pile + " stones left");
if (pile==0) {
won=true;
System.out.println("Play2 won"); // player 2 wins
break;
}
}
else {
System.out.println("I am inside else-loop for P2"); //invalid pick
for player2
System.out.println("Invalid pick");
}// end else
}
player2=false;
}// while loop
}}
Put player2=false; condition after while loop
Attached here the modified code:
import java.util.Scanner;
public class Project2 {
public static void main(String [] args) {
Scanner input= new Scanner(System.in);
int total = 15, max_stones = 5, num_stones_removed;
int pile = total; //all stones are in the pile to start
int count = total;
boolean won= false;
boolean player2=false;
boolean player1=false;
while (pile>0 && won==false) {
while (player1==false){
System.out.println("Player1- How many stones you want to remove between 1 and " + max_stones);
num_stones_removed= input.nextInt();
System.out.print(num_stones_removed);
if (num_stones_removed>=1 && num_stones_removed<=max_stones && num_stones_removed<=pile) {
pile =pile-num_stones_removed; //update pile
player1=true;
count--;
System.out.println("You have " + pile + " stones left");
if (pile==0) {
won=true;
System.out.println("Play1 won");
break;
}
} else {
System.out.println("Invalid pick");
}// end else
}
player1=false;
while (player2==false) {
System.out.println("Player2- How many stones you want to remove between 1 and " + max_stones);
num_stones_removed= input.nextInt();
if (num_stones_removed>=1 && num_stones_removed<=max_stones && num_stones_removed<=pile) {
pile =pile-num_stones_removed; //update pile
player2=true;
count--;
System.out.println("You have " + pile + " stones left");
if (pile==0) {
won=true;
System.out.println("Play2 won"); // player 2 wins
break;
}
}
else {
System.out.println("I am inside else-loop for P2"); //invalid pick for player2
System.out.println("Invalid pick");
}// end else
}
}// while loop
player2=false; //put this after while loop
}}
Output:
plz upvote if this found helpful for you