You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Java版简易Nim游戏如何实现玩家回合状态跟踪?

Nim游戏开发问题解决方案

回合跟踪实现方法

你注释中写出的boolean whoseTurn变量完全可以满足回合跟踪需求,使用规则如下:

  • 初始化值设为true,代表玩家先手
  • 每次当前回合操作完成后,执行whoseTurn = !whoseTurn即可切换到对方回合
  • 把原本强制连续执行用户、电脑操作的逻辑拆分,每次循环内先判断当前是谁的回合,再执行对应操作

现有代码其他需要修复点

  • 原有胜负判断条件完全不成立:1 <= remainingMarbles && remainingMarbles <= 2 && remainingMarbles < 0三个条件不可能同时满足,需要修正为:当最后一个拿弹珠的是电脑时,玩家获胜,反之玩家失败
  • 原有逻辑中,玩家操作后未判断剩余弹珠是否为0,直接执行电脑操作,会出现逻辑错误
  • 不要重复创建Scanner、Random实例,全局创建一次复用即可

修复后可运行代码

import java.util.Random;
import java.util.Scanner;

public class Nim{
    // 全局复用扫描器和随机数生成器
    private static final Scanner scan = new Scanner(System.in);
    private static final Random generator = new Random();
    private static final int MARBLES = 13;

    public static void main(String[] args) {
        System.out.println("************** Welcome to the game of Nim *******************");
        System.out.println("The object of this game is to make me take the last marble off the table");
        System.out.println("You must take at least 1 and you can take up to 3 marbles on your turn");
        System.out.println("You can go first");

        String input;
        do {
            int remainingMarbles = MARBLES;
            boolean isUserTurn = true; // true=玩家回合 false=电脑回合
            boolean isUserWin = false;

            System.out.println("There are " + MARBLES + " marbles on the table");

            while (remainingMarbles > 0) {
                int takeCount = 0;
                if (isUserTurn) {
                    takeCount = getUserSelection();
                } else {
                    takeCount = getComputerSelection();
                }
                remainingMarbles -= takeCount;
                // 当前操作的人拿了最后一颗弹珠,判负
                if (remainingMarbles <= 0) {
                    isUserWin = !isUserTurn;
                    break;
                }
                System.out.println("There are " + remainingMarbles + " marble(s) on the table.");
                // 切换回合
                isUserTurn = !isUserTurn;
            }

            if (isUserWin) {
                System.out.println("Congratulations! you won!");
            } else {
                System.out.println("Hard luck you lose!");
            }
            System.out.println("Want to play again? y/n");
            // 处理nextInt残留的换行符
            scan.nextLine();
            input = scan.nextLine().toLowerCase();
        } while (input.charAt(0) == 'y');
        scan.close();
    }

    private static int getUserSelection() {
        do {
            System.out.println("Enter 1, 2 or 3");
            int userSelection = scan.nextInt();
            if (isValidMove(userSelection)) {
                return userSelection;
            } else {
                System.out.println("Not a valid entry");
            }
        } while (true);
    }

    private static boolean isValidMove(int input) {
        return 1 <= input && input <= 3;
    }

    private static int getComputerSelection() {
        int computerSelection = 1 + generator.nextInt(3);
        System.out.println("The computer chooses " + computerSelection);
        return computerSelection;
    }
}

内容的提问来源于stack exchange,提问作者the_brass_bottle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 20:51:01