Exemplo n.º 1
0
 public Board RemovePiece(ChessPieces piece)
 {
     // this.ListChessPiece[piece.chessPiecePosition] = null;
     this.ListChessPiece.Remove(piece.chessPiecePosition);
     this.ListChessPiece[piece.chessPiecePosition] = null;
     return(this);
 }
Exemplo n.º 2
0
        public Board Clone()
        {
            Board board = new Board(Curentlayer.sideplayer);

            board.Blacklayer             = this.Blacklayer;
            board.Whitelayer             = this.Whitelayer;
            board.Curentlayer            = this.Curentlayer;
            board.nextMoveChessPieceSide = this.nextMoveChessPieceSide;

            for (int i = 0; i < 64; i++)
            {
                ChessPieces piece = this.GetPiece(i);
                if (piece != null)
                {
                    board.SetPiece(piece);
                }
            }


            foreach (Cell cell in this.listCell)
            {
                board.listCell.Add(cell);
            }

            return(board);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Lấy toàn bộ di chuyển có thể trong bàn cờ
        /// </summary>
        /// <param name="board"></param>
        /// <param name="side"></param>
        /// <returns></returns>
        List <Move> GetAllMove(Board board, ChessPieceSide?side = null)
        {
            //neu side=null co nghia la get toan bo quan co
            //nguoc lai chi get ben side thoi ne.

            List <Move> listmove = new List <Move>();

            //int count = 0;
            //string str = "";
            if (side != null)
            {
                foreach (Cell cell in board.listCell)
                {
                    ChessPieces piece = cell.GetChessPieces();
                    //neu tai vi tri cell nay co quan co
                    // va la quan co dang can lay thi lay cai move cua no
                    if (piece != null && piece.side == side)
                    {
                        //count++;
                        listmove.AddRange(piece.getLegalMoves(board));
                        //str = str + "   " +piece.chessPieceType.chessPieceName+"  "+piece.side+"   ";
                    }
                }
            }
            // MessageBox.Show("so quan co: " + count+": "+str);
            //MessageBox.Show(board.listCell.Count()+"");
            return(listmove);
        }
Exemplo n.º 4
0
 public static Cell CreateCell(int position, ChessPieces piece)
 {
     if (piece != null)
     {
         return(new OccupiedCell(position, piece));
     }
     else
     {
         return(new EmptyCell(position));
     }
 }
Exemplo n.º 5
0
        ///<summary>
        /// lấy tổng số điểm còn lại của các quân cờ trên bàn cờ
        ///</summary>
        int ValuePiece(Board board, ChessPieceSide side)
        {
            int sum = 0;

            foreach (Cell cell in board.listCell)
            {
                ChessPieces piece = cell.GetChessPieces();
                if (piece != null)
                {
                    sum += piece.chessPieceType.weight;
                }
            }

            return(sum);
        }
Exemplo n.º 6
0
        public override List <Move> getLegalMoves(Board board)
        {
            List <Move> ListCanMove = new List <Move>();

            foreach (int argument in legalMoveArguments)
            {
                int checkPosition = this.chessPiecePosition + (this.vector * argument);

                if (!board.checkedForLegalPosition(checkPosition))
                {
                    continue;
                }

                //neu di thang len ve phia truoc ma chua co quan nao chan lai( can tro)
                if (argument == 8 && !board.GetCell(checkPosition).Occupied())
                {
                    ListCanMove.Add(new NormalMove(this, checkPosition));
                }

                //neu xet den nhay qua 1 o cho truong hop con tot di lan dau
                if (argument == 16 && (this.chessPiecePosition / 8 == 1 && this.side == ChessPieceSide.BLACK ||
                                       this.chessPiecePosition / 8 == 6 && this.side == ChessPieceSide.WHITE))
                {
                    // neu ca 2 o phia truoc no dang trong thi co the di chuyen
                    int behindPosition = this.chessPiecePosition + (this.vector * 8);
                    if (!board.GetCell(checkPosition).Occupied() &&
                        !board.GetCell(behindPosition).Occupied())
                    {
                        ListCanMove.Add(new NormalMove(this, checkPosition));
                    }
                }

                //neu co quan dich nam cheo sat 1 ben thi co the tan cong
                //neu nhu khong vi pham nguyen tac di chuyen
                if (((argument == 9 && CheckArgument9()) || argument == 7 && CheckArgument7()) &&
                    board.GetCell(checkPosition).Occupied())
                {
                    ChessPieces occupiedPiece = board.GetCell(checkPosition).GetChessPieces();
                    if (occupiedPiece.side != this.side)
                    {
                        ListCanMove.Add(new AttackMove(this, checkPosition, occupiedPiece));
                        //MessageBox.Show("attack");
                    }
                }
            }

            return(ListCanMove);
        }
Exemplo n.º 7
0
        /// <summary>
        /// thực hiện di chuyển nước cờ trên boardlogic
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public virtual Board ExcuteMove(Board board)
        {
            if (TheKingDie(board))
            {
                string slide = (board.Curentlayer.sideplayer == ChessPieceSide.BLACK) ? " Black Win!!!" : "White Win!!!";
                // MessageBox.Show(slide);
                this.WinGame = slide;
                //se kiem tra o phia ngoai form Chinh de load lai ban co nua
                //o trong nay khong the goi toi ham resetGame. kho haha
            }

            //thay đổi loại cell trên bàn cờ cho 2 vị trí dang xet.
            // tao 1 quan co cung loai dua vao vi tri moi
            ChessPieces piece = actionPiece.movePiece(this);

            //thay doi cell
            board.listCell[this.GetCurrentPosition()] = new EmptyCell(this.GetCurrentPosition());
            board.listCell[this.destination]          = new OccupiedCell(this.destination, piece);

            //theo doi quan co tren list piece
            board.SetPiece(piece);
            board.RemovePiece(this.actionPiece);

            //neu la vua di chuyen thi set vi tri thang vua cho player.
            if (this.actionPiece.IsKing())
            {
                board.Curentlayer.king = (King)piece; //= vua o vi tri moi
            }

            //neu la o vi tri chuyen toi co the phong tuong cho tot
            //  if(this.IsPromote())
            //   {
            //mo hop thoai cho chonj tuong muon phong va set lai gia tri cho vi tri moi phong

            // vi khong goi duoc form ben GUI nen thuc hien ben CellGUI truoc khi goi Move.

            //  MessageBox.Show("cho nay de phong tuong hahaha");
            //  }

            //kiem tra nuoc co vua di co de doa tinh mang cua tuong doi phuong khong.
            checkDangerousForTheKing(board);

            return(board);
        }
Exemplo n.º 8
0
        /// <summary>
        /// kiểm tra nguy hiểm của nước cờ tới vua của đối phương để đưa ra cảnh báo
        /// </summary>
        void checkDangerousForTheKing(Board board)
        {
            List <Move> listMoveFuture = new List <Move>();
            ChessPieces p = (ChessPieces)board.GetCell(this.destination).GetChessPieces();

            listMoveFuture = p.getLegalMoves(board);

            King king = board.GetNextPlayer().king;

            foreach (Move m in listMoveFuture)
            {
                if (m.destination == king.chessPiecePosition)
                {
                    // MessageBox.Show("Chiếu tướng. hahahaa !!!");
                    this.chieuTuong = true;
                    break;
                }
            }
        }
        public Board Redo(Board board)
        {
            Move currentMove = listRedo.Peek();

            ChessPieces piece = currentMove.actionPiece.movePiece(currentMove);

            //thay doi cell
            board.listCell[currentMove.GetCurrentPosition()] = new EmptyCell(currentMove.GetCurrentPosition());
            board.listCell[currentMove.destination]          = new OccupiedCell(currentMove.destination, piece);

            //theo doi quan co tren list piece
            board.SetPiece(piece);
            board.RemovePiece(currentMove.actionPiece);

            //neu la vua di chuyen thi set vi tri thang vua cho player.
            if (currentMove.actionPiece.IsKing())
            {
                board.Curentlayer.king = (King)piece; //= vua o vi tri moi
            }

            return(board);
        }
Exemplo n.º 10
0
 public OccupiedCell(int position, ChessPieces piece) : base(position)
 {
     this.chessPiece = piece;
 }
Exemplo n.º 11
0
 public Move(ChessPieces piece, int des)
 {
     this.actionPiece = piece;
     this.destination = des;
 }
Exemplo n.º 12
0
 public AttackMove(ChessPieces piece, int des, ChessPieces pieceWasAttacked) : base(piece, des)
 {
     this.pieceWasAttacked = pieceWasAttacked;
 }
Exemplo n.º 13
0
 public NormalMove(ChessPieces piece, int des) : base(piece, des)
 {
 }
Exemplo n.º 14
0
        public ChessPieces GetPiece(int position)
        {
            ChessPieces piece = null;

            return(this.ListChessPiece.TryGetValue(position, out piece) ? piece : null);
        }
Exemplo n.º 15
0
 public Board SetPiece(ChessPieces piece)
 {
     this.ListChessPiece[piece.chessPiecePosition] = piece;
     return(this);
 }