public static List <int[]> cacNuocXungQuanh(int x, int y, bool flag = false) { List <int[]> result = new List <int[]>(); foreach (int[] i in DuLieuPhanTich.tap) { int x_temp = x + i[0]; int y_temp = y + i[1]; if (!TinhToan.vuotKichThuocMang(x_temp, y_temp)) { result.Add(new int[] { x_temp, y_temp }); } } if (flag) { foreach (int[] i in DuLieuPhanTich.tap_1) { int x_temp = x + i[0]; int y_temp = y + i[1]; if (!TinhToan.vuotKichThuocMang(x_temp, y_temp)) { result.Add(new int[] { x_temp, y_temp }); } } } return(result); }
static void Main(string[] args) { char[] type = TinhToan.getType(@"data/Type.txt"); MiniMax m = new MiniMax(type[0], type[1]); char[,] board = TinhToan.readFile(@"data/board.txt"); int level = TinhToan.getLevel(@"data/level.txt"); m.setBoard(board); int[] move = m.findBestMove(level); Console.WriteLine(move[0].ToString() + "|" + move[1].ToString()); TinhToan.setBoard(move, board, type[0]); TinhToan.printBoard(board); TinhToan.writeNextMove(move, @"data/nextMove.txt"); }
public void setMove(int[] move, char type) { int x = move[0]; int y = move[1]; if (type == 'X') { if (this.game_board[y, x] == '#') { this.cacNuocDaDiX.Insert(0, move); this.game_board[y, x] = 'X'; } else if (type == 'O') { if (this.game_board[y, x] == '#') { this.cacNuocDadiO.Insert(0, move); this.game_board[y, x] = 'O'; } } } TinhToan.printBoard(this.game_board); }
public static List <int[]> tapCacNuocCoTheDi(char[,] board, bool xetWin = false) { List <int[]> result = new List <int[]>(); for (int x = 0; x < 15; x++) { for (int y = 0; y < 15; y++) { if (board[y, x] != '#') { List <int[]> temp = cacNuocXungQuanh(x, y, xetWin); foreach (int[] move in temp) { if (!TinhToan.chech_move_exist(move, result) && board[move[1], move[0]] == '#') { result.Add(move); } } } } } return(result); }
public static List <List <int[]> > tapCacNuoc(int x, int y) { List <List <int[]> > result = new List <List <int[]> >(); int count = 0; foreach (int[][] i in DuLieuPhanTich.cacNuocXungQuanhMotNuoc) { int x1_temp = i[0][0] + x; int y1_temp = i[0][1] + y; int x2_temp = i[5][0] + x; int y2_temp = i[5][1] + y; if (!TinhToan.vuotKichThuocMang(x1_temp, y1_temp) && !TinhToan.vuotKichThuocMang(x2_temp, y2_temp)) { result.Add(new List <int[]>()); foreach (int[] j in i) { result[count].Add(new int[] { j[0] + x, j[1] + y }); } count++; } } return(result); }
public int[] findBestMove(int d) { int nuocHayVL = 10; List <int[]> coTheDi = TimNuocTrongBanCo.tapCacNuocCoTheDi(game_board, false); List <int[]> coTheDi_2 = TimNuocTrongBanCo.tapCacNuocCoTheDi(game_board, true); int maxScore = int.MinValue; //int minScore = int.MaxValue; int[] bestMove = new int[] { }; int depht = d; List <int[]> tapNuocDuocChon = new List <int[]>(); bool flag = true; List <int[]> nuoc_hay_X = new List <int[]>(); List <int[]> nuoc_hay_O = new List <int[]>(); foreach (int[] move in coTheDi_2) { TinhToan.setBoard(move, game_board, 'X'); if (TinhDiemBanCo.chacChanWin('X', game_board)) { TinhToan.removeBoard(move, game_board); return(move); } TinhToan.removeBoard(move, game_board); } foreach (int[] move in coTheDi_2) { TinhToan.setBoard(move, game_board, 'O'); if (TinhDiemBanCo.chacChanWin('O', game_board)) { tapNuocDuocChon.Add(move); foreach (int[] move1 in coTheDi_2) { if (move1[0] != move[0] && move1[1] != move[1]) { TinhToan.setBoard(move1, game_board, 'X'); if (!TinhDiemBanCo.chacChanWin('O', game_board)) { tapNuocDuocChon.Add(move1); } TinhToan.removeBoard(move1, game_board); } } } TinhToan.removeBoard(move, game_board); } if (flag && tapNuocDuocChon.Count == 0) { foreach (int[] move in coTheDi_2) { int VKL_1 = TinhDiemBanCo.nuocDacBietNewVersion(move[0], move[1], 'X', game_board); if (VKL_1 < nuocHayVL && VKL_1 > 0) { nuoc_hay_X.Clear(); nuoc_hay_X.Add(move); nuocHayVL = VKL_1; } else if (VKL_1 == nuocHayVL) { nuoc_hay_X.Add(move); } } foreach (int[] move in coTheDi_2) { int VKL_2 = TinhDiemBanCo.nuocDacBietNewVersion(move[0], move[1], 'O', game_board); if (VKL_2 < nuocHayVL && VKL_2 > 0) { nuoc_hay_O.Clear(); nuoc_hay_O.Add(move); nuocHayVL = VKL_2; } } if (nuoc_hay_X.Count != 0 || nuoc_hay_O.Count != 0) { tapNuocDuocChon = TinhToan.cong2List(nuoc_hay_X, nuoc_hay_O); } } if (tapNuocDuocChon.Count == 1) { return(tapNuocDuocChon[0]); } if (tapNuocDuocChon.Count == 0) { tapNuocDuocChon = TinhDiemBanCo.chonNuocToiUu(coTheDi, computerType, game_board); } foreach (int[] move in tapNuocDuocChon) { TinhToan.setBoard(move, game_board, 'X'); int temp = Score(maxScore, int.MaxValue, depht, 'O', game_board); TinhToan.removeBoard(move, game_board); Console.WriteLine(move[0].ToString() + "," + move[1].ToString() + ":" + temp.ToString()); if (temp == DuLieuPhanTich.MAXSCORE) { return(move); } if (temp >= maxScore) { bestMove = move; maxScore = temp; } } return(bestMove); }
private int Score(int min, int max, int depht, char type, char[,] board) { int maxScore = max; int minScore = min; List <int[]> coTheDi = TimNuocTrongBanCo.tapCacNuocCoTheDi(board, true); if (type == 'X') { if (TinhDiemBanCo.gameWin('X', board)) { return(DuLieuPhanTich.MAXSCORE); } } else { if (TinhDiemBanCo.gameWin('O', board)) { return(DuLieuPhanTich.MINSCORE); } } if (depht == 0) { int[] x = TinhDiemBanCo.tongDoNguyHiem('X', board); int[] o = TinhDiemBanCo.tongDoNguyHiem('O', board); int score = TinhToan.diemCuaMaTran(x) - TinhToan.diemCuaMaTran(o); return(score); } List <int[]> tapNuocDuocChon = TinhDiemBanCo.chonNuocToiUu(coTheDi, type, board); foreach (int[] move in tapNuocDuocChon) { if (type == 'X') { TinhToan.setBoard(move, board, 'X'); int temp = Score(minScore, maxScore, depht - 1, 'O', board); TinhToan.removeBoard(move, board); if (temp >= maxScore) { return(DuLieuPhanTich.MAXSCORE); } else if (minScore < temp && temp < maxScore) { minScore = temp; } else if (minScore == maxScore) { return(minScore); } } else { TinhToan.setBoard(move, board, 'O'); int temp = Score(minScore, maxScore, depht - 1, 'X', board); TinhToan.removeBoard(move, board); if (temp <= minScore) { return(DuLieuPhanTich.MINSCORE); } else if (minScore < temp && temp < maxScore) { maxScore = temp; } else if (minScore == maxScore) { return(maxScore); } } } if (type == 'X') { return(minScore); } else { return(maxScore); } }