我无法弄清楚如何在人类玩法之后让我的循环让计算机播放。
这是代码:(注意:对于任何缩进问题很抱歉。这个网站不喜欢我从文本板复制和粘贴。)
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...'"); }}}}
答案 0 :(得分:1)
问题在于if语句:
if (player == 1 && difficulty == 1);
你永远不会有玩家= 1,因此永远不会进入if区块。
<强>建议强>
我建议完全摆脱播放器变量。
将游戏变为自己的函数,该函数根据谁获胜返回一个布尔变量。如果玩家输入无效数量的宝石,则使用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
}
答案 1 :(得分:0)
{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"); }
选择大理石后,应将播放器更改为电脑。
{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");
player = 1;
}
实际上虽然我更愿意将播放器切换到if else语句的末尾,所以我可以决定谁是赢家。请记住,您应该在/计算机“合法选择”之后更改播放器= 0或1。这应该是你如何改变轮到你的游戏逻辑。
{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."); }}
// change to computer turn when there's still marble left
if (marbles > 0) {
player = 1;
}
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."); }}
// change turn to player when there is marble left still
if (marbles > 0) {
player = 0;
}
Then use a if else statement after breaking your while loop turns to false when the marbles left 0, meaning end of game to see.
// when the game stop on whichever player....
if (player = 1){
System.out.println("Computer won");
else
System.out.println("You won");