簡體   English   中英

無法獲得此Tic Tac Toe游戲公平競賽

[英]Can't get this Tic Tac Toe Game to play fair

這是與我合作的井字游戲。 我最初是在學期初創建這個游戲的,但出於指導老師的要求,我們現在要添加一個AI(人工智能),它可以模仿另一個玩家。 我最好的方法是制作一個具有隨機數生成器的方法。 這對我有用,但是當我運行井字游戲時,第二名玩家AI將覆蓋第一名玩家的舉動。 因此,為了防止這種情況的發生,我放入了一個條件語句,該條件語句可以檢查Jbuttun是否已禁用。 但是,它不允許我進行編譯。 然后給出的錯誤是不兼容的類型:(必需的布爾值:發現無效)。

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(無)。 您應該做的是檢查“啟用”的getter方法,因此:

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

注意“!” 在聲明之前,這使其否定

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

setEnabled()返回void,這就是為什么它給您錯誤的原因,也許您是說.isEnabled()

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

如果啟用了按鈕,則isEnabled()返回true;如果禁用,則返回false。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM