Java+Swing實現五子棋遊戲的示例代碼

一、系統介紹

1.開發環境

開發工具:Eclipse2021

JDK版本:jdk1.8

Mysql版本:8.0.13

2.技術選型

Java+Swing

3.系統功能

實現五子棋遊戲,開始遊戲,悔棋,認輸,退出功能。

二、系統展示

1.首頁

2.黑棋走

3.白棋走

三、部分代碼

AI.java

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-100110")
							|| (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;
				} else {
					code = array1[r1][c] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r1][c];// 記錄棋子
					code = array1[r1][c] + code;// 記錄棋子相連情況
				} else if (chess == array1[r1][c]) {
					code = array1[r1][c] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r1][c] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r1][c];// 記錄棋子
					code = array1[r1][c] + code;// 記錄棋子相連情況
				} else if (chess == array1[r1][c]) {
					code = array1[r1][c] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r][c1] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r][c1];// 記錄棋子
					code = array1[r][c1] + code;// 記錄棋子相連情況
				} else if (chess == array1[r][c1]) {
					code = array1[r][c1] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r][c1] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r][c1];// 記錄棋子
					code = array1[r][c1] + code;// 記錄棋子相連情況
				} else if (chess == array1[r][c1]) {
					code = array1[r][c1] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r1][c1];// 記錄棋子
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else if (chess == array1[r1][c1]) {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r1][c1];// 記錄棋子
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else if (chess == array1[r1][c1]) {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r1][c1];// 記錄棋子
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else if (chess == array1[r1][c1]) {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else {
					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;
				} else {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
					break;
				}
			} else {// 表示該位置有棋子
				if (chess == 0) {// 第一次出現棋子
					chess = array1[r1][c1];// 記錄棋子
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else if (chess == array1[r1][c1]) {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
				} else {
					code = array1[r1][c1] + code;// 記錄棋子相連情況
					break;
				}
			}
		}
		return code;
	}
}

Chess.java

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;
	}
}

Gobang.java

package com.sjsq;
 
public interface Gobang {  //接口  申明全局變量
	public static final int size = 40; // 設置格子大小為40
	public 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];    //記錄選擇的棋色
}

GobangListener.java

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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
 
import javax.swing.JOptionPane;
 
public class GobangListener extends MouseAdapter 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, "白棋獲勝");
    			}else {
    				JOptionPane.showMessageDialog(gm, "黑棋獲勝");
    			}
    			gm.removeMouseListener(this);
    			fff=false;
    			ggg=false;
    			}
    			if(flag[1]) {
    				if(co[0]) {
    	    			if(cco) {
    	    				JOptionPane.showMessageDialog(gm, "黑棋獲勝");
    	    			}else {
    	    				JOptionPane.showMessageDialog(gm, "白棋獲勝");
    	    			}
    	    			gm.removeMouseListener(this);
    	    			fff=false;
    	    			ggg=false;
    				}
    				if(co[1]) {
    	    			if(cco) {
    	    				JOptionPane.showMessageDialog(gm, "白棋獲勝");
    	    			}else {
    	    				JOptionPane.showMessageDialog(gm, "黑棋獲勝");
    	    			}
    	    			gm.removeMouseListener(this);
    	    			fff=false;
    	    			ggg=false;
    	        		array.Reset();
    				}
    			}
    			}
    		}
    	if (e.getActionCommand().equals("退出")) {   //退出遊戲
    		System.exit(0);
		}
    	if (e.getActionCommand().equals("人人對戰")) {  //選擇人人對戰模式 flag[0]為true,flag[1]為false
    		flag[0]=true;
    		flag[1]=false;
    		for(int i=0;i<array1.length;i++) {
    			Arrays.fill(array1[i], 0);
    		}
    		cco=true;
    		fff=true;
    		ggg=true;
    		array.Reset();
    		gm.repaint();
    	}
    	if (e.getActionCommand().equals("人機對戰")) {  //選擇人機對戰模式 flag[0]為false,flag[1]為true
    		flag[0]=false;
    		flag[1]=true;
    		for(int i=0;i<array1.length;i++) {
    			Arrays.fill(array1[i], 0);
    		}
    		cco=true;
    		fff=true;
    		ggg=true;
    		array.Reset();
    		gm.repaint();
    	}
    	if (e.getActionCommand().equals("黑子(先手)")) {  
    		co[0]=false;
    		co[1]=true;
    		for(int i=0;i<array1.length;i++) {
    			Arrays.fill(array1[i], 0);
    		}
    		cco=true;
    		fff=true;
    		ggg=true;
    		array.Reset();
    		gm.repaint();
    	}
    	if (e.getActionCommand().equals("白子(後手)")) {  
    		co[0]=true;
    		co[1]=false;
    		for(int i=0;i<array1.length;i++) {
    			Arrays.fill(array1[i], 0);
    		}
    		cco=false;
    		fff=true;
    		ggg=true;
    		array.Reset();
    		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.addMouseListener(this);
    		array.Reset();
    		gm.repaint();
    	}
    	if(co[0]) {
 
			for(int i=0;i<array1.length;i++) {
    			Arrays.fill(array1[i], 0);
    		}
//			cco=false;
    		gm.addMouseListener(this);
    		array.Reset();
    		gm.repaint();
    		
//    		g.setColor(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.addMouseListener(this);
        		array.Reset();
        		gm.repaint();
    		}
    		}
    	}
 
	public void mouseReleased(MouseEvent 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.setColor(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);
					}
					else {
						g.setColor(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, "黑棋獲勝");
						}else {
							JOptionPane.showMessageDialog(gm, "白棋獲勝");
						}
						gm.removeMouseListener(this);
						fff=false;
						ggg=false;
			    		array.Reset();
					}
					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.setColor(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, "黑棋獲勝");
							}else {
								JOptionPane.showMessageDialog(gm, "白棋獲勝");
							}
							gm.removeMouseListener(this);
							fff=false;
							ggg=false;
				    		array.Reset();
							cco=!cco;
						}
						cco=!cco;
					
					}
					
				}
			}
			if(!cco) {  //若cco為false,則機器下棋
				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.setColor(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, "白棋獲勝");
								}else {
									JOptionPane.showMessageDialog(gm, "黑棋獲勝");
								}
								gm.removeMouseListener(this);
								fff=false;
								ggg=false;
					    		array.Reset();
								cco=!cco;
							}
							cco=!cco;
						
						}
						
					}
				}
				if(!cco) {  //若cco為false,則機器下棋
					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;
					}
				}else {
					if(max<weightArray[i][j]&&array1[i][j]==0) {
						max=weightArray[i][j];
						xx=i;yy=j;
					}
				}
			}
		}
		if(array1[xx][yy]==0) {
			g.setColor(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, "黑棋獲勝");
				}else {
					JOptionPane.showMessageDialog(gm, "白棋獲勝");
				}
				gm.removeMouseListener(this);  //移除監聽,這時將不能對棋盤進行操作
				fff=false;         //設置不能進行悔棋
				ggg=false;         //設置不能進行認輸
	    		array.Reset();
			}
			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;
						}
					}else {
						if(max<weightArray[i][j]&&array1[i][j]==0) {
							max=weightArray[i][j];
							xx=i;yy=j;
						}
					}
				}
			}
			
			if(array1[xx][yy]==0) {
				g.setColor(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, "白棋獲勝");
					}else {
						JOptionPane.showMessageDialog(gm, "黑棋獲勝");
					}
					gm.removeMouseListener(this);  //移除監聽,這時將不能對棋盤進行操作
					fff=false;         //設置不能進行悔棋
					ggg=false;         //設置不能進行認輸
		    		array.Reset();
				}
				cco=!cco;
			}
		}
}
	
 
 
 
	}

以上就是Java+Swing實現五子棋遊戲的示例代碼的詳細內容,更多關於Java Swing五子棋的資料請關註WalkonNet其它相關文章!

推薦閱讀: