コード例 #1
0
ファイル: MoveValidator.cs プロジェクト: kvam/Kvam.Chess
        private IEnumerable<Move> CalculatePawnMoves()
        {
            var direction = Turn == "w" ? 1 : -1;

            var pawns = Pieces.Where(x => x.Value.StartsWith(Turn) && x.Value.EndsWith("p"))
                               .Select(x => ConvertFromChessCoordinateToXy(x.Key)).ToList();

            Func<int, bool> isInStartPosition = yPosition =>
                                                            {
                                                                if (Turn == "w" && yPosition == 1) return true;
                                                                return Turn == "b" && yPosition == 6;
                                                            };

            var moves = new List<Move>();
            foreach (var piece in pawns)
            {
                for (var i = 1; i <= 2; ++i)
                {
                    var move = new Move(piece.X, piece.Y, piece.X, piece.Y + i * direction);

                    if (!IsInside(move) || Board[move.X2, move.Y2] != null) break;

                    moves.Add(move);

                    if (!isInStartPosition(piece.Y)) break;

                }
            }

            foreach (var pos in new[] { -1, 1 })
            {
                var captureMoves = pawns.Select(p => new Move(p.X, p.Y, p.X + pos, p.Y + direction))
                    .Where(IsInside)
                    .Where(p => Board[p.X2, p.Y2] != null && Board[p.X2, p.Y2].StartsWith(Opponent));

                moves.AddRange(captureMoves);
            }

            return moves;
        }
コード例 #2
0
ファイル: MoveValidator.cs プロジェクト: kvam/Kvam.Chess
        private IEnumerable<Move> CalculateBishopMoves(string pieceName = "b")
        {
            var bishops = Pieces.Where(x => x.Value.StartsWith(Turn) && x.Value.EndsWith(pieceName))
                                .Select(x => ConvertFromChessCoordinateToXy(x.Key)).ToList();

            var moves = new List<Move>();
            foreach (var bishop in bishops)
            {
                foreach (var xDirection in new[] { 1, -1 })
                {
                    foreach (var yDirection in new[] { 1, -1 })
                    {
                        var x = xDirection;
                        var y = yDirection;

                        Move move;

                        while (IsInside((move = new Move(bishop.X, bishop.Y, bishop.X + x, bishop.Y + y))))
                        {
                            if (Board[move.X2, move.Y2] == null)
                            {
                                moves.Add(move);
                            }
                            else
                            {
                                if (Board[move.X2, move.Y2].StartsWith(Opponent))
                                {
                                    moves.Add(move);
                                }
                                break;
                            }
                            x += xDirection;
                            y += yDirection;

                        }
                    }
                }
            }

            return moves;
        }
コード例 #3
0
ファイル: MoveValidator.cs プロジェクト: kvam/Kvam.Chess
 private static bool IsInside(Move m)
 {
     return m.X2 >= 0 && m.X2 <= 7 && m.Y2 >= 0 && m.Y2 <= 7;
 }