Пример #1
0
        /// <summary>
        /// This constructor will initialize the ChessBoard as a standard chessboard.
        /// </summary>
        public ChessBoard()
        {
            board = new Piece[BOARD_SIZE, BOARD_SIZE];

            // A lot of pawns
            for (int i = 0; i < BOARD_SIZE; i++)
            {
                board[1, i] = new Pieces.Pawn(MoveDirection.Up, PieceColor.White);
                board[6, i] = new Pieces.Pawn(MoveDirection.Down, PieceColor.Black);
            }

            // Two kings
            board[0, 4] = new Pieces.King(MoveDirection.Up, PieceColor.White);
            board[7, 4] = new Pieces.King(MoveDirection.Down, PieceColor.Black);

            // Two queens
            board[0, 3] = new Pieces.Queen(MoveDirection.Up, PieceColor.White);
            board[7, 3] = new Pieces.Queen(MoveDirection.Down, PieceColor.Black);

            // All the bishops
            board[0, 2] = new Pieces.Bishop(MoveDirection.Up, PieceColor.White);
            board[0, 5] = new Pieces.Bishop(MoveDirection.Up, PieceColor.White);
            board[7, 5] = new Pieces.Bishop(MoveDirection.Down, PieceColor.Black);
            board[7, 2] = new Pieces.Bishop(MoveDirection.Down, PieceColor.Black);

            // All the knights
            board[0, 1] = new Pieces.Knight(MoveDirection.Up, PieceColor.White);
            board[0, 6] = new Pieces.Knight(MoveDirection.Up, PieceColor.White);
            board[7, 6] = new Pieces.Knight(MoveDirection.Down, PieceColor.Black);
            board[7, 1] = new Pieces.Knight(MoveDirection.Down, PieceColor.Black);

            // All the rooks
            board[0, 0] = new Pieces.Rook(MoveDirection.Up, PieceColor.White);
            board[0, 7] = new Pieces.Rook(MoveDirection.Up, PieceColor.White);
            board[7, 7] = new Pieces.Rook(MoveDirection.Down, PieceColor.Black);
            board[7, 0] = new Pieces.Rook(MoveDirection.Down, PieceColor.Black);
        }
Пример #2
0
        /// <summary>
        /// This constructor will set up the chessboard in accordance to the encoded networkBoard string.
        /// </summary>
        /// <param name="networkBoard">An encoded string representing a ChessBoard</param>
        public ChessBoard(string networkBoard)
        {
            board = new Piece[BOARD_SIZE, BOARD_SIZE];

            // All the pieces are seperated with a _
            string[] pieces = networkBoard.Split('_');
            foreach (string piece in pieces)
            {
                if (piece == "")
                {
                    continue;
                }

                string[] pieceInfo = piece.Split(',');

                // Handling numbers that are not numbers
                int x = 0;
                int y = 0;
                try
                {
                    x = int.Parse(pieceInfo[1]);
                    y = int.Parse(pieceInfo[2]);
                } catch (FormatException e)
                {
                    continue;
                }

                // No positioning outside of the board
                if (x >= BOARD_SIZE || x < 0 || y >= BOARD_SIZE || y < 0)
                {
                    continue;
                }

                PieceColor color = PieceColor.Black;
                if (pieceInfo[3] == "White")
                {
                    color = PieceColor.White;
                }

                MoveDirection direction = MoveDirection.Up;
                if (pieceInfo[4] == "Down")
                {
                    direction = MoveDirection.Down;
                }

                Piece newPiece = null;

                switch (pieceInfo[0])
                {
                case "bishop":
                    newPiece = new Pieces.Bishop(direction, color);
                    break;

                case "king":
                    newPiece = new Pieces.King(direction, color);
                    break;

                case "knight":
                    newPiece = new Pieces.Knight(direction, color);
                    break;

                case "pawn":
                    newPiece = new Pieces.Pawn(direction, color);
                    break;

                case "queen":
                    newPiece = new Pieces.Queen(direction, color);
                    break;

                case "rook":
                    newPiece = new Pieces.Rook(direction, color);
                    break;

                default:
                    Console.WriteLine("Unknown piece " + piece);
                    continue;
                    break;
                }

                board[y, x] = newPiece;
            }
        }