前言,既(2)之后,做了一下改进,如界面的优化,棋子的优化,AI对战的实现
还是回顾一下我们之前(1)(2)的工作
建立一个Chess包→界面类ChessUI→画棋子类DrawChess→监听器类ChessListener→回到ChessUI中添加监听器→ChessPosition类→ArrayList→悔棋→认输
以上是我们做到的
接下来首先是我们的
Basic接口
接口里放上我们要用到的图片,其他类就可以直接使用,当然,这一部分也可以不看,毕竟没什么用,用图片的话棋子就好看一点,哈哈哈
public interface Basic {
Image BLACKCHESS= new ImageIcon("image/chess/black.png").getImage(); //这里不能用ImageIcon
Image WHITECHESS= new ImageIcon("image/chess/white.png").getImage(); //这里不能用ImageIcon
Image CHESSBOARD= new ImageIcon("image/chess/ChessBoard.jpg").getImage(); //这里不能用ImageIcon
Image MESSAGEPICTURE= new ImageIcon("image/chess/MessagePictrue.jpg").getImage(); //这里不能用ImageIcon
Image LOGINPICTURE= new ImageIcon("image/chess/LoginPicture.jpg").getImage(); //这里不能用ImageIcon
Image LOGINPICTURE2= new ImageIcon("image/chess/LoginPicture2.jpg").getImage(); //这里不能用ImageIcon
}
优化过的ChessUI类
代码大大减少,把边框布局去掉了,直接用setBounds方法。
public class ChessUI extends JFrame implements Basic{
int[][] che = new int[30][30];
JLabel txtLable1 = new JLabel("准备开始");
JLabel txtLable2 = new JLabel(" ");
JComboBox<String> box = new JComboBox<String>();
ChessListener ch = new ChessListener(this);
public ArrayList<ChessPosition> ChessPosition = new ArrayList<ChessPosition>();// 保存每一步棋子的落子
public void chessFrame() {
// 设置窗体
setTitle("五子棋");
setSize(1400, 1000);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//文字显示框
JPanel txtP = new JPanel();
txtP.setBackground(Color.lightGray);
txtP.setBounds(50, 300, 150, 130);
this.add(txtP);
txtLable1.setFont(new Font("黑体", Font.PLAIN, 24));
txtLable2.setFont(new Font("黑体", Font.PLAIN, 24));
txtP.add(txtLable1);
txtP.add(txtLable2);
// 添加按钮
String[] butname = { "游戏说明", "悔棋", "认输", "开始游戏" };
JButton[] button = new JButton[4];
for (int i = 0; i < butname.length; i++) {
button[i] = new JButton(butname[i]);
button[i].setBounds(1200, 300+50*i, 160, 45);
this.add(button[i]);
}
this.add(box);
box.setBounds(1200,500,160,45);
box.addItem("--游戏模式--");
box.addItem("人人对战");
box.addItem("人机对战");
// 添加监听器
for (int i = 0; i < butname.length; i++) {
button[i].addActionListener(ch);// 添加发生操作的监听方法
}
box.addActionListener(ch);
setVisible(true);
addMouseListener(ch);
ch.g = this.getGraphics();
repaint();
}
public void board(Graphics g) {
g.drawLine(280, 60, 1160, 60);
g.drawLine(280, 60, 280, 940);
g.drawLine(280, 940, 1160, 940);
g.drawLine(1160, 60, 1160, 940);
for (int i = 0; i < 15; i++) {
g.setColor(Color.BLACK);
g.drawLine(300, 80 + i * 60, 1140, 80 + i * 60); // 画棋盘横线
g.drawLine(300 + i * 60, 80, 300 + i * 60, 920); // 画棋盘竖线
g.fillOval(473, 253, 15, 15);
g.fillOval(473, 733, 15, 15);
g.fillOval(473, 493, 15, 15);
g.fillOval(953, 253, 15, 15);
g.fillOval(953, 733, 15, 15);
g.fillOval(953, 493, 15, 15);
g.fillOval(713, 253, 15, 15);
g.fillOval(713, 733, 15, 15);
g.fillOval(713, 493, 15, 15);
}
// 设置界面
g.setColor(Color.BLACK);
g.drawRect(390, 945, 210, 40);
g.drawRect(650, 945, 210, 40);
g.setFont(new Font("楷体", Font.BOLD, 20));
g.drawString("黑方时间:", 400, 975);
g.drawString("白方时间:", 660, 975);
}
public void chess(Graphics g) {
// 画棋子
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (che[i][j] == 1) {
int x = 60 * i - 25;
int y = 60 * j + 60;
g.drawImage(BLACKCHESS,x, y, 50, 50,null);
} else if (che[i][j] == 2) {
int x = 60 * i - 25;
int y = 60 * j + 60;
g.drawImage(WHITECHESS,x, y, 50, 50,null);
}
}
}
}
public void paint(Graphics g) {
super.paint(g);
board(g);
chess(g);
}
public static void main(String[] args) {
ChessUI chess = new ChessUI();
chess.chessFrame();
}
}
如果了解的同学可以百度一下,这个方法,听我的介绍也可以
首先我们考虑一下我们下棋的过程,对面落下了一颗子,我们观察棋盘,一般有两种走法,一个是走自己的攻势,另一个是防对面的攻势,简而言之就是连自己的五子,或者是防止对面连出五子。
我们观察了棋盘后,发现对面有连成三个的趋势,自己也有成三个的趋势,但是我们在心里默默进行了权衡,发现如果我们不阻拦,就有输的风险,这是在我们的心里,阻拦对面的权值就高于走自己的攻势的权值
权值法就是这样一个方法,把棋盘上所有的情况都列出一个权值,然后用电脑模拟我们的大脑,进行权衡,然后下棋,这就是AI对战的基础方法,之后的博弈树,大家有兴趣也可以自学。
举个例子,我们已知之前设定的turn,1为黑棋,2为白棋,那么我们模拟棋盘情况,0120,就是黑棋白棋挨着各一个,旁边0代表空位,还没有棋子,方向不需要考虑,因为这些数字可以代表所有方向,只需要再后面的方法进行规定方向即可。
贴上一个参考版的权值表
public class ChessUI extends JFrame implements Basic {
int[][] che = new int[30][30];
public int[][] Hash=new int[30][30];
JLabel txtLable1 = new JLabel("准备开始");
JLabel txtLable2 = new JLabel(" ");
JComboBox<String> box = new JComboBox<String>();
ChessListener ch = new ChessListener(this);
public ArrayList<ChessPosition> ChessPosition = new ArrayList<ChessPosition>();// 保存每一步棋子的落子
public static HashMap<String, Integer> map = new HashMap<String, Integer>();
static {
// 被堵住
map.put("01", 25);// 眠1连
map.put("02", 22);// 眠1连
map.put("001", 17);// 眠1连
map.put("002", 12);// 眠1连
map.put("0001", 17);// 眠1连
map.put("0002", 12);// 眠1连
map.put("0102", 25);// 眠1连,15
map.put("0201", 22);// 眠1连,10
map.put("0012", 15);// 眠1连,15
map.put("0021", 10);// 眠1连,10
map.put("01002", 25);// 眠1连,15
map.put("02001", 22);// 眠1连,10
map.put("00102", 17);// 眠1连,15
map.put("00201", 12);// 眠1连,10
map.put("00012", 15);// 眠1连,15
map.put("00021", 10);// 眠1连,10
map.put("01000", 25);// 活1连,15
map.put("02000", 22);// 活1连,10
map.put("00100", 19);// 活1连,15
map.put("00200", 14);// 活1连,10
map.put("00010", 17);// 活1连,15
map.put("00020", 12);// 活1连,10
map.put("00001", 15);// 活1连,15
map.put("00002", 10);// 活1连,10
// 被墙堵住
map.put("0101", 65);// 眠2连,40
map.put("0202", 60);// 眠2连,30
map.put("0110", 80);// 眠2连,40
map.put("0220", 76);// 眠2连,30
map.put("011", 80);// 眠2连,40
map.put("022", 76);// 眠2连,30
map.put("0011", 65);// 眠2连,40
map.put("0022", 60);// 眠2连,30
map.put("01012", 65);// 眠2连,40
map.put("02021", 60);// 眠2连,30
map.put("01102", 80);// 眠2连,40
map.put("02201", 76);// 眠2连,30
map.put("01120", 80);// 眠2连,40
map.put("02210", 76);// 眠2连,30
map.put("00112", 65);// 眠2连,40
map.put("00221", 60);// 眠2连,30
map.put("01100", 100);// 活2连,40
map.put("02200", 75);// 活2连,30
map.put("01010", 90);// 活2连,40
map.put("02020", 70);// 活2连,30
map.put("00110", 90);// 活2连,40
map.put("00220", 75);// 活2连,30
map.put("00011", 90);// 活2连,40
map.put("00022", 70);// 活2连,30
// 被堵住
map.put("0111", 100);// 眠3连,150
map.put("0222", 140);// 眠3连,80
map.put("01112", 150);// 眠3连,100
map.put("02221", 140);// 眠3连,80
map.put("01110", 1100);// 活3连
map.put("02220", 1050);// 活3连
map.put("01101", 1000);// 活3连,130
map.put("02202", 800);// 活3连,110
map.put("01011", 1000);// 活3连,130
map.put("02022", 800);// 活3连,110
map.put("01111", 3000);// 4连,300
map.put("02222", 3500);// 4连,280
}
public void chessFrame() {
// 设置窗体
setTitle("五子棋");
setSize(1400, 1000);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 文字显示框
JPanel txtP = new JPanel();
txtP.setBackground(Color.lightGray);
txtP.setBounds(50, 300, 150, 130);
this.add(txtP);
txtLable1.setFont(new Font("黑体", Font.PLAIN, 24));
txtLable2.setFont(new Font("黑体", Font.PLAIN, 24));
txtP.add(txtLable1);
txtP.add(txtLable2);
// 添加按钮
String[] butname = { "游戏说明", "悔棋", "认输", "开始游戏" };
JButton[] button = new JButton[4];
for (int i = 0; i < butname.length; i++) {
button[i] = new JButton(butname[i]);
button[i].setBounds(1200, 300 + 50 * i, 160, 45);
this.add(button[i]);
}
this.add(box);
box.setBounds(1200, 500, 160, 45);
box.addItem("--游戏模式--");
box.addItem("人人对战");
box.addItem("人机对战");
// 添加监听器
for (int i = 0; i < butname.length; i++) {
button[i].addActionListener(ch);// 添加发生操作的监听方法
}
box.addActionListener(ch);
setVisible(true);
addMouseListener(ch);
ch.g = this.getGraphics();
repaint();
}
public void board(Graphics g) {
g.drawLine(280, 60, 1160, 60);
g.drawLine(280, 60, 280, 940);
g.drawLine(280, 940, 1160, 940);
g.drawLine(1160, 60, 1160, 940);
for (int i = 0; i < 15; i++) {
g.setColor(Color.BLACK);
g.drawLine(300, 80 + i * 60, 1140, 80 + i * 60); // 画棋盘横线
g.drawLine(300 + i * 60, 80, 300 + i * 60, 920); // 画棋盘竖线
g.fillOval(473, 253, 15, 15);
g.fillOval(473, 733, 15, 15);
g.fillOval(473, 493, 15, 15);
g.fillOval(953, 253, 15, 15);
g.fillOval(953, 733, 15, 15);
g.fillOval(953, 493, 15, 15);
g.fillOval(713, 253, 15, 15);
g.fillOval(713, 733, 15, 15);
g.fillOval(713, 493, 15, 15);
}
// 设置界面
g.setColor(Color.BLACK);
g.drawRect(390, 945, 210, 40);
g.drawRect(650, 945, 210, 40);
g.setFont(new Font("楷体", Font.BOLD, 20));
g.drawString("黑方时间:", 400, 975);
g.drawString("白方时间:", 660, 975);
}
public void chess(Graphics g) {
// 画棋子
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (che[i][j] == 1) {
int x = 60 * i - 25;
int y = 60 * j + 60;
g.drawImage(BLACKCHESS, x, y, 50, 50, null);
} else if (che[i][j] == 2) {
int x = 60 * i - 25;
int y = 60 * j + 60;
g.drawImage(WHITECHESS, x, y, 50, 50, null);
}
}
}
}
public void paint(Graphics g) {
super.paint(g);
board(g);
chess(g);
}
public static void main(String[] args) {
ChessUI chess = new ChessUI();
chess.chessFrame();
}
}
DrawChess类
public class DrawChess implements Basic {
int turn;
ChessUI ui;
Graphics g;
public void setG(ChessUI ui, int turn) {
this.ui = ui;
this.turn = turn;
}
public void chess(int x, int y) {
g = ui.getGraphics();
g.setFont(new Font("行楷", Font.BOLD, 20));
int h = (x + 30) / 60;// 行
int l = (y - 50) / 60;// 列
int xp = h * 60 - 25;
int yp = l * 60 + 60;
// 检查x,y
if (x >= 300 && x <= 1140 && y >= 80 && y <= 920) {
System.out.println(x + "++" + y);
// 画棋子
if (turn == 1) {
ui.che[h][l] = 1;
ui.ChessPosition.add(new ChessPosition(h, l));
g.drawImage(BLACKCHESS, xp, yp, 50, 50, null);
// 判断输赢
boolean b = judge(h, l);
if (b) {
JOptionPane.showMessageDialog(null, "黑方胜利", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
}
} else {
ui.che[h][l] = 2;
ui.ChessPosition.add(new ChessPosition(h, l));
g.drawImage(WHITECHESS, xp, yp, 50, 50, null);
// 判断输赢
boolean b = judge(h, l);
if (b) {
JOptionPane.showMessageDialog(null, "白方胜利", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
public boolean judge(int x, int y) {
// 竖着
int sum = 1;
// 向上看
for (int i = x - 1; i >= 0; i--) {
if (ui.che[i][y] == ui.che[x][y])
sum++;
else
break;
}
// 向下看
for (int i = x + 1; i < 20; i++) {
if (ui.che[i][y] == ui.che[x][y])
sum++;
else
break;
}
if (sum > 4)
return true;
// 横着
sum = 1;
// 向左看
for (int i = y - 1; i >= 0; i--) {
if (ui.che[x][i] == ui.che[x][y])
sum++;
else
break;
}
// 向右看
for (int i = y + 1; i < 19; i++) {
if (ui.che[x][i] == ui.che[x][y])
sum++;
else
break;
}
if (sum > 4)
return true;
// 左斜
sum = 1;
// 向左上看
for (int i = x - 1, j = y - 1; (i >= 0) && (j >= 0); i--, j--) {
if (ui.che[i][j] == ui.che[x][y])
sum++;
else
break;
}
// 向右下看
for (int i = x + 1, j = y + 1; (i < 20) && (j < 20); i++, j++) {
if (ui.che[i][j] == ui.che[x][y])
sum++;
else
break;
}
if (sum > 4)
return true;
// 右斜
sum = 1;
// 向右上看
for (int i = x - 1, j = y + 1; (i >= 0) && (j < 20); i--, j++) {
if (ui.che[i][j] == ui.che[x][y])
sum++;
else
break;
}
// 向左下看
for (int i = x + 1, j = y - 1; (i < 20) && (j >= 0); i++, j--) {
if (ui.che[i][j] == ui.che[x][y])
sum++;
else
break;
}
if (sum > 4)
return true;
// 没有连续五个
return false;
}
// AI联合算法
public Integer unionWeight(Integer a, Integer b) {
// 必须要先判断a,b两个数值是不是null
if ((a == null) || (b == null))
return 0;
// 一一:101/202
else if ((a >= 22) && (a <= 25) && (b >= 22) && (b <= 25))
return 60;
// 一二、二一:1011/2022
else if (((a >= 22) && (a <= 25) && (b >= 76) && (b <= 80))
|| ((a >= 76) && (a <= 80) && (b >= 22) && (b <= 25)))
return 800;
// 一三、三一、二二:10111/20222
else if (((a >= 10) && (a <= 25) && (b >= 1050) && (b <= 1100))
|| ((a >= 1050) && (a <= 1100) && (b >= 10) && (b <= 25))
|| ((a >= 76) && (a <= 80) && (b >= 76) && (b <= 80)))
return 3000;
// 眠三连和眠一连。一三、三一
else if (((a >= 22) && (a <= 25) && (b >= 140) && (b <= 150))
|| ((a >= 140) && (a <= 150) && (b >= 22) && (b <= 25)))
return 3000;
// 二三、三二:110111
else if (((a >= 76) && (a <= 80) && (b >= 1050) && (b <= 1100))
|| ((a >= 1050) && (a <= 1100) && (b >= 76) && (b <= 80)))
return 3000;
else
return 0;
}
public void AI(int x, int y) {
g = ui.getGraphics();
g.setFont(new Font("行楷", Font.BOLD, 20));
int h = (x + 30) / 60;// 行
int l = (y - 50) / 60;// 列
int xp = h * 60 - 25;
int yp = l * 60 + 60;
// 检查x,y
if (x >= 300 && x <= 1140 && y >= 80 && y <= 920) {
System.out.println(x + "++" + y);
if (turn == 1) {
ui.che[h][l] = 1;
ui.ChessPosition.add(new ChessPosition(h, l));
g.drawImage(BLACKCHESS, xp, yp, 50, 50, null);
// 判断输赢
boolean b = judge(h, l);
if (b) {
JOptionPane.showMessageDialog(null, "黑方胜利", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
}
}
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (ui.che[i][j] == 0) {
// 往左延伸
String connect = "0";
int jmin = Math.max(0, j - 4);
// 往左延伸
for (int positionj = j - 1; positionj >= jmin; positionj--) {
// 依次加上前面的棋子
connect += ui.che[i][positionj];
}
// 从数组中取出相应的权值,加到权值数组的当前位置中
Integer valueleft = ui.map.get(connect);
if (valueleft != null)
ui.Hash[i][j] += valueleft;
// 往右延伸
connect = "0";
int jmax = Math.min(14, j + 4);
for (int positionj = j + 1; positionj <= jmax; positionj++) {
// 依次加上前面的棋子
connect += ui.che[i][positionj];
}
// 从数组中取出相应的权值,加到权值数组的当前位置中
Integer valueright = ui.map.get(connect);
if (valueright != null)
ui.Hash[i][j] += valueright;
// 联合判断,判断行
ui.Hash[i][j] += unionWeight(valueleft, valueright);
// 向上延伸
connect = "0";
int imin = Math.max(0, i - 4);
for (int positioni = i - 1; positioni >= imin; positioni--) {
// 依次加上前面的棋子
connect += ui.che[positioni][j];
}
// 从数组中取出相应的权值,加到权值数组的当前位置中
Integer valueup = ui.map.get(connect);
if (valueup != null)
ui.Hash[i][j] += valueup;
// 往下延伸
connect = "0";
int imax = Math.min(14, i + 4);
for (int positioni = i + 1; positioni <= imax; positioni++) {
// 依次加上前面的棋子
connect += ui.che[positioni][j];
}
// 从数组中取出相应的权值,加到权值数组的当前位置中
Integer valuedowm = ui.map.get(connect);
if (valuedowm != null)
ui.Hash[i][j] += valuedowm;
// 联合判断,判断列
ui.Hash[i][j] += unionWeight(valueup, valuedowm);
// 往左上方延伸,i,j,都减去相同的数
connect = "0";
for (int position = -1; position >= -4; position--) {
if ((i + position >= 0) && (i + position <= 14) && (j + position >= 0) && (j + position <= 14))
connect += ui.che[i + position][j + position];
}
// 从数组中取出相应的权值,加到权值数组的当前位置
Integer valueLeftUp = ui.map.get(connect);
if (valueLeftUp != null)
ui.Hash[i][j] += valueLeftUp;
// 往右下方延伸,i,j,都加上相同的数
connect = "0";
for (int position = 1; position <= 4; position++) {
if ((i + position >= 0) && (i + position <= 14) && (j + position >= 0) && (j + position <= 14))
connect += ui.che[i + position][j + position];
}
// 从数组中取出相应的权值,加到权值数组的当前位置
Integer valueRightDown = ui.map.get(connect);
if (valueRightDown != null)
ui.Hash[i][j] += valueRightDown;
// 联合判断,判断左斜
ui.Hash[i][j] += unionWeight(valueLeftUp, valueRightDown);
// 往右上方延伸,i减,j加
connect = "0";
for (int position = 1; position <= 4; position++) {
if ((i - position >= 0) && (i - position <= 14) && (j + position >= 0) && (j + position <= 14))
connect += ui.che[i - position][j + position];
}
// 从数组中取出相应的权值,加到权值数组的当前位置
Integer valueRightUp = ui.map.get(connect);
if (valueRightUp != null)
ui.Hash[i][j] += valueRightUp;
// 往左下方延伸,i加,j减
connect = "0";
for (int position = 1; position <= 4; position++) {
if ((i + position >= 0) && (i + position <= 14) && (j - position >= 0) && (j - position <= 14))
connect += ui.che[i + position][j - position];
}
// 从数组中取出相应的权值,加到权值数组的当前位置
Integer valueLeftDowm = ui.map.get(connect);
if (valueLeftDowm != null)
ui.Hash[i][j] += valueLeftDowm;
// 联合判断,判断右斜
ui.Hash[i][j] += unionWeight(valueLeftUp, valueRightDown);
}
}
}
// 取出最大的权值
System.out.println("ui.Hash" + ui.Hash);
int AIi = 0, AIj = 0;
int weightmax = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (weightmax < ui.Hash[i][j]) {
weightmax = ui.Hash[i][j];
AIi = i;
AIj = j;
}
}
}
System.out.println(AIi + " " + AIj);
int xi = AIi * 60 - 25;
int yj = AIj * 60 + 60;
g.drawImage(WHITECHESS, xi, yj, 50, 50, null);
// 设置当前位置已经有棋子了,棋子为白子
ui.ChessPosition.add(new ChessPosition(AIi, AIj));
ui.che[AIi][AIj] = 2;
// 判断输赢
boolean b = judge(AIi, AIj);
if (b) {
JOptionPane.showMessageDialog(null, "白方胜利", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
}
}
}
ChessListener类
public class ChessListener implements MouseListener, ActionListener {
int x;
int y;
int turn = 0;
Graphics g;
DrawChess D = new DrawChess();
ChessUI ui;
public ChessListener() {
}
public ChessListener(ChessUI ui) {
this.ui = ui;
System.out.println("Listener ready");
// D.ui = ui;
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
if (turn == 0) {
return;
}
if (ui.box.getSelectedItem().equals("人人对战")) {
x = e.getX();
y = e.getY();
int h = (x + 30) / 60;// 行
int l = (y - 50) / 60;// 列
if (ui.che[h][l] != 0) {
JOptionPane.showMessageDialog(null, "此处已有棋子,请下在别处");
return;
}
D.setG(ui, turn);
D.chess(x, y);
if (x >= 300 && x <= 1140 && y >= 80 && y <= 920) {
if (turn == 1) {
turn++;
ui.txtLable2.setText("白方下棋");
} else if (turn == 2) {
turn--;
ui.txtLable2.setText("黑方下棋");
}
}
}
if(ui.box.getSelectedItem().equals("人机对战")) {
x = e.getX();
y = e.getY();
int h = (x + 30) / 60;// 行
int l = (y - 50) / 60;// 列
if(ui.che[h][l]!=0) {
JOptionPane.showMessageDialog(null, "此处已有棋子,请下在别处");
return;
}
ui.txtLable1.setText("超强人机");
ui.txtLable2.setText("加油!");
D.setG(ui,turn);
D.AI(x, y);
}
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (ui.box.getSelectedItem().equals("游戏模式")) {
if (cmd.equals("开始游戏")) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
ui.che[i][j] = 0;
ui.Hash[i][j] = 0;
}
}
ui.repaint();
turn = 1;
}
}
else if (cmd.equals("开始游戏")) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
ui.che[i][j] = 0;
ui.Hash[i][j] = 0;
}
}
ui.repaint();
turn = 1;
ui.txtLable2.setText("黑方下棋");
}
else if (cmd.equals("悔棋")) {
System.out.println("dada");
if (ui.ChessPosition.size() > 0) {
System.out.println("ssss");
ChessPosition cp = new ChessPosition();
cp = ui.ChessPosition.remove(ui.ChessPosition.size() - 1);
ui.che[cp.Listi][cp.Listj] = 0;
if (turn == 1) {
turn++;
} else {
turn--;
}
ui.repaint();
}
} else if (cmd.equals("游戏说明")) {
JOptionPane.showMessageDialog(null, "先连成五子者获胜", "游戏说明", JOptionPane.INFORMATION_MESSAGE);
}
else if (cmd.equals("认输")) {
if (turn == 1) {
JOptionPane.showMessageDialog(null, "恭喜白方获胜", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
} else if (turn == 2) {
JOptionPane.showMessageDialog(null, "恭喜黑方获胜", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
}
turn = 0;
}
}
}
其他的类基本没有变化,所以我就不占用字数了
以上的五子棋小游戏就算完成了,仍存在缺陷,但是我已经奔向下一个内容了,所以这点小的bug我也不在意了,想要解决无非是多花一点时间,但已经没必要了,我们学会了这其中的内容,就够了。
权值表的内容并非一成不变,大家完全可以修改里面的权值,按照你的设定来,我的AI就因为权值设定不好,有点傻,但是这些都是可以改进的,更何况还有博弈树这样的方法在,所以我们了解即可。
至于我的界面,我还添加了一个好看的初始界面,原本想加一个box选择象棋围棋五子棋的,但是兴趣精力有限,所以就只做了五子棋。
以上就是所有的五子棋小程序啦
over~