开发工具:eclip2021
jdk版本:jdk1.8
mysql版本:8.0.13
java+swing
实现五子棋游戏,开始游戏,悔棋,认输,退出功能。
1.首页
2.黑棋走
3.白棋走
package com.sjsq;import java.util.hashmap;public class ai implements gobang {static hashmap<string, integer> map = new hashmap<string, integer>();static {/*** 权值*/// 防守权值// 活1连map.put("010", 20); //map.put(key,value) key=键 value=值map.put("0-10", 10);// 眠1连map.put("-110", 1);map.put("1-10", 1);// 活2连map.put("0110", 200);map.put("0-1-10", 100);// 眠2连map.put("-1110", 20);map.put("1-1-10", 10);// 活3连map.put("01110", 7000);map.put("0-1-1-10", 5000);// 眠3连map.put("-11110", 50);map.put("1-1-1-10", 30);// 活4连map.put("011110", 10000);map.put("0-1-1-1-10", 10000);// 眠4连map.put("-111110", 10000);map.put("1-1-1-1-10", 10000);// 碰壁眠4连map.put("11110", 10000);map.put("-1-1-1-10", 10000);//进攻权值//活1连map.put("010",10);map.put("0-10",20);//眠1连map.put("-110",1);map.put("1-10",1);//活2连map.put("0110", 100);map.put("0-1-10",200);//眠2连map.put("-1110",10);map.put("1-1-10",20);//活3连map.put("01110", 5000);map.put("0-1-1-10",7000);//眠3连map.put("-11110",30);map.put("1-1-1-10",50);//活4连map.put("011110", 10000);map.put("0-1-1-1-10",10000);//眠4连map.put("-111110",10000);map.put("1-1-1-1-10",10000);//碰壁眠4连map.put("11110", 10000);map.put("-1-1-1-10", 15000);}static string code;static integer weight;public static void quan() {for (int r = 0; r < array1.length; r++) {for (int c = 0; c < array1[r].length; c++) {if (array1[r][c] == 0) {// 如果该位置没有棋子则开始统计code = counthl(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}// 把另外七个方向统计完毕后,就完成权值统计code = counthr(r, c);// 调用水平向右统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}code = countht(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}code = counthb(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}code = counthzxs(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}code = counthyxs(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}code = counthzxx(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}code = counthyxx(r, c);// 调用水平向左统计的方法weight = map.get(code);// 根据棋子相连情况获取对应的权值if (weight != null) {// 判断是否有该种棋子相连的情况weightarray[r][c] += weight;// 累加权值}// 针对每种情况都进行权值分析,保证五子棋规则的完善// 判断两个2连在一条直线但中间有一个空位的情况if ((counthl(r, c) + counthr(r, c) == "01100110")|| (counthl(r, c) + counthr(r, c) == "0-1-100-1-10")|| (counthl(r, c) + counthr(r, c) == "-11100110")|| (counthl(r, c) + counthr(r, c) == "1-1-100-1-10")|| (counthl(r, c) + counthr(r, c) == "0110-1110")|| (counthl(r, c) + counthr(r, c) == "0-1-101-1-10")|| (counthl(r, c) + counthr(r, c) == "-1110-1110")|| (counthl(r, c) + counthr(r, c) == "1-1-101-1-10")) {weightarray[r][c] = weightarray[r][c] + 5000; }if ((countht(r, c) + counthb(r, c) == "01100110")|| (countht(r, c) + counthb(r, c) == "0-1-100-1-10")|| (countht(r, c) + counthb(r, c) == "-11100110")|| (countht(r, c) + counthb(r, c) == "1-1-100-1-10")|| (countht(r, c) + counthb(r, c) == "0110-1110")|| (countht(r, c) + counthb(r, c) == "0-1-101-1-10")|| (countht(r, c) + counthb(r, c) == "-1110-1110")|| (countht(r, c) + counthb(r, c) == "1-1-101-1-10")) {weightarray[r][c] = weightarray[r][c] + 5000;}if ((counthzxs(r, c) + counthyxx(r, c) == "01100110")|| (counthzxs(r, c) + counthyxx(r, c) == "0-1-100-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "-11100110")|| (counthzxs(r, c) + counthyxx(r, c) == "1-1-100-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "0110-1110")|| (counthzxs(r, c) + counthyxx(r, c) == "0-1-101-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "-1110-1110")|| (counthzxs(r, c) + counthyxx(r, c) == "1-1-101-1-10")) {weightarray[r][c] = weightarray[r][c] + 5000;}if ((counthyxs(r, c) + counthzxx(r, c) == "01100110")|| (counthyxs(r, c) + counthzxx(r, c) == "0-1-100-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "-11100110")|| (counthyxs(r, c) + counthzxx(r, c) == "1-1-100-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "0110-1110")|| (counthyxs(r, c) + counthzxx(r, c) == "0-1-101-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "-1110-1110")|| (counthyxs(r, c) + counthzxx(r, c) == "1-1-101-1-10")) {weightarray[r][c] = weightarray[r]福建信息职业技术学院[c] + 5000;}// 判断一个活2连和一个活1连在一条直线上但中间有一个空位的情况if ((counthl(r, c) + counthr(r, c) == "0100110") || (counthl(r, c) + counthr(r, c) == "0-100-1-10")|| (counthl(r, c) + counthr(r, c) == "0110010")|| (counthl(r, c) + counthr(r, c) == "0-1-100-10")) {weightarray[r][c] = weightarray[r][c] + 3000;}if ((countht(r, c) + counthb(r, c) == "0100110") || (countht(r, c) + counthb(r, c) == "0-100-1-10")|| (countht(r, c) + counthb(r, c) == "0110010")|| (countht(r, c) + counthb(r, c) == "0-1-100-10")) {weightarray[r][c] = weightarray[r][c] + 3000;}if ((counthzxs(r, c) + counthyxx(r, c) == "0100110")|| (counthzxs(r, c) + counthyxx(r, c) == "0-100-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "0110010")|| (counthzxs(r, c) + counthyxx(r, c) == "0-1-100-10")) {weightarray[r][c] = weightarray[r][c] + 3000;}if ((counthyxs(r, c) + counthzxx(r, c) == "0100110")|| (counthyxs(r, c) + counthzxx(r, c) == "0-100-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "0110010")|| (counthyxs(r, c) + counthzxx(r, c) == "0-1-100-10")) {weightarray[r][c] = weightarray[r][c] + 3000;}// 眠3连的一端被堵了// "1-1-1-10"&"010" "0-1-1-10"&"010"if ((counthl(r, c) + counthr(r, c) == "1-1-1-10010")|| (counthl(r, c) + counthr(r, c) == "0101-1-1-10")|| (counthl(r, c) + counthr(r, c) == "1-1-1-100")|| (counthl(r, c) + counthr(r, c) == "01-1-1-10")|| (counthl(r, c) + counthr(r, c) == "1-1-1-100110")|| (counthl(r, c) + counthr(r, c) == "01101-1-1-10")) {weightarray[r][c] = 1;}if ((countht(r, c) + counthb(r, c) == "1-1-1-10010")|| (countht(r, c) + counthb(r, c) == "0101-1-1-10")|| (countht(r, c) + counthb(r, c) == "1-1-1-100")|| (countht(r, c) + counthb(r, c) == "01-1-1-10")|| (countht(r, c) + counthb(r, c) == "1-1-1-10011dota潮汐猎人0")|| (countht(r, c) + counthb(r, c) == "01101-1-1-10")) {weightarray[r][c] = 1;;}if ((counthzxs(r, c) + counthyxx(r, c) == "1-1-1-10010")|| (counthzxs(r, c) + counthyxx(r, c) == "0101-1-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "1-1-1-100")|| (counthzxs(r, c) + counthyxx(r, c) == "01-1-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "1-1-1-100110")|| (counthzxs(r, c) + counthyxx(r, c) == "01101-1-1-10")) {weightarray[r][c] = 1;}if ((counthyxs(r, c) + counthzxx(r, c) == "1-1-1-10010")|| (counthyxs(r, c) + counthzxx(r, c) == "0101-1-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "1-1-1-100")|| (counthyxs(r, c) + counthzxx(r, c) == "01-1-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "1-1-1-100110")|| (counthyxs(r, c) + counthzxx(r, c) == "01101-1-1-10")) {weightarray[r][c] = 1;}// 3连和1连在一条线上差一个棋位if ((counthl(r, c) + counthr(r, c) == "0-1-1-100-10")|| (counthl(r, c) + counthr(r, c) == "0-101-1-1-10")|| (counthl(r, c) + counthr(r, c) == "01110010")|| (counthl(r, c) + counthr(r, c) == "010-11110")|| (counthl(r, c) + counthr(r, c) == "0-100-1-1-10")|| (counthl(r, c) + counthr(r, c) == "1-1-1-100-10")|| (counthl(r, c) + counthr(r, c) == "01001110")|| (counthl(r, c) + counthr(r, c) == "-11110010")) {weightarray[r][c] = weightarray[r][c] + 4000;}if ((countht(r, c) + counthb(r, c) == "0-1-1-100-10")|| (countht(r, c) + counthb(r, c) == "0-101-1-1-10")|| (countht(r, c) + counthb(r, c) == "01110010")|| (countht(r, c) + counthb(r, c) == "010-11110")|| (countht(r, c) + counthb(r, c) == "0-100-1-1-10")|| (countht(r, c) + counthb(r, c) == "1-1-1-100-10")|| (countht(r, c) + counthb(r, c) == "01001110")|| (countht(r, c) + counthb(r, c) == "-11110010")) {weightarray[r][c] = weightarray[r][c] + 4000;}if (老师的话(counthzxs(r, c) + counthyxx(r, c) == "0-1-1-100-10")|| (counthzxs(r, c) + counthyxx(r, c) == "0-101-1-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "01110010")|| (counthzxs(r, c) + counthyxx(r, c) == "010-11110")|| (counthzxs(r, c) + counthyxx(r, c) == "0-100-1-1-10")|| (counthzxs(r, c) + counthyxx(r, c) == "1-1-1-100-10")|| (counthzxs(r, c) + counthyxx(r, c) == "01001110")|| (counthzxs(r, c) + counthyxx(r, c) == "-11110010"病史采集)) {weightarray[r][c] = weightarray[r][c] + 4000;}if ((counthyxs(r, c) + counthzxx(r, c) == "0-1-1-100-10")|| (counthyxs(r, c) + counthzxx(r, c) == "0-101-1-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "01110010")|| (counthyxs(r, c) + counthzxx(r, c) == "010-11110")|| (counthyxs(r, c) + counthzxx(r, c) == "0-100-1-1-10")|| (counthyxs(r, c) + counthzxx(r, c) == "1-1-1-100-10")|| (counthyxs(r, c) + counthzxx(r, c) == "01001110")|| (counthyxs(r, c) + counthzxx(r, c) == "-11110010")) {weightarray[r][c] = weightarray[r][c] + 4000;}}}}}// 水平向左统计的方法public static string counthl(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int r1 = r - 1; r1 >= 0; r1--) {if (array1[r1][c] == 0) {// 表示空位沒有棋子if (r1 + 1 == r) {// 相邻break;} el {code = array1[r1][c] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r1][c];// 记录棋子code = array1[r1][c] + code;// 记录棋子相连情况} el if (chess == array1[r1][c]) {code = array1[r1][c] + code;// 记录棋子相连情况} el {code = array1[r1][c] + code;// 记录棋子相连情况break;}}}return code;}// 水平向右统计的方法public static string counthr(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int r1 = r + 1; r1 < coloum; r1++) {if (array1[r1][c] == 0) {// 表示空位沒有棋子if (r1 - 1 == r) {// 相邻break;} el {code = array1[r1][c] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r1][c];// 记录棋子code = array1[r1][c] + code;// 记录棋子相连情况} el if (chess == array1[r1][c]) {code = array1[r1][c] + code;// 记录棋子相连情况} el {code = array1[r1][c] + code;// 记录棋子相连情况break;}}}return code;}// 竖直向上统计的方法public static string countht(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int c1 = c - 1; c1 >= 0; c1--) {if (array1[r][c1] == 0) {// 表示空位沒有棋子if (c1 + 1 == r) {// 相邻break;} el {code = array1[r][c1] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r][c1];// 记录棋子code = array1[r][c1] + code;// 记录棋子相连情况} el if (chess == array1[r][c1]) {code = array1[r][c1] + code;// 记录棋子相连情况} el {code = array1[r][c1] + code;// 记录棋子相连情况break;}}}return code;}// 竖直向下统计的方法public static string counthb(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int c1 = c + 1; c1 < row; c1++) {if (array1[r][c1] == 0) {// 表示空位沒没有棋子if (c1 - 1 == c) {// 相邻break;} el {code = array1[r][c1] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r][c1];// 记录棋子code = array1[r][c1] + code;// 记录棋子相连情况} el if (chess == array1[r][c1]) {code = array1[r][c1] + code;// 记录棋子相连情况} el {code = array1[r][c1] + code;// 记录棋子相连情况break;}}}return code;}// 左斜向上统计的方法public static string counthzxs(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int r1 = r - 1, c1 = c - 1; r1 >= 0 && c1 >= 0; r1--, c1--) {if (array1[r1][c1] == 0) {// 表示空位沒有棋子if (c1 + 1 == c && r1 + 1 == r) {// 相邻break;} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r1][c1];// 记录棋子code = array1[r1][c1] + code;// 记录棋子相连情况} el if (chess == array1[r1][c1]) {code = array1[r1][c1] + code;// 记录棋子相连情况} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}}}return code;}// 右斜向上统计的方法public static string counthyxs(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int r1 = r + 1, c1 = c - 1; c1 >= 0 && r1 < coloum; r1++, c1--) {if (array1[r1][c1] == 0) {// 表示空位沒有棋子if (r1 - 1 == r && c1 + 1 == c) {// 相邻break;} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r1][c1];// 记录棋子code = array1[r1][c1] + code;// 记录棋子相连情况} el if (chess == array1[r1][c1]) {code = array1[r1][c1] + code;// 记录棋子相连情况} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}}}return code;}// 左斜向下统计的方法public static string counthzxx(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int r1 = r - 1, c1 = c + 1; c1 < row && r1 >= 0; c1++, r1--) {if (array1[r1][c1] == 0) {// 表示空位沒有棋子if (c1 - 1 == c && r1 + 1 == r) {// 相邻break;} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r1][c1];// 记录棋子code = array1[r1][c1] + code;// 记录棋子相连情况} el if (chess == array1[r1][c1]) {code = array1[r1][c1] + code;// 记录棋子相连情况} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}}}return code;}// 右斜向下统计的方法public static string counthyxx(int r, int c) {string code = "0";int chess = 0;// 存储第一颗出现的棋子// 循环遍历for (int r1 = r + 1, c1 = c + 1; r1 < coloum && c1 < row; r1++, c1++) {if (array1[r1][c1] == 0) {// 表示空位沒有棋子if (c1 - 1 == c && r1 - 1 == r) {// 相邻break;} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}} el {// 表示该位置有棋子if (chess == 0) {// 第一次出现棋子chess = array1[r1][c1];// 记录棋子code = array1[r1][c1] + code;// 记录棋子相连情况} el if (chess == array1[r1][c1]) {code = array1[r1][c1] + code;// 记录棋子相连情况} el {code = array1[r1][c1] + code;// 记录棋子相连情况break;}}}return code;}}
package com.sjsq;import java.awt.color;//设置棋子颜色和棋子的落子位置public class chess extends object implements gobang{int coloum,row; //输入列数,行数color color; //棋盘颜色的设置public chess(int coloum,int row,color color) {this.coloum=coloum;this.row=row;this.color=color;}}
package com.sjsq;public interface gobang { //接口 申明全局变量public static final int size = 40; // 设置格子大小为40public static final int x = 20, y = 20; // 设置棋盘右上角的位置public static final int coloum = 15; //设置行数public static final int row = 15; //设置列数public static final int[][] array1 = new int[coloum][row]; //记录棋子位置的数组public static final int[][] weightarray = new int[coloum][row]; //记录棋盘每个位置的权值public static final boolean flag[] = new boolean[2]; //记录选择的模式public static final boolean co[] = new boolean[2]; //记录选择的棋色}
package com.sjsq;import java.awt.color;import java.awt.graphics;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.awt.event.mouadapter;import java.awt.event.mouevent;import java.util.arrays;import javax.swing.joptionpane;public class gobanglistener extends mouadapter implements actionlistener,gobang{private gobangmain gm; // 棋盘面板对象private graphics g; // 画笔对象boolean cco=true; //记录玩家下棋还是电脑下棋boolean fff=true; //记录是否能悔棋boolean ggg=true; //记录是否能认输private myarraylist<chess> array;int coloum1,row1;int xx,yy,max;public gobanglistener(gobangmain gm,myarraylist<chess> array) { //从gobangmain传窗体对象和记录棋子的数组this.gm = gm;this.array=array;}public void actionperformed(actionevent e) {if (e.getactioncommand().equals("悔棋")) {if(flag[0]) { //人人对战悔棋if(fff) {if(array.getsize()>1) {array1[coloum1][row1]=0;chess aaa=array.get(array.getsize()-2);coloum1=aaa.coloum;row1=aaa.row;array.delete();cco=!cco;gm.repaint();}}}if(flag[1]) { //人机对战悔棋if(fff) {if(cco) {if(array.getsize()>2) {array1[xx][yy]=0;chess aaa=array.get(array.getsize()-2);coloum1=aaa.coloum;row1=aaa.row;array.delete();array1[coloum1][row1]=0;chess bbb=array.get(array.getsize()-2);xx=bbb.coloum;yy=bbb.row;array.delete();gm.repaint();}}}}}if (e.getactioncommand().equals("认输")) {if(ggg) {if(flag[0]){if(cco) {joptionpane.showmessagedialog(gm, "白棋获胜");}el {joptionpane.showmessagedialog(gm, "黑棋获胜");}gm.removemoulistener(this);fff=fal;ggg=fal;}if(flag[1]) {if(co[0]) {if(cco) {joptionpane.showmessagedialog(gm, "黑棋获胜");}el {joptionpane.showmessagedialog(gm, "白棋获胜");}gm.removemoulistener(this);fff=fal;ggg=fal;}if(co[1]) {if(cco) {joptionpane.showmessagedialog(gm, "白棋获胜");}el {joptionpane.showmessagedialog(gm, "黑棋获胜");}gm.removemoulistener(this);fff=fal;ggg=fal;array.ret();}}}}if (e.getactioncommand().equals("退出")) { //退出游戏system.exit(0);}if (e.getactioncommand().equals("人人对战")) { //选择人人对战模式 flag[0]为true,flag[1]为falflag[0]=true;flag[1]=fal;for爱赣榆(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}cco=true;fff=true;ggg=true;array.ret();gm.repaint();}if (e.getactioncommand().equals("人机对战")) { //选择人机对战模式 flag[0]为fal,flag[1]为trueflag[0]=fal;flag[1]=true;for(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}cco=true;fff=true;ggg=true;array.ret();gm.repaint();}if (e.getactioncommand().equals("黑子(先手)")) { co[0]=fal;co[1]=true;for(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}cco=true;fff=true;ggg=true;array.ret();gm.repaint();}if (e.getactioncommand().equals("白子(后手)")) { co[0]=true;co[1]=fal;for(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}cco=fal;fff=true;ggg=true;array.ret();gm.repaint();}if (e.getactioncommand().equals("开始新游戏")) {if(flag[1]) {if (co[1]){for(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}gm.addmoulistener(this);array.ret();gm.repaint();}if(co[0]) {for(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}//cco=fal;gm.addmoulistener(this);array.ret();gm.repaint();// g.tcolor(color.black);//g.filloval(x+8*size-size/2, y+8*size-size/2, size, size);array1[7][7]=-1; //ai先手第一个子落点chess sh=new chess(7,7,color.black);array.add(sh);cco=true;fff=true;ggg=true;}}if(flag[0]) {for(int i=0;i<array1.length;i++) {arrays.fill(array1[i], 0);}cco=true;fff=true;ggg=true;gm.addmoulistener(this);array.ret();gm.repaint();}}}public void mourelead(mouevent e) { //鼠标松开的时候进行的操作if(flag[0]) { //选择人人对战模式进行的操作if (g == null)g = gm.getgraphics();int x = e.getx();int y = e.gety();coloum1 = (x-x+size/2)/size; row1 = (y-y+size/2)/size;if(coloum1<coloum&&row1<row) {if(array1[coloum1][row1]==0) {if(cco) {g.tcolor(color.black);g.filloval(x+coloum1*size-size/2, y+row1*size-size/2, size, size);array1[coloum1][row1]=1;chess sh=new chess(coloum1,row1,color.black);array.add(sh);}el {g.tcolor(color.white);g.filloval(x+coloum1*size-size/2, y+row1*size-size/2, size, size);array1[coloum1][row1]=-1;chess sh=new chess(coloum1,row1,color.white);array.add(sh);}judge jd=new judge(coloum1,row1);if(jd.judge()) {if(cco) {joptionpane.showmessagedialog(gm, "黑棋获胜");}el {joptionpane.showmessagedialog(gm, "白棋获胜");}gm.removemoulistener(this);fff=fal;ggg=fal;array.ret();}cco=!cco;}}}if(flag[1]) { //选择人机对战进行的操作if (g == null)g = gm.getgraphics();if(co[1]) {if(cco) { //若cco为true,则人下棋int x = e.getx();int y = e.gety();coloum1 = (x-x+size/2)/size; row1 = (y-y+size/2)/size;if(coloum1<coloum&&row1<row) {if(array1[coloum1][row1]==0) {g.tcolor(color.black);g.filloval(x+coloum1*size-size/2, y+row1*size-size/2, size, size);array1[coloum1][row1]=1;chess sh=new chess(coloum1,row1,color.black);array.add(sh);judge jd=new judge(coloum1,row1);if(jd.judge()) {if(cco) {joptionpane.showmessagedialog(gm, "黑棋获胜");}el {joptionpane.showmessagedialog(gm, "白棋获胜");}gm.removemoulistener(this);fff=fal;ggg=fal;array.ret();cco=!cco;}cco=!cco;}}}if(!cco) { //若cco为fal,则机器下棋aix();}}if(co[0]) {if(cco) { //若cco为true,则人下棋int x = e.getx();int y = e.gety();coloum1 = (x-x+size/2)/size; row1 = (y-y+size/2)/size;if(coloum1<coloum&&row1<row) {if(array1[coloum1][row1]==0) {g.tcolor(color.white);g.filloval(x+coloum1*size-size/2, y+row1*size-size/2, size, size);array1[coloum1][row1]=1;chess sh=new chess(coloum1,row1,color.white);array.add(sh);judge jd=new judge(coloum1,row1);if(jd.judge()) {if(cco) {joptionpane.showmessagedialog(gm, "白棋获胜");}el {joptionpane.showmessagedialog(gm, "黑棋获胜");}gm.removemoulistener(this);fff=fal;ggg=fal;array.ret();cco=!cco;}cco=!cco;}}}if(!cco) { //若cco为fal,则机器下棋aix();}}}}//调用ai进行下棋public void aix() {if(co[1]) {for(int i=0;i<weightarray.length;i++) {for(int j=0;j<weightarray[i].length;j++) {weightarray[i][j]=0;}}max=-1;ai.quan(); for(int i=0;i<weightarray.length;i++) {for(int j=0;j<weightarray[i].length;j++) {if(i<5&&j<5) {if(max<=weightarray[i][j]&&array1[i][j]==0) {max=weightarray[i][j];xx=i;yy=j;}}el {if(max<weightarray[i][j]&&array1[i][j]==0) {max=weightarray[i][j];xx=i;yy=j;}}}}if(array1[xx][yy]==0) {g.tcolor(color.white);g.filloval(x+xx*size-size/2, y+yy*size-size/2, size, size);array1[xx][yy]=-1;chess sh=new chess(xx,yy,color.white);array.add(sh);judge jd=new judge(xx,yy);if(jd.judge()) {if(cco) {joptionpane.showmessagedialog(gm, "黑棋获胜");}el {joptionpane.showmessagedialog(gm, "白棋获胜");}gm.removemoulistener(this); //移除监听,这时将不能对棋盘进行操作fff=fal; //设置不能进行悔棋ggg=fal; //设置不能进行认输array.ret();}cco=!cco;}}if(co[0]) {for(int i=0;i<weightarray.length;i++) {for(int j=0;j<weightarray[i].length;j++) {weightarray[i][j]=0;}}max=-1;ai.quan();for(int i=0;i<weightarray.length;i++) {for(int j=0;j<weightarray[i].length;j++) {if(i<5&&j<5) {if(max<=weightarray[i][j]&&array1[i][j]==0) {max=weightarray[i][j];xx=i;yy=j;}}el {if(max<weightarray[i][j]&&array1[i][j]==0) {max=weightarray[i][j];xx=i;yy=j;}}}}if(array1[xx][yy]==0) {g.tcolor(color.black);g.filloval(x+xx*size-size/2, y+yy*size-size/2, size, size);array1[xx][yy]=-1;chess sh=new chess(xx,yy,color.black);array.add(sh);judge jd=new judge(xx,yy);if(jd.judge()) {if(cco) {joptionpane.showmessagedialog(gm, "白棋获胜");}el {joptionpane.showmessagedialog(gm, "黑棋获胜");}gm.removemoulistener(this); //移除监听,这时将不能对棋盘进行操作fff=fal; //设置不能进行悔棋ggg=fal; //设置不能进行认输array.ret();}cco=!cco;}}}}
以上就是java+swing实现五子棋游戏的示例代码的详细内容,更多关于java swing五子棋的资料请关注www.887551.com其它相关文章!
本文发布于:2023-04-05 01:11:01,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/384bfb36f8480d92be0cc0469a2aa394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java+Swing实现五子棋游戏的示例代码.doc
本文 PDF 下载地址:Java+Swing实现五子棋游戏的示例代码.pdf
留言与评论(共有 0 条评论) |