重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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);
}
}
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();
}
}
int a[][]=new int[3][3];
for (int i = 0; i a.length; i++) {
for (int j = 0; j a.length; j++) {
a[i][j]=(int)(Math.random()*2);
}
}
int x=0;
for (int i = 0; i a.length; i++) {
for (int j = 0; j a.length; j++) {
x++;
System.out.print(a[i][j]+" ");
if(x%3==0)
System.out.println();
}
}
判断的话if(a[0][0]==a[0][2]==a[0][1])等等都写上就行了
我说下我的想法:
看坐标: 横坐标一致
或纵坐标一致
还有就是/.\两种情况下 固定的位置你事先固定好
当三个点都有了棋子就一直线
以前写过一个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判断, 比如人现在的情况是什么样的,有几个棋子连在一起了,电脑自己的情况是怎么样的)