-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)
    {

    }
}

フレームの AI:

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

ではlabelMouseClicked()、フィールドがすでにクリックされているかどうかを確認する必要があります。たとえば、次を確認してくださいchoiceLabel[choice].getBackground()

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

ゲームが終了したかどうかを確認するには、ブール値を返すcheckWinner()か、新しい関数を作成しますisGameEnd()labelMouseClicked()とで使用できますcheckWinner()

于 2012-06-15T06:02:24.657 に答える