如何让电脑花时间在游戏中玩游戏

时间:2015-11-06 05:11:56

标签: java

我正在制作一个Nim游戏。我希望让电脑花些时间来让游戏感觉更加逼真,而不是让电脑立即回归。

计算机移动的结果是,当按下按钮时,整个程序只能休眠1.6秒,然后两个玩家一起移动,计算机移动一次。

public void playersMove() throws BadLocationException {
    lastPlayer = 0; //for winning check purposes
    try {
        playersStones = Integer.parseInt(txtfPlayer.getText()); // gets input from player
        if (playersStones <= 3 && playersStones >= 1) {
            isValid = true; // play is valid
            stonesLeft -= playersStones;
            logBox.append("You have taken " + playersStones + " stones.\nThere are: " + stonesLeft + " stones left.");
            if(stonesLeft != 0){ //if the game is over it does not say it's another person's turn
                logBox.append("\nIt is the computer's turn.\n\n");
            }
        } else {
            isValid = false; //play is not valid
            logBox.append("Please only take 1-3 stones!\n\n");
        }
    } catch (NumberFormatException e) {
        isValid = false;//play is not valid
        logBox.append("Please only take 1-3 stones!\n\n");
    }
    winnerCheck(); // checks if player lost
}

public void computersMove() throws BadLocationException {
    if (isValid) {
        try {
            Thread.sleep(2300); // attempt to make the computer realistic
        } catch (InterruptedException ex) {}
        lastPlayer = 1; //for winning check purposes
        switch (stonesLeft) {
            //computer attempts to win
            //if winning moves not in range, will generate a random number to use
            case 1:
                stonesLeft = 0;
                logBox.append("The computer takes 1 stones.\n There are: 0 stones left.\n\n Y O U    H A V E    W O N\n\nPress the reset button to play a new game.");
                break;
            case 2:
                computersOutput(1);
                break;
            case 3:
                computersOutput(2);
                break;
            case 4:
                computersOutput(3);
                break;
            case 5:
                computersOutput(1);
                break;
            default:
                computersOutput((int) (Math.random() * (3 - 1 + 1) + 1));
                break;
        }
        winnerCheck(); //checks if computer lost
    }
}
private void buttonPlayActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
        if(lastPlayer == 1 && !gameOver){  //player makes play
            playersMove();
            if(lastPlayer == 0 && !gameOver && isValid){ //only if play is valid computer plays
                computersMove();
            }
        }
        if(!isValid){
            playersMove();
            computersMove();
        }
    } catch (BadLocationException ex) {}
}

3 个答案:

答案 0 :(得分:0)

你的方向正确。但是,我看到的唯一问题是你引入的延迟是静态的。因此,在这种情况下,每次移动不均匀的随机延迟会更合适。

通过使用指示秒数的整数值设置适当的minDelay和maxDelay值,使用以下代码段。

Thread.sleep(minDelay + (new Random.nextInt(maxDelay)*1000));

这会导致minDelay和maxDelay之间出现可变延迟。

答案 1 :(得分:0)

您可以使用java.swing.Timer让计算机等待一段时间。首先你需要导入它。然后你需要在计时器“滴答”时写下你想要做的事情:

ActionEvent action = new ActionEvent {
    public void actionPerformed(ActionEvent e) {
        //do what you want here

    }
}

现在你创建了计时器:

final Timer t = new Timer (delayInMilliseconds, action);

然后你开始吧:

t.start();

现在当计时器打勾时你想立即停止它,所以将actionPerformed更改为:

public void actionPerformed(ActionEvent e) {
    //do what you want here
    t.stop();
}

就是这样!

答案 2 :(得分:0)

整个程序停止的原因是因为计算机的移动和玩家的移动都发生在同一个线程上,所以当你调用Thread.sleep时整个线程都处于睡眠状态。

您可以为计算机创建另一个线程,并在移动之前让该线程休眠。当计算机完成移动时,您可以将其与当前线程一起加入以继续游戏。您可以阅读有关线程here的更多信息。

这是一个简单的例子:

public static class Computer implements Runnable {
    public void run() {
        try {
        Thread.sleep(2300);
        } catch (InterruptedException ex) {}
        // Implement computer's move here
    }
}

// In your main thread
public static void main(String[] args) {
    Thread computer = new Thread(new Computer());  // Create new computer

    // Implement player making his move here

    if(lastPlayer == 0 && !gameOver && isValid){
        computer.start();
    }

    computer.join(3000);  // Wait a maximum of 3 seconds for computer to finish its move

    // Continue with rest of program
}