我说下我的想法:
看坐标: 横坐标一致
或纵坐标一致
还有就是/.\两种情况下 固定的位置你事先固定好
当三个点都有了棋子就一直线
以前写过一个java的井字棋 ,
其中的重点是要判断每走一步后,是否有比赛的结果(输,赢,平)
可以使用swing 来作为外观进行显示.
表示棋盘如下
定义一个二维数组,每次走完后,匹配该数组, 如果匹配成功就赢了
效果图
当然了,因为井字棋比较简单, 可以写一个比较简单的判断局势,然后自动下棋的AI .
(AI使用了很多的if else判断, 比如人现在的情况是什么样的,有几个棋子连在一起了,电脑自己的情况是怎么样的)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JApplet
{
class ToeDialog extends JDialog
private int turn=XX;
ToeDialog(int cellsWide,int cellsHigh)
setTitle("The game itself");
Container cp=getContentPane();
cp.setLayout(new GridLayout(cellsWide,cellsHigh));
for(int i=0;icellsWide*cellsHigh;i++)
cp.add(new ToeButton());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
class ToeButton extends JPanel
private int state=BLANK;
public ToeButton()
addMouseListener(new ML());
public void paintComponent(Graphics g)
super.paintComponent(g);
int x1=0,y1=0;
if(state==XX)
g.drawLine(x1,y1,x1+wide,y1+high);
g.drawLine(x1,y1+high,x1+wide,y1);
if(state==OO)
class ML extends MouseAdapter
public void mousePressed(MouseEvent e)
if(state==BLANK)
state=turn;
turn=(turn==XX?OO:XX);
else
state=(state==XX?OO:XX);
repaint();
class BL implements ActionListener
public void actionPerformed(ActionEvent e)
JDialog d=new ToeDialog(Integer.parseInt(rows.getText()),Integer.parseInt(cols.getText()));
d.setVisible(true);
public void init()
JPanel p=new JPanel();
p.add(new JLabel("Rows",JLabel.CENTER));
p.add(rows);
p.add(new JLabel("Columns",JLabel.CENTER));
p.add(cols);
cp.add(p,BorderLayout.NORTH);
JButton b=new JButton("go");
b.addActionListener(new BL());
cp.add(b,BorderLayout.SOUTH);
public static void main(String[] args)
TicTacToe test=new TicTacToe();
JFrame frame=new JFrame("TicTacToe");
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
});
frame.getContentPane().add(test);
frame.setSize(100,100);
test.init();
test.start();
frame.setVisible(true);
以上就是土嘎嘎小编为大家整理的井字棋java代码实现相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!