java - Nim 游戏,电脑不会走棋

标签 java

我正在尝试为学校项目制作尼姆游戏,但每当计算机尝试移动时,它实际上不会做任何事情,除非剩下三 block 或更少的石头。我的临时代码也将在控制台中执行,因此它会运行它,但它不起作用。

这些是自定义空白:

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

最佳答案

我不知道你正在制作的游戏,但我认为这是你的错误。 如果剩下超过 3 block 石头,你就打电话

   }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时,它在您的代码中从未更改。

关于java - Nim 游戏,电脑不会走棋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536674/

相关文章:

java - 计算 Parse.com 数据 - Java

c# - Bentley-Ottmann 算法实现

java - 如何将普通java Color转换为IndexedColors

java - 打开 null 分析时允许 NullPointerException 的最佳方法

java - 我可以建议 JVM 何时进行垃圾收集吗?

java - AmazonSQSClientBuilder.defaultClient() java.lang.NoSuchFieldError : No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier

java - Ehcache 磁盘存储不干净关闭

java - Camel-cxf 2.18.2 抛出 TypeConversionException

java - 如何根据启动 Intent 来更改 Activity 内容?

java - 是否可以访问接口(interface)类中泛型类型的方法?