public Node GetMoves(CellValues [,] BoardCell) { Node node = new Node(); Random rand = new Random(); int count = rand.Next(4); // Lấy 1 số từ 0 -> 4 EBoard.ResetBoard(); GetGenResult(BoardCell); // Tìm nước đi if (CanWin) { node = PlayerMoves[1]; } else { EBoard.ResetBoard(); EvalueGomokuBoard(CellValues.Machine, BoardCell); node = EBoard.GetMaxNode(); if (!CanLose) for (int i = 0; i < count; i++) { EBoard.Board[node.Row, node.Column] = 0; node = EBoard.GetMaxNode(); } } Console.WriteLine("New pos: " + node.Row + " " + node.Column); return node; }
// Lấy thằng Node có giá trị lượng giá cao nhất public Node GetMaxNode() { int Max = Board[1, 1]; Node node = new Node(); for (int i = 1; i <= MAX_SQUARE; i++) for (int j = 1; j <= MAX_SQUARE; j++) if (Board[i, j] > Max) { node.Row = i; node.Column = j; Max = Board[i, j]; } return node; }
// Tìm đường đi public void GetGenResult(CellValues [,] BoardCell) { CanWin = false; CanLose = false; PlayerMoves = new Node[MaxDepth + 1]; for (int i = 0; i <= MaxDepth; i++) PlayerMoves[i] = new Node(); for (int i = 0; i < MaxStep; i++) PCMoves[i] = new Node(); Depth = 0; GenerateMoves(BoardCell); }
// Đệ quy sinh nước đi public void GenerateMoves(CellValues[,] BoardCell) { if (Depth >= MaxDepth) return; Depth++; CanWin = false; Node PCNode = new Node(); // Duong di quan ta. Node CompetitorNode = new Node(); // Duong di doi thu. int count = 0; // Tạo bảng lượng giá cho Machine EvalueGomokuBoard(CellValues.Machine, BoardCell); #region Lấy tất cả các bước đi tốt nhất vào danh sách các bước đi for (int i = 1; i <= MaxStep; i++) { PCNode = EBoard.GetMaxNode(); PCMoves[i] = PCNode; EBoard.Board[PCNode.Row, PCNode.Column] = 0; } #endregion #region Lấy các bước đi tốt nhất để thử count = 0; while (count < MaxStep) { count++; PCNode = PCMoves[count]; PlayerMoves.SetValue(PCNode, Depth); BoardCell[PCNode.Row, PCNode.Column] = CellValues.Machine; // Tìm nước đi tốt nhất EBoard.ResetBoard(); EvalueGomokuBoard(CellValues.Player1, BoardCell); for (int i = 1; i <= MaxStep; i++) { CompetitorNode = EBoard.GetMaxNode(); CompetitorMoves[i] = CompetitorNode; EBoard.Board[CompetitorNode.Row, CompetitorNode.Column] = 0; } for (int i = 1; i <= MaxStep; i++) { CompetitorNode = CompetitorMoves[i]; BoardCell[CompetitorNode.Row, CompetitorNode.Column] = CellValues.Player1; // Đi thử if ((CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 0, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 0, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, -1, 1, BoardCell)) && CurrentPlayer == CellValues.Machine) // Có thể thắng CanWin = true; if ((CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 0, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 0, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, -1, 1, BoardCell)) && CurrentPlayer == CellValues.Player1) // Có thể thua CanLose = true; // Loại bỏ nước đi thử if (CanLose || CanWin) { BoardCell[CompetitorNode.Row, CompetitorNode.Column] = CellValues.None; BoardCell[PCNode.Row, PCNode.Column] = CellValues.None; if (CanWin) CanLose = false; return; } else GenerateMoves(BoardCell); BoardCell[CompetitorNode.Row, CompetitorNode.Column] = CellValues.None; } BoardCell[PCNode.Row, PCNode.Column] = CellValues.None; } #endregion }
private void bw_DoWork(object sender, DoWorkEventArgs e) { Node node = new Node(); node = AMB.GetMoves(BoardCell); e.Result = node; }