根据Nim在编程世界中熟知的习惯来判断我不会解释游戏的精神。Java Nim代码:为什么我的循环无法正常工作
反正
所以,我想编码“非法移动”参数,否认大于一半岩石的总桩,然后返回到要求用户输入一个有效的数字的非法用户输入。问题在于它不是提示用户重新输入有效的输入,而是直接转向计算机。
这是我的代码。
import java.util.*;
public class Nim {
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int pile, turn, difficulty, pick;
pile = (int)(Math.random() * 90 + 10);
turn = (int)(Math.random() + .5);
difficulty = (int)(Math.random() + .5);
if(difficulty == 0)
System.out.println("Computer is set to HARD MODE.");
else if(difficulty == 1)
System.out.println("Computer is set to easy mode.");
System.out.println("The pile of rocks has " + pile + " total rocks. LETS GET IT ON");
while(pile>0)
{
if(turn == 0)
{
System.out.println("Your move.");
pick = in.nextInt();
if(pick >= 1 && pick <= pile/2){
pile = pile - pick;
System.out.println(pile + "left.");}
else {
System.out.println("Illegal Move.");
turn = 0;}
}
if(turn == 1 && difficulty == 1)
{
System.out.println("Computer's move.");
pick = (int)(Math.random()*pile/2+1);
pile = pile - pick;
System.out.println("Computer picks " + pick + " there are " + pile + " rocks left.");
}
else if(turn == 1 && difficulty == 0)
{
System.out.println("Computer's move.");
pick = (int)(Math.pow(2,n);
pile = pile - pick;
System.out.println("Computer picks " + pick + " there are " + pile + " rocks left.");
}
if(turn == 0)
turn = 1;
else turn = 0;
}
if(turn == 0)
System.out.println("Computer: This game is for noobs.");
else
System.out.println("Computer: Scrub.");
}}
正如你或许可以看到,它的假设来检测非法移动和重新转回到0再次提示用户的转,但它只是跳过计算机的转变。我不知道我做错了什么。
此外,我一直在试图找到一种更优雅的方式来编写计算机的'智能'模式。我的教授向我们展示了一种更为sl way的方式,他只用了一个数量(完美的方格 - 1),然后复制粘贴了不同数量的数值,但我希望能够用一个很好的紧凑公式得到它,因为它是一个更优雅的解决方案 - 我正在考虑要么使用平方根函数或功能函数,但我不确定。我可以得到任何帮助,将不胜感激。
这就是为什么每个编程的学生都应该被教导使用调试器。 –