gpt4 book ai didi

java - 无法让这款井字游戏公平进行

转载 作者:行者123 更新时间:2023-12-01 17:14:32 25 4
gpt4 key购买 nike

这是一个跟我合作的井字游戏。我最初在学期初创建了这个游戏,但是老师希望我们现在添加一个 AI(人工智能),它可以模仿其他玩家。我采取的最好方法是创建一个具有随机数生成器的方法。这对我有用,但是当我运行井字棋游戏时,第二个玩家人工智能会覆盖第一个玩家的 Action 。因此,为了防止这种情况发生,我放入了一个条件语句,可以检查 Jbuttun 是否被禁用。但是它不允许我编译。然后它给出的错误是不兼容的类型:(必需 boolean 值:找到 Void)。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;

public class TicTacToe extends JFrame
{
private final int HEIGHT = 450;
private final int WIDTH = 500;
private static JButton [] button = new JButton[9];
private static Action [] playerTurn = new Action[9];
private Font arial = new Font("Arial", Font.BOLD, 20);
private static int lockButtons = 0;
private boolean game = false;
private static Random rNum = new Random();



public TicTacToe ()
{
setTitle( "TTT");
setSize( HEIGHT, WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


setLayout(new GridLayout(4,3));

int num = 0;
for(int i = 0; i < 9; i++ )
{

button[i] = new JButton( "B" + (i + 1));
playerTurn[i] = new Action();
add(button[i]);
button[i].setBorder(BorderFactory.createLineBorder(Color.black,10));
button[i].setFont(arial);
button[i].addActionListener(playerTurn[i]);
}


setVisible(true);
}

private class Action implements ActionListener
{
public void actionPerformed(ActionEvent playerMove)
{
//Get button pressed using GetSource Command
JButton whatPlayer=(JButton)(playerMove.getSource());

for ( int x =0; x <= button.length; x++)
{
whatPlayer.setText("o");
whatPlayer.setEnabled(false);
validate();
JOptionPane.showMessageDialog(null," Computer's Turn ");
player2();
break;
}
}

public void player2()
{
for ( int i = 0; i <= button.length ; i++)
{
int num = rNum.nextInt(8);
button[num].setText( "x");
button[num].setEnabled(false);
validate();

// This is the block that is causing the compiler error
/*if( button[i].setEnabled(false))
{
JOptionPane.showMessageDialog(null," Button is disables ");
break;
}*/
break;
}
}

public void validate()
{
if(button[0].getText().equals(button[1].getText()) && button[1].getText().equals(button[2].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
gameOver();
return;
}
else if(button[3].getText().equals(button[4].getText()) && button[4].getText().equals(button[5].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[3].getText());
gameOver();
return;
}
else if(button[6].getText().equals(button[7].getText()) && button[7].getText().equals(button[8].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[6].getText());
gameOver();
return;
}
else if(button[0].getText().equals(button[3].getText()) && button[3].getText().equals(button[6].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
gameOver();
return;
}
else if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
gameOver();
return;
}
else if(button[2].getText().equals(button[5].getText()) && button[5].getText().equals(button[8].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
gameOver();
return;
}
else if(button[0].getText().equals(button[4].getText()) && button[4].getText().equals(button[8].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
gameOver();
return;
}
else if(button[2].getText().equals(button[4].getText()) && button[4].getText().equals(button[6].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
gameOver();
return;
}

int i;

for(i=0;i<button.length;i++)
{
if(button[i].isEnabled())
{
break;
}
}
if(i == button.length)
{
JOptionPane.showMessageDialog(null,"This was a Draw");
}
}
public void gameOver()
{
for( int x = 0; x < button.length; x++)
{
button[x].setEnabled(false);
}
}
}
public static void main(String[] arg)
{
new TicTacToe();
}
}

最佳答案

if( button[i].setEnabled(false))
{
JOptionPane.showMessageDialog(null," Button is disables ");
break;
}

您确实试图在条件语句中检查 void 类型,这是不可能的。由于 setEnabled 返回 void(什么也没有)。您应该做的是检查“Enabled”的 getter 方法,因此:

if (!button[i].isEnabled()) {
JOptionPane.showMessageDialog(null," Button is disables ");
break;
}

注意“!”在声明之前,这使其变得消极

关于java - 无法让这款井字游戏公平进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22746280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com