使用java制作五子棋

package bao;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
public class MyFreme extends JFrame {

static Graphics g; //新建画笔
static int [][] isArray = new int[16][16]; //储存棋子的二维数组
public static void main(String[] args) {
//isArray[5][5] = 1; 测试数据
MyFreme myfreme = new MyFreme(); //创建游戏窗口变量
myfreme.start(); //加载游戏窗口


}
public void start() {
MyFreme mf = new MyFreme();
mf.setSize(800, 800);
//mf.setLocation(200, 200);
mf.setVisible(true);
mf.setDefaultCloseOperation(3);
mf.setLocationRelativeTo(null);
mf.setBackground(Color.GRAY);
// 添加对鼠标点击的自定义监听
MyLisiten ml = new MyLisiten();
ml.setG(mf); //将当前游戏窗口导入监听类中
mf.addMouseListener(ml); // 为当前窗口添加监听

}
@Override
public void paint(Graphics g) {
// TODO 自动生成的方法存根
super.paint(g);
for (int i = 0; i <=15; i++) { //画行
g.drawLine(40, 40*i+40, 640, 40*i+40);
g.drawString(""+i, 40*i+20, 660);

}
for (int j = 0; j <=15; j++) { //画列
g.drawLine(40*j+40, 40, 40*j+40, 640);
g.drawString(""+j,20, 40*j+20);
}
//画棋子
for (int i = 0; i < 15; i++) {

for (int j = 0; j < 15; j++) {
//黑子为1
if (isArray[i][j]==1 ) {
int countX = 40*i+20;
int countY = 40*j+20;
g.setColor(Color.black);
g.fillOval(countX, countY, 40, 40);
}
//白子为2
if (isArray[i][j]==2 ) {
int countX = 40*i+20;
int countY = 40*j+20;
g.setColor(Color.white);
g.fillOval(countX, countY, 40, 40);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
}

package bao;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyLisiten implements MouseListener{
public MyFreme mf;
int turn = 1; // 1表示该黑子下棋了 2表示白子下棋
int end = 0; //游戏结束判定 end=【5】时游戏结束
public void setG(MyFreme mf) {
this.mf = mf;
}
/*public int gameCompleteUP(int arrX,int arrY) {
//判断游戏是否结束
if(arrY<=15 && end<=5) {
if(mf.isArray[arrX][arrY] == 1) {
end++;
}

int up = gameCompleteUP(arrX,arrY+1);
int down = gameCompleteUP(arrX,arrY-1);
end = up+down;
}
return end;

}*/
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
int x = e.getX(); //获取鼠标点击的坐标
int y = e.getY();
int arrX,arrY; // 记录鼠标坐标相对应的数组位置

// 进行位置范围的判定 与点击位置相近的为下棋的位置
if(x%40<=20 && y%40<=20) {
arrX = x/40-1;
arrY = y/40-1;
}else if (x%40>20 && y%40<=20) {
arrX = x/40;
arrY = y/40-1;
}else if (x%40<20 && y%40>20) {
arrX = x/40-1;
arrY = y/40;
}else {
arrX = x/40;
arrY = y/40;
}
if(mf.isArray[arrX][arrY]!= 0) {
System.out.println("这个位置已经有棋子了,请换个位置!");
}else {
Graphics g = mf.getGraphics();
System.out.println(arrX + " " +arrY); // 初步测试是否实现鼠标监听的功能

if(turn == 1) { //黑子下棋
int countX = 40*arrX+20;
int countY = 40*arrY+20;
g.setColor(Color.black);
g.fillOval(countX, countY, 40, 40);
mf.isArray[arrX][arrY] = 1;
//gameCompleteUP(arrX, arrY);
if(end>=5) {
System.out.println("黑子获胜!");
return;
}

turn++;
}else { //白子下棋
int countX = 40*arrX+20;
int countY = 40*arrY+20;
g.setColor(Color.white);
g.fillOval(countX, countY, 40, 40);
mf.isArray[arrX][arrY] = 2;
turn--;
}
}

}

@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根

}

@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根

}

posted @ 2019-10-12 13:41  李艳艳665  阅读(874)  评论(0编辑  收藏  举报