2015-11-05 40 views
1

我正在嘗試爲學校項目製作Nim遊戲,但是隻要計算機試圖進行移動,除非剩下三個或更少的岩石,否則它不會實際執行任何操作。我的臨時代碼也將在控制檯中執行,所以它通過它運行,它只是不工作。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); 
    } 
    } 
} 

The computer never attempts to make a move unless there is 3 or less rocks left

+0

這裏的失敗具體是什麼? – Greg

+0

我添加了失敗的pciture,電腦從未真正嘗試過隨機移動,(隨機n從1-3的岩石數量),但是當剩下3個或更少的岩石時,它將會採取行動贏得勝利。 –

回答

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)); 
} 

因爲你只是改變了變量,而不調用方法上的值。(我假定重要的變量是'rocksLeft',它在你的代碼中從來沒有變過,當值大於3.