Beispiel #1
0
            public Square(ChessGameState game, Square previous)
            {
                Game = game;

                if (previous.Piece != null)
                {
                    Piece = new SinglePiece(previous.Piece);
                }

                LastUpdate = previous.LastUpdate;
            }
Beispiel #2
0
        public ChessGameState GetNextMove(int x1, int y1, int x2, int y2)
        {
            ChessGameState toReturn = null;

            if (squareInBoard(x1, y1) &&
                squareInBoard(x2, y2))
            {
                toReturn = this[x1, y1].PossibleMoves.FirstOrDefault(
                    (elem) => elem.MovedTo.X == x2 && elem.MovedTo.Y == y2);
            }

            return(toReturn);
        }
Beispiel #3
0
        private ChessGameState(ChessGameState previous)
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    board[i, j] = new Square(this, previous.board[i, j]);
                }
            }

            WhiteChecked = new Lazy <bool>(() => CheckForCheck(Color.White));
            BlackChecked = new Lazy <bool>(() => CheckForCheck(Color.Black));
        }
Beispiel #4
0
        private ChessGameState generateStateForNextMove(Pair movedTo)
        {
            ChessGameState newGameState = new ChessGameState(this);

            if (Tour == Color.White)
            {
                newGameState.Tour = Color.Black;
            }
            else
            {
                newGameState.Tour = Color.White;
            }

            newGameState.TourNumber = TourNumber + 1;

            newGameState.MovedTo = movedTo;

            return(newGameState);
        }
Beispiel #5
0
        /// <summary>
        /// Compute all possible moves for exact square.
        /// </summary>
        private void ComputePossibleMoves(int column, int row)
        {
            Action <int, int> moveDiagonaly = (int x, int y) =>
            {
                int c = column + x;
                int r = row + y;

                while (squareInBoard(c, r) &&
                       this[c, r].Piece == null)
                {
                    ChessGameState newState = generateStateForNextMove(new Pair(c, r));
                    newState[c, r].Piece        = newState[column, row].Piece;
                    newState[column, row].Piece = null;
                    if (!newState.IsChecked(Tour))
                    {
                        this[column, row].PossibleMoves.Add(newState);
                    }

                    c += x;
                    r += y;
                }

                if (squareInBoard(c, r) &&
                    this[c, r].Piece != null &&
                    this[c, r].Piece.Color != Tour)
                {
                    ChessGameState newState = generateStateForNextMove(new Pair(c, r));
                    newState[c, r].Piece        = newState[column, row].Piece;
                    newState[column, row].Piece = null;
                    if (!newState.IsChecked(Tour))
                    {
                        this[column, row].PossibleMoves.Add(newState);
                    }
                }
            };

            Action <int, int> moveJump = (int x, int y) =>
            {
                int c = column + x;
                int r = row + y;

                if (squareInBoard(c, r) &&
                    ((this[c, r].Piece != null &&
                      this[c, r].Piece.Color != Tour) ||
                     this[c, r].Piece == null))
                {
                    ChessGameState newState = generateStateForNextMove(new Pair(c, r));
                    newState[c, r].Piece        = newState[column, row].Piece;
                    newState[column, row].Piece = null;
                    if (!newState.IsChecked(Tour))
                    {
                        this[column, row].PossibleMoves.Add(newState);
                    }
                }
            };

            switch (this[column, row].Piece.Piece)
            {
            case PieceType.Pawn:
            {
                int direction = 0;

                if (Tour == Color.White)
                {
                    direction = 1;
                }
                else
                {
                    direction = -1;
                }

                Action <PieceType> pawnPromote = (PieceType toPiece) =>
                {
                    if (!squareInBoard(column, row + direction))
                    {
                        ChessGameState newState = generateStateForNextMove(new Pair(column, row));
                        newState[column, row].Piece.Piece = toPiece;
                        newState[column, row].LastUpdate  = newState.TourNumber;
                        if (!newState.IsChecked(Tour))
                        {
                            this[column, row].PossibleMoves.Add(newState);
                        }
                    }
                };

                Func <int, bool> pawnHasEnemyOnDiagonal = (int side) =>
                {
                    return
                        (squareInBoard(column + side, row + direction) &&
                         this[column + side, row + direction].Piece != null &&
                         this[column + side, row + direction].Piece.Color != Tour);
                };

                Action <int> pawnBeat = (int side) =>
                {
                    if (pawnHasEnemyOnDiagonal(side))
                    {
                        ChessGameState newState = generateStateForNextMove(new Pair(column + side, row + direction));
                        newState[column + side, row + direction].Piece = newState[column, row].Piece;
                        newState[column, row].Piece = null;
                        if (!newState.IsChecked(Tour))
                        {
                            this[column, row].PossibleMoves.Add(newState);
                        }
                    }
                };

                Action pawnMove = () =>
                {
                    if (pawnHasEnemyOnDiagonal(1) ||
                        pawnHasEnemyOnDiagonal(-1))
                    {
                        return;
                    }

                    if (squareInBoard(column, row + direction) &&
                        this[column, row + direction].Piece == null)
                    {
                        ChessGameState newState = generateStateForNextMove(new Pair(column, row + direction));
                        newState[column, row + direction].Piece = newState[column, row].Piece;
                        newState[column, row].Piece             = null;
                        if (!newState.IsChecked(Tour))
                        {
                            this[column, row].PossibleMoves.Add(newState);
                        }

                        // Double move
                        if (this[column, row].LastUpdate == 0 &&
                            squareInBoard(column, row + 2 * direction) &&
                            this[column, row + 2 * direction].Piece == null)
                        {
                            ChessGameState newState2 = generateStateForNextMove(new Pair(column, row + 2 * direction));
                            newState2[column, row + 2 * direction].Piece = newState2[column, row].Piece;
                            newState2[column, row].Piece = null;
                            if (!newState2.IsChecked(Tour))
                            {
                                this[column, row].PossibleMoves.Add(newState2);
                            }
                        }
                    }
                };

                Action <int> pawnPassat = (int side) =>
                {
                    if (squareInBoard(column + side, row) &&
                        this[column + side, row].Piece != null &&
                        this[column + side, row].Piece.Piece == PieceType.Pawn &&
                        this[column + side, row].Piece.Color != Tour &&
                        this[column + side, row].LastUpdate == TourNumber &&
                        squareInBoard(column + side, row + 2 * direction) &&
                        this[column + side, row + 2 * direction].Piece == null &&
                        this[column + side, row + 2 * direction].LastUpdate == TourNumber)
                    {
                        ChessGameState newState = generateStateForNextMove(new Pair(column + side, row + direction));
                        newState[column + side, row + direction].Piece = newState[column, row].Piece;
                        newState[column, row].Piece        = null;
                        newState[column + side, row].Piece = null;
                        if (!newState.IsChecked(Tour))
                        {
                            this[column, row].PossibleMoves.Add(newState);
                        }
                    }
                };

                pawnPromote(PieceType.Queen);
                pawnPromote(PieceType.Rook);
                pawnPromote(PieceType.Knight);
                pawnPromote(PieceType.Bishop);

                pawnBeat(-1);
                pawnBeat(1);

                pawnMove();

                pawnPassat(-1);
                pawnPassat(1);

                break;
            }

            case PieceType.Rook:
            {
                moveDiagonaly(1, 0);
                moveDiagonaly(-1, 0);
                moveDiagonaly(0, 1);
                moveDiagonaly(0, -1);
                break;
            }

            case PieceType.Bishop:
            {
                moveDiagonaly(1, 1);
                moveDiagonaly(-1, 1);
                moveDiagonaly(1, -1);
                moveDiagonaly(-1, -1);
                break;
            }

            case PieceType.Queen:
            {
                moveDiagonaly(1, 0);
                moveDiagonaly(-1, 0);
                moveDiagonaly(0, 1);
                moveDiagonaly(0, -1);

                moveDiagonaly(1, 1);
                moveDiagonaly(-1, 1);
                moveDiagonaly(1, -1);
                moveDiagonaly(-1, -1);
                break;
            }

            case PieceType.Knight:
            {
                moveJump(1, 2);
                moveJump(2, 1);
                moveJump(2, -1);
                moveJump(1, -2);
                moveJump(-1, -2);
                moveJump(-2, -1);
                moveJump(-2, 1);
                moveJump(-1, 2);
                break;
            }

            case PieceType.King:
            {
                Action castlingLeft = () =>
                {
                    if (this[column, row].LastUpdate == 0 &&
                        this[0, row].Piece != null &&
                        this[0, row].LastUpdate == 0 &&
                        this[1, row].Piece == null &&
                        this[2, row].Piece == null &&
                        this[3, row].Piece == null)
                    {
                        ChessGameState newState = generateStateForNextMove(new Pair(2, row));
                        newState[2, row].Piece = newState[4, row].Piece;
                        newState[4, row].Piece = null;
                        ChessGameState newCrossState = generateStateForNextMove(new Pair(3, row));
                        newState[3, row].Piece = newState[4, row].Piece;
                        newState[3, row].Piece = null;
                        if (!this.IsChecked(Tour) &&
                            !newState.IsChecked(Tour) &&
                            !newCrossState.IsChecked(Tour))
                        {
                            newState[3, row].Piece = newState[0, row].Piece;
                            newState[0, row].Piece = null;

                            this[column, row].PossibleMoves.Add(newState);
                        }
                    }
                };

                Action castlingRight = () =>
                {
                    if (this[column, row].LastUpdate == 0 &&
                        this[7, row].Piece != null &&
                        this[7, row].LastUpdate == 0 &&
                        this[5, row].Piece == null &&
                        this[6, row].Piece == null)
                    {
                        ChessGameState newState = generateStateForNextMove(new Pair(6, row));
                        newState[6, row].Piece = newState[4, row].Piece;
                        newState[4, row].Piece = null;
                        ChessGameState newCrossState = generateStateForNextMove(new Pair(5, row));
                        newState[5, row].Piece = newState[4, row].Piece;
                        newState[4, row].Piece = null;
                        if (!this.IsChecked(Tour) &&
                            !newState.IsChecked(Tour) &&
                            !newCrossState.IsChecked(Tour))
                        {
                            newState[5, row].Piece = newState[7, row].Piece;
                            newState[7, row].Piece = null;

                            this[column, row].PossibleMoves.Add(newState);
                        }
                    }
                };

                moveJump(1, 1);
                moveJump(1, 0);
                moveJump(1, -1);
                moveJump(0, -1);
                moveJump(-1, -1);
                moveJump(-1, 0);
                moveJump(-1, 1);
                moveJump(0, 1);
                castlingLeft();
                castlingRight();
                break;
            }
            }
        }
Beispiel #6
0
            public Square(ChessGameState game)
            {
                Game = game;

                LastUpdate = 0;
            }