Exemplo n.º 1
0
        /// <summary>
        /// Gets moves in direction of vector, until figure of playing player is encountered, or positions ain't valid
        /// </summary>
        /// <param name="playerColor">Capturing player</param>
        /// <param name="from">Position to move from</param>
        /// <param name="vector">Vector to move to</param>
        private static List <GameMove> CanMoveOrAttackIterative(ChessboardModel chessboard, PlayerColor playerColor, ChessPosition from, GameMoveVector vector)
        {
            var acumulator = new List <GameMove>();

            var temp = from;

            while (true)
            {
                temp = temp.Add(vector);

                var attack = CanAttack(chessboard, playerColor, from, temp);
                var move   = CanMove(chessboard, playerColor, from, temp);

                acumulator.Add(move);
                acumulator.Add(attack);

                if (attack != null)
                {
                    return(acumulator);
                }

                if (move == null)
                {
                    return(acumulator);
                }
            }
        }
Exemplo n.º 2
0
 public GameData(ChessboardModel chessboard, PlayerColor playerOnMove, GameState endState)
 {
     Chessboard   = chessboard;
     PlayerOnMove = playerOnMove;
     EndState     = endState;
     RecordOfGame = new List <string>();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets all possible moves of pawns of given player
        /// </summary>
        private static List <GameMove> GetMovesForPawns(ChessboardModel chessboard, PlayerColor playerColor)
        {
            var acumulator = new List <GameMove>();

            var moveVector     = playerColor == PlayerColor.White ? new GameMoveVector(0, 1) : new GameMoveVector(0, -1);
            var captureVector1 = playerColor == PlayerColor.White ? new GameMoveVector(1, 1) : new GameMoveVector(1, -1);
            var captureVector2 = playerColor == PlayerColor.White ? new GameMoveVector(-1, 1) : new GameMoveVector(-1, -1);

            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    var position = new ChessPosition(x, y);
                    var figure   = chessboard.GetFigureOnPosition(position);
                    if (
                        figure != null &&
                        figure.Type == FigureType.Pawn &&
                        figure.Color == playerColor
                        )
                    {
                        acumulator.Add(CanAttack(chessboard, playerColor, position, position.Add(captureVector1)));
                        acumulator.Add(CanAttack(chessboard, playerColor, position, position.Add(captureVector2)));
                        var moveOnce = CanMove(chessboard, playerColor, position, position.Add(moveVector));
                        acumulator.Add(moveOnce);
                        if (moveOnce != null && !figure.Moved)
                        {
                            acumulator.Add(CanMove(chessboard, playerColor, position, moveOnce.To.Add(moveVector)));
                        }
                    }
                }
            }

            acumulator.RemoveAll(x => x == null);
            return(acumulator);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets all possible moves of bishops of given player
        /// </summary>
        private static List <GameMove> GetMovesForBishops(ChessboardModel chessboard, PlayerColor playerColor)
        {
            var acumulator = new List <GameMove>();

            var upLeft    = new GameMoveVector(-1, 1);
            var upRight   = new GameMoveVector(1, 1);
            var downLeft  = new GameMoveVector(-1, -1);
            var downRight = new GameMoveVector(1, -1);

            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    var position = new ChessPosition(x, y);
                    var figure   = chessboard.GetFigureOnPosition(position);
                    if (
                        figure != null &&
                        figure.Type == FigureType.Bishop &&
                        figure.Color == playerColor
                        )
                    {
                        acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, upLeft));
                        acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, upRight));
                        acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, downLeft));
                        acumulator.AddRange(CanMoveOrAttackIterative(chessboard, playerColor, position, downRight));
                    }
                }
            }

            acumulator.RemoveAll(x => x == null);
            return(acumulator);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get all possible moves for given player
        /// </summary>
        private static List <GameMove> GetAllMoves(ChessboardModel chessboard, PlayerColor playerColor)
        {
            var acumulator = new List <GameMove>();

            acumulator.AddRange(GetMovesForPawns(chessboard, playerColor));
            acumulator.AddRange(GetMovesForKing(chessboard, playerColor));
            acumulator.AddRange(GetMovesForKnights(chessboard, playerColor));
            acumulator.AddRange(GetMovesForRooks(chessboard, playerColor));
            acumulator.AddRange(GetMovesForBishops(chessboard, playerColor));
            acumulator.AddRange(GetMovesForQueens(chessboard, playerColor));

            return(acumulator);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets move, if there is no figure in the place moving to, null otherwise
        /// </summary>
        /// <param name="playerColor">Moving player</param>
        /// <param name="from">Position to move from</param>
        /// <param name="to">Position to move to</param>
        private static GameMove CanMove(ChessboardModel chessboard, PlayerColor playerColor, ChessPosition from, ChessPosition to)
        {
            if (!to.IsValid())
            {
                return(null);
            }

            var moveTo = chessboard.GetFigureOnPosition(to);

            if (moveTo != null)
            {
                return(null);
            }

            return(new GameMove(from, to, chessboard.GetFigureOnPosition(from).Type, chessboard.GetFigureOnPosition(to)?.Type));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets all possible moves of king of given player
        /// </summary>
        private static List <GameMove> GetMovesForKing(ChessboardModel chessboard, PlayerColor playerColor)
        {
            var acumulator = new List <GameMove>();

            var up        = new GameMoveVector(0, 1);
            var left      = new GameMoveVector(-1, 0);
            var down      = new GameMoveVector(0, -1);
            var right     = new GameMoveVector(1, 0);
            var upLeft    = new GameMoveVector(-1, 1);
            var upRight   = new GameMoveVector(1, 1);
            var downLeft  = new GameMoveVector(-1, -1);
            var downRight = new GameMoveVector(1, -1);

            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    var position = new ChessPosition(x, y);
                    var figure   = chessboard.GetFigureOnPosition(position);
                    if (
                        figure != null &&
                        figure.Type == FigureType.King &&
                        figure.Color == playerColor
                        )
                    {
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(up)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(left)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(down)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(right)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(upLeft)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(upRight)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(downLeft)));
                        acumulator.Add(CanMoveOrAttack(chessboard, playerColor, position, position.Add(downRight)));
                    }
                }
            }

            acumulator.RemoveAll(x => x == null);
            return(acumulator);
        }