skip to Main Content

This is a tic tac toe game to cooperate with me. I have originally created this game in the beginning of the semester, however out instructor wants us to now add an AI ( artificial intelligence ), which can imitate another player. The best approach I took is to make a method that has a random number generator. This worked for me, however when I was running the tic tac toe game the 2nd player AI would overwrite the 1st player move. So in order to prevent this from happening I put in a conditional statement which can check if the Jbuttun is disabled. However it is not allowing me to compile. Then error it is giving is incompatible type: ( required Boolean: found 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();
}   
}

2

Answers


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

    setEnabled() returns void that’s why it gives you error, maybe you mean .isEnabled()?

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

    isEnabled() returns true if the button is enabled, false if disabled.

    Login or Signup to reply.
  2. if( button[i].setEnabled(false))
    {
        JOptionPane.showMessageDialog(null," Button is disables ");
        break;
    }
    

    You are indeed trying to check a void type in a conditional statement, this can’t be done.
    Since setEnabled returns void (nothing).
    What you should be doing is checking the getter method of ‘Enabled’, so:

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

    Note the ‘!’ before the statement, this makes it negative

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search