Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nim游戏不能用Java_Java_Methods_Boolean - Fatal编程技术网

Nim游戏不能用Java

Nim游戏不能用Java,java,methods,boolean,Java,Methods,Boolean,它可以工作,除了它只是让电脑移动一次,然后循环玩家的移动直到结束。另一个问题是,我尝试(使用布尔方法)使其不可能使用,例如,如果只剩下2块石头,则不可能使用3块石头,这似乎也不起作用。。。我是java新手,请帮助基本上,游戏很简单。你需要组织你的代码,这样你就可以跟踪剩下的石头数量。因此,你需要一个顶级while循环,只要stones>0就可以循环,然后你需要一个变量来跟踪是轮到电脑玩还是轮到玩家玩,初始值为false的布尔值playerTurn 基本上,当(stones>0)开始第一个循环时,

它可以工作,除了它只是让电脑移动一次,然后循环玩家的移动直到结束。另一个问题是,我尝试(使用布尔方法)使其不可能使用,例如,如果只剩下2块石头,则不可能使用3块石头,这似乎也不起作用。。。我是java新手,请帮助基本上,游戏很简单。你需要组织你的代码,这样你就可以跟踪剩下的石头数量。因此,你需要一个顶级while循环,只要
stones>0
就可以循环,然后你需要一个变量来跟踪是轮到电脑玩还是轮到玩家玩,初始值为false的布尔值
playerTurn

基本上,当(stones>0)开始第一个循环时,会发生以下情况:

  • 如果选中playerTurn,则在第一个循环中返回false,因为计算机进行第一个循环
  • 否则条件就被执行了
  • 电脑拣石头
  • 石头是经过验证的
  • 设置playerTurn=true,让玩家下一步玩
  • if条件检查stones==0如果是,则计算机为赢家,执行
    继续语句,将控件带到
    while(stones>0)
  • 如果是石头!=0,则当stones>0时控制返回,循环检查yes
  • 若条件检查玩家是否回合,是的,则允许玩家提供任何数量的石头
  • 一旦进入playerTurn if条件,请将playerTurn设置为false,以便计算机下一步播放
  • 验证石头的数量
  • 减去石头的数量
  • 检查如果石头==0,那么玩家是赢家,控制回到while(石头>0),否则退出if条件,控制回到while(石头>0)
上述过程继续进行,直到在决定谁是赢家的if(playerTurn)或else区块内满足条件if(stones==0)

有这么多循环,需要用if条件替换一些循环。我已经修改了你的游戏方法,请看下面(仅游戏方法):

以下是示例输出:

The computer takes 2 stone(s)
There are 23 stones remaining.
How many stones will you take?(max 3): 
2
There are 21 stones remaining.
The computer takes 3 stone(s)
There are 18 stones remaining.
How many stones will you take?(max 3): 
1
There are 17 stones remaining.
The computer takes 1 stone(s)
There are 16 stones remaining.
How many stones will you take?(max 3): 
3
There are 13 stones remaining.
The computer takes 3 stone(s)
There are 10 stones remaining.
How many stones will you take?(max 3): 
2
There are 8 stones remaining.
The computer takes 1 stone(s)
There are 7 stones remaining.
How many stones will you take?(max 3): 
3
There are 4 stones remaining.
The computer takes 3 stone(s)
There are 1 stones remaining.
How many stones will you take?(max 3): 
1
There are 0 stones remaining.
You win.

我采用了您的主要游戏方法,并使用伪代码解释了其意图。我是这样理解的:

    Computer chooses number of stones
    make sure computer picked a valid amount

    loop while computer picked a valid amount

        reduce remaining amount by computer's choice

        computer chooses

        check if the computer won
           return if it did

        ask player to choose, and validate input

        make sure player picked a valid amount

        loop on valid player input and stones remain

            reduce remaining amount by player's choice

            validate player input

             check if player won
               return if he did
通过遵循伪代码中的逻辑,我发现一旦玩家拾取了有效数量的石头,程序就会减少石头的数量,直到达到零,而不会让计算机或玩家再次拾取。我还注意到对某些方法的不必要调用。 (游戏中确实存在一个缺陷,剩余的石头数量可能会降到零以下,但我这里不讨论这个问题)

当我想到如何玩游戏时,这里是我想到的伪代码:

 set computer as current player
 loop while there are stones remaining
    have current player pick stones, validate selection
    reduce remaining stones count
    check if someone won
      return if the game is over
    switch computer/player as current player
根据这些伪代码,我提出了一个

public static void gamePlay() {
    int stonesRemaining = 25;
    System.out.println("The game begins with " + stonesRemaining + " stones");

    boolean playersTurn = false;
    int stonesJustTaken = 0;
    while (stonesRemaining >= 0) {
        if (playersTurn) {
            stonesJustTaken = playerMoves(stonesRemaining);
        } else {
            stonesJustTaken = computerMoves(stonesRemaining);
            System.out.println("The computer takes " + stonesJustTaken + " stone(s)");
        }

        stonesRemaining = stonesRemaining - stonesJustTaken;
        System.out.println("There are " + stonesRemaining + " stones remaining.");

        if (stonesRemaining <= 0) {
            printWinningMessage(playersTurn);
            return;
        }
        playersTurn = !playersTurn;
    }
}
publicstaticvoidgameplay(){
int stonesRemaining=25;
System.out.println(“游戏以“+stonesRemaining+”stones开始”);
布尔playersTurn=false;
int stonesjusttake=0;
while(stonesRemaining>=0){
如果(球员轮换){
Stonesjusttake=玩家移动(stonesRemaining);
}否则{
StonesJusttake=计算机移动(stonesRemaining);
System.out.println(“计算机接受”+石头接受”+“石头接受”);
}
stonesRemaining=stonesRemaining-已取石头;
System.out.println(“有”+石头保留+“石头剩余”);

如果(stonesRemaining)我不确定你想在这里建立什么游戏规则(以及你到底在问什么),但有一件事让你眼前一亮——你的第一个方法可以被
return computerMoves(stones)取代
和整个第二个方法使用
return player==1 | | player==2 | | player==3;
。编写
if(something){return true;}else{return false;}
是多余的,只要使用
return something;
他就可以用if(something==true)和if(something==false)替换所有条件if(something==true)用if(!某物)
public static void gamePlay() {
    int stonesRemaining = 25;
    System.out.println("The game begins with " + stonesRemaining + " stones");

    boolean playersTurn = false;
    int stonesJustTaken = 0;
    while (stonesRemaining >= 0) {
        if (playersTurn) {
            stonesJustTaken = playerMoves(stonesRemaining);
        } else {
            stonesJustTaken = computerMoves(stonesRemaining);
            System.out.println("The computer takes " + stonesJustTaken + " stone(s)");
        }

        stonesRemaining = stonesRemaining - stonesJustTaken;
        System.out.println("There are " + stonesRemaining + " stones remaining.");

        if (stonesRemaining <= 0) {
            printWinningMessage(playersTurn);
            return;
        }
        playersTurn = !playersTurn;
    }
}