Why does the other player never gets its turn in my Last Stone Game (Nim) implementation?

huangapple 未分类评论77阅读模式
标题翻译

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 &gt; 0 &amp;&amp; User &lt; 4 &amp;&amp; left &gt;= User) {
			return true;
		} else {
			System.out.print(&quot;Invalid entry!!&quot;);
			return false;
		}
	}

	public static int userMove(int left) {
		Scanner input = new Scanner(System.in);
		System.out.print(&quot;There are &quot; + left + &quot; stones. How many would you like? &quot;);
		int User = input.nextInt();
		return User;
	}

	public static int computerMove(int left) {
		int cpuStones;
		cpuStones = generateStones();
		System.out.print(&quot;There are &quot; + left + &quot;stones. The computer takes&quot; + cpuStones + &quot; stones&quot;);
		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(&quot;The Computer beats the User!&quot;);
		} else {
			System.out.println(&quot;The User beats the Computer!&quot;);
		}
	}

	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 &lt;= 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 &lt;= 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.

huangapple
  • 本文由 发表于 2020年5月30日 19:48:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/62101970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定