Example #1
0
        public Node GetMaxNode()
        {
            int r, c, MaxValue = 0;
            Node n = new Node();

            for (r = 1; r <= Size; r++)
                for (c = 1; c <= Size; c++)
                    if (Board[r, c] > MaxValue)
                    {
                        n.Row = r; n.Column = c;
                        MaxValue = Board[r, c];
                    }

            return n;
        }
Example #2
0
        //Hàm lấy vị trí có giá trị cao nhất trên bàn cờ
        public Node GetMaxNode()
        {
            Node n = new Node();
            int maxValue = GiaTri[0, 0];
            Node[] arrMaxNodes = new Node[289];
            for (int i = 0; i < 289; i++)
            {
                arrMaxNodes[i] = new Node();
            }
            int count = 0;
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (GiaTri[i, j] > maxValue)
                    {
                        n.Row = i;
                        n.Column = j;
                        maxValue = GiaTri[i, j];
                    }
                }
            }

            //Với mục đích không lặp lại bước đi giống như lần trước
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (GiaTri[i, j] == maxValue)
                    {
                        n.Row = i;
                        n.Column = j;
                        arrMaxNodes[count] = n;
                        count++;
                    }
                }
            }
            //Đường đi sẽ là ngẫu nhiên

            Random r = new Random();
            int soNgauNhien = r.Next(count);
            return arrMaxNodes[soNgauNhien];
        }
Example #3
0
 private void Board_ComAutoPlay(Node node)
 {
     if(Type == PlayingType.ComOnline && !Board.isEndGame)
     {
         socket.Emit("MyStepIs", JObject.FromObject(new { row = node.Row - 1, col = node.Column - 1}));
     }
     else
     {
         CaroButton button = (CaroButton)this.Dispatcher.Invoke(new GetButtonDelegate(GetButton), node.Row, node.Column);
         if (button != null)
         {
             this.Dispatcher.Invoke(() =>
             {
                 Ellipse ell = new Ellipse();
                 ell.Width = ell.Height = 25;
                 RadialGradientBrush RadialBrush = new RadialGradientBrush();
                 RadialBrush.GradientOrigin = new Point(0.9, 0.9);
                 RadialBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
                 RadialBrush.GradientStops.Add(new GradientStop(Colors.White, 0.75));
                 ell.Stroke = Brushes.Black;
                 ell.Fill = RadialBrush;
                 button.Content = ell;
             });
         }
     }
     
 }
Example #4
0
        // Hàm lấy nước đi cho máy
        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();
        }
Example #5
0
        // 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;

            // Luong gia cho ma tran.
            EValueBoardViewModel(CellPlayer.Player2);

            // 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);
                BoardCells[MyNode.Row, MyNode.Column] = CellPlayer.Player2;

                // Tim cac nuoc di toi uu cua doi thu.
                EBoard.ResetBoard();
                EValueBoardViewModel(CellPlayer.Player1);

                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];
                    BoardCells[HisNode.Row, HisNode.Column] = CellPlayer.Player1;
                    // Kiem tra ket qua nuoc di.
                    if (TestWiner(MyNode.Row, MyNode.Column) == CellPlayer.Player2)
                        Win = true;
                    if (TestWiner(HisNode.Row, HisNode.Column) == CellPlayer.Player1)
                        lose = true;

                    if (lose)
                    {
                        // Loai nuoc di thu.
                        Lose = true;
                        BoardCells[HisNode.Row, HisNode.Column] = CellPlayer.None;
                        BoardCells[MyNode.Row, MyNode.Column] = CellPlayer.None;
                        return;
                    }

                    if (Win)
                    {
                        // Loai nuoc di thu.
                        BoardCells[HisNode.Row, HisNode.Column] = CellPlayer.None;
                        BoardCells[MyNode.Row, MyNode.Column] = CellPlayer.None;
                        return;
                    }
                    else GenerateMoves(); // tim tiep.
                    // Loai nuoc di thu.
                    BoardCells[HisNode.Row, HisNode.Column] = CellPlayer.None;
                }

                BoardCells[MyNode.Row, MyNode.Column] = CellPlayer.None;
            }

        }
Example #6
0
        // Định nghĩa hàm máy tự động chơi
        public void ComPlay()
        {
            EBoard.ResetBoard();
            // Gọi hàm phát sinh nước đi cho máy
            GetGenResult();
            Random rand = new Random();
            int count = rand.Next(4);
            Node node = new Node();
            if (Win) // Tim thay.
            {
                node = WinMoves[1];
            }
            else
            {
                EBoard.ResetBoard();
                // Lượng giá theo người chơi là máy.
                EValueBoardViewModel(CellPlayer.Player2);
                node = EBoard.GetMaxNode();
                if (!Lose)
                    for (int i = 0; i < count; i++)
                    {
                        EBoard.Board[node.Row, node.Column] = 0;
                        node = EBoard.GetMaxNode();
                    }
            }
            PlayAt(node.Row, node.Column);

            if (!isEndGame)
            {
                if (ComAutoPlay != null)
                    ComAutoPlay(node);  // Gọi thực hiện sự kiện ComAutoPlay
            }
            else
                ResetBoard();
            
        }
Example #7
0
 //Hàm lấy thông tin 5 ô Win hoặc Lose
 private void OnWinOrLose()
 {
     Node node = new Node();
     for (int i = 0; i < 5; i++)
     {
         node = OWin.GiaTri[i];
         HinhVuong hv = new HinhVuong();
         hv.Height = 40;
         hv.Width = 40;
         hv.Opacity = 100;
         hv.HorizontalAlignment = 0;
         hv.VerticalAlignment = 0;
         hv.Margin = new Thickness(node.Column * length - 2, node.Row * length - 3, 0, 0);
         grdBanCo.Children.Add(hv);
     }
 }
Example #8
0
        //Hàm đánh cờ
        public void grdBanCo_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            System.GC.Collect();//Thu gôm rác
            if (this.Option.WhoPlayWith == Player.Com)//Nếu chọn kiểu chơi đánh với máy
            {
                Point toado = e.GetPosition(grdBanCo); //Lấy tọa độ chuột
                //Xử lý tọa độ
                int cl = ((int)toado.X / length);
                int rw = ((int)toado.Y / length);

                Node node = new Node();
                if (board[rw, cl] == Player.None) //Nếu ô bấm chưa có cờ
                {
                    if (currPlayer == Player.Human && end == Player.None)//Nếu lượt đi là người và trận đấu chưa kết thúc
                    {
                        board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng
                        DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi
                        end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa
                        if (end == Player.Human)//Nếu người thắng cuộc là người
                        {
                            OnWin();//Khai báo sự kiện Win
                            OnWinOrLose();//Hiển thị 5 ô Win.
                            
                        }
                        else if (end == Player.None) //Nếu trận đấu chưa kết thúc
                        {
                            currPlayer = Player.Com;//Thiết lập lại lượt chơi
                            OnHumanDanhXong(); // Khai báo sự kiện người đánh xong
                        }
                    }
                    if (currPlayer == Player.Com && end == Player.None)//Nếu lượt đi là máy và trận đấu chưa kết thúc
                    {
                        //Tìm đường đi cho máy
                        eBoard.ResetBoard();
                        LuongGia(Player.Com);//Lượng giá bàn cờ cho máy
                        node = eBoard.GetMaxNode();//lưu vị trí máy sẽ đánh
                        int r, c;
                        r = node.Row; c = node.Column;
                        board[r, c] = currPlayer; //Lưu loại cờ vừa đánh vào mảng
                        DrawDataBoard(r, c, true, true); //Vẽ con cờ theo lượt chơi
                        end = CheckEnd(r, c);//Kiểm tra xem trận đấu kết thúc chưa

                        if (end == Player.Com)//Nếu máy thắng
                        {
                            OnLose();//Khai báo sư kiện Lose
                            OnWinOrLose();//Hiển thị 5 ô Lose.
                        }
                        else if (end == Player.None)
                        {
                            currPlayer = Player.Human;//Thiết lập lại lượt chơi
                            OnComDanhXong();// Khai báo sự kiện người đánh xong
                        }
                    }

                }
            }
            else if (this.Option.WhoPlayWith == Player.Human) //Nếu chọn kiểu chơi 2 người đánh với nhau
            {
                //Player.Com sẽ đóng vai trò người chơi thứ 2
                Point toado = e.GetPosition(grdBanCo);//Lấy thông tin tọa độ chuột
                //Xử lý tọa độ
                int cl = ((int)toado.X / length);
                int rw = ((int)toado.Y / length);
                if (board[rw, cl] == Player.None)//Nếu ô bấm chưa có cờ
                {
                    if (currPlayer == Player.Human && end == Player.None)//Nếu lượt đi là người và trận đấu chưa kết thúc
                    {
                        board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng
                        DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi
                        end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa
                        if (end == Player.Human)//Nếu người chơi 1 thắng
                        {
                            currPlayer = Player.Human; //Thiết lập lại lượt chơi
                            OnWin();//Khai báo sư kiện Win
                            OnWinOrLose();//Hiển thị 5 ô Win.
                        }
                        else
                        {
                            currPlayer = Player.Com;//Thiết lập lại lượt chơi
                            OnHumanDanhXong();// Khai báo sự kiện người chơi 1 đánh xong
                        }
                    }
                    else if (currPlayer == Player.Com && end == Player.None)
                    {
                        board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng
                        DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi
                        end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa
                        if (end == Player.Com)//Nếu người chơi 2 thắng
                        {
                            OnWin();//Khai báo sư kiện Win
                            OnWinOrLose();//Hiển thị 5 ô Win.
                           // MessageBox.Show("Player B chien thang");
                        }
                        else
                        {
                            currPlayer = Player.Human;//Thiết lập lại lượt chơi
                            OnComDanhXong();// Khai báo sự kiện người chơi 2 đánh xong
                        }
                    }
                }
            }
            else if (this.Option.WhoPlayWith == Player.Online) // chọn người chơi online
            {
                Point toado = e.GetPosition(grdBanCo); //Lấy tọa độ chuột
                //Xử lý tọa độ
                int cl = ((int)toado.X / length);
                int rw = ((int)toado.Y / length);
                Node node = new Node();
                if (board[rw, cl] == Player.None) //Nếu ô bấm chưa có cờ
                {
                    if (currPlayer == Player.Human && end == Player.None)// /Nếu lượt đi là mình và trận đấu chưa kết thúc
                    {
                        connect.guitoado(MainWindow.socket, rw, cl);
                        board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng
                      //  Thread.Sleep(5000);
                        DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi
                        end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa
                        if (end == Player.Human)//Nếu người thắng cuộc là mình
                        {
                            OnWin();//Khai báo sự kiện Win
                            OnWinOrLose();//Hiển thị 5 ô Win.
                        }
                        else if (end == Player.None) //Nếu trận đấu chưa kết thúc
                        {
                            currPlayer = Player.Online;//Thiết lập lại lượt chơi
                            OnHumanDanhXong(); // Khai báo sự kiện người đánh xong
                        }


                    }
                    else if (currPlayer == Player.Online && end == Player.None)//Nếu lượt đi là người chơi online và trận đấu chưa kết thúc
                    {
                        
                        board[rows, columns] = currPlayer;//Lưu loại cờ vừa đánh vào mảng
                        DrawDataBoard(rows, columns, true, true);//Vẽ con cờ theo lượt chơi

                        end = CheckEnd(rows, columns);//Kiểm tra xem trận đấu kết thúc chưa

                        if (end == Player.Online)//Nếu người chơi 2 thắng
                        {
                            OnWin();//Khai báo sư kiện Win
                            OnWinOrLose();//Hiển thị 5 ô Win.
                          
                        }
                        else
                        {
                            currPlayer = Player.Human;//Thiết lập lại lượt chơi
                            OnComDanhXong();// Khai báo sự kiện người chơi 2 đánh xong
                        }

                    }


                }
            }
        }