문제

Ok after rearranging me logical approach of this tic tac toe game I was able to clean a few bugs except the biggest one. Why is the AI rewriting the user selection. I have tried multiple approaches to this problem however it is still over writing the users moves. How or what should I do in order to prevent this from happening. I have my code posted with inline comments in order to show where my problem is originating from

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();
private static int player = 1;



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++)
                {
                    if (player == 1)
                    {
                        whatPlayer.setText("O");
                        whatPlayer.setEnabled(false);
                        validate();
                        JOptionPane.showMessageDialog(null," Computer's Turn ");
                        player++;
                    }

                   // this block is the root and cause of my head ache

                    if( player == 2)
                    {
                        int num = rNum.nextInt(8);
                        button[num].setText("X");
                        JOptionPane.showMessageDialog(null,"" + num ); // this is for debugging
                        button[num].setEnabled(false);
                        validate();
                        player--;

                    // This conditional statement is not being executed

                    if(button[num].equals("X") || button[num].equals("O"))
                        {
                            JOptionPane.showMessageDialog(null," Button is disables ");
                            return;
                        }

                        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();
}   
}
도움이 되었습니까?

해결책

Your "AI" consists of this line:

int num = rNum.nextInt(8);

That's just picking one of 8 (why not 9?) squares at random, with no checking whether or not the space is already taken.

If you really want to just pick a random space, you need to do so in a loop:

int num;
do {
   num = rNum.nextInt(9);
} while (!button[num].isEnabled()); // Keep going until we find a vacant space
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top