Nim游戏 - 指定获胜者

时间:2012-11-23 02:18:06

标签: 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();
            }


        }
    }
}
}
}

我不知道如何在sizeheap(留下的石头数量)达到0时实现指定玩家1或玩家2成为赢家的方法。任何帮助都将受到赞赏。此外,当sizeheap达到负数时,它将显示“输入错误”,但之后插入的任何其他数字也会显示“错误输入”。

谢谢!

1 个答案:

答案 0 :(得分:2)

基本上,您只需要重写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标志可能是不必要的