NimGame问题请协助

时间:2014-11-28 09:01:34

标签: java

我的Nim游戏在我的方法的第三个选项上无限运行时遇到了问题。代码运行游戏并呈现胜利者,但它无限运行。在此代码中使用第三种方法来运行游戏许多不同的时间并记录每台计算机赢得的次数。

import java.util.Scanner;

    public class NimGame
    {
        public static void main(String[] args)
        {
            int computerMove = 0;
            int computer2Move = 0;
        int userMove = 0;
        int elementsRemaining = 0;
        int take = 0;
        int mode;
        int times;
        int initial = 0;
        int comp1wins = 0;
        int comp2wins = 0;

        Scanner sc = new Scanner(System.in);

        System.out.println("Which mode would you like to run the game?");
        System.out.println("1) Player Mode ");
        System.out.println("2) AI vs AI Single Mode");
        System.out.println("3) AI vs AI building mode");
        mode = sc.nextInt();

        if(mode==1)
        {
            System.out.println();
            System.out.println();
            System.out.println("Enter number of elements to start.");
            elementsRemaining = sc.nextInt();
            System.out.println("How many elements can you take at a time?");
            take = sc.nextInt();

            while(elementsRemaining > 0)
            {
                computerMove = getComputerMove(elementsRemaining, take);
                System.out.println("Computer takes "+computerMove);
                elementsRemaining -= computerMove;

                System.out.println("Now there are "+elementsRemaining+" elements remaning.");
                System.out.println();

                if(elementsRemaining <= 0) 
                {
                    System.out.println("Computer wins!");
                    return;
                }

                System.out.println("How many elements do you want to remove? (1 to " + take + ")");
                userMove = sc.nextInt();

                while((userMove <1) || (userMove > take))
                {
                    System.out.println("Taking " + userMove + " elements is not allowed, please choose from 1 to " + take + ".");
                    System.out.println();
                    userMove = sc.nextInt();
                }

                elementsRemaining -= userMove;

                System.out.println("Now there are " + elementsRemaining + " elements remaining.");
                System.out.println();


                if(elementsRemaining <= 0) 
                {
                    System.out.println("You win!");
                    System.out.println();
                    return;
                }
            }
        }
        else if(mode==2)
        {
            System.out.println();
            System.out.println();
            System.out.println("Enter number of elements to start.");
            elementsRemaining = sc.nextInt();
            System.out.println("How many elements can you take at a time?");
            take = sc.nextInt();

            while(elementsRemaining > 0)
            {
                computerMove = getComputerMove(elementsRemaining, take);
                System.out.println("Computer takes "+computerMove);
                elementsRemaining -= computerMove;

                System.out.println("Now there are "+elementsRemaining+" elements remaning.");
                System.out.println();

                if(elementsRemaining <= 0) 
                {
                    System.out.println("Computer 1 wins!");
                    return;
                }

                computer2Move = getComputer2Move(elementsRemaining, take);
                System.out.println("Computer takes "+computerMove);
                elementsRemaining -= computerMove;

                System.out.println("Now there are "+elementsRemaining+" elements remaning.");
                System.out.println();

                if(elementsRemaining <= 0) 
                {
                    System.out.println("Computer 2 wins!");
                    return;
                }
            }
        }
        else if(mode==3)
        {

            System.out.println("How many times would you like to run the game?");
            times = sc.nextInt();

            System.out.println();
            System.out.println();
            System.out.println("Enter number of elements to start.");
            initial = sc.nextInt();
            System.out.println("How many elements can you take at a time?");
            take = sc.nextInt();

            elementsRemaining = initial;

            for(int i=0; i<times; i++)
            {
                while(elementsRemaining > 0)
                {
                    computerMove = getComputerMove(elementsRemaining, take);
                    System.out.println("Computer takes "+computerMove);
                    elementsRemaining -= computerMove;

                    System.out.println("Now there are "+elementsRemaining+" elements remaning.");
                    System.out.println();

                    if(elementsRemaining <= 0) 
                    {
                        System.out.println("Computer 1 wins!");
                        comp1wins++;
                        elementsRemaining = initial;

                    }

                    computer2Move = getComputer2Move(elementsRemaining, take);
                    System.out.println("Computer takes "+computer2Move);
                    elementsRemaining -= computer2Move;

                    System.out.println("Now there are "+elementsRemaining+" elements remaning.");
                    System.out.println();

                    if(elementsRemaining <= 0) 
                    {
                        System.out.println("Computer 2 wins!");
                        comp2wins++;
                        elementsRemaining = initial;

                    }
                }
            }
            System.out.println("Computer 1 wins: " + comp1wins);
            System.out.println("Computer 2 wins: " + comp2wins);
            return;
        }
        else
        {
        }

    }





    public static int getComputerMove(int left, int take)
    {
        if(left == 1)
        {
           return left;
        }
        else
        {
           return (int)(Math.random()*take)+1;
        }
    }

    public static int getComputer2Move(int left, int take)
    {
        if(left == 1)
        {
           return left;
        }
        else
        {
           return (int)(Math.random()*take)+1;
        }
    }
}

如果我不得不猜测这个问题,我不得不说我的for循环中嵌套的while循环有问题。任何帮助或建议的更改都将非常感激。

2 个答案:

答案 0 :(得分:0)

你是对的。如果elementsRemaining低于零,则将其设置为initial值。因此,每次在while循环开始时grater然后为零。如果再次运行while循环,则应该使用布尔变量来决定。

答案 1 :(得分:0)

在最后一个你写了两次

 if(elementsRemaining <= 0) 
 {
    ....
    elementsRemaining = initial;
 }

但elementsRemaining&lt; = 0正好是退出内部循环所需的条件!