Java五子棋人机对战AI算法

自己写的人机对战,思路是给每个未下的棋子进行评分,活二、活三、冲四等分别代表不同分数,取最高分数进行落子

简单写了个前端页面,大家可以玩玩看

https://www.linzhehao.cn/gobang/single

下面是Java的核心代码及用法

一、示例用法

public class MyTest {
	public static void main(String[] args) {
		// 初始化服务
		ComputerService service = new ComputerService();
		
		// 设置玩家的数字(1或2),不设置的话默认是1
		// 如果设置成1,电脑就是2,如果设置成2,电脑就是1
		service.setPlayerType(1);
		
		// 构造一个15*15的二维数组棋盘
		Integer[][] checkBox = {
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
		
		// 让电脑下一颗棋子保存到r对象
		R r = service.getResult(checkBox);
		
		// 如果电脑获胜
		if("电脑获胜".equals(r.getState())) {
			// 输出电脑下的棋子
			System.out.println(r);
			System.out.println(r.getState());
		}
		// 如果玩家获胜
		else if ("玩家获胜".equals(r.getState())) {
			System.out.println(r.getState());
		} else {
			// 输出电脑下的棋子
			System.out.println(r);
		}
	}
}

二、机器人代码

import java.util.*;

public class ComputerService {
	// 临时储存分数
	private int count1;
	private int count2;
	private int count3;
	private int count4;
	private int count5;
	// 电脑五子
	private int computerFive = 1000;
	// 玩家五子
	private int playerFive = 980;
	// 电脑活四
	private int computerActiveFour = 900;
	// 电脑冲四
	private int computerChongFour = 800;
	// 玩家活四
	private int playerActiveFour = 600;
	// 玩家冲四
	private int playerChongFour = 450;
	// 电脑活三
	private int computerActiveThree = 350;
	// 玩家活三
	private int playerActiveThree = 300;
	// 电脑眠三
	private int computerSleepThree = 200;
	// 玩家眠三
	private int playerSleepThree = 150;
	// 玩家多个活二
	private int playerManyActiveTwo = 100;
	// 电脑活二
	private int computerActiveTwo = 50;
	// 周围有棋子
	private int hasAround = 10;

	private int[][] scores = new int[15][15]; // 分数
	public int x, y;                         // 当前的x和y值
	private Integer[][] checkBox;

	private RegularFactory factory = new RegularFactory();

	// 判断电脑是黑棋还是白棋
	private enum Type {
		black(1), white(2);
		private int num;
		Type(int num) {
			this.num = num;
		}
		public int getNum() {
			return num;
		}
	}

	// 人类和电脑的颜色
	private Type player = Type.black;
	private Type computer = Type.white;

	/**
	 * 设置玩家的类型
	 * @param type 1或2
	 */
	public void setPlayerType(int type) {
		player = type == 1 ? Type.black : Type.white;
		computer = type == 2 ? Type.black : Type.white;
	}

	/**
	 * 获取结果
	 * @param checkBox 棋盘数据
	 * @return 电脑下的坐标
	 */
	public R getResult(Integer[][] checkBox) {
		this.checkBox = checkBox;
		factory.setCheckBox(checkBox);
		long startTime = System.currentTimeMillis();

		// 如果玩家胜利
		R playerIsWin = playerIsWin();
		if(playerIsWin != null) {
			return playerIsWin;
		}

		// 如果电脑胜利
		R computerIsWin = computerIsWin();
		if(computerIsWin != null) {
			return computerIsWin;
		}

		// 获取分数
		int maxScore = -1; // 最大分数
		for(int i=0;i<15;i++) {
			for(int j=0;j<15;j++) {
				scores[i][j] = getScore(i, j);
				// 统计最大分数
				if(scores[i][j] > maxScore) {
					maxScore = scores[i][j];
				}
			}
		}

		// 统计所有最大分数的坐标
		List maxR = new ArrayList<>();
		for(int i=0;i<15;i++) {
			for(int j=0;j<15;j++) {
				if(scores[i][j] == maxScore) {
					maxR.add(new R(i, j));
				}
			}
		}

		// 输出每一步的棋盘信息和分数信息
		// output();

		// 随机返回一个R
		int random = new Random().nextInt(maxR.size());
		long stopTime = System.currentTimeMillis();
		System.out.println("计算完成,耗时" + (stopTime - startTime) + "毫秒");
		return maxR.get(random);
	}

	/**
	 * 打分
	 * @param x x轴
	 * @param y y轴
	 * @return 分数
	 */
	private int getScore(int x, int y) {
		this.x = x;
		this.y = y;
		// 如果已经下了棋子
		if(checkBox[x][y] != 0) {
			return -1;
		}

		// 电脑成为五子
		if(becomeFive(computer.getNum(), x, y, true)) {
			return computerFive;
		}

		// 玩家成为五子
		if(becomeFive(player.getNum(), x, y, true)) {
			return playerFive;
		}

		// 电脑活四
		count1 = becomeActiveFour(computer.getNum());
		if(count1 > 0) {
			// 电脑冲四
			count2 = becomeChongFour(computer.getNum());
			if(count2 > 0) {
				// 电脑活三
				count3 = becomeActiveThree(computer.getNum());
				if(count3 > 0) {
					// 电脑眠三
					count4 = becomeSleepThree(computer.getNum());
					if(count4 > 0) {
						// 电脑活二
						count5 = becomeActiveTwo(computer.getNum());
						if(count5 > 0) {
							return computerActiveFour + count1 + count2 + count3 + count4 + count5;
						}
						return computerActiveFour + count1 + count2 + count3 + count4;
					}
					// 电脑活二
					count4 = becomeActiveTwo(computer.getNum());
					if(count4 > 0) {
						return computerActiveFour + count1 + count2 + count3 + count4;
					}
					return computerActiveFour + count1 + count2 + count3;
				}
				// 电脑眠三
				count3 = becomeSleepThree(computer.getNum());
				if(count3 > 0) {
					// 电脑活二
					count5 = becomeActiveTwo(computer.getNum());
					if(count5 > 0) {
						return computerActiveFour + count1 + count2 + count3 + count4 + count5;
					}
					return computerActiveFour + count1 + count2 + count3;
				}
				// 电脑活二
				count3 = becomeActiveTwo(computer.getNum());
				if(count3 > 0) {
					return computerActiveFour + count1 + count2 + count3;
				}
				return computerActiveFour + count1 + count2;
			}
			// 电脑活三
			count2 = becomeActiveThree(computer.getNum());
			if(count2 > 0) {
				// 电脑眠三
				count3 = becomeSleepThree(computer.getNum());
				if(count3 > 0) {
					// 电脑活二
					count4 = becomeActiveTwo(computer.getNum());
					if(count4 > 0) {
						return computerActiveFour + count1 + count2 + count3 + count4;
					}
					return computerActiveFour + count1 + count2 + count3;
				}
				// 电脑活二
				count3 = becomeActiveTwo(computer.getNum());
				if(count3 > 0) {
					return computerActiveFour + count1 + count2 + count3;
				}
				return computerActiveFour + count1 + count2;
			}
			// 电脑眠三
			count2 = becomeSleepThree(computer.getNum());
			if(count2 > 0) {
				// 电脑活二
				count3 = becomeActiveTwo(computer.getNum());
				if(count3 > 0) {
					return computerActiveFour + count1 + count2 + count3;
				}
				return computerActiveFour + count1 + count2;
			}
			// 电脑活二
			count2 = becomeActiveTwo(computer.getNum());
			if(count2 > 0) {
				return computerActiveFour + count1 + count2;
			}
			return computerActiveFour + count1;
		}

		// 电脑冲四
		count1 = becomeChongFour(computer.getNum());
		if(count1 > 0) {
			// 电脑活三
			count2 = becomeActiveThree(computer.getNum());
			if(count2 > 0) {
				// 电脑眠三
				count3 = becomeSleepThree(computer.getNum());
				if(count3 > 0) {
					// 电脑活二
					count4 = becomeActiveTwo(computer.getNum());
					if(count4 > 0) {
						return computerChongFour + count1 + count2 + count3 + count4;
					}
					return computerChongFour + count1 + count2 + count3;
				}
				// 电脑活二
				count3 = becomeActiveTwo(computer.getNum());
				if(count3 > 0) {
					return computerChongFour + count1 + count2 + count3;
				}
				return computerChongFour + count1 + count2;
			}
			// 电脑眠三
			count2 = becomeSleepThree(computer.getNum());
			if(count2 > 0) {
				// 电脑活二
				count3 = becomeActiveTwo(computer.getNum());
				if(count3 > 0) {
					return computerChongFour + count1 + count2 + count3;
				}
				return computerChongFour + count1 + count2;
			}
			// 电脑活二
			count2 = becomeActiveTwo(computer.getNum());
			if(count2 > 0) {
				return computerChongFour + count1 + count2;
			}
			return computerChongFour + count1;
		}

		// 玩家活四
		count1 = becomeActiveFour(player.getNum());
		if(count1 > 0) {
			// 玩家冲四
			count2 = becomeChongFour(player.getNum());
			if(count2 > 0) {
				// 玩家活三
				count3 = becomeActiveThree(player.getNum());
				if(count3 > 0) {
					// 玩家眠三
					count4 = becomeSleepThree(player.getNum());
					if(count4 > 0) {
						// 玩家活二
						count5 = becomeActiveTwo(player.getNum());
						if(count5 > 0) {
							return playerActiveFour + count1 + count2 + count3 + count4 + count5;
						}
						return playerActiveFour + count1 + count2 + count3 + count4;
					}
					// 玩家活二
					count4 = becomeActiveTwo(player.getNum());
					if(count4 > 0) {
						return playerActiveFour + count1 + count2 + count3 + count4;
					}
					return playerActiveFour + count1 + count2 + count3;
				}
				// 玩家眠三
				count3 = becomeSleepThree(player.getNum());
				if(count3 > 0) {
					// 玩家活二
					count5 = becomeActiveTwo(player.getNum());
					if(count5 > 0) {
						return playerActiveFour + count1 + count2 + count3 + count4 + count5;
					}
					return playerActiveFour + count1 + count2 + count3;
				}
				// 玩家活二
				count3 = becomeActiveTwo(player.getNum());
				if(count3 > 0) {
					return playerActiveFour + count1 + count2 + count3;
				}
				return playerActiveFour + count1 + count2;
			}
			// 玩家活三
			count2 = becomeActiveThree(player.getNum());
			if(count2 > 0) {
				// 玩家眠三
				count3 = becomeSleepThree(player.getNum());
				if(count3 > 0) {
					// 玩家活二
					count4 = becomeActiveTwo(player.getNum());
					if(count4 > 0) {
						return playerActiveFour + count1 + count2 + count3 + count4;
					}
					return playerActiveFour + count1 + count2 + count3;
				}
				// 玩家活二
				count3 = becomeActiveTwo(player.getNum());
				if(count3 > 0) {
					return playerActiveFour + count1 + count2 + count3;
				}
				return playerActiveFour + count1 + count2;
			}
			// 玩家眠三
			count2 = becomeSleepThree(player.getNum());
			if(count2 > 0) {
				// 玩家活二
				count3 = becomeActiveTwo(player.getNum());
				if(count3 > 0) {
					return playerActiveFour + count1 + count2 + count3;
				}
				return playerActiveFour + count1 + count2;
			}
			// 玩家活二
			count2 = becomeActiveTwo(player.getNum());
			if(count2 > 0) {
				return playerActiveFour + count1 + count2;
			}
			return playerActiveFour + count1;
		}

		// 玩家冲四
		count1 = becomeChongFour(player.getNum());
		if(count1 > 0) {
			// 玩家活三
			count2 = becomeActiveThree(player.getNum());
			if(count2 > 0) {
				// 玩家眠三
				count3 = becomeSleepThree(player.getNum());
				if(count3 > 0) {
					// 玩家活二
					count4 = becomeActiveTwo(player.getNum());
					if(count4 > 0) {
						return playerChongFour + count1 + count2 + count3 + count4;
					}
					return playerChongFour + count1 + count2 + count3;
				}
				// 玩家活二
				count3 = becomeActiveTwo(player.getNum());
				if(count3 > 0) {
					return playerChongFour + count1 + count2 + count3;
				}
				return playerChongFour + count1 + count2;
			}
			// 玩家眠三
			count2 = becomeSleepThree(player.getNum());
			if(count2 > 0) {
				// 电脑活二
				count3 = becomeActiveTwo(player.getNum());
				if(count3 > 0) {
					return playerChongFour + count1 + count2 + count3;
				}
				return playerChongFour + count1 + count2;
			}
			// 玩家活二
			count2 = becomeActiveTwo(player.getNum());
			if(count2 > 0) {
				return playerChongFour + count1 + count2;
			}
			return playerChongFour + count1;
		}

		// 电脑活三
		count1 = becomeActiveThree(computer.getNum());
		if(count1 > 0) {
			// 电脑眠三
			count2 = becomeSleepThree(computer.getNum());
			if(count2 > 0) {
				// 电脑活二
				count3 = becomeActiveTwo(computer.getNum());
				if(count3 > 0) {
					return computerActiveThree + count1 + count2 + count3;
				}
				return computerActiveThree + count1 + count2;
			}
			// 电脑活二
			count2 = becomeActiveTwo(computer.getNum());
			if(count2 > 0) {
				return computerActiveThree + count1 + count2;
			}
			return computerActiveThree + count1;
		}

		// 玩家活三
		count1 = becomeActiveThree(player.getNum());
		if(count1 > 0) {
			// 玩家眠三
			count2 = becomeSleepThree(player.getNum());
			if(count2 > 0) {
				// 玩家活二
				count3 = becomeActiveTwo(player.getNum());
				if(count3 > 0) {
					return playerActiveThree + count1 + count2 + count3;
				}
				return playerActiveThree + count1 + count2;
			}
			// 玩家活二
			count2 = becomeActiveTwo(player.getNum());
			if(count2 > 0) {
				return playerActiveThree + count1 + count2;
			}
			return playerActiveThree + count1;
		}

		// 电脑眠三
		count1 = becomeSleepThree(computer.getNum());
		if(count1 > 0) {
			// 电脑活二
			count2 = becomeActiveTwo(computer.getNum());
			if(count2 > 0) {
				return computerSleepThree + count1 + count2;
			}
			return computerSleepThree + count1;
		}

		// 玩家眠三
		count1 = becomeSleepThree(player.getNum());
		if(count1 > 0) {
			// 电脑活二
			count2 = becomeActiveTwo(player.getNum());
			if(count2 > 0) {
				return playerSleepThree + count1 + count2;
			}
			return playerSleepThree + count1;
		}

		// 玩家多个活二
		count1 = becomeActiveTwo(player.getNum());
		if(count1 > 1) {
			return playerManyActiveTwo + count1;
		}

		// 电脑活二
		count1 = becomeActiveTwo(computer.getNum());
		if(count1 > 0) {
			return computerActiveTwo + count1;
		}

		// 如果玩家有棋子
		if(hasAround(player.getNum())) {
			return hasAround;
		}
		return 0;
	}

	// 判断周围是否有对方的棋子
	private boolean hasAround(int user) {
		// 所有周围的坐标
		R[] surrounding = new R[] {
			new R(x - 1, y),
			new R(x + 1, y),
			new R(x, y - 1),
			new R(x, y + 1),
			new R(x - 1, y - 1),
			new R(x - 1, y + 1),
			new R(x + 1, y - 1),
			new R(x + 1, y + 1)
		};
		// 遍历这些坐标
		for(R surround: surrounding) {
			// 如果坐标存在
			if(piecesIsExists(user, surround)) {
				return true;
			}
		}
		return false;
	}

	// 成为活二
	private int becomeActiveTwo(int user) {
		factory.setCheckBox(checkBox);
		factory.set(user, x, y);
		factory.addRegular(new Integer[]{-2}, new Integer[]{-3, -1, +1});
		factory.addRegular(new Integer[]{-1}, new Integer[]{-3, -2, +1});
		factory.addRegular(new Integer[]{-1}, new Integer[]{-2, +1, +2});
		factory.addRegular(new Integer[]{+1}, new Integer[]{-2, -1, +2});
		factory.addRegular(new Integer[]{+1}, new Integer[]{-1, +2, +3});
		factory.addRegular(new Integer[]{+2}, new Integer[]{-1, +1, +3});
		return getCount();
	}

	// 成为眠三
	private int becomeSleepThree(int user) {
		factory.setCheckBox(checkBox);
		factory.set(user, x, y);
		factory.addRegular(new Integer[]{-1, -2}, new Integer[]{-3, -4}, +1);
		factory.addRegular(new Integer[]{+1, -1}, new Integer[]{-2, -3}, +2);
		factory.addRegular(new Integer[]{+1, +2}, new Integer[]{-1, -2}, +3);
		factory.addRegular(new Integer[]{-2, -3}, new Integer[]{-1, -4}, +1);
		factory.addRegular(new Integer[]{-1, -3}, new Integer[]{-2, -4}, +1);
		factory.addRegular(new Integer[]{-2, +1}, new Integer[]{-1, -3}, +2);
		factory.addRegular(new Integer[]{-1, +2}, new Integer[]{-2, +1}, +3);
		factory.addRegular(new Integer[]{-2, -3}, new Integer[]{-1, -4}, -4);
		factory.addRegular(new Integer[]{-1, -3}, new Integer[]{-2, -4}, -4);
		factory.addRegular(new Integer[]{-2, +1}, new Integer[]{-1, -3}, -3);
		factory.addRegular(new Integer[]{-1, +2}, new Integer[]{-2, +1}, -2);
		return getCount();
	}

	// 成为活三
	private int becomeActiveThree(int user) {
		factory.set(user, x, y);
		factory.addRegular(new Integer[]{-2, -1}, new Integer[]{-3, +1});
		factory.addRegular(new Integer[]{-1, +1}, new Integer[]{-2, +2});
		factory.addRegular(new Integer[]{+1, +2}, new Integer[]{-1, +3});
		factory.addRegular(new Integer[]{-3, -2}, new Integer[]{-4, -1, +1});
		factory.addRegular(new Integer[]{-3, -1}, new Integer[]{-4, -2, +1});
		factory.addRegular(new Integer[]{+3, +2}, new Integer[]{+4, +1, -1});
		factory.addRegular(new Integer[]{+3, +1}, new Integer[]{+4, +2, -1});
		factory.addRegular(new Integer[]{-2, +1}, new Integer[]{-3, +2, -1});
		factory.addRegular(new Integer[]{-1, +2}, new Integer[]{-2, +1, +3});

		List rowResult = factory.getRowResult();
		List columnResult = factory.getColumnResult();
		List leftResult = factory.getLeftResult();
		List rightResult = factory.getRightResult();
		// 统计数量
		int count = 0;
//		count += hasTrue(rowResult);
//		count += hasTrue(columnResult);
//		count += hasTrue(leftResult);
//		count += hasTrue(rightResult);
		count += rowResult.get(0) || rowResult.get(1) || rowResult.get(2) ? 3 : (rowResult.get(3) || rowResult.get(4) || rowResult.get(5) || rowResult.get(6) || rowResult.get(7) || rowResult.get(8) ? 2 : 0);
		count += columnResult.get(0) || columnResult.get(1) || columnResult.get(2) ? 3 : (columnResult.get(3) || columnResult.get(4) || columnResult.get(5) || columnResult.get(6) || columnResult.get(7) || columnResult.get(8) ? 2 : 0);
		count += leftResult.get(0) || leftResult.get(1) || leftResult.get(2) ? 3 : (leftResult.get(3) || leftResult.get(4) || leftResult.get(5) || leftResult.get(6) || leftResult.get(7) || leftResult.get(8) ? 2 : 0);
		count += rightResult.get(0) || rightResult.get(1) || rightResult.get(2) ? 3 : (rightResult.get(3) || rightResult.get(4) || rightResult.get(5) || rightResult.get(6) || rightResult.get(7) || rightResult.get(8) ? 2 : 0);
		return count;
	}

	// 判断电脑是否胜利
	private R computerIsWin() {
		for(int i=0;i<15;i++) {
			for(int j=0;j<15;j++) {
				if(checkBox[i][j] == 0 && becomeFive(computer.getNum(), i, j, true)) {
					return new R("电脑获胜", i, j);
				}
			}
		}
		return null;
	}

	// 判断玩家是否胜利
	private R playerIsWin() {
		for(int i=0;i<15;i++) {
			for(int j=0;j<15;j++) {
				if(checkBox[i][j] == player.getNum() && becomeFive(player.getNum(), i, j, false)) {
					return new R("玩家获胜", i, j);
				}
			}
		}
		return null;
	}

	// 成为冲四
	private int becomeChongFour(int user) {
		factory.set(user, x, y);
		factory.addRegular(new Integer[]{-3, -2, -1}, new Integer[]{-4}, +1);
		factory.addRegular(new Integer[]{-3, -2, -1}, new Integer[]{+1}, -4);
		factory.addRegular(new Integer[]{-2, -1, +1}, new Integer[]{-3}, +2);
		factory.addRegular(new Integer[]{-2, -1, +1}, new Integer[]{+2}, -3);
		factory.addRegular(new Integer[]{-1, +1, +2}, new Integer[]{-2}, +3);
		factory.addRegular(new Integer[]{-1, +1, +2}, new Integer[]{+3}, -2);
		factory.addRegular(new Integer[]{+1, +2, +3}, new Integer[]{-1}, +4);
		factory.addRegular(new Integer[]{+1, +2, +3}, new Integer[]{+4}, -1);
		return getCount();
	}

	// 成为活四的数量
	private int becomeActiveFour(int user) {
		factory.setCheckBox(checkBox);
		factory.set(user, x, y);
		factory.addRegular(new Integer[]{-3, -2, -1}, new Integer[]{-4, +1});
		factory.addRegular(new Integer[]{-2, -1, +1}, new Integer[]{-3, +2});
		factory.addRegular(new Integer[]{-1, +1, +2}, new Integer[]{-2, +3});
		factory.addRegular(new Integer[]{+1, +2, +3}, new Integer[]{-1, +4});

		int count = 0;
		count += hasTrue(factory.getRowResult());
		count += hasTrue(factory.getColumnResult());
		count += hasTrue(factory.getLeftResult());
		count += hasTrue(factory.getRightResult());
		return count;
	}

	/**
	 * 成为五子或更多
	 * @param scoring true:用来打分,false:用来判断输赢
	 */
	private boolean becomeFive(int user, int x, int y, boolean scoring) {
		// 假设棋子已下
		if(scoring) {
			checkBox[x][y] = user;
		}

		// 横向判断
		for(int i=0;i<=y;i++) {
			for(int j=14;j>=y;j--) {
				List rList = new ArrayList<>();
				for(int k=i;k<=j;k++) {
					rList.add(new R(x, k));
				}
				boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
				if(has && rList.size() >= 5) {
					if(scoring) {
						checkBox[x][y] = 0;
					}
					return true;
				}
			}
		}

		// 竖向判断
		for(int i=0;i<=x;i++) {
			for(int j=14;j>=x;j--) {
				List rList = new ArrayList<>();
				for(int k=i;k<=j;k++) {
					rList.add(new R(k, y));
				}
				boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
				if(has && rList.size() >= 5) {
					if(scoring) {
						checkBox[x][y] = 0;
					}
					return true;
				}
			}
		}

		// 右斜判断
		{
			// 取出起始点的坐标
			R start = new R(x, y);
			while(start.getX() != 0 && start.getY() != 0) {
				start.increase(-1, -1);
			}
			// 取出终点坐标
			R stop = new R(x, y);
			while(stop.getX() != 14 && stop.getY() != 14) {
				stop.increase(1, 1);
			}
			// 开始判断
			for(int i1=start.getX(),i2=start.getY();i1<=x;i1++,i2++) {
				for(int j1=stop.getX(),j2=stop.getY();j1>=x;j1--,j2--) {
					List rList = new ArrayList<>();
					for(int k1=i1,k2=i2;k1<=j1;k1++,k2++) {
						rList.add(new R(k1, k2));
					}
					boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
					//System.out.println(new R(i1, i2) + "-" + new R(j1, j2) + ": " + has);
					if(has && rList.size() >= 5) {
						if(scoring) {
							checkBox[x][y] = 0;
						}
						return true;
					}
				}
			}
		}

		// 左斜判断
		{
			// 取出起点的坐标
			R start = new R(x, y);
			while(start.getX() != 0 && start.getY() != 14) {
				start.increase(-1, 1);
			}
			// 取出终点坐标
			R stop = new R(x, y);
			while(stop.getX() != 14 && stop.getY() != 0) {
				stop.increase(1, -1);
			}
			for(int i1=start.getX(),i2=start.getY();i1<=x;i1++,i2--) {
				for(int j1=stop.getX(),j2=stop.getY();j1>=x;j1--,j2++) {
					List rList = new ArrayList<>();
					for(int k1=i1,k2=i2;k1<=j1;k1++,k2--) {
						rList.add(new R(k1, k2));
					}
					boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
					//System.out.println(new R(i1, i2) + "-" + new R(j1, j2) + ": " + has);
					if(has && rList.size() >= 5) {
						if(scoring) {
							checkBox[x][y] = 0;
						}
						return true;
					}
				}
			}
		}

		if(scoring) {
			checkBox[x][y] = 0;
		}
		return false;
	}

	// 输出
	private void output() {
		// 输出棋盘
		for(int i=0;i<15;i++) {
			System.out.print("{");
			for(int j=0;j<15;j++) {
				System.out.print(checkBox[j][i]);
				if(j != 14) {
					System.out.print(", ");
				}
			}
			System.out.println("},");
		}
		System.out.println();

		// 输出坐标
		for(int i=0;i<15;i++) {
			for (int j = 0; j < 15; j++) {
				System.out.print(scores[j][i] + "\t");
			}
			System.out.println("\n");
		}
	}

	/**
	 * 是否有true
	 * @param resultList 要统计的集合
	 * @return 如果有true返回1,如果没有则返回0
	 */
	private int hasTrue(List resultList) {
		for(Boolean b: resultList) {
			if(b) {
				return 1;
			}
		}
		return 0;
	}

	/**
	 * 统计数量
	 * @return
	 */
	private int getCount() {
		int count = 0;
		count += hasTrue(factory.getRowResult());
		count += hasTrue(factory.getColumnResult());
		count += hasTrue(factory.getLeftResult());
		count += hasTrue(factory.getRightResult());
		return count;
	}

	/**
	 * 判断这些棋子是否都存在
	 * @param user 用户,用来比较
	 * @param pieces 棋子的坐标
	 * @return 是否都存在
	 */
	private boolean piecesIsExists(int user, R... pieces) {
		for(R piece: pieces) {
			// 如果是边缘或者越界
			if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
				return false;
			}
			// 如果和用户的棋子不一致
			if(!checkBox[piece.getX()][piece.getY()].equals(user)) {
				return false;
			}
		}
		return true;
	}
}

class RegularFactory {
	private Integer[][] checkBox;
	private int user, x, y;

	// 横向判断
	private List rowsIsExists = new ArrayList<>();
	private List rowsIsEmpty = new ArrayList<>();
	private List rowsIsPlayerOrBounds = new ArrayList<>();

	// 竖向判断
	private List columnsIsExists = new ArrayList<>();
	private List columnsIsEmpty = new ArrayList<>();
	private List columnsIsPlayerOrBounds = new ArrayList<>();

	// 左斜判断
	private List leftIsExists = new ArrayList<>();
	private List leftIsEmpty = new ArrayList<>();
	private List leftIsPlayerOrBounds = new ArrayList<>();

	// 右斜判断
	private List rightIsExists = new ArrayList<>();
	private List rightIsEmpty = new ArrayList<>();
	private List rightIsPlayOrBounds = new ArrayList<>();

	// 初始化工厂
	public RegularFactory() {
	}
	public RegularFactory(Integer[][] checkBox, int user, int x, int y) {
		this.checkBox = checkBox;
		this.user = user;
		this.x = x;
		this.y = y;
	}
	// 设置棋盘
	public void setCheckBox(Integer[][] checkBox) {
		this.checkBox = checkBox;
	}
	// 设置坐标
	public void set(int user, int x, int y) {
		this.user = user;
		this.x = x;
		this.y = y;
		clearList();
	}

	// 清除所有集合
	public void clearList() {
		rowsIsExists.clear();
		columnsIsExists.clear();
		leftIsExists.clear();
		rightIsExists.clear();

		rowsIsEmpty.clear();
		columnsIsEmpty.clear();
		leftIsEmpty.clear();
		rightIsEmpty.clear();

		rowsIsPlayerOrBounds.clear();
		columnsIsPlayerOrBounds.clear();
		leftIsPlayerOrBounds.clear();
		rightIsPlayOrBounds.clear();
	}

	// 添加规则
	public void addRegular(Integer[] yExists, Integer[] yEmpty) {
		R[] rowsExists = new R[yExists.length];
		R[] rowsEmpty = new R[yEmpty.length];
		R[] columnsExists = new R[yExists.length];
		R[] columnsEmpty = new R[yEmpty.length];
		R[] leftExists = new R[yExists.length];
		R[] leftEmpty = new R[yEmpty.length];
		R[] rightExists = new R[yExists.length];
		R[] rightEmpty = new R[yEmpty.length];

		for(int i=0;i getRowResult() {
		List rowResult = new ArrayList<>();
		for(int i=0;i getColumnResult() {
		List columnResult = new ArrayList<>();
		for(int i=0;i getLeftResult() {
		List leftResult = new ArrayList<>();
		for(int i=0;i getRightResult() {
		List rightResult = new ArrayList<>();
		for(int i=0;i 14 || piece.getY() < 0 || piece.getY() > 14) {
				return false;
			}
			// 如果和用户的棋子不一致
			if(!checkBox[piece.getX()][piece.getY()].equals(user)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断这些棋子是否都为空
	 * @param pieces 棋子的坐标
	 * @return 是否都为空
	 */
	private boolean piecesIsEmpty(R... pieces) {
		for(R piece: pieces) {
			// 如果是边缘或者越界
			if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
				return false;
			}
			// 如果不为空
			if(!checkBox[piece.getX()][piece.getY()].equals(0)) {
				return false;
			}
		}
		return true;
	}


	/**
	 * 判断棋子是否是对方或者边界
	 * @param piece 棋子的坐标
	 * @retur 结果
	 */
	private boolean pieceIsPlayerOrBounds(int user, R piece) {
		int contraryUser = user == 1 ? 2 : 1; // 对手的颜色
		// 如果越界
		if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
			return true;
		}
		// 如果是玩家
		return checkBox[piece.getX()][piece.getY()].equals(contraryUser);
	}
}

//机器人返回的数据
class R {
	private String state = "返回成功";
	private Integer x;
	private Integer y;

	public R() {
	}

	public R(String state, Integer x, Integer y) {
		this.state = state;
		this.x = x;
		this.y = y;
	}

	public R(Integer x, Integer y) {
		this.x = x;
		this.y = y;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Integer getX() {
		return x;
	}

	public void setX(Integer x) {
		this.x = x;
	}

	public Integer getY() {
		return y;
	}

	public void setY(Integer y) {
		this.y = y;
	}

	// 增加
	public void increase(int x, int y) {
		if(this.x != null) {
			this.x += x;
		}
		if(this.y != null) {
			this.y += y;
		}
	}

	@Override
	public String toString() {
		return "R{" +
			"x=" + x +
			", y=" + y +
			'}';
	}
}

你可能感兴趣的:(Java,java,人工智能,算法)