我正在尝试为学校项目制作一个Nim游戏,但每当计算机试图移动时,除非有三个或更少的岩石,否则它实际上不会做任何事情。我的临时代码也将在控制台中执行,因此它贯穿它,它只是不起作用。
这些是自定义空洞:
public void winnerCheck(){
if(rocksLeft == 0 && lastPlayer == 0){
logBox.append("Plose gameOver");
}else if(rocksLeft == 0 && lastPlayer == 1){
logBox.append("Close gameOver");
}
//temp
System.out.println("winnerCheck() successful");
}
public void playersMove() throws BadLocationException{
lastPlayer = 0;
//used to gather players input and attempt to make a move
try{
playersRocks = Integer.parseInt(txtfPlayer.getText());
if(playersRocks <= 3 && playersRocks>=1){
rocksLeft -= playersRocks;
logBox.append("You have taken "+playersRocks+" rocks.\nThere are: "+rocksLeft+" rocks left.\nIt is the computer's turn.\n\n");
}else{
isValid = false;
}
}catch(NumberFormatException e){
isValid = false;
}
//temp
System.out.println("playersMove() successful");
}
public void computerAttempt(int computersRocks){
//this void is a snippet for the computer trying to make a play.
//contains only outputs and rocksLeft altering equation
logBox.append("\nThe computer is making a play.\n");
try {
//makes the game feel more realistic, as the computer takes time to make a move.
Thread.sleep(1600);
} catch (InterruptedException ex) {
Logger.getLogger(GameOfNim.class.getName()).log(Level.SEVERE, null, ex);
}
rocksLeft -= computersRocks;
logBox.append("The computer has taken "+computersRocks+" rocks.\nThere are: "+rocksLeft+" rocks left.\nIt is your turn!\n\n");
}
public void computersMove() throws BadLocationException{
lastPlayer = 1;
//computer will attempt to win, if not possible at the time it will take a random number
if(rocksLeft == 3){
computerAttempt(2);
}else if(rocksLeft == 2){
computerAttempt(1);
}else if(rocksLeft == 1){
logBox.append("The computer takes 1 rock.\n There are: 0 rocks left.\n\n Y O U H A V E W O N\n\n");
}else if(rocksLeft > 3){
computersRocks = (int) (Math.random()*(3-1+1)+1);
}
//temp
System.out.println("computersMove() succesful");
}
这是两个按钮:
private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {
//starts/resets game
rocksLeft = (int)(Math.random()*(30-15+1)+15);
buttonStart.setText("Reset");
buttonPlay.setEnabled(true);
logBox.setText("T h e g a m e h a s b e g u n !\n\nThere are: "+rocksLeft+" rocks left.\nIt is your turn!\n\n");
}
private void buttonPlayActionPerformed(java.awt.event.ActionEvent evt) {
//check if game is over
if(rocksLeft == 0){
logBox.append("The game has completed!\n Press the reset button to play again!");
System.out.println("gameOver succesful");
}else{
try {
//allows each player to move then checks if the player won or not
playersMove();
winnerCheck();
computersMove();
winnerCheck();
//temp
System.out.println("buttonPlay() succesful\n");
} catch (BadLocationException ex) {
Logger.getLogger(GameOfNim.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 0 :(得分:1)
我不知道你正在制作的游戏,但我认为这是你的错误。 如果剩下3个以上的岩石,请致电
}else if(rocksLeft > 3){
computersRocks = (int) (Math.random()*(3-1+1)+1);
}
我认为应该是
}else if(rocksLeft > 3){
computerAttempt((int) (Math.random()*(3-1+1)+1));
}
因为你刚刚更改了变量而没有在值上调用方法。(我认为重要的变量是&#39; rocksLeft&#39;它们在代码中从未改变过,当值为> 3时。