⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 robot.java

📁 一个关于JAVA五子棋的算法
💻 JAVA
字号:
package daima;

public class Robot {
	QiPan qb;
	Main main;

	int levelState = 1;

	int first = 0;

	// 下落此子后的得分
	int[][] blackScore = new int[15][15];

	int[][] whiteScore = new int[15][15];

	int blackHighScore;

	int whiteHigeScore;

	int[] zu = new int[7];

	boolean blackWin, whiteWin, blackWin4, whiteWin4;

	public Robot(QiPan qb) {
		this.qb = qb;
	}

	public void start() {
		this.blackWin = false;
		this.whiteWin = false;
		this.first = 0;
	}

	public void setLevel(int level) {
		this.levelState = level;
	}

	public void next() {

		first++;
		System.out.println("ba" + this.qb.lastQiZi);

		if (this.qb.lastQiZi == -1) {
			System.out.println("c");
			qb.putDown(new Coor((int) (Math.random() * 3) + 6, (int) (Math
					.random() * 3) + 6), !qb.ifBlack);
		} else
			qb.putDown(this.getAttack(), !qb.ifBlack);
		qb.canPutBen = true;
	}

	public Coor getAttack() {
		Coor z1 = this.getHighBlackScore();
		Coor z2 = this.getHighWhiteScore();
		if (z1 == null || z2 == null) {
			this.qb.setStr("棋盘满,游戏结束");
			this.qb.setGameOver(true);
		}

		System.out.println("黑==" + this.blackHighScore + " 白=="
				+ this.whiteHigeScore);

		if (qb.ifBlack) {
			if (this.whiteWin4) {
				System.out.println("必胜白点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				this.whiteWin = false;
				this.blackWin = false;
				return z2;
			} else if (this.blackWin4) {
				System.out.println("必胜黑点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				this.whiteWin = false;
				this.blackWin = false;
				return z1;
			}
		} else {
			if (this.blackWin4) {
				System.out.println("必胜黑点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				return z1;
			} else if (this.whiteWin4) {
				System.out.println("必胜白点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				return z2;
			}
		}

		if (qb.ifBlack) {
			if (this.whiteWin) {
				System.out.println("必胜白点");
				this.whiteWin = false;
				this.blackWin = false;
				return z2;
			} else if (this.blackWin) {
				System.out.println("必胜黑点");
				this.whiteWin = false;
				this.blackWin = false;
				return z1;
			}
		} else {
			if (this.blackWin) {
				System.out.println("必胜黑点");
				this.whiteWin = false;
				this.blackWin = false;
				return z1;
			} else if (this.whiteWin) {
				System.out.println("必胜白点");
				this.whiteWin = false;
				this.blackWin = false;
				return z2;
			}
		}

		if (qb.ifBlack) {
			if (this.blackHighScore > this.whiteHigeScore) {
				System.out.println("防守");
				return z1;
			} else {
				System.out.println("进攻");
				return z2;
			}

		} else {
			if (this.blackHighScore < this.whiteHigeScore) {
				System.out.println("防守");
				return z2;
			} else {
				System.out.println("进攻");
				return z1;
			}
		}
	}

	public Coor getHighBlackScore() {
		int high = -1;
		Coor[] hz = new Coor[8];
		for (int i = 0; i <= 14; i++)
			for (int j = 0; j <= 14; j++) {
				if (high < this.blackScore[i][j]) {
					if (qb.qiGe[i][j] == qb.none_state) {
						high = this.blackScore[i][j];
						hz[0] = new Coor(0, 0);
						hz[0].x = i;
						hz[0].y = j;
					}
				}
			}
		int curr = 0;
		stop: {
			for (int i = 0; i <= 14; i++)
				for (int j = 0; j <= 14; j++) {
					if (this.blackScore[i][j] == high) {
						if (qb.qiGe[i][j] == qb.none_state) {
							if (++curr > 7)
								break stop;
							hz[curr] = new Coor(0, 0);
							hz[curr].x = i;
							hz[curr].y = j;
						}
					}

				}
		}
		this.blackHighScore = high;
		return hz[(int) (Math.random() * curr)];
	}

	public Coor getHighWhiteScore() {
		int high = -1;
		Coor[] hz = new Coor[8];
		for (int i = 0; i <= 14; i++)
			for (int j = 0; j <= 14; j++) {
				if (high < this.whiteScore[i][j]) {
					if (qb.qiGe[i][j] == qb.none_state) {
						high = this.whiteScore[i][j];
						hz[0] = new Coor(0, 0);
						hz[0].x = i;
						hz[0].y = j;
					}
				}
				// System.out.print(whiteScore[i][j]+" ");
			}

		int curr = 0;
		stop: {
			for (int i = 0; i <= 14; i++)
				for (int j = 0; j <= 14; j++) {
					if (this.whiteScore[i][j] == high) {
						if (qb.qiGe[i][j] == qb.none_state) {
							if (++curr > 7)
								break stop;
							hz[curr] = new Coor(0, 0);
							hz[curr].x = i;
							hz[curr].y = j;
						}
					}

				}
		}
		this.whiteHigeScore = high;
		return hz[(int) (Math.random() * curr)];
	}

	public void updateScore() {// System.out.print("updateScore(),");
		this.whiteWin4 = false;
		this.blackWin4 = false;
		this.whiteWin = false;
		this.blackWin = false;
		int hei = 0;
		int bai = 0;
		for (int i = 0; i <= 14; i++)
			for (int j = 0; j <= 14; j++) {
				this.blackScore[i][j] = 0;
				this.whiteScore[i][j] = 0;

				// ----------------纵向检查-------------------
				int m = i - 4;
				if (m < 0)
					m = 0;
				for (; m <= i && m + 4 <= 14; m++) {
					hei = bai = 0;
					for (int n = m; n <= m + 4; n++) {
						zu[n - m + 1] = qb.qiGe[n][j];
						if (qb.qiGe[n][j] == qb.hei_state)
							hei++;
						else if (qb.qiGe[n][j] == qb.bai_state)
							bai++;
					}
					zu[i - m + 1] = 3;
					if (m - 1 < 0)
						zu[0] = 2;
					else
						zu[0] = qb.qiGe[m - 1][j];
					if (m + 5 > 14)
						zu[6] = 2;
					else
						zu[6] = qb.qiGe[m + 5][j];
					addScore(hei, bai, i, j);
				}

				// -------------------横向检查-----------------
				m = j - 4;
				if (m < 0)
					m = 0;
				for (; m <= j && m + 4 <= 14; m++) {
					hei = bai = 0;
					for (int n = m; n <= m + 4; n++) {
						zu[n - m + 1] = qb.qiGe[i][n];
						if (qb.qiGe[i][n] == qb.hei_state)
							hei++;
						else if (qb.qiGe[i][n] == qb.bai_state)
							bai++;
					}
					zu[j - m + 1] = 3;

					if (m - 1 < 0)
						zu[0] = 2;
					else
						zu[0] = qb.qiGe[i][m - 1];
					if (m + 5 > 14)
						zu[6] = 2;
					else
						zu[6] = qb.qiGe[i][m + 5];
					addScore(hei, bai, i, j);
				}

				// -----------------左上到右下-----------------
				m = 4;
				if (i - m < 0)
					m = i;
				if (j - m < 0)
					m = j;
				for (; m >= 0; m--) {
					if (i - m + 4 > 14)
						break;
					if (j - m + 4 > 14)
						break;
					hei = bai = 0;
					for (int n = m; n >= m - 4; n--) {
						zu[m - n + 1] = qb.qiGe[i - n][j - n];
						if (qb.qiGe[i - n][j - n] == qb.hei_state)
							hei++;
						else if (qb.qiGe[i - n][j - n] == qb.bai_state)
							bai++;
					}
					zu[m + 1] = 3;

					if (i - m - 1 < 0 || j - m - 1 < 0)
						zu[0] = 2;
					else
						zu[0] = qb.qiGe[i - m - 1][j - m - 1];
					if (i - m + 5 > 14 || j - m + 5 > 14)
						zu[6] = 2;
					else
						zu[6] = qb.qiGe[i - m + 5][j - m + 5];
					addScore(hei, bai, i, j);
				}

				// -------------------左下到右上-------------------
				m = 4;
				if (i + m > 14)
					m = 14 - i;
				if (j - m < 0)
					m = j;
				for (; m >= 0; m--) {
					hei = bai = 0;
					if (i + m - 4 < 0)
						break;
					if (j - m + 4 > 14)
						break;

					for (int n = m; n >= m - 4; n--) {
						zu[m - n + 1] = qb.qiGe[i + n][j - n];
						if (qb.qiGe[i + n][j - n] == qb.hei_state)
							hei++;
						else if (qb.qiGe[i + n][j - n] == qb.bai_state)
							bai++;
					}
					zu[m + 1] = 3;
					if (j - m - 1 < 0 || i + m + 1 > 14)
						zu[0] = 2;
					else
						zu[0] = qb.qiGe[i + m + 1][j - m - 1];
					if (j - m + 5 > 14 || i + m - 5 < 0)
						zu[6] = 2;
					else
						zu[6] = qb.qiGe[i + m - 5][j - m + 5];
					addScore(hei, bai, i, j);
				}
			}
	}// method updateScore()

	private void addScore(int hei, int bai, int i, int j) {
		int k = 0;
		if (this.levelState == 1)
			k = 0;
		else {
			if (first <= 3)
				;
			else // (first>3)
			{
				if (this.qb.ifBlack)
					k = 1;
				else
					k = -1;
			}
		}
		if (bai == 0) {
			for (int m = 1; m <= 5; m++) {
				if (zu[m] == 3)
					zu[m] = -1;
			}
			if (hei == 0)
				this.blackScore[i][j] += 1;
			else if (hei == 1)
				this.blackScore[i][j] += 3;
			else if (hei == 2) {
				if (this.levelState == 3) {
					int v = 0;
					if (zu[0] == 2 || zu[0] == 1)
						v++;
					if (zu[6] == 2 || zu[6] == 1)
						v++;
					if (zu[1] == -1 && zu[3] == -1 && zu[5] == -1) {
						if (v == 2)
							this.blackScore[i][j] += 3;
						else if (v == 1)
							this.blackScore[i][j] += 9;
						else if (v == 0)
							this.blackScore[i][j] += 27;
					} else {
						if (v == 2)
							this.blackScore[i][j] += 4;
						else if (v == 0)
							this.blackScore[i][j] += 54;
						else {
							if (zu[1] == 0 && zu[5] == 0)
								this.blackScore[i][j] += 54;
							else if (zu[1] == -1) {
								if (zu[0] == 0 || zu[0] == -1)
									this.blackScore[i][j] += 54;
								else
									this.blackScore[i][j] += 9;
							} else if (zu[5] == -1) {
								if (zu[6] == 0 || zu[6] == -1)
									this.blackScore[i][j] += 54;
								else
									this.blackScore[i][j] += 9;
							}
						}
					}
				} else {
					this.blackScore[i][j] += 9;
				}
			}

			else if (hei == 3) {
				int m = this.blackScore[i][j];
				if (true) {
					int v = 0;
					if (zu[0] == 2 || zu[0] == 1)
						v++;
					if (zu[6] == 2 || zu[6] == 1)
						v++;
					if (v == 2) {
						this.blackScore[i][j] += 40;
					} else if (v == 1) {
						if (zu[0] == 2 || zu[0] == 1) {
							if (zu[1] == 0) {
								this.blackWin = true;
								this.blackScore[i][j] += 1000;
							} else {
								this.blackScore[i][j] += 40;
							}
						} else if (zu[6] == 2 || zu[6] == 1) {
							if (zu[5] == 0) {
								this.blackWin = true;
								this.blackScore[i][j] += 1000;
							} else {
								this.blackScore[i][j] += 40;
							}
						}
					} else if (v == 0) {
						if (zu[1] == 0 || zu[5] == 0) {
							this.blackWin = true;
							this.blackScore[i][j] += 1000;
						} else {
							this.blackScore[i][j] += 63;
						}
					}

				}
				if (this.levelState == 3)
					;
				else {
					this.blackScore[i][j] = m;
					this.blackScore[i][j] += 27;
				}
			}

			else if (hei == 4) {
				this.blackWin4 = true;
				this.blackScore[i][j] += 10000;
			} else if (hei == 5) {
				qb.gameOver = true;
				qb.str = "五连珠,黑胜~!";
			}
		}
		if (hei == 0) {
			for (int m = 1; m <= 5; m++) {
				if (zu[m] == -1)
					zu[m] = 1;
			}
			if (bai == 0)
				this.whiteScore[i][j] += 1;
			else if (bai == 1)
				this.whiteScore[i][j] += 3 + k;
			else if (bai == 2) {
				if (this.levelState == 3) {
					int v = 0;
					if (zu[0] == 2 || zu[0] == -1)
						v++;
					if (zu[6] == 2 || zu[6] == -1)
						v++;
					if (zu[1] == 1 && zu[3] == 1 && zu[5] == 1) {
						if (v == 2)
							this.whiteScore[i][j] += 3 + k * 2;
						else if (v == 1)
							this.whiteScore[i][j] += 9 + k * 2;
						else if (v == 0)
							this.whiteScore[i][j] += 27 + k * 2;
					} else {
						if (v == 2)
							this.whiteScore[i][j] += 4 + k * 2;
						else if (v == 0)
							this.whiteScore[i][j] += 54 + k * 2;
						else {
							if (zu[1] == 0 && zu[5] == 0)
								this.whiteScore[i][j] += 54 + k * 2;
							else if (zu[1] == 1) {
								if (zu[0] == 0 || zu[0] == 1)
									this.whiteScore[i][j] += 54 + k * 2;
								else
									this.whiteScore[i][j] += 9 + k * 2;
							} else if (zu[5] == 1) {
								if (zu[6] == 0 || zu[6] == 1)
									this.whiteScore[i][j] += 54 + k * 2;
								else
									this.whiteScore[i][j] += 9 + k * 2;
							}
						}
					}
				} else {
					this.whiteScore[i][j] += 9 + k * 2;
				}
			} else if (bai == 3) {
				int m = this.whiteScore[i][j];

				if (true) {
					int v = 0;
					if (zu[0] == 2 || zu[0] == -1)
						v++;
					if (zu[6] == 2 || zu[6] == -1)
						v++;
					if (v == 2) {
						this.whiteScore[i][j] += 40 + k * 3;
					} else if (v == 1) {
						if (zu[0] == 2 || zu[0] == -1) {
							if (zu[1] == 0) {
								this.whiteWin = true;
								this.whiteScore[i][j] += 1000 + k * 3;
							} else {
								this.whiteScore[i][j] += 40 + k * 3;
							}
						} else if (zu[6] == 2 || zu[6] == -1) {
							if (zu[5] == 0) {
								this.whiteWin = true;
								this.whiteScore[i][j] += 1000 + k * 3;
							} else {
								this.whiteScore[i][j] += 40 + k * 3;
							}
						}
					} else if (v == 0) {
						if (zu[1] == 0 || zu[5] == 0) {
							this.whiteWin = true;
							this.whiteScore[i][j] += 1000 + k * 3;
						} else {
							this.whiteScore[i][j] += 63 + k * 3;
						}
					}

				}
				if (this.levelState == 3)
					;
				else {
					this.whiteScore[i][j] = m;
					this.whiteScore[i][j] += 27 + k * 3;
				}
			} else if (bai == 4) {
				this.whiteWin4 = true;
				this.whiteScore[i][j] += 10000 + k * 4;
			}

			else if (bai == 5) {
				qb.gameOver = true;
				qb.str = "五连珠,白胜~!";
				//main.loss.setEnabled(false);
			}

		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -