-2

我有一个问题,我有一个游戏,井字游戏,我不知道如何做到这一点,所以当有人获胜或有人移动时,它会挡住某个位置,而当有人获胜时,它不会让你做任何事情,它在 JFrame 内,我可以随时更改 O 和 X 这是代码

package project;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import java.util.Random;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Project extends JFrame
{
    static JButton startButton = new JButton();

    static ImageIcon image = new ImageIcon("src/project/imagesX1.jpg");
    static ImageIcon imageX = new ImageIcon("src/project/images1.jpg");

    static JButton restart = new JButton();

    static JLabel label0 = new JLabel();
    static JLabel label1 = new JLabel();
    static JLabel label2 = new JLabel();
    static JLabel label3 = new JLabel();
    static JLabel label4 = new JLabel();
    static JLabel label5 = new JLabel();
    static JLabel label6 = new JLabel();
    static JLabel label7 = new JLabel();
    static JLabel label8 = new JLabel();

    static JLabel vert1 = new JLabel();
    static JLabel vert2 = new JLabel();
    static JLabel horiz1 = new JLabel();
    static JLabel horiz2 = new JLabel();

    static JLabel showWinner = new JLabel();

    static int[] gridMark = new int[9];
    static boolean xTurn = true;
    static int numberClicks = 0;
    static Timer drawTimer;

    static JLabel[] choiceLabel = new JLabel[9];
    static int[] labelValue = new int[9];

    static int screen = 0;

    static int whosTurn = 0;
    static int counter = 0;

    public static void main(String[] args)
    {
        new Project().show();
    }
    public Project()
    {
        setTitle("Tic Tac Toe");
        setVisible(true);
        setResizable(true);
        addWindowListener(new WindowAdapter() 
                {
                    public void windowClosing(WindowEvent e) 
        {
            System.exit(0);
        }
        });
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints myGrid = new GridBagConstraints();
        getContentPane().setBackground(Color.white);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());



        for (int i = 0; i < 9; i++)
        {
            gridMark[i] = 0;
        }

        for (int i = 0; i < 9; i++)
        {
            labelValue[i] = 0;
        }

        choiceLabel[0] = label0;
        choiceLabel[1] = label1;
        choiceLabel[2] = label2;
        choiceLabel[3] = label3;
        choiceLabel[4] = label4;
        choiceLabel[5] = label5;
        choiceLabel[6] = label6;
        choiceLabel[7] = label7;
        choiceLabel[8] = label8;

        //ROW 1
        int x = 0;

        for (int i = 0; i < 3; i++)
        {
            myGrid = new GridBagConstraints();
            choiceLabel[i].setBackground(Color.white);
            choiceLabel[i].setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
            choiceLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
            choiceLabel[i].setOpaque(true);
            choiceLabel[i].setForeground(Color.black);
            myGrid.gridx = x;
            x+=2;
            myGrid.gridy = 0;
            //gridConstraints.insets = new Insets(10, 10, 10, 10);
            myGrid.ipadx = 40;
            myGrid.ipady = 40;
            getContentPane().add(choiceLabel[i], myGrid);
            choiceLabel[i].addMouseListener(new MouseAdapter() 
            {
                public void mouseClicked(MouseEvent e) 
                {
                    labelMouseClicked(e);
                }
            });
        }

        //ROW 2
        x = 0;

        for (int i = 3; i < 6; i++)
        {
            myGrid = new GridBagConstraints();
            choiceLabel[i].setBackground(Color.white);
            choiceLabel[i].setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
            choiceLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
            choiceLabel[i].setOpaque(true);
            choiceLabel[i].setForeground(Color.black);
            myGrid.gridx = x;
            x+=2;
            myGrid.gridy = 3;
            myGrid.ipadx = 40;
            myGrid.ipady = 40;
            getContentPane().add(choiceLabel[i], myGrid);
            choiceLabel[i].addMouseListener(new MouseAdapter() 
            {
                public void mouseClicked(MouseEvent e) 
                {
                    labelMouseClicked(e);
                }
            });
        }

        //ROW 3
        x = 0;

        for (int i = 6; i < 9; i++)
        {
            myGrid = new GridBagConstraints();
            choiceLabel[i].setBackground(Color.white);
            choiceLabel[i].setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
            choiceLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
            choiceLabel[i].setOpaque(true);
            choiceLabel[i].setForeground(Color.black);
            myGrid.gridx = x;
            x+=2;
            myGrid.gridy = 5;
            myGrid.ipadx = 40;
            myGrid.ipady = 40;
            getContentPane().add(choiceLabel[i], myGrid);
            choiceLabel[i].addMouseListener(new MouseAdapter() 
            {
                public void mouseClicked(MouseEvent e) 
                {
                    labelMouseClicked(e);
                }
            });
        }

        myGrid = new GridBagConstraints();
        horiz1.setOpaque(true);
        horiz1.setBackground(Color.blue);
        myGrid.gridx = 0;
        myGrid.gridy = 1;
        myGrid.gridwidth = 5;
        myGrid.ipadx = 500;
        myGrid.ipady = 2;
        getContentPane().add(horiz1, myGrid);

        myGrid = new GridBagConstraints();
        horiz2.setOpaque(true);
        horiz2.setBackground(Color.blue);
        myGrid.gridx = 0;
        myGrid.gridy = 4;
        myGrid.gridwidth = 5;
        myGrid.ipadx = 500;
        myGrid.ipady = 2;
        getContentPane().add(horiz2, myGrid);

        myGrid = new GridBagConstraints();
        vert1.setOpaque(true);
        vert1.setBackground(Color.blue);
        vert1.setVerticalAlignment(SwingConstants.CENTER);
        myGrid.gridx = 1;
        myGrid.gridy = 0;
        myGrid.gridheight = 6;
        myGrid.ipadx = 2;
        myGrid.ipady = 500;
        //gridConstraints.insets = new Insets(10, 0, 10, 0);
        getContentPane().add(vert1, myGrid);

        myGrid = new GridBagConstraints();
        vert2.setOpaque(true);
        vert2.setBackground(Color.blue);
        myGrid.gridx = 3;
        myGrid.gridy = 0;
        myGrid.gridheight = 6;
        myGrid.ipadx = 2;
        myGrid.ipady = 500;
        //gridConstraints.insets = new Insets(10, 0, 10, 0);
        getContentPane().add(vert2, myGrid);


        myGrid = new GridBagConstraints();
        showWinner.setBackground(Color.white);
        showWinner.setOpaque(true);
        myGrid.gridx = 0;
        myGrid.gridy = 6;
        myGrid.gridwidth = 5;
        myGrid.gridheight = 6;
        getContentPane().add(showWinner, myGrid);

        restart.setText("Restart");
        myGrid.gridx = 1;
        myGrid.gridy = 7;
        getContentPane().add(restart, myGrid);
        restart.setVisible(false);
        restart.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                restartActionPerformed(e);
            }
        });

        pack();
    }

    private void labelMouseClicked(MouseEvent e)
    {
        Component clickedComponent = e.getComponent();
        int choice;
        for (choice = 0; choice < 9; choice++)
        {
            if (clickedComponent == choiceLabel[choice])
            {
                break;
            }
        }
        choiceLabel[choice].setBackground(Color.WHITE);

        if (whosTurn == 0)
        {
            choiceLabel[choice].setIcon(image);
            labelValue[choice] = 1;
            whosTurn = 1;
        }


        else if (whosTurn == 1)
        {
            choiceLabel[choice].setIcon(imageX);
            labelValue[choice] = 5;
            whosTurn = 0;
        }

        counter += 1;
        checkWinner();
    }
    public static void checkWinner()
    {
        if (labelValue[0] + labelValue[1] + labelValue[2] == 3 || labelValue[3] + labelValue[4] + labelValue[5] == 3 || labelValue[6] + labelValue[7] + labelValue[8] == 3 ||
                labelValue[0] + labelValue[3] + labelValue[6] == 3 || labelValue[1] + labelValue[4] + labelValue[7] == 3 || labelValue[2] + labelValue[5] + labelValue[8] == 3 || 
                labelValue[0] + labelValue[4] + labelValue[8] == 3 || labelValue[2] + labelValue[4] + labelValue[6] == 3)
        {
            showWinner.setFont(new Font("Arial" , Font.BOLD, 30));
            showWinner.setText("X wins!");
            restart.setVisible(true);

        }

        else if (labelValue[0] + labelValue[1] + labelValue[2] == 15 || labelValue[3] + labelValue[4] + labelValue[5] == 15 || labelValue[6] + labelValue[7] + labelValue[8] == 15 ||
                labelValue[0] + labelValue[3] + labelValue[6] == 15 || labelValue[1] + labelValue[4] + labelValue[7] == 15 || labelValue[2] + labelValue[5] + labelValue[8] == 15 || 
                labelValue[0] + labelValue[4] + labelValue[8] == 15 || labelValue[2] + labelValue[4] + labelValue[6] == 15)
        {
            showWinner.setFont(new Font("Arial" , Font.BOLD, 30));
            showWinner.setText("O wins!");
            restart.setVisible(true);
        }

        else if (counter == 9)
        {
            showWinner.setFont(new Font("Arial" , Font.BOLD, 30));
            showWinner.setText("It's a draw");
            restart.setVisible(true);
        }

    }

    public static int getGridSelected (int a, int b, int c, int d)
    {
        if(gridMark[a] + gridMark[b] + gridMark[c] == d)
        {
            if(gridMark[a] == 0) return a;
            else if(gridMark[b] == 0) return b;
            else if(gridMark[c] == 0) return c;
        }
        return -1;
    }
    public void restartActionPerformed(ActionEvent e)
    {

    }
}

人工智能框架:

public static void makeMove()
{
    myFrame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });
    int gridSelected;
    int y;
    Random r = new Random();
    Graphics myGraphics = myFrame.getGraphics();

    //find gridSelected for "can win"
    if (getGridSelected(0,1,2,10) != -1) gridSelected = getGridSelected(0,1,2,10);
    else if (getGridSelected(3,4,5,10) != -1) gridSelected = getGridSelected(3,4,5,10);
    else if (getGridSelected(6,7,8,10) != -1) gridSelected = getGridSelected(6,7,8,10);
    else if (getGridSelected(0,3,6,10) != -1) gridSelected = getGridSelected(0,3,6,10);
    else if (getGridSelected(1,4,7,10) != -1) gridSelected = getGridSelected(1,4,7,10);
    else if (getGridSelected(2,5,8,10) != -1) gridSelected = getGridSelected(2,5,8,10);
    else if (getGridSelected(0,4,8,10) != -1) gridSelected = getGridSelected(0,4,8,10);
    else if (getGridSelected(2,4,6,10) != -1) gridSelected = getGridSelected(2,4,6,10);

    //find gridSelected for "block win"
    else if (getGridSelected(0,1,2,2) != -1) gridSelected = getGridSelected(0,1,2,2);
    else if (getGridSelected(3,4,5,2) != -1) gridSelected = getGridSelected(3,4,5,2);
    else if (getGridSelected(6,7,8,2) != -1) gridSelected = getGridSelected(6,7,8,2);
    else if (getGridSelected(0,3,6,2) != -1) gridSelected = getGridSelected(0,3,6,2);
    else if (getGridSelected(1,4,7,2) != -1) gridSelected = getGridSelected(1,4,7,2);
    else if (getGridSelected(2,5,8,2) != -1) gridSelected = getGridSelected(2,5,8,2);
    else if (getGridSelected(0,4,8,2) != -1) gridSelected = getGridSelected(0,4,8,2);
    else if (getGridSelected(2,4,6,2) != -1) gridSelected = getGridSelected(2,4,6,2);

    //is the center available?
    else if (gridMark[4] == 0)gridSelected = 4;

    //if all else fail pick a random blank square
    else
    {
        while(true)
        {
            gridSelected = r.nextInt(9);
            if (gridMark[gridSelected] == 0)break;
        }
    }
    int x = 40 + (gridSelected % 3) * 80;
    if (gridSelected <= 2) y = 50;
    else if (gridSelected <= 5) y = 130;
    else y = 210;

    // draw O
    gridMark[gridSelected] = 5;
    myGraphics.setColor(Color.red);
    myGraphics.drawOval(x, y, 60, 60);
    checkWinner();
    xTurn = true;
    if (numberClicks != 10)
    {
        numberClicks++;
    }

}
4

2 回答 2

0
public class Project extends JFrame
{
    static boolean gameHasEnded;

    ...

    public Project()
    {
        ...
        gameHasEnded = false;
    }

    ...

    private void labelMouseClicked(MouseEvent e)
    {
        // If any player has already won, abort. Label will not be changed.
        // Players turn will not be switched.
        if (gameHasEnded) return;

        ...

        // If the label has already been clicked, abort.
        if (labelValue[choice] != 0) return;

        choiceLabel[choice].setBackground(Color.WHITE);

        ...
    }

    public static void checkWinner()
    {
        ...
        restart.setVisible(true);
        gameHasEnded = true;
        ...
        restart.setVisible(true);
        gameHasEnded = true;
        ...
        restart.setVisible(true);
        gameHasEnded = true;
        ...
    }

    public void restartActionPerformed(ActionEvent e)
    {
        for (int i = 0; i < 9; i++)
        {
            labelValue[i] = 0;
            choiceLabel[i].setIcon(null);
            choiceLabel[i].setBackground(Color.white);
        }
        showWinner.setText("");
        restart.setVisible(false);
        counter = 0;
        whosTurn = 0;
        gameHasEnded = false;
    }
}

编辑:您似乎有两个“网格”。JFrame 版本使用labelValue和 Fame 版本gridMark

  1. 删除字段和的声明gridMark,因为它们不应该被使用。numberClicksxTurn
  2. gridMark用替换所有引用labelValue
  3. numberClicks用替换所有引用counter
  4. 更改xTurn = true;whosTurn = 0;
  5. 替换绘图命令。(例如myGraphics.drawOval(x, y, 60, 60);)与choiceLabel[gridSelected].setIcon(imageX);
  6. 在调用checkWinner()in之后labelMouseClicked,插入:

    if (!gameHasEnded)
    {
        makeMove();
    }
    

一个提示:删除您在 JFrame 版本中未使用的所有字段。这将使所有对 Frame-version 变量的引用都被突出显示为错误。

于 2012-06-15T05:58:07.240 回答
0

labelMouseClicked()您必须检查该字段是否已被单击。例如检查choiceLabel[choice].getBackground()

if (choiceLabel[choice].getBackground().equal(Color.WHITE))
{
  // already clicked
  return;
}

要检查游戏是否结束,您可以返回一个布尔值,checkWinner()或者更好地为它创建一个新函数:isGameEnd(). 您可以在labelMouseClicked()和中使用它checkWinner()

于 2012-06-15T06:02:24.657 回答