Ejemplo n.º 1
0
 public ChessGame()
 {
     WhoseTurn = Player.White;
     _moves = new List<Move>();
     Board = new ChessPiece[8][];
     ChessPiece kw = new ChessPiece(Piece.King, Player.White);
     ChessPiece kb = new ChessPiece(Piece.King, Player.Black);
     ChessPiece qw = new ChessPiece(Piece.Queen, Player.White);
     ChessPiece qb = new ChessPiece(Piece.Queen, Player.Black);
     ChessPiece rw = new ChessPiece(Piece.Rook, Player.White);
     ChessPiece rb = new ChessPiece(Piece.Rook, Player.Black);
     ChessPiece nw = new ChessPiece(Piece.Knight, Player.White);
     ChessPiece nb = new ChessPiece(Piece.Knight, Player.Black);
     ChessPiece bw = new ChessPiece(Piece.Bishop, Player.White);
     ChessPiece bb = new ChessPiece(Piece.Bishop, Player.Black);
     ChessPiece pw = new ChessPiece(Piece.Pawn, Player.White);
     ChessPiece pb = new ChessPiece(Piece.Pawn, Player.Black);
     ChessPiece o = ChessPiece.None;
     Board = new ChessPiece[8][]
     {
         new[] { rb, nb, bb, qb, kb, bb, nb, rb },
         new[] { pb, pb, pb, pb, pb, pb, pb, pb },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { pw, pw, pw, pw, pw, pw, pw, pw },
         new[] { rw, nw, bw, qw, kw, bw, nw, rw }
     };
 }
Ejemplo n.º 2
0
 protected static ChessPiece[][] CloneBoard(ChessPiece[][] originalBoard)
 {
     Utilities.ThrowIfNull(originalBoard, "originalBoard");
     ChessPiece[][] newBoard = new ChessPiece[originalBoard.Length][];
     for (int i = 0; i < originalBoard.Length; i++)
     {
         newBoard[i] = new ChessPiece[originalBoard[i].Length];
         Array.Copy(originalBoard[i], newBoard[i], originalBoard[i].Length);
     }
     return newBoard;
 }
Ejemplo n.º 3
0
 protected ChessGame(ChessPiece[][] board, IEnumerable<Move> moves, bool validateCheck)
 {
     if (moves == null)
         throw new ArgumentNullException("moves");
     if (moves.Count() == 0)
         throw new ArgumentException("The Count of moves has to be greater than 0.");
     Board = CloneBoard(board);
     _moves = new List<Move>(moves);
     WhoseTurn = Utilities.GetOpponentOf(_moves.Last().Player);
     foreach (Move m in _moves)
     {
         if (!_whiteKingMoved && m.Player == Player.White && m.OriginalPosition == new Position(File.E, Rank.One))
             _whiteKingMoved = true;
         if (!_blackKingMoved && m.Player == Player.Black && m.OriginalPosition == new Position(File.E, Rank.Eight))
             _blackKingMoved = true;
         if (!_whiteRookAMoved && m.Player == Player.White && m.OriginalPosition == new Position(File.A, Rank.One))
             _whiteRookAMoved = true;
         if (!_whiteRookHMoved && m.Player == Player.White && m.OriginalPosition == new Position(File.H, Rank.One))
             _whiteRookHMoved = true;
         if (!_blackRookAMoved && m.Player == Player.Black && m.OriginalPosition == new Position(File.A, Rank.Eight))
             _blackRookAMoved = true;
         if (!_blackRookHMoved && m.Player == Player.Black && m.OriginalPosition == new Position(File.H, Rank.Eight))
             _blackRookHMoved = true;
     }
     if (!validateCheck)
         return;
 }
Ejemplo n.º 4
0
 public ChessGame(ChessPiece[][] board, IEnumerable<Move> moves) :
     this(board, moves, true)
 {
 }
Ejemplo n.º 5
0
 protected virtual void SetPieceAt(File file, Rank rank, ChessPiece piece)
 {
     Utilities.ThrowIfNull(piece, "piece");
     Board[(int)rank][(int)file] = piece;
 }
Ejemplo n.º 6
0
        protected virtual bool ApplyMove(Move move, bool alreadyValidated, bool validateHasAnyValidMoves)
        {
            Utilities.ThrowIfNull(move, "move");
            if (!alreadyValidated && !IsValidMove(move))
                return false;
            ChessPiece movingPiece = GetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank);
            ChessPiece newPiece = movingPiece;
            if (movingPiece.Piece == Piece.Pawn)
            {
                PositionDistance pd = new PositionDistance(move.OriginalPosition, move.NewPosition);
                if (pd.DistanceX == 1 && pd.DistanceY == 1 && GetPieceAt(move.NewPosition).Piece == Piece.None)
                { // en passant
                    SetPieceAt(move.NewPosition.File, move.OriginalPosition.Rank, ChessPiece.None);
                }
                if (move.NewPosition.Rank == (move.Player == Player.White ? Rank.Eight : Rank.One))
                {
                    newPiece = new ChessPiece(move.Promotion, move.Player);
                }
            }
            else if (movingPiece.Piece == Piece.King)
            {
                if (movingPiece.Player == Player.White)
                    _whiteKingMoved = true;
                else
                    _blackKingMoved = true;

                if (new PositionDistance(move.OriginalPosition, move.NewPosition).DistanceX == 2)
                {
                    Rank rank = move.Player == Player.White ? Rank.One : Rank.Eight;
                    File rookFile = move.NewPosition.File == File.C ? File.A : File.H;
                    File newRookFile = move.NewPosition.File == File.C ? File.D : File.F;
                    SetPieceAt(newRookFile, rank, new ChessPiece(Piece.Rook, move.Player));
                    SetPieceAt(rookFile, rank, ChessPiece.None);
                }
            }
            else if (movingPiece.Piece == Piece.Rook)
            {
                if (move.Player == Player.White)
                {
                    if (move.OriginalPosition.File == File.A && move.OriginalPosition.Rank == Rank.One)
                        _whiteRookAMoved = true;
                    else if (move.OriginalPosition.File == File.H && move.OriginalPosition.Rank == Rank.One)
                        _whiteRookHMoved = true;
                }
                else
                {
                    if (move.OriginalPosition.File == File.A && move.OriginalPosition.Rank == Rank.Eight)
                        _blackRookAMoved = true;
                    else if (move.OriginalPosition.File == File.H && move.OriginalPosition.Rank == Rank.Eight)
                        _blackRookHMoved = true;
                }
            }
            SetPieceAt(move.NewPosition.File, move.NewPosition.Rank, newPiece);
            SetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank, ChessPiece.None);
            WhoseTurn = Utilities.GetOpponentOf(move.Player);
            _moves.Add(move);
            return true;
        }
Ejemplo n.º 7
0
 protected ChessGame(ChessPiece[][] board, Player whoseTurn, bool validateCheck)
 {
     Board = CloneBoard(board);
     _moves = new List<Move>();
     WhoseTurn = whoseTurn;
     ChessPiece e1 = GetPieceAt(File.E, Rank.One);
     ChessPiece e8 = GetPieceAt(File.E, Rank.Eight);
     ChessPiece a1 = GetPieceAt(File.A, Rank.One);
     ChessPiece h1 = GetPieceAt(File.H, Rank.One);
     ChessPiece a8 = GetPieceAt(File.A, Rank.Eight);
     ChessPiece h8 = GetPieceAt(File.H, Rank.Eight);
     if (e1.Piece != Piece.King || e1.Player != Player.White)
         _whiteKingMoved = true;
     if (e8.Piece != Piece.King || e8.Player != Player.Black)
         _blackKingMoved = true;
     if (a1.Piece != Piece.Rook || a1.Player != Player.White)
         _whiteRookAMoved = true;
     if (h1.Piece != Piece.Rook || h1.Player != Player.White)
         _whiteRookHMoved = true;
     if (a8.Piece != Piece.Rook || a8.Player != Player.Black)
         _blackRookAMoved = true;
     if (h8.Piece != Piece.Rook || h8.Player != Player.Black)
         _blackRookHMoved = true;
     if (!validateCheck)
         return;
 }
Ejemplo n.º 8
0
 public ChessGame(ChessPiece[][] board, Player whoseTurn) :
     this(board, whoseTurn, true)
 {
 }
Ejemplo n.º 9
0
        protected virtual bool ApplyMove(Move move, bool alreadyValidated, bool validateHasAnyValidMoves)
        {
            Utilities.ThrowIfNull(move, "move");
            if (!alreadyValidated && !IsValidMove(move))
            {
                return(false);
            }
            ChessPiece movingPiece = GetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank);
            ChessPiece newPiece    = movingPiece;

            if (movingPiece.Piece == Piece.Pawn)
            {
                PositionDistance pd = new PositionDistance(move.OriginalPosition, move.NewPosition);
                if (pd.DistanceX == 1 && pd.DistanceY == 1 && GetPieceAt(move.NewPosition).Piece == Piece.None)
                { // en passant
                    SetPieceAt(move.NewPosition.File, move.OriginalPosition.Rank, ChessPiece.None);
                }
                if (move.NewPosition.Rank == (move.Player == Player.White ? Rank.Eight : Rank.One))
                {
                    newPiece = new ChessPiece(move.Promotion, move.Player);
                }
            }
            else if (movingPiece.Piece == Piece.King)
            {
                if (movingPiece.Player == Player.White)
                {
                    _whiteKingMoved = true;
                }
                else
                {
                    _blackKingMoved = true;
                }

                if (new PositionDistance(move.OriginalPosition, move.NewPosition).DistanceX == 2)
                {
                    Rank rank        = move.Player == Player.White ? Rank.One : Rank.Eight;
                    File rookFile    = move.NewPosition.File == File.C ? File.A : File.H;
                    File newRookFile = move.NewPosition.File == File.C ? File.D : File.F;
                    SetPieceAt(newRookFile, rank, new ChessPiece(Piece.Rook, move.Player));
                    SetPieceAt(rookFile, rank, ChessPiece.None);
                }
            }
            else if (movingPiece.Piece == Piece.Rook)
            {
                if (move.Player == Player.White)
                {
                    if (move.OriginalPosition.File == File.A && move.OriginalPosition.Rank == Rank.One)
                    {
                        _whiteRookAMoved = true;
                    }
                    else if (move.OriginalPosition.File == File.H && move.OriginalPosition.Rank == Rank.One)
                    {
                        _whiteRookHMoved = true;
                    }
                }
                else
                {
                    if (move.OriginalPosition.File == File.A && move.OriginalPosition.Rank == Rank.Eight)
                    {
                        _blackRookAMoved = true;
                    }
                    else if (move.OriginalPosition.File == File.H && move.OriginalPosition.Rank == Rank.Eight)
                    {
                        _blackRookHMoved = true;
                    }
                }
            }
            SetPieceAt(move.NewPosition.File, move.NewPosition.Rank, newPiece);
            SetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank, ChessPiece.None);
            WhoseTurn = Utilities.GetOpponentOf(move.Player);
            _moves.Add(move);
            return(true);
        }
Ejemplo n.º 10
0
        protected virtual bool IsValidMove(Move move, bool validateCheck)
        {
            Utilities.ThrowIfNull(move, "move");
            if (move.OriginalPosition.Equals(move.NewPosition))
            {
                return(false);
            }
            ChessPiece piece = GetPieceAt(move.OriginalPosition.File, move.OriginalPosition.Rank);

            if (move.Player != WhoseTurn)
            {
                return(false);
            }
            if (piece.Player != move.Player)
            {
                return(false);
            }
            if (GetPieceAt(move.NewPosition).Player == move.Player)
            {
                return(false);
            }
            switch (piece.Piece)
            {
            case Piece.King:
                if (!IsValidMoveKing(move))
                {
                    return(false);
                }
                break;

            case Piece.Pawn:
                if (!IsValidMovePawn(move))
                {
                    return(false);
                }
                break;

            case Piece.Queen:
                if (!IsValidMoveQueen(move))
                {
                    return(false);
                }
                break;

            case Piece.Rook:
                if (!IsValidMoveRook(move))
                {
                    return(false);
                }
                break;

            case Piece.Bishop:
                if (!IsValidMoveBishop(move))
                {
                    return(false);
                }
                break;

            case Piece.Knight:
                if (!IsValidMoveKnight(move))
                {
                    return(false);
                }
                break;

            default:
                return(false);
            }
            if (validateCheck && WouldBeInCheckAfter(move, move.Player))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 11
0
 protected virtual void SetPieceAt(File file, Rank rank, ChessPiece piece)
 {
     Utilities.ThrowIfNull(piece, "piece");
     Board[(int)rank][(int)file] = piece;
 }