Java版AI五子棋游戏
程序员文章站
2024-02-25 23:05:27
本文实例为大家分享了java五子棋游戏的具体代码,供大家参考,具体内容如下
ai思路:通过判断棋盘上每个空位的分数,去分数最高的几个点,随机下棋
分数计算思路:能成5个...
本文实例为大家分享了java五子棋游戏的具体代码,供大家参考,具体内容如下
ai思路:通过判断棋盘上每个空位的分数,去分数最高的几个点,随机下棋
分数计算思路:能成5个说明能赢了,给最高分
不能成5个,对方能成5个,说明对方要赢了,给第二高分
能成活4,给第三高分
能成活3,给第四高分
能成冲4,给第五高分
能成冲3,给第六高分
能成活2,给第七高分
能成冲2,给第八高分
其他,给最低分
分数设定可自己定义。
因为是去年写的了,思路记得大概就是这样。最近根据书上写了个棋类游戏的设计框架,待完善后再发上来,应该会更新ai思路
下面是去年写的ai五子棋的代码:
package fivechessclient; import java.awt.cursor; import java.awt.dimension; import java.awt.graphics; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import java.awt.image.bufferedimage; import java.io.file; import java.util.arraylist; import java.util.random; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; /** * * @classname: game * @description: ai五子棋 * @author xiaoxiong * @date 2015年7月3日 下午8:59:02 * */ public class game { bufferedimage table; bufferedimage black; bufferedimage white; bufferedimage selected; /** * 棋子个数 */ private static int board_size = 15; /** * 棋盘宽高 */ private final int table_width = 535; private final int table_height = 536; /** * 棋盘15等分 */ private final int rate = table_width / board_size; /** * 棋盘外边距 */ private final int x_offset = 5; private final int y_offset = 6; /** * 棋盘 */ private int[][] board = new int[board_size][board_size]; /** * ai分数 */ private int[][] computerscore = new int[board_size][board_size]; // private int[][] gamerscore = new int[board_size][board_size]; jframe f = new jframe("五子棋--小熊"); chessboard chessboard = new chessboard(); private static int selectedx = -1; private static int selectedy = -1; private static int computerx = -1; private static int computery = -1; private static boolean flaggamer = false; // 记录玩家是否赢了 private static boolean flagcomputer = false; // 记录电脑是否赢了 private static int computerscore = 0; // 电脑最大分数 private static int comx, comy; // 玩家下子坐标 private final int huo = 1; private final int chong = 2; private static int chesscou = 0; /** * 记录找到的分数一样的棋子,随机下这些棋子中的一个,以防步法固定 */ private arraylist<chessxy> chesslist = new arraylist<chessxy>(); random rand = new random(); /** * * @title: initto @description: 重置游戏 @param @return void @throws */ public void initto() { for (int i = 0; i < board_size; ++i) { for (int j = 0; j < board_size; ++j) { board[i][j] = 0; computerscore[i][j] = 100000; } } chesscou = 0; computerx = -1; computery = -1; flaggamer = false; flagcomputer = false; } /** * * @title: isrun @description: 判断该位置是否可以走 @param @param x @param @param * y @param @return @return boolean @throws */ public boolean isrun(int x, int y) { if (board[x][y] == 0) { return true; } return false; } /** * * @title: iswin @description: 判断下该子是否能赢 @param @param f 颜色 @param @param x * 坐标 @param @param y @param @return @return boolean @throws */ public boolean iswin(int f, int x, int y) { int i, count = 1; boolean up, down, right, left, rup, lup, rdown, ldown; up = down = right = left = rup = lup = rdown = ldown = true; /** * * 上下 * */ for (i = 1; i < 5; ++i) { if ((y + i) < board_size) { if (board[x][y + i] == f && down) count++; else down = false; } if ((y - i) >= 0) { if (board[x][y - i] == f && up) count++; else up = false; } } if (count >= 5) { return true; } count = 1; /** * * 左右 * */ for (i = 1; i < 5; ++i) { if ((x + i) < board_size) { if (board[x + i][y] == f && right) count++; else right = false; } if ((x - i) >= 0) { if (board[x - i][y] == f && left) count++; else left = false; } } if (count >= 5) { return true; } count = 1; /** * * 左上右下 * */ for (i = 1; i < 5; ++i) { if ((x + i) < board_size && (y + i) < board_size) { if (board[x + i][y + i] == f && rdown) count++; else rdown = false; } if ((x - i) >= 0 && (y - i) >= 0) { if (board[x - i][y - i] == f && lup) count++; else lup = false; } } if (count >= 5) { return true; } count = 1; /** * * 右上左下 * */ for (i = 1; i < 5; ++i) { if ((x + i) < board_size && (y - i) >= 0) { if (board[x + i][y - i] == f && rup) count++; else rup = false; } if ((x - i) >= 0 && (y + i) < board_size) { if (board[x - i][y + i] == f && ldown) count++; else ldown = false; } } if (count >= 5) { return true; } return false; } /** * * @title: computer_ai @description: ai下棋 @param @return void @throws */ public void computer_ai() { computerscore = 0; for (int i = 0; i < board_size; ++i) { for (int j = 0; j < board_size; ++j) { computerscore[i][j] = 0; // gamerscore[i][j] = 0; } } getscore(); for (int i = 0; i < board_size; ++i) { for (int j = 0; j < board_size; ++j) { if (computerscore[i][j] == computerscore) { chessxy chess = new chessxy(i, j); chesslist.add(chess); } } } int n = rand.nextint(chesslist.size()); // 电脑根据分值一样的点随机走,防止每次都走相同的步数 comx = chesslist.get(n).x; comy = chesslist.get(n).y; chesslist.clear(); } /** * * @title: getscore @description: 评分 @param @return void @throws */ public void getscore() { for (int i = 0; i < board_size; ++i) { for (int j = 0; j < board_size; ++j) { if (board[i][j] == 0) { if (iswin(2, i, j)) // 电脑能赢,故给分最高,因为可以结束,所以不再检测 { computerscore = 13; computerscore[i][j] = 13; return; } else if (iswin(1, i, j)) // 电脑不能赢,玩家能赢,要阻止,所以给12分 { computerscore = 12; computerscore[i][j] = 12; } else if (ishuoorchong(2, i, j, 4, huo)) // 电脑玩家都不能赢,电脑能形成活四,给11分 { computerscore = (computerscore > 11 ? computerscore : 11); computerscore[i][j] = 11; } else if (ishuoorchong(2, i, j, 4, chong)) // 电脑玩家都不能赢,电脑能形成冲四,给10分 { computerscore = (computerscore > 10 ? computerscore : 10); computerscore[i][j] = 10; } else if (ishuoorchong(1, i, j, 4, huo)) // 电脑玩家都不能赢,玩家能形成活四,给9分 { computerscore = (computerscore > 9 ? computerscore : 9); computerscore[i][j] = 9; } else if (ishuoorchong(2, i, j, 3, huo)) // 电脑玩家都不能赢,电脑能形成活三,给8分 { computerscore = (computerscore > 8 ? computerscore : 8); computerscore[i][j] = 8; } else if (ishuoorchong(1, i, j, 4, chong)) // 电脑玩家都不能赢,玩家能形成冲四,给7分 { computerscore = (computerscore > 7 ? computerscore : 7); computerscore[i][j] = 7; } else if (ishuoorchong(2, i, j, 3, chong)) // 电脑玩家都不能赢,电脑能形成冲三,给6分 { computerscore = (computerscore > 6 ? computerscore : 6); computerscore[i][j] = 6; } else if (ishuoorchong(2, i, j, 2, huo)) // 电脑玩家都不能赢,电脑能形成活二,给5分 { computerscore = (computerscore > 5 ? computerscore : 5); computerscore[i][j] = 5; } else if (ishuoorchong(1, i, j, 3, chong)) // 电脑玩家都不能赢,玩家能形成冲三,给4分 { computerscore = (computerscore > 4 ? computerscore : 4); computerscore[i][j] = 4; } else if (ishuoorchong(1, i, j, 2, huo)) // 电脑玩家都不能赢,玩家能形成活二,给3分 { computerscore = (computerscore > 3 ? computerscore : 3); computerscore[i][j] = 3; } else if (ishuoorchong(2, i, j, 2, chong)) // 电脑玩家都不能赢,电脑能形成冲二,给2分 { computerscore = (computerscore > 2 ? computerscore : 2); computerscore[i][j] = 2; } else if (ishuoorchong(1, i, j, 2, chong)) // 电脑玩家都不能赢,玩家能形成冲二,给1分 { computerscore = (computerscore > 1 ? computerscore : 1); computerscore[i][j] = 1; } else { computerscore[i][j] = 0; } } } } } /** * * @title: ishuoorchong @description: 判断是否为活 @param @param f @param @param * x @param @param y @param @param num @param @param * horc @param @return @return boolean @throws */ private boolean ishuoorchong(int f, int x, int y, int num, int horc) // 活 { num += 1; int i, count = 1; boolean terminal1 = false; boolean terminal2 = false; boolean up, down, right, left, rup, lup, rdown, ldown; up = down = right = left = rup = lup = rdown = ldown = true; /** * * 上下 * */ for (i = 1; i < num; ++i) { if ((y + i) < board_size) { if (board[x][y + i] == f && down) count++; else { if (board[x][y + i] == 0 && down) { terminal1 = true; } down = false; } } if ((y - i) >= 0) { if (board[x][y - i] == f && up) count++; else { if (board[x][y - i] == 0 && up) { terminal2 = true; } up = false; } } } if (count == num - 1 && horc == huo && terminal1 && terminal2) { return true; } if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) { return true; } count = 1; terminal1 = false; terminal2 = false; /* 左右 */ for (i = 1; i < num; ++i) { if ((x + i) < board_size) { if (board[x + i][y] == f && right) count++; else { if (board[x + i][y] == 0 && right) { terminal1 = true; } right = false; } } if ((x - i) >= 0) { if (board[x - i][y] == f && left) count++; else { if (board[x - i][y] == 0 && left) { terminal2 = true; } left = false; } } } if (count == num - 1 && horc == huo && terminal1 && terminal2) { return true; } if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) { return true; } count = 1; terminal1 = false; terminal2 = false; /** * * 左上右下 * */ for (i = 1; i < num; ++i) { if ((x + i) < board_size && (y + i) < board_size) { if (board[x + i][y + i] == f && rdown) count++; else { if (board[x + i][y + i] == 0 && rdown) { terminal1 = true; } rdown = false; } } if ((x - i) >= 0 && (y - i) >= 0) { if (board[x - i][y - i] == f && lup) count++; else { if (board[x - i][y - i] == 0 && lup) { terminal2 = true; } lup = false; } } } if (count == num - 1 && horc == huo && terminal1 && terminal2) { return true; } if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) { return true; } count = 1; terminal1 = false; terminal2 = false; /** * * 右上左下 * */ for (i = 1; i < num; ++i) { if ((x + i) < board_size && (y - i) >= 0) { if (board[x + i][y - i] == f && rup) count++; else { if (board[x + i][y - i] == 0 && rup) { terminal1 = true; } rup = false; } } if ((x - i) >= 0 && (y + i) < board_size) { if (board[x - i][y + i] == f && ldown) count++; else { if (board[x - i][y + i] == 0 && ldown) { terminal2 = true; } ldown = false; } } } if (count == num - 1 && horc == huo && terminal1 && terminal2) { return true; } if (count == num - 1 && horc == chong && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) { return true; } return false; } public void init() throws exception { table = imageio.read(new file("image/board.jpg")); black = imageio.read(new file("image/black.gif")); white = imageio.read(new file("image/white.gif")); selected = imageio.read(new file("image/selected.gif")); for (int i = 0; i < board_size; ++i) { for (int j = 0; j < board_size; ++j) { board[i][j] = 0; computerscore[i][j] = 0; } } chessboard.setpreferredsize(new dimension(table_width, table_height)); chessboard.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e) { int xpos = (int) ((e.getx() - x_offset) / rate); int ypos = (int) ((e.gety() - y_offset) / rate); // system.out.println("1 " + xpos + " " + ypos); if (isrun(xpos, ypos)) { flaggamer = iswin(1, xpos, ypos); board[xpos][ypos] = 1; chesscou++; // do //电脑下棋,随机 // { // comx = (rand.nextint(535) - x_offset) / rate; // comy = (rand.nextint(536) - y_offset) / rate; // } while (!isrun(comx, comy)); if (chesscou == (board_size * board_size)) { joptionpane.showmessagedialog(null, "不相上下!!!\n再来一盘吧!!!", "结束", joptionpane.error_message); initto(); } else { computer_ai(); // 电脑下棋,ai算法 chesscou++; // system.out.println("2 " + comx + " " + comy); flagcomputer = iswin(2, comx, comy); board[comx][comy] = 2; computerx = comx; computery = comy; } } chessboard.repaint(); if (flaggamer) { joptionpane.showmessagedialog(null, "厉害厉害!!!\n你赢了!!!", "结束", joptionpane.error_message); initto(); } else if (flagcomputer) { joptionpane.showmessagedialog(null, "哈哈哈哈!!!\n你输了!!!", "结束", joptionpane.error_message); initto(); } } public void mouseexited(mouseevent e) { selectedx = -1; selectedy = -1; chessboard.repaint(); } }); chessboard.addmousemotionlistener(new mousemotionadapter() { public void mousemoved(mouseevent e) { selectedx = (e.getx() - x_offset) / rate; selectedy = (e.gety() - y_offset) / rate; chessboard.repaint(); } }); f.add(chessboard); f.setcursor(new cursor(cursor.hand_cursor)); f.setdefaultcloseoperation(jframe.exit_on_close); f.setresizable(false); f.pack(); f.setvisible(true); } public static void main(string[] args) throws exception { game game = new game(); game.init(); } @suppresswarnings("serial") class chessboard extends jpanel { public void paint(graphics g) { g.drawimage(table, 0, 0, null); if (selectedx >= 0 && selectedy >= 0) { g.drawimage(selected, selectedx * rate + x_offset, selectedy * rate + y_offset, null); } if (computerx >= 0 && computery >= 0) { g.drawimage(selected, computerx * rate + x_offset, computery * rate + y_offset, null); } for (int i = 0; i < board_size; ++i) { for (int j = 0; j < board_size; ++j) { if (board[i][j] == 1) { g.drawimage(black, i * rate + x_offset, j * rate + y_offset, null); } if (board[i][j] == 2) { g.drawimage(white, i * rate + x_offset, j * rate + y_offset, null); } } } } } } class chessxy { int x; int y; public chessxy(int x, int y) { this.x = x; this.y = y; } }
更多精彩游戏,请参考专题《java经典小游戏》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。