Exemple #1
0
        private bool CastleMovement(GameMoveEntity movement, GameStateEntity state)
        {
            var   king       = state.GameBoard.GetPieceAt(movement.CurrentPos);
            var   rook       = state.GameBoard.GetPieceAt(movement.RequestedPos);
            Point newKingPos = movement.RequestedPos;

            if (movement.RequestedPos.X == 0 && movement.RequestedPos.Y == 0)
            {
                newKingPos = new Point(1, 0);
            }
            else if (movement.RequestedPos.X == 7 && movement.RequestedPos.Y == 0)
            {
                newKingPos = new Point(5, 0);
            }
            else if (movement.RequestedPos.X == 0 && movement.RequestedPos.Y == 7)
            {
                newKingPos = new Point(2, 7);
            }
            else if (movement.RequestedPos.X == 7 && movement.RequestedPos.Y == 7)
            {
                newKingPos = new Point(6, 7);
            }

            GameStateEntity mockState = state.Clone();

            mockState.GameBoard.PlacePieceAt(newKingPos, mockState.GameBoard.GetPieceAt(movement.CurrentPos));
            mockState.GameBoard.PlacePieceAt(movement.CurrentPos, new GamePiece(PieceType.None, Color.None));

            return(!king.HasMoved &&
                   !rook.HasMoved &&
                   rook.Type == PieceType.Rook &&
                   Utilities.PathIsClear(movement, state.GameBoard) &&
                   Utilities.StepOnOwnPiece(movement, state) &&
                   !Utilities.CheckedAfterCastling(mockState, mockState.ActivePlayer));
        }
Exemple #2
0
        public static bool IsDiagonal(GameMoveEntity movement, GameStateEntity state)
        {
            int deltaX = Math.Abs(movement.RequestedPos.X - movement.CurrentPos.X);
            int deltaY = Math.Abs(movement.RequestedPos.Y - movement.CurrentPos.Y);

            return(deltaX == deltaY);
        }
Exemple #3
0
        public static bool CheckedAfterCastling(GameStateEntity state, Color kingColor)
        {
            var   board = state.GameBoard;
            Point king  = FindKing(state, kingColor);

            for (int y = 0; y < board.Width(); y++)
            {
                for (int x = 0; x < board.Width(); x++)
                {
                    var type  = board.GetPieceAt(new Point(x, y)).Type;
                    var color = board.GetPieceAt(new Point(x, y)).Color;
                    if ((type == PieceType.Bishop || type == PieceType.Queen) && color != state.ActivePlayer)
                    {
                        var potentialThreat = new GameMoveEntity(type, new Point(x, y), new Point(king.X, king.Y), state.ActivePlayer);
                        if (Utilities.IsDiagonal(potentialThreat, state) && Utilities.PathIsClear(potentialThreat, board))
                        {
                            return(true);
                        }
                    }
                    if ((type == PieceType.Rook || type == PieceType.Queen) && color != state.ActivePlayer)
                    {
                        Console.WriteLine("INNE I ROOK");
                        var potentialThreat = new GameMoveEntity(type, new Point(x, y), new Point(king.X, king.Y), state.ActivePlayer);
                        if (Utilities.IsLinear(potentialThreat, state) && Utilities.PathIsClear(potentialThreat, board))
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemple #4
0
        private GameMoveEntity SetColorAndType(GameMoveEntity movement, GameStateEntity state)
        {
            movement.Color = state.GameBoard.GetPieceAt(movement.CurrentPos).Color;
            movement.Type  = state.GameBoard.GetPieceAt(movement.CurrentPos).Type;

            return(movement);
        }
Exemple #5
0
        public bool KingIsChecked(GameStateEntity state, Color kingColor)
        {
            Console.WriteLine("Active player: " + state.ActivePlayer);
            Console.WriteLine("King color: " + kingColor);
            var board         = state.GameBoard;
            var kingPos       = Utilities.FindKing(state, kingColor);
            var opponentColor = kingColor == Color.White ? Color.Black : Color.White;

            for (int y = 0; y < board.Width(); y++)
            {
                for (int x = 0; x < board.Width(); x++)
                {
                    var piece = board.GetPieceAt(new Point(x, y));

                    if (piece.Color == opponentColor)
                    {
                        GameMoveEntity moveToKing = new GameMoveEntity(piece.Type, new Point(x, y), kingPos, piece.Color);

                        if (rules.All(rule => rule.IsValid(moveToKing, state)))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #6
0
        private GameStateEntity ExecuteMove(GameMoveEntity movement, GameStateEntity state)
        {
            bool hasMoved      = true;
            var  movedPiece    = state.GameBoard.GetPieceAt(movement.CurrentPos);
            var  target        = state.GameBoard.GetPieceAt(movement.RequestedPos);
            var  opponentColor = state.ActivePlayer == Color.White ? Color.Black : Color.White;

            if (Castling(movedPiece, target))
            {
                state = PerformCastling(movement, state);
            }
            else
            {
                state.GameBoard.PlacePieceAt(movement.CurrentPos, new GamePiece(PieceType.None, Color.None));
                state.GameBoard.PlacePieceAt(movement.RequestedPos, new GamePiece(movement.Type, movement.Color, hasMoved));
            }
            if (ruleBook.KingIsChecked(state, opponentColor))
            {
                if (Checkmate(state))
                {
                    state.Winner = state.ActivePlayer;
                }
                state.KingIsChecked = true;
                return(state);
            }

            state.KingIsChecked = false;
            return(state);
        }
Exemple #7
0
        public bool IsValid(GameMoveEntity movement, GameStateEntity state)
        {
            if (!movement.Type.Equals(PieceType.King))
            {
                return(true);
            }

            return(CastleMovement(movement, state) || NormalMovement(movement, state));
        }
Exemple #8
0
 public bool IsValid(GameMoveEntity movement, GameStateEntity state)
 {
     if (!movement.Type.Equals(PieceType.Bishop))
     {
         return(true);
     }
     if (Utilities.StepOnOwnPiece(movement, state))
     {
         return(false);
     }
     return(Utilities.IsDiagonal(movement, state) && Utilities.PathIsClear(movement, state.GameBoard));
 }
Exemple #9
0
        private bool NormalMovement(GameMoveEntity movement, GameStateEntity state)
        {
            if (Utilities.StepOnOwnPiece(movement, state))
            {
                return(false);
            }

            int deltaX = Math.Abs(movement.RequestedPos.X - movement.CurrentPos.X);
            int deltaY = Math.Abs(movement.RequestedPos.Y - movement.CurrentPos.Y);

            return(deltaX < 2 && deltaY < 2);
        }
Exemple #10
0
 public bool IsValid(GameMoveEntity movement, GameStateEntity state)
 {
     if (movement.Type != PieceType.Pawn)
     {
         return(true);
     }
     if (Utilities.StepOnOwnPiece(movement, state))
     {
         return(false);
     }
     return(NormalMovement(movement, state) ||
            ChargeMovement(movement, state) ||
            AttackMovement(movement, state));
 }
Exemple #11
0
        private bool AttackMovement(GameMoveEntity movement, GameStateEntity state)
        {
            int deltaX = Math.Abs(movement.RequestedPos.X - movement.CurrentPos.X);
            int deltaY = movement.RequestedPos.Y - movement.CurrentPos.Y;
            var target = state.GameBoard.GetPieceAt(movement.RequestedPos);

            if (movement.Color == Color.White)
            {
                return(deltaX == 1 && deltaY == -1 && target.Color == Color.Black);
            }
            else
            {
                return(deltaX == 1 && deltaY == 1 && target.Color == Color.White);
            }
        }
Exemple #12
0
        public bool IsValid(GameMoveEntity movement, GameStateEntity state)
        {
            if (!movement.Type.Equals(PieceType.Knight))
            {
                return(true);
            }
            if (Utilities.StepOnOwnPiece(movement, state))
            {
                return(false);
            }

            int deltaX = Math.Abs(movement.RequestedPos.X - movement.CurrentPos.X);
            int deltaY = Math.Abs(movement.RequestedPos.Y - movement.CurrentPos.Y);

            return(deltaX == 2 && deltaY == 1 || deltaX == 1 && deltaY == 2);
        }
Exemple #13
0
        private GameStateEntity CheckForPromotion(GameMoveEntity movement, GameStateEntity state)
        {
            if (movement.Type == PieceType.Pawn)
            {
                if (state.ActivePlayer == Color.White)
                {
                    state.PawnIsPromoted = movement.RequestedPos.Y == 0;
                }
                else
                {
                    state.PawnIsPromoted = movement.RequestedPos.Y == state.GameBoard.Width() - 1;
                }
            }

            return(state);
        }
Exemple #14
0
        public bool MoveIsValid(GameMoveEntity movement, GameStateEntity state)
        {
            bool moveIsAllowed = rules.All(rule => rule.IsValid(movement, state));
            bool kingIsChecked = false;

            if (moveIsAllowed)
            {
                var clonedState = state.Clone();

                clonedState.GameBoard.PlacePieceAt(movement.RequestedPos, new GamePiece(movement.Type, movement.Color));
                clonedState.GameBoard.PlacePieceAt(movement.CurrentPos, new GamePiece(PieceType.None, Color.None));
                clonedState.ActivePlayer = state.ActivePlayer == Color.White ? Color.Black : Color.White;
                kingIsChecked            = KingIsChecked(clonedState, state.ActivePlayer);
            }

            return(moveIsAllowed && !kingIsChecked);
        }
Exemple #15
0
        private bool NormalMovement(GameMoveEntity movement, GameStateEntity state)
        {
            bool destinationEmpty = state.GameBoard.GetPieceAt(movement.RequestedPos).Type == PieceType.None;
            int  deltaX           = movement.RequestedPos.X - movement.CurrentPos.X;
            int  deltaY           = movement.RequestedPos.Y - movement.CurrentPos.Y;

            if (!destinationEmpty || deltaX != 0)
            {
                return(false);
            }
            if (movement.Color == Color.White)
            {
                return(deltaY == -1);
            }
            else
            {
                return(deltaY == 1);
            }
        }
Exemple #16
0
        private bool ChargeMovement(GameMoveEntity movement, GameStateEntity state)
        {
            int  deltaX           = Math.Abs(movement.RequestedPos.X - movement.CurrentPos.X);
            int  deltaY           = movement.RequestedPos.Y - movement.CurrentPos.Y;
            var  gamePiece        = state.GameBoard.GetPieceAt(movement.CurrentPos);
            bool destinationEmpty = state.GameBoard.GetPieceAt(movement.RequestedPos).Type == PieceType.None;

            if (deltaX != 0 || !Utilities.PathIsClear(movement, state.GameBoard) || !destinationEmpty)
            {
                return(false);
            }
            if (movement.Color == Color.White)
            {
                return(deltaX == 0 && deltaY == -2 && !gamePiece.HasMoved);
            }
            else
            {
                return(deltaX == 0 && deltaY == 2 && !gamePiece.HasMoved);
            }
        }
Exemple #17
0
        public static bool PathIsClear(GameMoveEntity movement, IGameBoard board)
        {
            int deltaX = movement.RequestedPos.X - movement.CurrentPos.X;
            int deltaY = movement.RequestedPos.Y - movement.CurrentPos.Y;
            int stepX  = deltaX == 0 ? 0 : deltaX / System.Math.Abs(deltaX);
            int stepY  = deltaY == 0 ? 0 : deltaY / System.Math.Abs(deltaY);
            int currX  = movement.CurrentPos.X;
            int currY  = movement.CurrentPos.Y;

            for (int i = 1; i < Math.Max(Math.Abs(deltaX), Math.Abs(deltaY)); i++)
            {
                var piece = board.GetPieceAt(new Point(currX + i * stepX, currY + i * stepY));
                if (piece.Type != PieceType.None)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #18
0
        private GameStateEntity PerformCastling(GameMoveEntity piece, GameStateEntity state)
        {
            var hasMoved = true;

            if (piece.Color == Color.White)
            {
                if (piece.RequestedPos.X == 7)
                {
                    state.GameBoard.PlacePieceAt(new Point(7, 7), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(4, 7), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(6, 7), new GamePiece(PieceType.King, Color.White, hasMoved));
                    state.GameBoard.PlacePieceAt(new Point(5, 7), new GamePiece(PieceType.Rook, Color.White, hasMoved));
                }
                else
                {
                    state.GameBoard.PlacePieceAt(new Point(0, 7), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(4, 7), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(2, 7), new GamePiece(PieceType.King, Color.White, hasMoved));
                    state.GameBoard.PlacePieceAt(new Point(3, 7), new GamePiece(PieceType.Rook, Color.White, hasMoved));
                }
            }
            else
            {
                if (piece.RequestedPos.X == 7)
                {
                    state.GameBoard.PlacePieceAt(new Point(7, 0), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(3, 0), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(5, 0), new GamePiece(PieceType.King, Color.Black, hasMoved));
                    state.GameBoard.PlacePieceAt(new Point(4, 0), new GamePiece(PieceType.Rook, Color.Black, hasMoved));
                }
                else
                {
                    state.GameBoard.PlacePieceAt(new Point(0, 0), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(3, 0), new GamePiece(PieceType.None, Color.None));
                    state.GameBoard.PlacePieceAt(new Point(1, 0), new GamePiece(PieceType.King, Color.Black, hasMoved));
                    state.GameBoard.PlacePieceAt(new Point(2, 0), new GamePiece(PieceType.Rook, Color.Black, hasMoved));
                }
            }
            return(state);
        }
Exemple #19
0
        private List <Point> FindPiecesThatReachTarget(GameStateEntity state, Point target, Color playerColor)
        {
            var piecesThatReachTarget = new List <Point>();
            var board = state.GameBoard;

            for (int y = 0; y < board.Width(); y++)
            {
                for (int x = 0; x < board.Width(); x++)
                {
                    var piece = board.GetPieceAt(new Point(x, y));
                    if (piece.Color == playerColor)
                    {
                        GameMoveEntity move = new GameMoveEntity(piece.Type, new Point(x, y), target, piece.Color);
                        if (ruleBook.MoveIsValid(move, state))
                        {
                            piecesThatReachTarget.Add(new Point(x, y));
                        }
                    }
                }
            }
            return(piecesThatReachTarget);
        }
Exemple #20
0
        public GameStateEntity MovePiece(GameMoveEntity movement)
        {
            try
            {
                var gameState = database.GetState();
                movement = SetColorAndType(movement, gameState);

                if (ruleBook.MoveIsValid(movement, gameState))
                {
                    gameState = ExecuteMove(movement, gameState);
                    gameState = CheckForPromotion(movement, gameState);
                    gameState = ChangeActivePlayer(gameState);
                    database.SaveState(gameState);

                    return(database.GetState());
                }

                return(gameState);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #21
0
        public GameStateEntity TransformPiece(GameMoveEntity piece)
        {
            try
            {
                var state = database.GetState();
                state.GameBoard.PlacePieceAt(piece.RequestedPos, new GamePiece(piece.Type, piece.Color));
                state.PawnIsPromoted = false;

                // We need to swap active player when checking if king is checked
                // since the rules mandate that you can only move your own piece
                Color opponentColor = state.ActivePlayer;
                state.ActivePlayer  = state.ActivePlayer == Color.White ? Color.Black : Color.White;
                state.KingIsChecked = ruleBook.KingIsChecked(state, opponentColor);
                state.ActivePlayer  = state.ActivePlayer == Color.White ? Color.Black : Color.White;

                database.SaveState(state);

                return(database.GetState());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #22
0
 public static bool StepOnOwnPiece(GameMoveEntity movement, GameStateEntity state)
 {
     return(state.GameBoard.GetPieceAt(movement.RequestedPos).Color == movement.Color);
 }
Exemple #23
0
 public static bool IsLinear(GameMoveEntity movement, GameStateEntity state)
 {
     return(movement.RequestedPos.X == movement.CurrentPos.X ||
            movement.RequestedPos.Y == movement.CurrentPos.Y);
 }
Exemple #24
0
 public bool IsValid(GameMoveEntity movement, GameStateEntity state)
 {
     return(movement.Color == state.ActivePlayer);
 }