java - Nim 游戏 - 指定获胜者

标签 java

import java.util.Scanner;
/**
 *@author Andy 
 *@verison 21.11.2012
 */
public class NimGame
{
public static void main (String[] args)
{
    System.out.println ("**********      Hello Welcome to the game Nim           *********");
    System.out.println ("               The game is relatively simple....        ");
    System.out.println ("               This is a game for two players.          "); 
    System.out.println ("            There is a heap containing 10 to 20 stones.");
    System.out.println ("           Players take turns to remove 1-4 stones     "); 
    System.out.println ("           The player who removes the last stone wins. ");
    System.out.println ("******************************************************************");

    Scanner scan = new Scanner (System.in);
    int heapSize = 15;
    int stones = 0;
    boolean nextInteger = false;
    boolean lessThanFour = false;
    String player1 = "Player 1";
    String player2 = "Player 2";
    String player = player1;

    System.out.println ("The number of stones currently in the heap is :- " + heapSize);
    System.out.println();

    while (heapSize > 0)
    {

        nextInteger = false;
        lessThanFour = false;
        System.out.println (player + ":" + "how many stones will you take from the heap?");
        System.out.println();
        while (nextInteger == false && lessThanFour == false)
        {

            if (scan.hasNextInt())
            {
                nextInteger = true;
                stones = scan.nextInt();

                if (stones <=4 && stones >0)
                {
                    System.out.println();
                    System.out.println ("You picked " + stones);
                    heapSize = (heapSize - stones);
                    if (heapSize >= 0)
                    {
                    System.out.println();
                    System.out.println ("There are " + heapSize + "stones left");  
                    System.out.println();
                    lessThanFour = true;
                    System.out.println();
                    if (player.equals(player1))
                    {
                        player = player2;
                    }
                    else

                    {
                        player = player1;
                    }



                }

                else 

                {
                    System.out.println ("Bad input, please try again");
                    nextInteger = false;
                    scan.nextLine();
                }
            }

            else 

            {
                System.out.println ("Bad input, please try again");
                scan.nextLine();
            }


        }
    }
}
}
}

我不知道如何实现一种方法来指定玩家 1 或玩家 2 在 sizeheap(剩余棋子数)达到 0 时成为赢家。任何帮助将不胜感激。此外,当 sizeheap 达到负数时,它将显示“错误输入”,但之后插入的任何其他数字也会显示“错误输入”。

谢谢!

最佳答案

基本上,您只需要重写 if (heapSize >= 0) 即可显示一条获胜消息:

if (heapSize > 0) {...} else { ...win... }

这是关键部分,经过修复、简化和编辑:

if (stones <= 4 && stones > 0) {
    System.out.println ("\nYou picked " + stones);
    heapSize = (heapSize - stones);

    if (heapSize > 0) {
        System.out.println ("\nThere are " + heapSize + " stones left\n\n");  

        // Could use a ternary operator here:
        // player = (player.equals(player1) ? player2 : player1);

        if (player.equals(player1)) {
            player = player2;
        }
        else {
            player = player1;
        }
    }
    else {
        if (player.equals(player1)) {
            System.out.println("Player 1 wins!");
        }
        else {
            System.out.println("Player 2 wins!");
        }       
    }
}

更多提示:

  • 您可以只使用换行符 \n 而不是 System.out.println()
  • lessThanFour 标志可能是不必要的

关于java - Nim 游戏 - 指定获胜者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522359/

相关文章:

java - Java Android 中大写字母背后的理论是什么?

java - 使用 Java 8 初始化新对象并设置值

java - 在 java 中使用 linux grep 命令时出错

java swing jtable 在单个表中从数据库添加两个表

java - 应对两个对象(实体)之间的字段

java - 使用 Java SE 6(和之前的版本)更精确地重新抛出异常

java - 如何创建MouseClick事件来检测哪个节点被点击? -javafx

java.io.StreamCorruptedException : invalid stream header: 7371007E

java - 为什么当我在此 java 代码中执行读取-更新-写入时不存在竞争条件?

java - 平方根法需要很长时间才能执行第一次尝试