Ejemplo n.º 1
0
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            var list = new List<Point>();

            //nước lên bên phải
            if (_point == new Point(3, 0) || _point == new Point(3, 7) || _point.X == 4)
            {
                if (board.CheckPoint(_point.X + 1, _point.Y + 1) * _id <= 0) list.Add(new Point(_point.X + 1, _point.Y + 1));
            }

            //nước lên bên trái
            if (_point == new Point(5, 0) || _point.X == 4 || _point == new Point(5, 7))
            {
                if (board.CheckPoint(_point.X - 1, _point.Y + 1) * _id <= 0) list.Add(new Point(_point.X - 1, _point.Y + 1));
            }

            //nước xuống bên phải
            if (_point.X == 4 || _point == new Point(3, 2) || _point == new Point(3, 9))
            {
                if (board.CheckPoint(_point.X + 1, _point.Y - 1) * _id <= 0) list.Add(new Point(_point.X + 1, _point.Y - 1));
            }

            //nước xuống bên trái
            if (_point.X == 4 || _point == new Point(5, 2) || _point == new Point(5, 9))
            {
                if (board.CheckPoint(_point.X - 1, _point.Y - 1) * _id <= 0) list.Add(new Point(_point.X - 1, _point.Y - 1));
            }

            return list;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Cắt tỉa Alpha - Beta
        /// </summary>
        /// <param name="board">thế cờ hiện tại</param>
        /// <param name="alpha">giá trị âm vô cùng</param>
        /// <param name="beta">giá trị dương vô cùng</param>
        /// <param name="depth">độ sâu tìm kiếm</param>
        /// <param name="player">người chơi 1 là Max, -1 là Min</param>
        /// <returns></returns>
        /// 
        public Computer AlphaBetaPruning(ChessBoard board, int alpha, int beta, int depth, int player)
        {
            Computer com = new Computer();

            if (depth == 0 || board.CheckWinNegativeTeam() || board.CheckWinPositiveTeam())
            {
                com.Value = board.Value;
                com.Board = board.Clone();
                return com;
            }

            if (player == 1)
            {
                foreach (var b in GenerateBoardPositiveTeam(board))
                {

                    int temp = AlphaBetaPruning(b, alpha, beta, depth - 1, -1).Value;
                    //Debug.WriteLine("temp1 = {0}, alpha = {1}, depth = {2}", temp, alpha, depth);
                    i++;
                    if (temp > alpha)
                    {
                        alpha = temp;
                        com.Value = alpha;
                        com.Board = b.Clone();
                    }

                    if (beta <= alpha) return com;
                }

                return com;
            }
            else
            {
                foreach (var b in GenerateBoardNegativeTeam(board))
                {

                    int temp = AlphaBetaPruning(b, alpha, beta, depth - 1, 1).Value;
                    i++;
                    //Debug.WriteLine("temp2 = {0}, beta = {1}, depth = {2}", temp, beta, depth);
                    if (temp < beta)
                    {
                        beta = temp;
                        com.Board = b.Clone();
                        com.Value = beta;
                    }

                    if (beta <= alpha) return com;
                }

                return com;
            }
        }
Ejemplo n.º 3
0
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            var list = new List<Point>();

            if (_point.Y == 2 || _point.Y == 7)
            {
                if (_point.X != 8)
                {
                    if (board.CheckPoint(_point.X + 1, _point.Y + 1) == 0 && (board.CheckPoint(_point.X + 2, _point.Y + 2) * _id) <= 0)
                        list.Add(new Point(_point.X + 2, _point.Y + 2));
                    if (board.MatrixPosition[_point.X + 1, _point.Y - 1] == 0 && (board.MatrixPosition[_point.X + 2, _point.Y - 2] * _id) <= 0)
                        list.Add(new Point(_point.X + 2, _point.Y - 2));
                }

                if (_point.X != 0)
                {
                    if (board.MatrixPosition[_point.X - 1, _point.Y + 1] == 0 && (board.MatrixPosition[_point.X - 2, _point.Y + 2]*_id) <= 0)
                        list.Add(new Point(_point.X - 2, _point.Y + 2));
                    if (board.MatrixPosition[_point.X - 1, _point.Y - 1] == 0 && (board.MatrixPosition[_point.X - 2, _point.Y - 2] * _id) <= 0)
                        list.Add(new Point(_point.X - 2, _point.Y - 2));
                }
            }
            else if (_point.Y == 0 || _point.Y == 5)
            {
                if (board.MatrixPosition[_point.X - 1, _point.Y + 1] == 0 && (board.MatrixPosition[_point.X - 2, _point.Y + 2] * _id) <= 0)
                    list.Add(new Point(_point.X - 2, _point.Y + 2));
                if (board.MatrixPosition[_point.X + 1, _point.Y + 1] == 0 && (board.MatrixPosition[_point.X + 2, _point.Y + 2] * _id) <= 0)
                    list.Add(new Point(_point.X + 2, _point.Y + 2));
            }
            else if (_point.Y == 4 || _point.Y == 9)
            {
                if (board.MatrixPosition[_point.X + 1, _point.Y - 1] == 0 && (board.MatrixPosition[_point.X + 2, _point.Y - 2] * _id) <= 0)
                    list.Add(new Point(_point.X + 2, _point.Y - 2));
                if (board.MatrixPosition[_point.X - 1, _point.Y - 1] == 0 && (board.MatrixPosition[_point.X - 2, _point.Y - 2] * _id) <= 0)
                    list.Add(new Point(_point.X - 2, _point.Y - 2));
            }
            return list;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Lấy nước đi kế tiếp
        /// </summary>
        /// <param name="board">bàn cờ</param>
        /// <returns>danh sách vị trí</returns>
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            List<Point> list = new List<Point>();

            //id dương âm xác định hướng tấn công, khởi tạo những quân id > 0 ở trên, < 0 ở dưới
            //xác định tốt không được đi lùi
            if (_id > 0)
            {
                //con tốt nào cũng có nước đi tiến 1 nước trừ _point.Y = 9
                if (_point.Y < 9)
                {
                    if (board.CheckPoint(_point.X, _point.Y + 1) <= 0)
                    {
                        list.Add(new Point(_point.X, _point.Y + 1));
                    }
                }

                //xác định nước đi ngang khi qua sông
                if (_point.Y >= 5)
                {
                    //vị trí trừ x = 8(sát mép phải) thì có thể đi ngang sang phải
                    if (_point.X < 8)
                    {
                        if (board.MatrixPosition[_point.X + 1, _point.Y] <= 0)
                        {
                            list.Add(new Point(_point.X + 1, _point.Y));
                        }
                    }

                    //các vị trí trừ sát mép trái đều có thể đi ngang sang trái
                    if (_point.X > 0)
                    {
                        if (board.MatrixPosition[_point.X - 1, _point.Y] <= 0)
                        {
                            list.Add(new Point(_point.X - 1, _point.Y));
                        }
                    }
                }
            }

            //tương tự với id < 0
            else
            {
                //nước đi tiến với quân có id < 0 là 1 nước giảm tọa độ Y đi 1
                if (_point.Y > 0)
                {

                    if (board.CheckPoint(_point.X, _point.Y - 1) >= 0)
                    {
                        list.Add(new Point(_point.X, _point.Y - 1));
                    }

                }

                //sang sông
                if (_point.Y < 5)
                {
                    //nước đi ngang bên phải
                    if (_point.X < 8)
                    {
                        if (board.MatrixPosition[_point.X + 1, _point.Y] >= 0)
                        {
                            list.Add(new Point(_point.X + 1, _point.Y));
                        }

                    }
                    //nước đi ngang bên trái
                    if (_point.X > 0)
                    {
                        if (board.MatrixPosition[_point.X - 1, _point.Y] >= 0)
                        {
                            list.Add(new Point(_point.X - 1, _point.Y));
                        }

                    }
                }
            }

            return list;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sao chép bàn cờ
        /// </summary>
        /// <param name="board">bàn cờ</param>
        public void Clone(ChessBoard board)
        {
            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 10; j++) MatrixPosition[i, j] = board.MatrixPosition[i, j];

            foreach (var chess in board._chessPieces)
            {
                foreach (var piece in this._chessPieces)
                {
                    if (piece.Id == chess.Id)
                    {
                        piece.Clone(chess);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sao chép 1 bàn cờ
        /// </summary>
        /// <returns>Trả về 1 bản copy mới</returns>
        public ChessBoard Clone()
        {
            var board = new ChessBoard();

            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 10; j++) board.MatrixPosition[i, j] = this.MatrixPosition[i, j];

            foreach (var chess in this._chessPieces)
            {
                foreach (var piece in board._chessPieces)
                {
                    if (piece.Id == chess.Id)
                    {
                        piece.Clone(chess);
                    }
                }
            }
            return board;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            board = new ChessBoard();
            btnUndo = new Entity(Content.Load<Texture2D>("undo"), new Vector2(0, 650));
            computer = new Computer();

            base.Initialize();
        }
Ejemplo n.º 8
0
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            var list = new List<Point>();

            int x = _point.X;
            int y = _point.Y;

            int i = x - 1;
            int j = y - 1;

            //tim ve phia duoi
            while (true)
            {
                if (j < 0 || j > 9) break;

                if (board.MatrixPosition[x, j] == 0)
                {
                    list.Add(new Point(x, j));
                }
                else if (board.GetPoint(x, j) * _id > 0) break;
                else
                {
                    list.Add(new Point(x, j));
                    break;
                }

                j--;
            }

            //tim phia tren
            j = y + 1;
            while (true)
            {
                if (j < 0 || j > 9) break;

                if (board.MatrixPosition[x, j] == 0)
                {
                    list.Add(new Point(x, j));
                }
                else if (board.GetPoint(x, j) * _id > 0) break;
                else
                {
                    list.Add(new Point(x, j));
                    break;
                }
                j++;
            }

            //tim cho trong ben trai quan xe
            while (true)
            {
                if (i < 0 || i > 8) break;

                if (board.MatrixPosition[i, y] == 0)
                {
                    list.Add(new Point(i, y));
                }
                else if (board.GetPoint(i, y) * _id > 0) break;
                else
                {
                    list.Add(new Point(i, y));
                    break;
                }

                i--;
            }

            //tim ve ben phai quan xe
            i = x + 1;
            while (true)
            {
                if (i < 0 || i > 8) break;

                if (board.MatrixPosition[i, y] == 0)
                {
                    list.Add(new Point(i, y));
                }
                else if (board.GetPoint(i, y) * _id > 0) break;
                else
                {
                    list.Add(new Point(i, y));
                    break;
                }

                i++;
            }

            return list;
        }
Ejemplo n.º 9
0
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            var list = new List<Point>();

            //quân có id dương
            if (_id > 0)
            {
                //nước tiến 2
                if (_point.Y <= 7)
                {
                    if (_point.X > 0)
                    {
                        if (board.MatrixPosition[_point.X - 1, _point.Y + 2] <= 0 && board.MatrixPosition[_point.X, _point.Y + 1] == 0)
                            list.Add(new Point(_point.X - 1, _point.Y + 2));
                    }

                    if (_point.X < 8)
                    {
                        if (board.MatrixPosition[_point.X + 1, _point.Y + 2] <= 0 && board.MatrixPosition[_point.X, _point.Y + 1] == 0)
                            list.Add(new Point(_point.X + 1, _point.Y + 2));
                    }
                }

                //nước tiến 1
                if (_point.Y <= 8)
                {
                    if (_point.X > 1)
                    {
                        if (board.MatrixPosition[_point.X - 2, _point.Y + 1] <= 0 && board.MatrixPosition[_point.X - 1, _point.Y] == 0)
                            list.Add(new Point(_point.X - 2, _point.Y + 1));
                    }

                    if (_point.X < 7)
                    {
                        if (board.MatrixPosition[_point.X + 2, _point.Y + 1] <= 0 && board.MatrixPosition[_point.X + 1, _point.Y] == 0)
                            list.Add(new Point(_point.X + 2, _point.Y + 1));
                    }
                }

                //nước lùi 1
                if (_point.Y >= 1)
                {
                    if (_point.X > 1)
                    {
                        if (board.MatrixPosition[_point.X - 2, _point.Y - 1] <= 0 && board.MatrixPosition[_point.X - 1, _point.Y] == 0)
                            list.Add(new Point(_point.X - 2, _point.Y - 1));
                    }

                    if (_point.X < 7)
                    {
                        if (board.MatrixPosition[_point.X + 2, _point.Y - 1] <= 0 && board.MatrixPosition[_point.X + 1, _point.Y] == 0)
                            list.Add(new Point(_point.X + 2, _point.Y - 1));
                    }
                }

                //nước lùi 2
                if (_point.Y >= 2)
                {
                    if (_point.X > 0)
                    {
                        if (board.MatrixPosition[_point.X - 1, _point.Y - 2] <= 0 && board.MatrixPosition[_point.X, _point.Y - 1] == 0)
                            list.Add(new Point(_point.X - 1, _point.Y - 2));
                    }

                    if (_point.X < 8)
                    {
                        if (board.MatrixPosition[_point.X + 1, _point.Y - 2] <= 0 && board.MatrixPosition[_point.X, _point.Y - 1] == 0)
                            list.Add(new Point(_point.X + 1, _point.Y - 2));
                    }
                }

            }
            //tương tự
            else
            {
                if (_point.Y <= 7)
                {
                    if (_point.X > 0)
                    {
                        if (board.CheckPoint(_point.X - 1, _point.Y + 2) >= 0 && board.CheckPoint(_point.X, _point.Y + 1) == 0)
                            list.Add(new Point(_point.X - 1, _point.Y + 2));
                    }

                    if (_point.X < 8)
                    {
                        if (board.CheckPoint(_point.X + 1, _point.Y + 2) >= 0 && board.CheckPoint(_point.X, _point.Y + 1) == 0)
                            list.Add(new Point(_point.X + 1, _point.Y + 2));
                    }
                }

                if (_point.Y <= 8)
                {
                    if (_point.X > 1)
                    {
                        if (board.CheckPoint(_point.X - 2, _point.Y + 1) >= 0 && board.CheckPoint(_point.X - 1, _point.Y) == 0)
                            list.Add(new Point(_point.X - 2, _point.Y + 1));
                    }

                    if (_point.X < 7)
                    {
                        if (board.CheckPoint(_point.X + 2, _point.Y + 1) >= 0 && board.CheckPoint(_point.X + 1, _point.Y) == 0)
                            list.Add(new Point(_point.X + 2, _point.Y + 1));
                    }
                }

                if (_point.Y >= 1)
                {
                    if (_point.X > 1)
                    {
                        if (board.CheckPoint(_point.X - 2, _point.Y - 1) >= 0 && board.CheckPoint(_point.X - 1, _point.Y) == 0)
                            list.Add(new Point(_point.X - 2, _point.Y - 1));
                    }

                    if (_point.X <= 6)
                    {
                        if (board.CheckPoint(_point.X + 2, _point.Y - 1) >= 0 && board.CheckPoint(_point.X + 1, _point.Y) == 0)
                            list.Add(new Point(_point.X + 2, _point.Y - 1));
                    }
                }

                if (_point.Y >= 2)
                {
                    if (_point.X > 0)
                    {
                        if (board.CheckPoint(_point.X - 1, _point.Y - 2) >= 0 && board.CheckPoint(_point.X, _point.Y - 1) == 0)
                            list.Add(new Point(_point.X - 1, _point.Y - 2));
                    }

                    if (_point.X < 8)
                    {
                        if (board.CheckPoint(_point.X + 1, _point.Y - 2) >= 0 && board.CheckPoint(_point.X, _point.Y - 1) == 0)
                            list.Add(new Point(_point.X + 1, _point.Y - 2));
                    }
                }
            }
            return list;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Di chuyển quân cờ
        /// </summary>
        /// <param name="destination">vị trí đích</param>
        /// <param name="board">bàn cờ</param>
        /// <returns>True nếu di chuyển thành công, false ngược lại</returns>
        public virtual bool Move(Point destination, ChessBoard board)
        {
            if (destination == new Point(-1, -1)) return false;

            var list = GetNextMoves(board);
            foreach (var pos in list)
            {
                if (destination == pos)
                {
                    board.MatrixPosition[_point.X, _point.Y] = 0;

                    var chess = board[destination];
                    if (chess != null)
                    {
                        chess._isDied = true;
                        chess.Point = new Microsoft.Xna.Framework.Point(-1, -1);
                    }
                    _point = destination;
                    board.MatrixPosition[destination.X, destination.Y] = _id;
                    return true;
                }
            }
            return false;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Lấy danh sách các nước đi kế tiếp
 /// </summary>
 /// <remarks>Dựa vào vị trí của nó trên bàn cờ để có được danh sách nước đi hợp lệ</remarks>
 /// <param name="board">bàn cờ</param>
 /// <returns>Danh sách các điểm có thể đi</returns>
 public virtual List<Point> GetNextMoves(ChessBoard board)
 {
     return null;
 }
Ejemplo n.º 12
0
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            var list = new List<Point>();

            if (_id > 0)
            {
                //có thêm 1 nước đi bên phải nếu x < 5
                if (_point.X < 5)
                {
                    if (board.CheckPoint(_point.X + 1, _point.Y) <= 0) list.Add(new Point(_point.X + 1, _point.Y));
                }

                //có thêm 1 nước đi bên trái nếu x > 3
                if (_point.X > 3)
                {
                    if (board.CheckPoint(_point.X - 1, _point.Y) <= 0)
                        list.Add(new Point(_point.X - 1, _point.Y));
                }

                if (_point.Y > 0)
                {
                    if (board.CheckPoint(_point.X, _point.Y - 1) <= 0)
                        list.Add(new Point(_point.X, _point.Y - 1));
                }

                if (_point.Y < 2)
                {
                    if (board.CheckPoint(_point.X, _point.Y + 1) <= 0)
                        list.Add(new Point(_point.X, _point.Y + 1));
                }
            }
            else
            {
                //có thêm 1 nước đi bên phải nếu x < 5
                if (_point.X < 5)
                {
                    if (board.CheckPoint(_point.X + 1, _point.Y) >= 0) list.Add(new Point(_point.X + 1, _point.Y));
                }

                //có thêm 1 nước đi bên trái nếu x > 3
                if (_point.X > 3)
                {
                    if (board.CheckPoint(_point.X - 1, _point.Y) >= 0)
                        list.Add(new Point(_point.X - 1, _point.Y));
                }

                //nước lùi
                if (_point.Y > 0 || _point.Y > 7 )
                {
                    if (board.CheckPoint(_point.X, _point.Y - 1) >= 0)
                        list.Add(new Point(_point.X, _point.Y - 1));
                }

                //nước tiến
                if (_point.Y < 2 || _point.Y < 9)
                {
                    if (board.CheckPoint(_point.X, _point.Y + 1) >= 0)
                        list.Add(new Point(_point.X, _point.Y + 1));
                }
            }
            return list;
        }
Ejemplo n.º 13
0
        public override List<Point> GetNextMoves(ChessBoard board)
        {
            var list = new List<Point>();
            int i, j;
            int x = _point.X;
            int y = _point.Y;

            if (_id > 0)
            {
                i = x - 1;
                //tìm về phía trái quân cờ
                while (true)
                {
                    if (i < 0 || i > 8) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[i, y] == 0)
                    {
                        list.Add(new Point(i, y));
                    }
                    //nếu có quân cờ, tìm tiếp về bên trái xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i - 1;
                        while (true)
                        {
                            if (j < 0) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[j, y] > 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[j, y] < 0)
                            {
                                list.Add(new Point(j, y));
                                break;
                            }
                            j--;
                        }
                        break;
                    }
                    i--;
                }

                i = x + 1;
                //tìm về phía phải quân cờ
                while (true)
                {
                    if (i < 0 || i > 8) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[i, y] == 0)
                    {
                        list.Add(new Point(i, y));
                    }
                    //nếu có quân cờ, tìm tiếp về bên phải xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i + 1;
                        while (true)
                        {
                            if (j < 0 || j > 8) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[j, y] > 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[j, y] < 0)
                            {
                                list.Add(new Point(j, y));
                                break;
                            }
                            j++;
                        }
                        break;
                    }
                    i++;
                }

                i = y - 1;
                //tìm về phía trên quân cờ
                while (true)
                {
                    if (i < 0 || i > 9) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[x, i] == 0)
                    {
                        list.Add(new Point(x, i));
                    }
                    //nếu có quân cờ, tìm tiếp về bên trên xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i - 1;
                        while (true)
                        {
                            if (j < 0) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[x, j] > 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[x, j] < 0)
                            {
                                list.Add(new Point(x, j));
                                break;
                            }
                            j--;
                        }
                        break;
                    }
                    i--;
                }

                i = y + 1;
                //tìm về phía dưới quân cờ
                while (true)
                {
                    if (i < 0 || i > 9) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[x, i] == 0)
                    {
                        list.Add(new Point(x, i));
                    }
                    //nếu có quân cờ, tìm tiếp về bên dưới xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i + 1;
                        while (true)
                        {
                            if (j < 0 || j > 9) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[x, j] > 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[x, j] < 0)
                            {
                                list.Add(new Point(x, j));
                                break;
                            }
                            j++;
                        }
                        break;
                    }
                    i++;
                }
            }
            //tương tự với quân âm
            else
            {
                i = x - 1;
                //tìm về phía trái quân cờ
                while (true)
                {
                    if (i < 0 || i > 8) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[i, y] == 0)
                    {
                        list.Add(new Point(i, y));
                    }
                    //nếu có quân cờ, tìm tiếp về bên trái xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i - 1;
                        while (true)
                        {
                            if (j < 0) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[j, y] < 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[j, y] > 0)
                            {
                                list.Add(new Point(j, y));
                                break;
                            }
                            j--;
                        }

                        //không có quân cờ nào thoát vòng lặp
                        break;
                    }
                    i--;
                }

                i = x + 1;
                //tìm về phía phải quân cờ
                while (true)
                {
                    if (i < 0 || i > 8) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[i, y] == 0)
                    {
                        list.Add(new Point(i, y));
                    }
                    //nếu có quân cờ, tìm tiếp về bên phải xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i + 1;
                        while (true)
                        {
                            if (j < 0 || j > 8) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[j, y] < 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[j, y] > 0)
                            {
                                list.Add(new Point(j, y));
                                break;
                            }
                            j++;
                        }
                        break;
                    }
                    i++;
                }

                i = y - 1;
                //tìm về phía trên quân cờ
                while (true)
                {
                    if (i < 0 || i > 9) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[x, i] == 0)
                    {
                        list.Add(new Point(x, i));
                    }
                    //nếu có quân cờ, tìm tiếp về bên trên xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i - 1;
                        while (true)
                        {
                            if (j < 0) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[x, j] < 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[x, j] > 0)
                            {
                                list.Add(new Point(x, j));
                                break;
                            }
                            j--;
                        }
                        break;
                    }
                    i--;
                }

                i = y + 1;
                //tìm về phía dưới quân cờ
                while (true)
                {
                    if (i < 0 || i > 9) break;

                    //nếu vị trí này trên bàn cờ trống, thêm nước đi cho quân cờ
                    if (board.MatrixPosition[x, i] == 0)
                    {
                        list.Add(new Point(x, i));
                    }
                    //nếu có quân cờ, tìm tiếp về bên dưới xem có quân địch không, có thì thêm nước đi
                    else
                    {
                        j = i + 1;
                        while (true)
                        {
                            if (j < 0 || j > 9) break;
                            //gặp quân mình thì thoát vòng lặp
                            if (board.MatrixPosition[x, j] < 0) break;
                            //gặp quân địch thêm nước đi rồi thoát
                            else if (board.MatrixPosition[x, j] > 0)
                            {
                                list.Add(new Point(x, j));
                                break;
                            }
                            j++;
                        }
                        break;
                    }
                    i++;
                }
            }
            return list;
        }
Ejemplo n.º 14
0
 public ChessBoard GetNextBoard(ChessBoard board)
 {
     Computer com = new Computer();
     i = 0;
     com = AlphaBetaPruning(board, -10000, 10000, 5, 1);
     Debug.WriteLine("Count = " + i);
     return com.Board;
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Sinh các thế cờ quân mã dương
        /// </summary>
        /// <param name="board">bàn cờ hiện tại</param>
        /// <returns>danh sách các thế cờ(bàn cờ)</returns>
        public List<ChessBoard> GenerateBoardPositiveTeam(ChessBoard board)
        {
            var list = new List<ChessBoard>();

            foreach (var chess in board.PositiveTeam)
            {
                foreach (var move in chess.GetNextMoves(board))
                {
                    board.MovePiece(chess, move); //đi thử quân cờ, quân cờ chess thuộc board nên phải di chuyển rồi mới copy
                    var boardTemp = board.Clone(); //copy bàn cờ
                    board.Undo(); //undo
                    list.Add(boardTemp); //thêm bàn cờ với nước đi kế tiếp
                }
            }

            return list;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// sinh các thế cờ quân mã âm
        /// </summary>
        /// <param name="board">bàn cờ hiện tại</param>
        /// <returns>danh sách các thế cờ(bàn cờ)</returns>
        public List<ChessBoard> GenerateBoardNegativeTeam(ChessBoard board)
        {
            var list = new List<ChessBoard>();

            foreach (var chess in board.NegativeTeam)
            {
                foreach (var move in chess.GetNextMoves(board))
                {
                    board.MovePiece(chess, move);
                    var boardTemp = board.Clone();
                    board.Undo();
                    list.Add(boardTemp);
                }
            }

            return list;
        }