游戏执行的效果图:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ticktacktoe {
public static void main(String[] args) {
new DrawSee2();
}
}
class DrawSee2 extends JFrame {
private static final long serialVersionUID = 2L;
private Graphics jg;
private static final int w2=200;
private static final int x1=100;
private static final int y1=100;
private static final int x2=700;
private static final int y2=700;
private int[][] position;
public Color c=Color.RED;
private int isEnd2=0;
public DrawSee2() {
setBounds(300, 50, 800, 800);
this.setTitle("雷雷的井子游戏");
setVisible(true);
position=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++) {
position[i][j]=0;
}
}
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
jg = this.getGraphics();
paintComponents(jg);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(isEnd2==1) {
JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
int x = e.getX(), y = e.getY();
int dx = (x - x1) / w2, dy = (y - y1) / w2;
int newx=dx*w2+x1,newy=dy*w2+y1;
paintComponents2(jg,newx,newy,200,200);
if(c==Color.RED) {
position[dx][dy]=1;
}
if(c==Color.green) {
position[dx][dy]=2;
}
winOrNot();
}
});
}
public void paintComponents(Graphics g) {
try {
g.setColor(Color.RED);
for(int i=0;i<4;i++) {
g.drawLine(x1, y1+i*w2, x2, y1+i*w2);
g.drawLine(x1+i*w2, y1, x1+i*w2, y2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponents2(Graphics g,int x,int y,int width,int height) {
try {
g.setColor(c);
if(c==Color.RED) {
c=Color.green;
}
else {
c=Color.red;
}
g.fillOval(x, y, width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
public void winOrNot() {
if(position[0][0]==position[1][0]&&position[1][0]==position[2][0]) {
if(position[0][0]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[0][1]==position[1][1]&&position[1][1]==position[2][1]) {
if(position[0][1]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[0][2]==position[1][2]&&position[1][2]==position[2][2]) {
if(position[0][2]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[0][0]==position[0][1]&&position[0][1]==position[0][2]) {
if(position[0][0]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[1][0]==position[1][1]&&position[1][1]==position[1][2]) {
if(position[1][0]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[2][0]==position[2][1]&&position[2][1]==position[2][2]) {
if(position[2][0]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[0][0]==position[1][1]&&position[1][1]==position[2][2]) {
if(position[0][0]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(position[2][0]==position[1][1]&&position[1][1]==position[0][2]) {
if(position[2][0]!=0) {
isEnd2=1;
if(c!=Color.red) {
JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
if(c!=Color.green) {
JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}