1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
| import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.ArrayList; import java.util.List;
public class CompeteAI { private static int ROW = 15; private static int COL = 15; static final int MAX_NODE = 2; static final int MIN_NODE = 1; private static int bestScore = 0; private static int bestX = 1; private static int bestY = 2;
public static int[] miniMaxSearch(int curDepth, int depth, int[][] board, int alpha, int beta, int role) {
int[] best = new int[3];
Board Board = new Board(board);
List<int[][]> childBoardList = new ArrayList<int[][]>(); List<int[]> dirList = new ArrayList<int[]>(); List<Integer> scoreList = new ArrayList<Integer>();
for (int[] step : Board.getValidMoves(Board.getBoard())) { Board childBoard = new Board(board); dirList.add(step); childBoard.putChess(step[0], step[1], role + 1); childBoardList.add(childBoard.getBoard()); }
if (curDepth == depth || Board.isWin((role ^ 1) + 1)) { best[0] = evaluate(board, role ^ 1); return best; } else {
if (curDepth % 2 == 0) { for (int[][] childBoard : childBoardList) { int[] res = miniMaxSearch(curDepth + 1, depth, childBoard, alpha, beta, role ^ 1); scoreList.add(res[bestScore]); alpha = Math.max(alpha, res[bestScore]); if (alpha >= beta) { break; } }
if (curDepth == 0) { int bestIndex = scoreList.indexOf(alpha); best[bestX] = dirList.get(bestIndex)[0]; best[bestY] = dirList.get(bestIndex)[1]; } best[bestScore] = alpha; return best;
} else { for (int[][] childBoard : childBoardList) { int[] res = miniMaxSearch(curDepth + 1, depth, childBoard, alpha, beta, role ^ 1); scoreList.add(res[bestScore]); beta = Math.min(beta, res[bestScore]); if (alpha >= beta) { break; } } best[bestScore] = beta; return best; } } }
public static int evaluate(int[][] board, int role) { int value = 0; for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (board[i][j] != 0) { value += evaluatePoint(board, i, j, role); } } } return value; }
private static final Pattern FIVE_BLACK = Pattern.compile("1{5}"); private static final Pattern FOUR_BLACK = Pattern.compile("011110"); private static final Pattern RUSH_FOUR_BLACK = Pattern.compile("1(101|011|110)1"); private static final Pattern DEAD_FOUR_BLACK = Pattern.compile("01111(2|3)|(2|3)11110"); private static final Pattern THREE_BLACK = Pattern.compile("0(1011|1101)0"); private static final Pattern RUSH_THREE_BLACK = Pattern.compile("1(100|010|001)1"); private static final Pattern DEAD_THREE_BLACK = Pattern.compile("0(1011|1101)(2|3)|(2|3)(1011|1101)0"); private static final Pattern TWO_BLACK = Pattern.compile("01010"); private static final Pattern DEAD_TWO_BLACK = Pattern.compile("0101(2|3)|(2|3)1010");
private static final Pattern[] mBlack = { FIVE_BLACK, FOUR_BLACK, RUSH_FOUR_BLACK, DEAD_FOUR_BLACK, THREE_BLACK, RUSH_THREE_BLACK, DEAD_THREE_BLACK, TWO_BLACK, DEAD_TWO_BLACK };
private static final Pattern FIVE_WHITE = Pattern.compile("2{5}"); private static final Pattern FOUR_WHITE = Pattern.compile("022220"); private static final Pattern RUSH_FOUR_WHITE = Pattern.compile("2(202|022|220)2"); private static final Pattern DEAD_FOUR_WHITE = Pattern.compile("02222(1|3)|(1|3)22220"); private static final Pattern THREE_WHITE = Pattern.compile("0(2022|2202)0"); private static final Pattern RUSH_THREE_WHITE = Pattern.compile("2(200|020|002)2"); private static final Pattern DEAD_THREE_WHITE = Pattern.compile("0(2022|2202)(1|3)|(1|3)(2022|2202)0"); private static final Pattern TWO_WHITE = Pattern.compile("02020"); private static final Pattern DEAD_TWO_WHITE = Pattern.compile("0202(1|3)|(1|3)2020");
private static final Pattern[] mWhite = { FIVE_WHITE, FOUR_WHITE, RUSH_FOUR_WHITE, DEAD_FOUR_WHITE, THREE_WHITE, RUSH_THREE_WHITE, DEAD_THREE_WHITE, TWO_WHITE, DEAD_TWO_WHITE };
public static int pattern2point(Pattern pattern) { switch (pattern.toString()) { case "1{5}": return 100000; case "011110": return 10000; case "1(101|011|110)1": return 1000; case "01111(2|3)|(2|3)11110": return 100; case "0(1011|1101)0": return 100; case "1(100|010|001)1": return 10; case "0(1011|1101)(2|3)|(2|3)(1011|1101)0": return 5; case "01010": return 5; case "0101(2|3)|(2|3)1010": return 1; case "2{5}": return - Integer.MAX_VALUE; case "022220": return -100000; case "2(202|022|220)2": return -1000; case "02222(1|3)|(1|3)22220": return -100; case "0(2022|2202)0": return -100; case "2(200|020|002)2": return -10; case "0(2022|2202)(1|3)|(1|3)(2022|2202)0": return -5; case "02020": return -5; case "0202(1|3)|(1|3)2020": return -1; default: return 0; } }
public static int evaluatePoint(int[][] board, int x, int y, int role) { int value = 0; Board Board = new Board(board);
Board.putChess(x, y, role + 1);
String dirROW = ""; String dirCOL = ""; String dirLeftDiagonal = ""; String dirRightDiagonal = "";
for (int i = x - 5; i < x + 5; i++) { if (i >= 0 && i < ROW) { dirROW += board[x][i]; } else{ dirROW += "3"; } }
for (int i = y - 5; i < y + 5; i++) { if (i >= 0 && i < COL) { dirCOL += board[i][y]; } else{ dirCOL += "3"; } }
for (int i = x - 5, j = y - 5; i < x + 5 && j < y + 5; i++, j++) { if (i >= 0 && i < ROW && j >= 0 && j < COL) { dirLeftDiagonal += board[i][j]; } else{ dirLeftDiagonal += "3"; } }
for (int i = x - 5, j = y + 5; i < x + 5 && j > y - 5; i++, j--) { if (i >= 0 && i < ROW && j >= 0 && j < COL) { dirRightDiagonal += board[i][j]; } else{ dirRightDiagonal += "3"; } } String[] dirs = { dirROW, dirCOL, dirLeftDiagonal, dirRightDiagonal };
for (String dir : dirs) { for (Pattern pattern : mBlack) { Matcher matcher = pattern.matcher(dir); int mul = (role == 0)? 1 : -1; while (matcher.find()) { value += mul * pattern2point(pattern); } } for (Pattern pattern : mWhite) { Matcher matcher = pattern.matcher(dir); int mul = (role == 0)? -1 : 1; while (matcher.find()) { value += mul * pattern2point(pattern); } } }
return value; }
public static void copyBoard(int[][] board, int[][] newBoard) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { newBoard[i][j] = board[i][j]; } } } }
|