我说下我的想法:
成都创新互联主打移动网站、做网站、成都做网站、网站改版、网络推广、网站维护、域名申请、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。
看坐标: 横坐标一致
或纵坐标一致
还有就是/.\两种情况下 固定的位置你事先固定好
当三个点都有了棋子就一直线
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NinePannel extends JFrame{
public NinePannel(){
this.setSize(320,340);
this.setLayout(new GridLayout(3,3));
for(int i=0;i9;i++){
this.add(new MyPanel());
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class MyPanel extends JPanel{
int i=-1;
public MyPanel(){
this.setSize(100,100);
this.addMouseListener(new MyListener(this));
}
public void paint(Graphics arg0) {
super.paint(arg0);
arg0.drawRect(0, 0, 100, 100);
if(i!=-1){
if(i%2==0){
arg0.drawOval(0, 0, 100, 100);
}else{
arg0.drawLine(0, 0, 100, 100);
arg0.drawLine(100, 0, 0, 100);
}
}
i++;
}
class MyListener extends MouseAdapter{
MyPanel myPanel;
public MyListener(MyPanel myPanel){
this.myPanel = myPanel;
}
public void mousePressed(MouseEvent arg0) {
myPanel.repaint();
}
}
}
public static void main(String[] args){
new NinePannel();
}
}
以前写过一个java的井字棋 ,
其中的重点是要判断每走一步后,是否有比赛的结果(输,赢,平)
可以使用swing 来作为外观进行显示.
表示棋盘如下
0 1 2
3 4 5
6 7 8
定义一个二维数组,每次走完后,匹配该数组, 如果匹配成功就赢了
int[][] WIN = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 },
{ 2, 4, 6 } };
效果图
当然了,因为井字棋比较简单, 可以写一个比较简单的判断局势,然后自动下棋的AI .
(AI使用了很多的if else判断, 比如人现在的情况是什么样的,有几个棋子连在一起了,电脑自己的情况是怎么样的)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JApplet
{
private JTextField rows=new JTextField("3");
private JTextField cols=new JTextField("3");
private static final int BLANK=0,XX=1,OO=2;
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());
setSize(cellsWide*50,cellsHigh*50);
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;
int x2=getSize().width-1;
int y2=getSize().height-1;
g.drawRect(x1,y1,x2,y2);
x1=x2/4;
y1=y2/4;
int wide=x2/2,high=y2/2;
if(state==XX)
{
g.drawLine(x1,y1,x1+wide,y1+high);
g.drawLine(x1,y1+high,x1+wide,y1);
}
if(state==OO)
g.drawOval(x1,y1,x1+wide/2,y1+high/2);
}
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.setLayout(new GridLayout(2,2));
p.add(new JLabel("Rows",JLabel.CENTER));
p.add(rows);
p.add(new JLabel("Columns",JLabel.CENTER));
p.add(cols);
Container cp=getContentPane();
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);
}
}