英文:
Why does the other player never gets its turn in my Last Stone Game (Nim) implementation?
问题
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 { // This part is now translated
computerMove(left);
temp = computerMove(left);
if (left <= temp) {
playLastStone(UserOrCpu);
break;
} else {
left -= temp;
}
}
UserOrCpu += 1;
}
}
}
Note: I've added translations for the comments in the code. If you have any further questions or need assistance, feel free to ask.
英文:
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?
答案1
得分: 2
在你提供的代码中,你的main
方法实际上被简化为以下形式:
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); // 执行了第二次计算机移动?
if (left <= temp) {
playLastStone(UserOrCpu);
break;
} else {
left -= temp;
}
}
UserOrCpu += 1;
}
}
提示: 最好使用一个集成开发环境(如IntelliJ)来编写代码。这样的代码编辑器会立即指出代码中的“明显”问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论