Java 如何让电脑花时间玩游戏
我在和尼姆玩游戏。我希望让电脑花时间移动,让游戏感觉更真实,而不是让电脑立即后退 在电脑移动中发生的是,当按下按钮时,整个程序仅休眠1.6秒,然后同时播放播放器移动和电脑移动Java 如何让电脑花时间玩游戏,java,Java,我在和尼姆玩游戏。我希望让电脑花时间移动,让游戏感觉更真实,而不是让电脑立即后退 在电脑移动中发生的是,当按下按钮时,整个程序仅休眠1.6秒,然后同时播放播放器移动和电脑移动 public void playersMove() throws BadLocationException { lastPlayer = 0; //for winning check purposes try { playersStones = Integer.parseInt(txtfPl
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) {}
}
public void playersMove()引发BadLocationException{
lastPlayer=0;//用于中奖检查
试一试{
playersStones=Integer.parseInt(txtfPlayer.getText());//从播放器获取输入
如果(playersStones=1){
isValid=true;//播放有效
stonesLeft-=玩家石;
logBox.append(“你已经拿走了”+playersStones+”石头。\n这里有:“+stonesLeft+”石头剩余。”);
如果(Stonsleft!=0){//如果游戏结束,它不会说轮到另一个人了
logBox.append(“\n轮到计算机了。\n\n”);
}
}否则{
isValid=false;//播放无效
logBox.append(“请只取1-3块石头!\n\n”);
}
}捕获(数字格式){
isValid=false;//播放无效
logBox.append(“请只取1-3块石头!\n\n”);
}
winnerCheck();//检查播放器是否丢失
}
public void computersMove()引发BadLocationException{
如果(有效){
试一试{
Thread.sleep(2300);//尝试使计算机逼真
}catch(InterruptedException ex){}
lastPlayer=1;//用于中奖检查
开关(Stonsleft){
//计算机试图获胜
//如果获胜的移动不在范围内,将生成一个随机数以供使用
案例1:
stonesLeft=0;
logBox.append(“计算机拿走了1块石头。\n还有:0块石头。\n\n是的,没有\n\n按重置按钮玩新游戏。”);
打破
案例2:
计算机输出(1);
打破
案例3:
计算机输出(2);
打破
案例4:
计算机输出(3);
打破
案例5:
计算机输出(1);
打破
违约:
计算机输出((int)(Math.random()*(3-1+1)+1));
打破
}
winnerCheck();//检查计算机是否丢失
}
}
private void buttonPlayActionPerformed(java.awt.event.ActionEvent evt){
试一试{
如果(lastPlayer==1&&!gameOver){//玩家开始游戏
playersMove();
如果(lastPlayer==0&&!gameOver&&isValid){//仅当播放有效时计算机播放
计算机移动();
}
}
如果(!isValid){
playersMove();
计算机移动();
}
}catch(BadLocationException ex){}
}
您的方向是正确的。但是,我看到的唯一问题是,您引入的延迟是静态的。因此,在这种情况下,并非每次移动都一致的随机延迟更合适
通过使用指示秒数的整数值设置适当的minDelay和maxDelay值,使用以下代码段
Thread.sleep(minDelay + (new Random.nextInt(maxDelay)*1000));
这将导致minDelay和maxDelay之间的可变延迟。您可以使用
java.swing.Timer
让计算机等待一段时间。首先,您当然需要导入它。然后你需要写下当计时器“滴答”时你想做什么:
现在创建计时器:
final Timer t = new Timer (delayInMilliseconds, action);
然后你开始:
t.start();
现在,当计时器滴答作响时,您希望立即停止计时,因此将actionPerformed
更改为:
public void actionPerformed(ActionEvent e) {
//do what you want here
t.stop();
}
就这样 整个程序停止的原因是,计算机的移动和玩家的移动都发生在同一个线程上,因此当您调用thread.sleep时,整个线程都会进入休眠状态 您可以为计算机创建另一个线程,并在移动之前使该线程处于休眠状态。当计算机完成移动后,您可以将其与当前线程连接起来继续游戏。您可以阅读更多关于线程的信息 下面是一个简单的例子:
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
}
尝试使用timertask!一个线程=两个线程都停止。两个线程=BAM!!
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
}