Exemplo n.º 1
0
 public static void PrintBoardView(ChessBoardView pBoard)
 {
     PieceView[,] view = pBoard.GetViewBoard();
     Square[,] view2   = pBoard.GetChessBoard().GetBoard();
     for (int i = 0; i < view2.GetLength(0); i++)
     {
         for (int j = 0; j < view2.GetLength(1); j++)
         {
             Console.Write(view2[i, j].HasPiece() + " ");
         }
         Console.WriteLine();
     }
 }
Exemplo n.º 2
0
        private static List <BoardPosition> CheckCastling(BoardPosition bp, ChessBoardView pChessBoard)
        {
            List <BoardPosition> boardPositionList = new List <BoardPosition>();

            boardPositionList.Add(bp);
            Piece     p  = pChessBoard.GetChessBoard().GetBoard()[bp.X, bp.Y].GetPiece();
            PieceView pv = pChessBoard.GetViewBoard()[bp.X, bp.Y];

            int i = bp.X;
            int j = bp.Y;

            if (p.color == Piece.Colors.WHITE)
            {
                if (!pv.GetHasMoved())
                {
                    if (pChessBoard.GetChessBoard().GetBoard()[0, 7].GetPiece() != null)
                    {
                        if (pChessBoard.GetChessBoard().GetBoard()[0, 7].GetPiece().pieceType.type == PieceType.Types.ROOK)
                        {
                            if (!pChessBoard.GetViewBoard()[0, 7].GetHasMoved())
                            {
                                if (!pChessBoard.GetChessBoard().GetBoard()[1, 7].HasPiece() &&
                                    !pChessBoard.GetChessBoard().GetBoard()[2, 7].HasPiece() &&
                                    !pChessBoard.GetChessBoard().GetBoard()[3, 7].HasPiece())
                                {
                                    if (!SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(0, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(1, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(2, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(3, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(5, 7), pChessBoard))
                                    {
                                        boardPositionList.Add(new BoardPosition(2, 7));
                                    }
                                }
                            }
                        }
                    }
                    if (pChessBoard.GetChessBoard().GetBoard()[7, 7].GetPiece() != null)
                    {
                        if (pChessBoard.GetChessBoard().GetBoard()[7, 7].GetPiece().pieceType.type == PieceType.Types.ROOK)
                        {
                            if (!pChessBoard.GetViewBoard()[7, 7].GetHasMoved())
                            {
                                if (!pChessBoard.GetChessBoard().GetBoard()[6, 7].HasPiece() &&
                                    !pChessBoard.GetChessBoard().GetBoard()[5, 7].HasPiece())
                                {
                                    if (!SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(4, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(5, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(6, 7), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(7, 7), pChessBoard))
                                    {
                                        boardPositionList.Add(new BoardPosition(6, 7));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (p.color == Piece.Colors.BLACK)
            {
                if (!pv.GetHasMoved())
                {
                    if (pChessBoard.GetChessBoard().GetBoard()[0, 0].GetPiece() != null)
                    {
                        if (pChessBoard.GetChessBoard().GetBoard()[0, 0].GetPiece().pieceType.type == PieceType.Types.ROOK)
                        {
                            if (!pChessBoard.GetViewBoard()[0, 0].GetHasMoved())
                            {
                                if (!pChessBoard.GetChessBoard().GetBoard()[1, 0].HasPiece() &&
                                    !pChessBoard.GetChessBoard().GetBoard()[2, 0].HasPiece() &&
                                    !pChessBoard.GetChessBoard().GetBoard()[3, 0].HasPiece())
                                {
                                    if (!SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 0].GetPiece(), new BoardPosition(1, 0), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 0].GetPiece(), new BoardPosition(2, 0), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 0].GetPiece(), new BoardPosition(3, 0), pChessBoard))
                                    {
                                        boardPositionList.Add(new BoardPosition(2, 0));
                                    }
                                }
                            }
                        }
                    }
                    if (pChessBoard.GetChessBoard().GetBoard()[7, 0].GetPiece() != null)
                    {
                        if (pChessBoard.GetChessBoard().GetBoard()[7, 0].GetPiece().pieceType.type == PieceType.Types.ROOK)
                        {
                            if (!pChessBoard.GetViewBoard()[7, 0].GetHasMoved())
                            {
                                if (!pChessBoard.GetChessBoard().GetBoard()[6, 0].HasPiece() &&
                                    !pChessBoard.GetChessBoard().GetBoard()[5, 0].HasPiece())
                                {
                                    if (!SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(5, 0), pChessBoard) &&
                                        !SquareIsThreatened(pChessBoard.GetChessBoard().GetBoard()[4, 7].GetPiece(), new BoardPosition(6, 0), pChessBoard))
                                    {
                                        boardPositionList.Add(new BoardPosition(6, 0));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(boardPositionList);
        }
Exemplo n.º 3
0
        private static List <BoardPosition> GetPawnMoves(BoardPosition bp, ChessBoardView pChessBoard)
        {
            List <BoardPosition> boardPositionList = new List <BoardPosition>();

            boardPositionList.Add(bp);
            Piece p = pChessBoard.GetChessBoard().GetBoard()[bp.X, bp.Y].GetPiece();


            int i = bp.X;
            int j = bp.Y;

            // Right now white is assumed to be at the bottom of the board
            // TODO: find a way to make sure the color is not the factor
            // TODO: add the ability to double jump at the begining and the ability
            // to change pawn into other piece when it reaches the other side of the board
            if (p.color == Piece.Colors.WHITE)
            {
                if (j > 0)
                {
                    if (j > 1)
                    {
                        if (!(pChessBoard.GetViewBoard()[i, j]).GetHasMoved() &&
                            !pChessBoard.GetChessBoard().GetBoard()[i, j - 2].HasPiece() &&
                            !pChessBoard.GetChessBoard().GetBoard()[i, j - 1].HasPiece())
                        {
                            boardPositionList.Add(new BoardPosition(i, j - 2));
                        }
                    }
                    j--;
                    if (!pChessBoard.GetChessBoard().GetBoard()[i, j].HasPiece())
                    {
                        boardPositionList.Add(new BoardPosition(i, j));
                    }
                    if (i > 0)
                    {
                        i--;
                        if (pChessBoard.GetChessBoard().GetBoard()[i, j].HasPiece())
                        {
                            if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i, j].GetPiece()))
                            {
                                boardPositionList.Add(new BoardPosition(i, j));
                            }
                        }
                    }
                    i = bp.X;
                    if (i < 7)
                    {
                        i++;
                        if (pChessBoard.GetChessBoard().GetBoard()[i, j].HasPiece())
                        {
                            if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i, j].GetPiece()))
                            {
                                boardPositionList.Add(new BoardPosition(i, j));
                            }
                        }
                    }
                }
            }
            else
            {
                if (j < 7)
                {
                    if (j < 7)
                    {
                        if (!(pChessBoard.GetViewBoard()[i, j]).GetHasMoved() &&
                            !pChessBoard.GetChessBoard().GetBoard()[i, j + 2].HasPiece())
                        {
                            boardPositionList.Add(new BoardPosition(i, j + 2));
                        }
                    }

                    j++;
                    if (!pChessBoard.GetChessBoard().GetBoard()[i, j].HasPiece())
                    {
                        boardPositionList.Add(new BoardPosition(i, j));
                    }
                    i--;
                    if (i > 0)
                    {
                        if (pChessBoard.GetChessBoard().GetBoard()[i, j].HasPiece())
                        {
                            if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i, j].GetPiece()))
                            {
                                boardPositionList.Add(new BoardPosition(i, j));
                            }
                        }
                    }
                    i = bp.X;
                    if (i < 7)
                    {
                        i++;
                        if (pChessBoard.GetChessBoard().GetBoard()[i, j].HasPiece())
                        {
                            if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i, j].GetPiece()))
                            {
                                boardPositionList.Add(new BoardPosition(i, j));
                            }
                        }
                    }
                }
            }


            return(boardPositionList);
        }
Exemplo n.º 4
0
        private static List <BoardPosition> GetKingMoves(BoardPosition bp, ChessBoardView pChessBoard)
        {
            List <BoardPosition> boardPositionList = new List <BoardPosition>();

            boardPositionList.Add(bp);
            Piece     p  = pChessBoard.GetChessBoard().GetBoard()[bp.X, bp.Y].GetPiece();
            PieceView pv = pChessBoard.GetViewBoard()[bp.X, bp.Y];

            int i = bp.X;
            int j = bp.Y;

            boardPositionList.AddRange(CheckCastling(bp, pChessBoard));
            ///////////////
            if (i >= 1)
            {
                if (j >= 1)
                {
                    if (!pChessBoard.GetChessBoard().GetBoard()[i - 1, j - 1].HasPiece())
                    {
                        boardPositionList.Add(new BoardPosition(i - 1, j - 1));
                    }
                    else
                    {
                        if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i - 1, j - 1].GetPiece()))
                        {
                            boardPositionList.Add(new BoardPosition(i - 1, j - 1));
                        }
                    }
                }

                if (!pChessBoard.GetChessBoard().GetBoard()[i - 1, j].HasPiece())
                {
                    boardPositionList.Add(new BoardPosition(i - 1, j));
                }
                else
                {
                    if (j < 7)
                    {
                        if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i - 1, j + 1].GetPiece()))
                        {
                            boardPositionList.Add(new BoardPosition(i - 1, j + 1));
                        }
                    }
                }

                if (j <= 6)
                {
                    if (!pChessBoard.GetChessBoard().GetBoard()[i - 1, j + 1].HasPiece())
                    {
                        boardPositionList.Add(new BoardPosition(i - 1, j + 1));
                    }
                    else
                    {
                        if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i - 1, j + 1].GetPiece()))
                        {
                            boardPositionList.Add(new BoardPosition(i - 1, j + 1));
                        }
                    }
                }
            }
            if (j >= 1)
            {
                if (!pChessBoard.GetChessBoard().GetBoard()[i, j - 1].HasPiece())
                {
                    boardPositionList.Add(new BoardPosition(i, j - 1));
                }
                else
                {
                    if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i, j - 1].GetPiece()))
                    {
                        boardPositionList.Add(new BoardPosition(i, j - 1));
                    }
                }
            }
            if (j <= 6)
            {
                if (!pChessBoard.GetChessBoard().GetBoard()[i, j + 1].HasPiece())
                {
                    boardPositionList.Add(new BoardPosition(i, j + 1));
                }
                else
                {
                    if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i, j + 1].GetPiece()))
                    {
                        boardPositionList.Add(new BoardPosition(i, j + 1));
                    }
                }
            }
            if (i <= 6)
            {
                if (j >= 1)
                {
                    if (!pChessBoard.GetChessBoard().GetBoard()[i + 1, j - 1].HasPiece())
                    {
                        boardPositionList.Add(new BoardPosition(i + 1, j - 1));
                    }
                    else
                    {
                        if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i + 1, j - 1].GetPiece()))
                        {
                            boardPositionList.Add(new BoardPosition(i + 1, j - 1));
                        }
                    }
                }

                if (!pChessBoard.GetChessBoard().GetBoard()[i + 1, j].HasPiece())
                {
                    boardPositionList.Add(new BoardPosition(i + 1, j));
                }
                else
                {
                    if (j < 7)
                    {
                        if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i + 1, j + 1].GetPiece()))
                        {
                            boardPositionList.Add(new BoardPosition(i + 1, j + 1));
                        }
                    }
                }

                if (j <= 6)
                {
                    if (!pChessBoard.GetChessBoard().GetBoard()[i + 1, j + 1].HasPiece())
                    {
                        boardPositionList.Add(new BoardPosition(i + 1, j + 1));
                    }
                    else
                    {
                        if (!p.IsSameColor(pChessBoard.GetChessBoard().GetBoard()[i + 1, j + 1].GetPiece()))
                        {
                            boardPositionList.Add(new BoardPosition(i + 1, j + 1));
                        }
                    }
                }
            }
            return(boardPositionList);
        }