這是一個tic tac腳趾遊戲與我合作。我原本在學期開始時創建了這個遊戲,但是教練希望我們現在添加一個AI(人工智能),它可以模仿另一個玩家。我採取的最佳方法是製作一個具有隨機數生成器的方法。這對我來說很有效,但是當我運行tic tac toe遊戲時,第二位玩家AI會覆蓋第一位玩家移動。所以爲了防止這種情況發生,我放入了一個條件語句來檢查Jbuttun是否被禁用。但是它不允許我編譯。然後它給出的錯誤是不兼容的類型:(必需的布爾值:找到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();
}
}
你的問題說你的代碼有編譯錯誤。但它編譯得很好。 – aliteralmind
我知道我的player 2方法中有一個條件語句,它已經作爲塊語句放置,因爲它是在一個塊語句中編譯的。 – EasyE