Java nim玩家转圈游戏

Java nim玩家转圈游戏,java,loops,Java,Loops,我很难弄清楚如何让我的循环在人玩之后让电脑玩 代码如下:(注意:很抱歉出现缩进问题。本网站不喜欢我从textpad复制和粘贴。) import java.util.*; GameOfNim类{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); System.out.println(“欢迎来到Nim的游戏!”); System.out.println(“-----------------------------------”); System.out.prin

我很难弄清楚如何让我的循环在人玩之后让电脑玩

代码如下:(注意:很抱歉出现缩进问题。本网站不喜欢我从textpad复制和粘贴。)

import java.util.*;
GameOfNim类{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“欢迎来到Nim的游戏!”);
System.out.println(“-----------------------------------”);
System.out.println(“规则很简单:”);
System.out.println(“1.你必须取一个,但最多取一半的弹珠。”);
System.out.println(“2.拿走最后一颗弹珠的玩家输了!”);
System.out.println(“--------------------------------------------------------------”);
System.out.println(“要开始游戏,请选择一个难度,其中0=hard&1=easy:”);
智力弹珠,玩家,难度,选择;
弹珠=(int)(数学随机()*90+10);
player=(int)(Math.random()+0.5);
难度=(int)(Math.random()+0.5);
pick=(int)(in.nextInt());
如果(难度==0){
System.out.println(“计算机现在处于Beast模式!”);
}否则如果(难度==1){
System.out.println(“计算机现在处于简易模式”);
}
System.out.println(“这堆弹珠有:“+marbles+”总弹珠数。游戏开始!”);
{while(弹珠>0){
如果(player==0){//person
系统输出打印(“挑选一些弹珠:”);
pick=in.nextInt();}
如果(拾取>=1&&pick marbles/2&&marbles>1){
System.out.println(“错误:非法移动”);
player=0;继续;}
else{marbles=marbles-pick;System.out.println(marbles+“left.”);}
如果(玩家==1&&难度==1){System.out.println(“轮到计算机拾取”);
拾取=(int)(Math.random()*marbles/2+1);
弹珠=弹珠-拾取;
println(“计算机拾取:“+拾取+”弹珠。“+”有:“+弹珠+”弹珠剩余”);}
否则如果(玩家==1&&难度==0){
System.out.println(“轮到计算机挑选”);
如果(大理石>63){pick=marbles-63;marbles=marbles-pick;}
else if(大理石>31){pick=大理石-32;大理石=大理石-pick;}else if(大理石>15){pick=大理石-15;大理石=大理石-pick;}else if(大理石>7){pick=大理石-7;大理石=大理石-pick;}else if(大理石>3){pick=大理石-3;大理石=大理石-pick;}
else{pick=(int)(Math.random()*marbles/2+1);marbles=marbles-pick;
println(“计算机拾取:“+拾取+”弹珠。“+”有:“+弹珠+”弹珠剩余”);}
{if(player==0)//切换
player=1;//代码
else-player=0;}{if(player==0)
System.out.println(“计算机:我放弃!你赢了!”);
else System.out.println(“计算机:“…你真可怜…””);}}
{while(大理石>0){
如果(player==0){//person
系统输出打印(“挑选一些弹珠:”);
pick=in.nextInt();}
如果(选择>=1&&选择0){
如果(player==0){//person
系统输出打印(“挑选一些弹珠:”);
pick=in.nextInt();}
如果(选择>=1&&选择0){
如果(player==0){//person
系统输出打印(“挑选一些弹珠:”);
pick=in.nextInt();}
如果(拾取>=1&&pick marbles/2&&marbles>1){
System.out.println(“错误:非法移动”);
player=0;继续;}
else{marbles=marbles-pick;System.out.println(marbles+“left.”);}
//当还剩下弹珠时,切换到计算机转弯
如果(大理石>0){
玩家=1;
}
如果(玩家==1&&难度==1){System.out.println(“轮到计算机拾取”);
拾取=(int)(Math.random()*marbles/2+1);
弹珠=弹珠-拾取;
println(“计算机拾取:“+拾取+”弹珠。“+”有:“+弹珠+”弹珠剩余”);}
否则如果(玩家==1&&难度==0){
System.out.println(“轮到计算机挑选”);
如果(大理石>63){pick=marbles-63;marbles=marbles-pick;}
else if(大理石>31){pick=大理石-32;大理石=大理石-pick;}else if(大理石>15){pick=大理石-15;大理石=大理石-pick;}else if(大理石>7){pick=大理石-7;大理石=大理石-pick;}else if(大理石>3){pick=大理石-3;大理石=大理石-pick;}
else{pick=(int)(Math.random()*marbles/2+1);marbles=marbles-pick;
println(“计算机拾取:“+拾取+”弹珠。“+”有:“+弹珠+”弹珠剩余”);}
//当弹珠还剩下时,将回合改为玩家
如果(大理石>0){
玩家=0;
}
然后在打破while循环后使用if-else语句,当弹珠离开0时,while循环变为false,这意味着游戏结束。
//当游戏在任何一个玩家身上停止时。。。。
如果(玩家=1){
System.out.println(“计算机赢得”);
其他的
System.out.println(“你赢了”);

问题在于此if语句:

if (player == 1 && difficulty == 1);
你永远不会让player=1,因此它永远不会进入if块

建议

我建议完全去掉player变量

将游戏转换为它自己的函数,该函数根据谁赢返回一个布尔变量。如果玩家输入的石头数无效,则使用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
 }

如果需要帮助,您需要修复缩进,并更详细地描述出现的问题
{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");
if (player == 1 && difficulty == 1);
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 (marbles > 0) {
      while (true) {
             //Player makes move
             if (validMove) {
                     //do move
                     break;
              } else {
                     //Output error and then the loop repeats itself
              }
      }

      //Computer move
 }