gpt4 book ai didi

java - 井字棋实现

转载 作者:行者123 更新时间:2023-12-02 07:38:39 26 4
gpt4 key购买 nike

我已经实现了可能是井字棋游戏最简单的解决方案,我使用了一个 GUI 来表示它。所以我想问你是否可以给我一些建议和想法如何使它更优雅。我试图改进的部分是 TicTacToe 类以及 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);
}

}

最佳答案

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

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,则我们有平局)

然后,使用模型更新板 View 。

关于java - 井字棋实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com