//Download source code tai Sharecode.vn public Node GetMaxNode() { int r, c, MaxValue = 0; Node n = new Node(); for (r = 1; r <= Height; r++) for (c = 1; c <= Width; c++) if (Board[r, c] > MaxValue) { n.Row = r; n.Column = c; MaxValue = Board[r, c]; } return n; }
// ************** HANDLE EVENT ************************** public void HandleMouseDown(MouseEventArgs e) { Random rand = new Random(); int count = rand.Next(4); int r = (e.Y / 20) + OffsetH; int c = (e.X / 20) + OffsetW; Node node = new Node(); if (Board[r, c] == Player.None) { // nguoi choi di co. if (CurrPlayer == Player.Human && End == Player.None) { Parent.SendMessage(0); Board[r, c] = CurrPlayer; CurrPlayer = Player.Machine; End = CheckEnd(r, c); if (End != Player.None) { if (End == Player.Human) Parent.SendMessage(1); if (End == Player.Machine) Parent.SendMessage(2); } } //Download source code tai Sharecode.vn // May di co. if (CurrPlayer == Player.Machine && End == Player.None) { // Tim nuoc di chien thang. EBoard.ResetBoard(); GetGenResult(); if (Win) // Tim thay. { node = WinMoves[1]; } else { EBoard.ResetBoard(); EvalueGomokuBoard(Player.Machine); node = EBoard.GetMaxNode(); if (!Lose) for (int i = 0; i < count; i++) { EBoard.Board[node.Row, node.Column] = 0; node = EBoard.GetMaxNode(); } } // May di quan. r = node.Row; c = node.Column; Board[r, c] = CurrPlayer; CurrPlayer = Player.Human; // Kiem tra tran dau ket thuc chua ? End = CheckEnd(r, c); if (End != Player.None) { if (End == Player.Human) Parent.SendMessage(1); if (End == Player.Machine) Parent.SendMessage(2); } } } DrawBoard(); }
//Download source code tai Sharecode.vn // Goi Generator - Tim duong di cho may. public void GetGenResult() { Win = Lose = false; // Xoa mang duong di. WinMoves = new Node[MaxDepth + 1]; for (int i = 0; i <= MaxDepth; i++) WinMoves[i] = new Node(); // Xoa stack. for (int i = 0; i < MaxBreadth; i++) MyMoves[i] = new Node(); Depth = 0; GenerateMoves(); if (Win && !Lose) Parent.SendMessage(3); }
// Ham de quy - Sinh nuoc di cho may. public void GenerateMoves() { if (Depth >= MaxDepth) return; Depth++; bool lose = false; Win = false; Node MyNode = new Node(); // Duong di quan ta. Node HisNode = new Node(); // Duong di doi thu. int count = 0; //Download source code tai Sharecode.vn // Luong gia cho ma tran. EvalueGomokuBoard(Player.Machine); // Lay MaxBreadth nuoc di tot nhat. for (int i = 1; i <= MaxBreadth; i++) { MyNode = EBoard.GetMaxNode(); MyMoves[i] = MyNode; EBoard.Board[MyNode.Row, MyNode.Column] = 0; } // Lay nuoc di ra khoi danh sach - Danh thu nuoc di. count = 0; while (count < MaxBreadth) { count++; MyNode = MyMoves[count]; WinMoves.SetValue(MyNode, Depth); Board[MyNode.Row, MyNode.Column] = Player.Machine; // Tim cac nuoc di toi uu cua doi thu. EBoard.ResetBoard(); EvalueGomokuBoard(Player.Human); for (int i = 1; i <= MaxBreadth; i++) { HisNode = EBoard.GetMaxNode(); HisMoves[i] = HisNode; EBoard.Board[HisNode.Row, HisNode.Column] = 0; } for (int i = 1; i <= MaxBreadth; i++) { HisNode = HisMoves[i]; Board[HisNode.Row, HisNode.Column] = Player.Human; // Kiem tra ket qua nuoc di. if (CheckEnd(MyNode.Row, MyNode.Column) == Player.Machine) Win = true; if (CheckEnd(HisNode.Row, HisNode.Column) == Player.Human) lose = true; if (lose) { // Loai nuoc di thu. Lose = true; Board[HisNode.Row, HisNode.Column] = Player.None; Board[MyNode.Row, MyNode.Column] = Player.None; return; } if (Win) { // Loai nuoc di thu. Board[HisNode.Row, HisNode.Column] = Player.None; Board[MyNode.Row, MyNode.Column] = Player.None; return; } else GenerateMoves(); // tim tiep. // Loai nuoc di thu. Board[HisNode.Row, HisNode.Column] = Player.None; } Board[MyNode.Row, MyNode.Column] = Player.None; } }