我无法弄清楚如何让我的循环在人类播放后让计算机播放。
这是代码:(注意:对于任何缩进问题,我们深表歉意。该网站不喜欢我从文本板复制和粘贴的内容。)
import java.util.*;
class GameOfNim {
public static void main(String [] args) {
Scanner in = new Scanner (System.in);
System.out.println("Welcome to the game of Nim!");
System.out.println("------------------------------");
System.out.println("The rules are simple: ");
System.out.println("1. You must take one but at most half of the marbles.");
System.out.println("2. The player that takes the last marble loses!");
System.out.println("-------------------------------------------------------");
System.out.println("To start the game choose a difficulty where 0 = hard & 1 = easy: ");
int marbles, player, difficulty, pick;
marbles = (int)(Math.random()*90+10);
player = (int)(Math.random()+0.5);
difficulty = (int)(Math.random()+0.5);
pick = (int)(in.nextInt());
if(difficulty == 0) {
System.out.println("The Computer is now in Beast Mode!");
}else if(difficulty == 1) {
System.out.println("The Computer is now in easy mode.");
}
System.out.println("The pile of marbles has: " + marbles + " total marbles. Game On!");
{while(marbles>0) {
if(player==0) { //person
System.out.print("Pick some marbles: ");
pick = in.nextInt(); }
if (pick >= 1 && pick <= marbles/2) {
marbles = marbles - pick; System.out.println(marbles+"left"); }
else if(pick < 1 || pick > marbles/2 && marbles > 1) {
System.out.println("Error: Illegal Move.");
player = 0; continue; }
else { marbles = marbles - pick; System.out.println(marbles + "left."); }}
if(player == 1 && difficulty ==1) { System.out.println("Computer's turn to pick");
pick = (int)(Math.random()*marbles/2+1);
marbles = marbles - pick;
System.out.println("computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left.");}
else if(player == 1 && difficulty == 0) {
System.out.println("Computer's turn to pick");
if(marbles > 63) {pick = marbles - 63; marbles = marbles - pick;}
else if(marbles > 31) {pick = marbles - 32; marbles = marbles - pick;} else if(marbles > 15) {pick = marbles - 15; marbles = marbles - pick;} else if(marbles > 7) {pick = marbles - 7; marbles = marbles - pick;} else if(marbles > 3) {pick = marbles - 3; marbles = marbles - pick;}
else{ pick = (int)(Math.random()*marbles/2+1); marbles = marbles - pick;
System.out.println("Computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left."); }}
{if(player==0)//switching
player=1; //code
else player=0; } {if(player==0)
System.out.println("Computer: 'I give up! You win!");
else System.out.println("Computer: '...You are pathetic...'"); }}}}
最佳答案
问题出在这个if语句上:
if (player == 1 && difficulty == 1);
你永远不会有player = 1,因此它永远不会进入if block 。
推荐
我建议完全摆脱玩家变量。
将游戏变成它自己的函数,该函数根据谁获胜返回一个 boolean 变量。使用 continue 语句,如果玩家输入的棋子数量无效,则无需在函数中检查轮到谁,因为当玩家移动时,它自然只会进入计算机的回合。
所以你的程序将如下所示:
public static boolean game(/*Suitable parameters(ie. number of stones etc.*/)) {
while (marbles > 0) {
//Play game
}
}
public static void main(String[] args) {
//Set up stuff
if (game(/*Suitable parameters(ie. number of stones etc.*/)) {
//Player wins
} else {
//Computer wins
}
}
在游戏中,不必担心玩家变量的最干净的方法可能是在游戏循环内有一个 while 循环,只有当玩家做出有效的移动时才会退出:
while (marbles > 0) {
while (true) {
//Player makes move
if (validMove) {
//do move
break;
} else {
//Output error and then the loop repeats itself
}
}
//Computer move
}
关于java - nim 玩家回合循环游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313817/