Beispiel #1
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);
        }
Beispiel #2
0
 public GameMove(ChessPosition from, ChessPosition to, FigureType who, FigureType?toWhom)
 {
     From   = from;
     To     = to;
     Who    = who;
     ToWhom = toWhom;
 }
Beispiel #3
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);
        }
Beispiel #4
0
        /// <summary>
        /// Decodes textual representation of move into object
        /// </summary>
        /// <returns>Move if it's valid, null otherwise</returns>
        private static GameMove DecodeMove(GameData game, string move)
        {
            var figure    = GetFigureForCharacter(move[0]);
            var from      = new ChessPosition(GetPositionForCharacter(move[1]), int.Parse(move[2].ToString()) - 1);
            var isCapture = move[3] == 'x';
            var to        = new ChessPosition(GetPositionForCharacter(move[4]), int.Parse(move[5].ToString()) - 1);

            var fromFigure = game.Chessboard.GetFigureOnPosition(from);
            var toFigure   = game.Chessboard.GetFigureOnPosition(to);

            if (figure != fromFigure.Type)
            {
                return(null);
            }
            if (isCapture)
            {
                if (game.Chessboard.GetFigureOnPosition(to) == null)
                {
                    return(null);
                }
            }
            else
            {
                if (game.Chessboard.GetFigureOnPosition(to) != null)
                {
                    return(null);
                }
            }

            return(new GameMove(from, to, fromFigure.Type, toFigure?.Type));
        }
Beispiel #5
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);
        }
Beispiel #6
0
 /// <summary>
 /// Checks, whether two positions correspond to the same square on chessboard
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 public static bool IsEquivalent(ChessPosition lhs, ChessPosition rhs)
 {
     return(lhs.X == rhs.X && lhs.Y == rhs.Y);
 }
Beispiel #7
0
 /// <summary>
 /// Checks, whether two positions correspond to the same square on chessboard
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 public bool IsEquivalent(ChessPosition other)
 {
     return(ChessPosition.IsEquivalent(this, other));
 }
Beispiel #8
0
 /// <summary>
 /// Returns figure on given position
 /// </summary>
 /// <param name="position">Position of demanded figure</param>
 /// <returns>Figure on position, null if there ain't no figure</returns>
 public Figure GetFigureOnPosition(ChessPosition position)
 {
     return(Figures[position.X, position.Y]);
 }
Beispiel #9
0
 /// <summary>
 /// Adds figure to chessboard
 /// </summary>
 /// <param name="figure">Added figure</param>
 /// <param name="position">Added figure position</param>
 public void AddFigure(Figure figure, ChessPosition position)
 {
     Figures[position.X, position.Y] = figure;
 }
Beispiel #10
0
 /// <summary>
 /// Performs deletion of figure on chessboard
 /// </summary>
 /// <param name="position">Position of figure to delete</param>
 public void Delete(ChessPosition position)
 {
     Figures[position.X, position.Y] = null;
 }
Beispiel #11
0
 /// <summary>
 /// Performs move of figure on chessboard
 /// </summary>
 /// <param name="from">Move origin positon</param>
 /// <param name="to">Move destination position</param>
 public void MoveTo(ChessPosition from, ChessPosition to)
 {
     Figures[to.X, to.Y]       = Figures[from.X, from.Y];
     Figures[from.X, from.Y]   = null;
     Figures[to.X, to.Y].Moved = true;
 }
Beispiel #12
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));
        }
Beispiel #13
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);
                }
            }
        }