0

我已经实现了游戏 tictactoe 的最简单的解决方案,我使用了一个 gui 来表示它。所以我问你是否可以给我一些建议和想法,如何让它更优雅。我要改进的部分是井字游戏类以及mousePressed 和mouseEntered 方法。这是代码。

/**The TicTacToe class with the GUI
 */
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class TicTacToe extends JFrame implements MouseListener
{
    JLabel[] jl ={
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel()} ;

    ImageIcon[] ox = {
            new ImageIcon("tic/o.gif"),
            new ImageIcon("tic/x.gif")  
    };

    static boolean isFirst;
    static int counter = 1;

    public TicTacToe()
    {

        JPanel panel = new JPanel(new GridLayout(3,3));

        Border border = new LineBorder(Color.red, 2);
        Border border2 = new LineBorder(Color.blue, 2);



        for(int i=0; i<jl.length; i++)
        {
            jl[i].setBorder(border2);
            jl[i].addMouseListener(this); 
            panel.add(jl[i] );
        }
        panel.setBorder(border);
        this.add(panel);




    }

    public static String count()
    {
        counter++;

        if(counter % 2 != 0)
        {
            isFirst  = true;
            return "First";
        }
            isFirst = false;
            return "Second";

    }


    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }


    @Override
    public void mousePressed(MouseEvent me)
    {
        int count = 0;
        count();


        for(int i=0; i<jl.length; i++)
        {

            if(jl[0].getIcon() == ox[0]   && jl[1].getIcon() == ox[0] && jl[2].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won ");
                System.exit(0);
            }

            else if(jl[3].getIcon() == ox[0]   &&  jl[4].getIcon() == ox[0] && jl[5].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won ");
                System.exit(0);
            }

            else if(jl[6].getIcon() == ox[0]   && jl[7].getIcon() == ox[0] && jl[8].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won ");
                System.exit(0);
            }


            else if(jl[0].getIcon() == ox[0]   && jl[4].getIcon() == ox[0] && jl[8].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won with the major diagonal ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[0]   && jl[4].getIcon() == ox[0] && jl[6].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won with the subdiagonal ");
                System.exit(0);
            }

            else if(jl[0].getIcon() == ox[0]   && jl[3].getIcon() == ox[0] && jl[6].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won  ");
                System.exit(0);
            }

            else if(jl[1].getIcon() == ox[0]   && jl[4].getIcon() == ox[0] && jl[7].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won  ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[0]   && jl[5].getIcon() == ox[0] && jl[8].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won  ");
                System.exit(0);
            }

            else if(jl[0].getIcon() == ox[1]   && jl[1].getIcon() == ox[1] && jl[2].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won ");
                System.exit(0);
            }

            else if(jl[3].getIcon() == ox[1]   &&  jl[4].getIcon() == ox[1] && jl[5].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won ");
                System.exit(0);
            }

            else if(jl[6].getIcon() == ox[1]   && jl[7].getIcon() == ox[1] && jl[8].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won ");
                System.exit(0);
            }


            else if(jl[0].getIcon() == ox[1]   && jl[4].getIcon() == ox[1] && jl[8].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won with the major diagonal ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[1]   && jl[4].getIcon() == ox[1] && jl[6].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won with the subdiagonal ");
                System.exit(0);
            }

            else if(jl[0].getIcon() == ox[1]   && jl[3].getIcon() == ox[1] && jl[6].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won  ");
                System.exit(0);
            }

            else if(jl[1].getIcon() == ox[1]   && jl[4].getIcon() == ox[1] && jl[7].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won  ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[1]   && jl[5].getIcon() == ox[1] && jl[8].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won  ");
                System.exit(0);
            }




            if(me.getSource() == jl[i])
            {

                if(jl[i].getIcon() == ox[0] || jl[i].getIcon() == ox[1])
                {
                    JOptionPane.showMessageDialog(null,
                            "You can't insert at " + (i + 1) +  " the place is already taken ");
                    break;
                }


                if(isFirst)
                {
                    jl[i].setIcon(ox[0]);
                }
                else
                {
                    jl[i].setIcon(ox[1]);
                }
            }
        }



    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) 
    {
        if((jl[0].getIcon() == ox[0] || jl[0].getIcon() == ox[1]) && (jl[1].getIcon() == ox[0] || jl[1].getIcon() == ox[1] ) &&
                 (jl[2].getIcon() == ox[0] || jl[2].getIcon() == ox[1]) && (jl[3].getIcon() == ox[0] || jl[3].getIcon() == ox[1] ) &&
                    (jl[4].getIcon() == ox[0] || jl[4].getIcon() == ox[1]) && (jl[5].getIcon() == ox[0] || jl[5].getIcon() == ox[1] ) &&
                    (jl[6].getIcon() == ox[0] || jl[6].getIcon() == ox[1]) && (jl[7].getIcon() == ox[0] || jl[7].getIcon() == ox[1] )
                    && (jl[8].getIcon() == ox[0] || jl[8].getIcon() == ox[1] ))
            {

                    JOptionPane.showMessageDialog(null,
                            "Draw");
                    System.exit(0);

            }

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

/** The tester class with the main method and the frame
 */
import javax.swing.JFrame;
public class TestTicTacToe 
{
    public static void main(String[] args)
    {
//      JFrame application = new JFrame();
        TicTacToe frame = new TicTacToe();
        frame.setLocationByPlatform(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.setVisible(true);
    }

}
4

1 回答 1

2

不要将图标用作状态!为板子和字段上的标记使用适当的(数字)模型,例如

int[] board = new int[3][3];
final static int X_MARK = 1;
final static int O_MARK = -1;

因此,如果玩家点击某个区域,

  1. 检查该字段是否为空(对应的数组单元格的值为 0)
  2. 将单元格值更改为 1 或 -1
  3. 计算我们是赢家还是平局(只需计算行、列和对角线的总和,如果总和为 3 或 -3,则我们有赢家,否则,如果没有字段为 0,我们有平局)

然后,使用模型更新板视图。

于 2012-08-08T10:16:16.843 回答