Esempio n. 1
0
        // calculate the possible moves for the king piece and insert them into passed array
        private void GetKingMoves(Cell source, ArrayList moves)
        {
            Cell newcell;

            // King can move to any of it's neighbor cells at the distance of one cell

            // check if king can move to top
            newcell = MainBoard.TopCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to left
            newcell = MainBoard.LeftCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to right
            newcell = MainBoard.RightCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to bottom
            newcell = MainBoard.BottomCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to top-left
            newcell = MainBoard.TopLeftCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to top-right
            newcell = MainBoard.TopRightCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to bottom-left
            newcell = MainBoard.BottomLeftCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }
            // check if king can move to bottom-right
            newcell = MainBoard.BottomRightCell(source);
            if (newcell != null && !newcell.IsOwned(source))           // target cell is empty or is owned by the enemy piece
            {
                moves.Add(newcell);
            }

            // Check castling or tower moves for the king
            if (MainBoard[source].piece.Moves == 0)
            {
                Cell CastlingTarget = null;                     // The cell where king will be moved in case of castling

                // As king has not yet moved, so castling is possible
                newcell = MainBoard.RightCell(source);
                if (newcell != null && newcell.IsEmpty())               // cell is empty
                {
                    if (!CauseCheck(new Move(source, newcell)))         // Inbetween cell is not under check
                    {
                        newcell = MainBoard.RightCell(newcell);
                        if (newcell != null && newcell.IsEmpty())                                  // cell is empty
                        {
                            CastlingTarget = newcell;                                              // This will be the king destination position
                            newcell        = MainBoard.RightCell(newcell);
                            if (newcell != null && !newcell.IsEmpty() && newcell.piece.Moves == 0) // check if the rook piece has not yet moved
                            {
                                moves.Add(CastlingTarget);                                         // Add this as possible move
                            }
                        }
                    }
                }

                // Check on the left side
                newcell = MainBoard.LeftCell(source);
                if (newcell != null && newcell.IsEmpty())               // cell is empty
                {
                    if (!CauseCheck(new Move(source, newcell)))         // Inbetween cell is not under check
                    {
                        newcell = MainBoard.LeftCell(newcell);
                        if (newcell != null && newcell.IsEmpty())                       // cell is empty
                        {
                            CastlingTarget = newcell;                                   // This will be the king destination position
                            newcell        = MainBoard.LeftCell(newcell);
                            if (newcell != null && newcell.IsEmpty())                   // cell is empty
                            {
                                newcell = MainBoard.LeftCell(newcell);
                                if (newcell != null && !newcell.IsEmpty() && newcell.piece.Moves == 0)  // check if the rook piece has not yet moved
                                {
                                    moves.Add(CastlingTarget);                                          // Add this as possible move
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        // calculate the possible moves for the Rook piece and insert them into passed array
        private void GetRookMoves(Cell source, ArrayList moves)
        {
            Cell newcell;

            // Check all the move squars available in top direction
            newcell = MainBoard.TopCell(source);
            while (newcell != null)             // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty())          //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                    break;                          // force quite the loop execution
                }

                if (newcell.IsOwned(source))          //next cell contains owner object
                {
                    break;                            // force quite the loop execution
                }
                newcell = MainBoard.TopCell(newcell); // keep moving in the top direction
            }

            // Check all the move squars available in left direction
            newcell = MainBoard.LeftCell(source);
            while (newcell != null)             // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty())          //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                    break;                          // force quite the loop execution
                }

                if (newcell.IsOwned(source))           //next cell contains owner object
                {
                    break;                             // force quite the loop execution
                }
                newcell = MainBoard.LeftCell(newcell); // keep moving in the left direction
            }

            // Check all the move squars available in right direction
            newcell = MainBoard.RightCell(source);
            while (newcell != null)             // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty())          //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                    break;                          // force quite the loop execution
                }

                if (newcell.IsOwned(source))            //next cell contains owner object
                {
                    break;                              // force quite the loop execution
                }
                newcell = MainBoard.RightCell(newcell); // keep moving in the right direction
            }

            // Check all the move squars available in bottom direction
            newcell = MainBoard.BottomCell(source);
            while (newcell != null)             // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty())          //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                    break;                          // force quite the loop execution
                }

                if (newcell.IsOwned(source))             //next cell contains owner object
                {
                    break;                               // force quite the loop execution
                }
                newcell = MainBoard.BottomCell(newcell); // keep moving in the bottom direction
            }
        }
Esempio n. 3
0
        // calculate the possible moves for the queen piece and insert them into passed array
        private void GetQueenMoves(Cell source, ArrayList moves)
        {
            Cell newcell;

            // Check all the move squars available in top-left direction
            newcell = MainBoard.TopLeftCell(source);
            if (newcell != null)       // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty()) //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                }
            }

            // Check all the move squars available in top-right direction
            newcell = MainBoard.TopRightCell(source);
            if (newcell != null)       // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty()) //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                }
            }

            // Check all the move squars available in bottom-left direction
            newcell = MainBoard.BottomLeftCell(source);
            if (newcell != null)       // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty()) //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                }
            }

            // Check all the move squars available in the bottom-right direction
            newcell = MainBoard.BottomRightCell(source);
            if (newcell != null)       // move as long as cell is available in this direction
            {
                if (newcell.IsEmpty()) //next cell is available for move
                {
                    moves.Add(newcell);
                }

                if (newcell.IsOwnedByEnemy(source)) //next cell is owned by the enemy object
                {
                    moves.Add(newcell);             // Add this to available location
                }
            }
        }
Esempio n. 4
0
        // calculate the possible moves for the pawn object and insert them into passed array
        private void GetPawnMoves(Cell source, ArrayList moves)
        {
            Cell newcell;

            if (source.piece.Side.isWhite())
            {
                // Calculate moves for the white piece
                newcell = MainBoard.TopCell(source);
                if (newcell != null && newcell.IsEmpty())               // Top cell is available for the move
                {
                    moves.Add(newcell);
                }

                // Check the 2nd top element from source
                //if (newcell != null && newcell.IsEmpty())
                //{
                //	newcell = MainBoard.TopCell(newcell);
                //	if (newcell!=null && source.piece.Moves == 0 && newcell.IsEmpty()) // 2nd top cell is available and piece has not yet moved
                //		moves.Add(newcell);
                //}

                // Check top-left cell for enemy piece
                newcell = MainBoard.TopLeftCell(source);
                if (newcell != null && newcell.IsOwnedByEnemy(source))               // Top cell is available for the move
                {
                    moves.Add(newcell);
                }

                // Check top-right cell for enemy piece
                newcell = MainBoard.TopRightCell(source);
                if (newcell != null && newcell.IsOwnedByEnemy(source))               // Top cell is available for the move
                {
                    moves.Add(newcell);
                }

                // Check for possible En Passant Move
                Move LastPawnMove = LastMoveWasPawnBegin();

                if (LastPawnMove != null)                               // last move was a pawn move
                {
                    if (source.row == LastPawnMove.EndCell.row)         // Can do En Passant
                    {
                        if (LastPawnMove.EndCell.col == source.col - 1) // En Passant pawn is on left side
                        {
                            newcell = MainBoard.TopLeftCell(source);
                            if (newcell != null && newcell.IsEmpty())                           // Top cell is available for the move
                            {
                                moves.Add(newcell);
                            }
                        }

                        if (LastPawnMove.EndCell.col == source.col + 1)                         // En Passant pawn is on left side
                        {
                            newcell = MainBoard.TopRightCell(source);
                            if (newcell != null && newcell.IsEmpty())                           // Top cell is available for the move
                            {
                                moves.Add(newcell);
                            }
                        }
                    }
                }
            }
            else
            {
                // Calculate moves for the black piece
                newcell = MainBoard.BottomCell(source);
                if (newcell != null && newcell.IsEmpty())               // bottom cell is available for the move
                {
                    moves.Add(newcell);
                }

                // Check the 2nd bottom cell from source
                //if (newcell!=null && newcell.IsEmpty())
                //{
                //	newcell = MainBoard.BottomCell(newcell);
                //	if (newcell!=null && source.piece.Moves == 0 && newcell.IsEmpty()) // 2nd bottom cell is available and piece has not yet moved
                //		moves.Add(newcell);
                //}

                // Check bottom-left cell for enemy piece
                newcell = MainBoard.BottomLeftCell(source);
                if (newcell != null && newcell.IsOwnedByEnemy(source))               // Bottom cell is available for the move
                {
                    moves.Add(newcell);
                }

                // Check bottom-right cell for enemy piece
                newcell = MainBoard.BottomRightCell(source);
                if (newcell != null && newcell.IsOwnedByEnemy(source))               // Bottom cell is available for the move
                {
                    moves.Add(newcell);
                }

                // Check for possible En Passant Move
                Move LastPawnMove = LastMoveWasPawnBegin();

                if (LastPawnMove != null)                               // last move was a pawn move
                {
                    if (source.row == LastPawnMove.EndCell.row)         // Can do En Passant
                    {
                        if (LastPawnMove.EndCell.col == source.col - 1) // En Passant pawn is on left side
                        {
                            newcell = MainBoard.BottomLeftCell(source);
                            if (newcell != null && newcell.IsEmpty())                           // Bottom cell is available for the move
                            {
                                moves.Add(newcell);
                            }
                        }

                        if (LastPawnMove.EndCell.col == source.col + 1)                         // En Passant pawn is on left side
                        {
                            newcell = MainBoard.BottomRightCell(source);
                            if (newcell != null && newcell.IsEmpty())                           // Bottom cell is available for the move
                            {
                                moves.Add(newcell);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        // calculate the possible moves for the knight piece and insert them into passed array
        private void GetKnightMoves(Cell source, ArrayList moves)
        {
            Cell newcell;

            // First check top two left and right moves for knight
            newcell = MainBoard.TopCell(source);
            if (newcell != null)
            {
                newcell = MainBoard.TopLeftCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }

                newcell = MainBoard.TopCell(source);
                newcell = MainBoard.TopRightCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }
            }
            // Now check 2nd bottom left and right cells
            newcell = MainBoard.BottomCell(source);
            if (newcell != null)
            {
                newcell = MainBoard.BottomLeftCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }

                newcell = MainBoard.BottomCell(source);
                newcell = MainBoard.BottomRightCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }
            }
            // Now check 2nd Left Top and bottom cells
            newcell = MainBoard.LeftCell(source);
            if (newcell != null)
            {
                newcell = MainBoard.TopLeftCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }

                newcell = MainBoard.LeftCell(source);
                newcell = MainBoard.BottomLeftCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }
            }
            // Now check 2nd Right Top and bottom cells
            newcell = MainBoard.RightCell(source);
            if (newcell != null)
            {
                newcell = MainBoard.TopRightCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }

                newcell = MainBoard.RightCell(source);
                newcell = MainBoard.BottomRightCell(newcell);
                // target cell is empty or is owned by the enemy piece
                if (newcell != null && !newcell.IsOwned(source))
                {
                    moves.Add(newcell);
                }
            }
        }
Esempio n. 6
0
 // Returns the cell on the bottom-right of the current cell
 public Cell BottomRightCell(Cell cell)
 {
     return(this[cell.row + 1, cell.col + 1]);
 }
Esempio n. 7
0
        // Verify conditions for black side to start counting down until stalemale
        public int VerifyCountforBlack()
        {
            // board verification starts at a1 (top left of the board)
            Cell newcell = m_cells["a1"];
            // counters for enemy rooks, bishops, knights, queens and all the pieces on the board
            int counterRook = 0, counterBishop = 0, counterKnight = 0, counterPieces = 0;

            // sum of total value of the white / black pieces
            int totalWhiteWeight = 0, totalBlackWeight = 0;

            // go through all the pieces to check for pawns.
            // if they exist, no stalemale can take place
            while (newcell != null)
            {
                Cell newNewcell = newcell;
                while (newNewcell != null)
                {
                    if (!newNewcell.IsEmpty())
                    {
                        // if any pawn is found no stalemale can happen
                        if (newNewcell.piece.IsPawn())
                        {
                            return(-1);
                        }
                        // sum up all the player specific pieces to find who is at disatvantage
                        if (newNewcell.piece.Side.isWhite())
                        {
                            totalWhiteWeight += newNewcell.piece.GetWeight();
                        }
                        if (newNewcell.piece.Side.isBlack())
                        {
                            totalBlackWeight += newNewcell.piece.GetWeight();
                        }
                    }
                    newNewcell = this.RightCell(newNewcell);
                }
                newcell = this.BottomCell(newcell);
            }

            // if the side at advantage calls for stalemale countdown, it cannot happen
            if (totalBlackWeight >= totalWhiteWeight)
            {
                return(-1);
            }

            newcell = m_cells["a1"];
            while (newcell != null)
            {
                Cell newNewcell = newcell;
                while (newNewcell != null)
                {
                    if (!newNewcell.IsEmpty())
                    {
                        counterPieces++; // count all the pieces on the board

                        if (newNewcell.piece.Side.isWhite() && newNewcell.piece.IsRook())
                        {
                            counterRook++; // count white rooks
                        }
                        if (newNewcell.piece.Side.isWhite() && newNewcell.piece.IsBishop())
                        {
                            counterBishop++;// count white bishops
                        }
                        if (newNewcell.piece.Side.isWhite() && newNewcell.piece.IsKnight())
                        {
                            counterKnight++;// count white knights
                        }
                        // if there is any white piece other than the king, stalemale in 64 moves
                        if (newNewcell.piece.Side.isBlack() && newNewcell.piece.IsRook())
                        {
                            return(64);
                        }
                        if (newNewcell.piece.Side.isBlack() && newNewcell.piece.IsBishop())
                        {
                            return(64);
                        }
                        if (newNewcell.piece.Side.isBlack() && newNewcell.piece.IsKnight())
                        {
                            return(64);
                        }
                        if (newNewcell.piece.Side.isBlack() && newNewcell.piece.IsQueen())
                        {
                            return(64);
                        }
                    }
                    newNewcell = this.RightCell(newNewcell);
                }
                newcell = this.BottomCell(newcell);
            }

            // if there are 2 enemy rooks, stalemale in (8 minus number of pieces) moves
            if (counterRook == 2)
            {
                return(8 - counterPieces);
            }
            // if there is 1 enemy rook, stalemale in (16 minus number of pieces) moves
            if (counterRook == 1)
            {
                return(16 - counterPieces);
            }
            // if there are 2 enemy bishops, stalemale in (22 minus number of pieces) moves
            if (counterBishop == 2)
            {
                return(22 - counterPieces);
            }
            // if there are 2 enemy knights, stalemale in (32 minus number of pieces) moves
            if (counterKnight == 2)
            {
                return(32 - counterPieces);
            }
            // if there is 1 enemy bishop, stalemale in (44 minus number of pieces) moves
            if (counterBishop == 1)
            {
                return(44 - counterPieces);
            }
            // otherwise stalemale in (64 minus number of pieces) moves
            return(64 - counterPieces);
        }
Esempio n. 8
0
 // Returns the cell on the top-right of the current cell
 public Cell TopRightCell(Cell cell)
 {
     return(this[cell.row - 1, cell.col + 1]);
 }
Esempio n. 9
0
 // Returns the cell on the bottom-left of the current cell
 public Cell BottomLeftCell(Cell cell)
 {
     return(this[cell.row + 1, cell.col - 1]);
 }
Esempio n. 10
0
 // Returns the cell on the top-left of the current cell
 public Cell TopLeftCell(Cell cell)
 {
     return(this[cell.row - 1, cell.col - 1]);
 }
Esempio n. 11
0
 // Returns the cell on the bottom of the given cell
 public Cell BottomCell(Cell cell)
 {
     return(this[cell.row + 1, cell.col]);
 }
Esempio n. 12
0
 // Returns the cell on the right of the given cell
 public Cell RightCell(Cell cell)
 {
     return(this[cell.row, cell.col + 1]);
 }
Esempio n. 13
0
 // Returns the cell on the left of the given cell
 public Cell LeftCell(Cell cell)
 {
     return(this[cell.row, cell.col - 1]);
 }
Esempio n. 14
0
 // Returns the cell on the top of the given cell
 public Cell TopCell(Cell cell)
 {
     return(this[cell.row - 1, cell.col]);
 }