作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了可能是井字棋游戏最简单的解决方案,我使用了一个 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;
因此,如果玩家点击某个字段,
然后,使用模型更新板 View 。
关于java - 井字棋实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862184/
我有一个模板,可以获取从外部程序生成的所有信息。我试图让这个模板更现代一点,而不是 1992 年。 用于处理表格的页面。它看起来不太好,所以我试图移除 table ,让屏幕上的所有内容都流畅。 对于文
我使用 bootstrap 井来模拟卡片。我目前有两种不同类型的卡片,“普通”卡片位于屏幕中间,“特殊”卡片位于左侧和右侧。 我正在尝试复制的模板: 问题: 1.) bootstrap 中的井似乎不想
我有一个井 div,我想在 Angular 落附上一个小文本/图像,如此处所示 ( http://draw.to/D46BqsC )...并让该元素相对于井放置,以便它发生变化位置和井一样。什么是最好
我是一名优秀的程序员,十分优秀!