Reputation: 1
package HomePlace;
import java.util.Random;
import java.util.Scanner;
public class LastStone {
public static boolean isValidEntry(int left, int User) {
if (left > 0 && User < 4 && left >= User) {
return true;
} else {
System.out.print("Invalid entry!!");
return false;
}
}
public static int userMove(int left) {
Scanner input = new Scanner(System.in);
System.out.print("There are " + left + " stones. How many would you like? ");
int User = input.nextInt();
return User;
}
public static int computerMove(int left) {
int cpuStones;
cpuStones = generateStones();
System.out.print("There are " + left + "stones. The computer takes" + cpuStones + " stones");
return cpuStones;
}
public static int generateStones() {
Random sr = new Random();
int gstones = sr.nextInt(2) + 1;
return gstones;
}
public static void playLastStone(int UserOrCpu) {
Scanner input = new Scanner(System.in);
if (UserOrCpu % 2 == 1) {
System.out.println("The Computer beats the User!");
} else {
System.out.println("The User beats the Computer!");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random r = new Random();
int left = r.nextInt(16) + 15;
int UserOrCpu = 1, User = 1, temp;
while (true) {
if (UserOrCpu % 2 == 1) {
User = userMove(left);
if (isValidEntry(left, User)) {
if (left == User) {
playLastStone(UserOrCpu);
break;
}
left -= User;
} else {
continue;
}
} else {
computerMove(left);
temp = computerMove(left);
if (left <= temp) {
playLastStone(UserOrCpu);
break;
} else {
left -= temp;
}
}
}
UserOrCpu += 1;
}
}
My computer player cpu never takes its turn in my implementation of the Last Stone game or Nim.
I found similar questions but none helped with solving my problem.
I debugged my code line by line but couldn't figure out why my second } else {
in my main
method is never executed?
left -= User;
} else {
continue;
}
} else { // why is it never executed?
Upvotes: 0
Views: 124
Reputation: 2338
In your given code, your main
method actually reduces itself to
public static void main(String[] args) {
Random r = new Random();
int left = r.nextInt(16) + 15;
int userOrCpu = 1, user;
while (true) {
user = userMove(left);
if (isValidEntry(left, user)) {
if (left == user) {
playLastStone(userOrCpu);
break;
}
left -= user;
}
}
}
And so it's now clear that your computer player cpu
never gets its turn.
The problem is that your UserOrCpu += 1;
assignment isn't in the right scope, and so doesn't have any effect.
The "fixed" main
method should look like this
public static void main(String[] args) {
Random r = new Random();
int left = r.nextInt(16) + 15;
int UserOrCpu = 1, User = 1, temp;
while (true) {
if (UserOrCpu % 2 == 1) {
User = userMove(left);
if (isValidEntry(left, User)) {
if (left == User) {
playLastStone(UserOrCpu);
break;
}
left -= User;
} else {
continue;
}
} else {
computerMove(left);
temp = computerMove(left); // executes computer move a 2nd time?
if (left <= temp) {
playLastStone(UserOrCpu);
break;
} else {
left -= temp;
}
}
UserOrCpu += 1;
}
}
Hint: Get yourself an IDE (like IntelliJ) for writing code. Such a code editor will indicate "obvious" problems with your code immediately.
Upvotes: 2