Exemplo n.º 1
0
        public List <Field> GetAccessibleFields(BoardController bc)
        {
            var fields = new List <Field>();

            switch (Piece.FenChar)
            {
            case 'k':
            case 'K':
                fields.AddRange(MoveHelper.GetAccessibleFieldsKing(bc, Piece));
                break;

            case 'q':
            case 'Q':
                fields.AddRange(MoveHelper.GetAccessibleFieldsBishop(bc, Piece));
                fields.AddRange(MoveHelper.GetAccessibleFieldsRook(bc, Piece));
                break;

            case 'r':
            case 'R':
                fields.AddRange(MoveHelper.GetAccessibleFieldsRook(bc, Piece));
                break;

            case 'b':
            case 'B':
                fields.AddRange(MoveHelper.GetAccessibleFieldsBishop(bc, Piece));
                break;

            case 'n':
            case 'N':
                fields.AddRange(MoveHelper.GetAccessibleFieldsKnight(bc, Piece));
                break;

            case 'p':
            case 'P':
                var dY = Piece.PlayerAffiliation == PlayerAffiliation.White ? 1 : -1;
                fields.AddRange(MoveHelper.GetAccessibleFieldsPawn(bc, Piece, dY));
                break;
            }

            return(fields);
        }
Exemplo n.º 2
0
        public static List <Field> GetAccessibleFieldsKnight(BoardController boardController, Piece piece)
        {
            var list = new List <Field>();

            var field       = piece.Field;
            var affiliation = piece.PlayerAffiliation;

            var board = boardController.Board;

            var fields = board.Fields;
            var x      = field.X;
            var y      = field.Y;

            var dX = new[] { 1, 2, 2, 1, -1, -2, -2, -1 };
            var dY = new[] { -2, -1, 1, 2, 2, 1, -1, -2 };

            for (var i = 0; i < 8; i++)
            {
                var nX = x + dX[i];
                var nY = y + dY[i];

                if (nX < 0 || nY < 0 || nX >= board.Width || nY >= board.Height)
                {
                    continue;
                }

                var nField = fields[nX, nY];
                var nPiece = nField.Piece;

                if (nPiece == null || nPiece.PlayerAffiliation != affiliation)
                {
                    list.Add(nField);
                }
            }

            return(list);
        }
Exemplo n.º 3
0
        public static List <Field> GetAccessibleFieldsRook(BoardController boardController, Piece piece)
        {
            var list = new List <Field>();

            var field       = piece.Field;
            var affiliation = piece.PlayerAffiliation;

            var board = boardController.Board;

            var fields = board.Fields;
            var x      = field.X;
            var y      = field.Y;

            for (var i = x + 1; i < board.Width; i++)
            {
                var nField = fields[i, y];
                var nPiece = nField.Piece;

                if (nPiece == null)
                {
                    list.Add(nField);
                    continue;
                }

                if (nPiece.PlayerAffiliation != affiliation)
                {
                    list.Add(nField);
                }

                break;
            }

            for (var i = x - 1; i >= 0; i--)
            {
                var nField = fields[i, y];
                var nPiece = nField.Piece;

                if (nPiece == null)
                {
                    list.Add(nField);
                    continue;
                }

                if (nPiece.PlayerAffiliation != affiliation)
                {
                    list.Add(nField);
                }

                break;
            }

            for (var i = y + 1; i < board.Height; i++)
            {
                var nField = fields[x, i];
                var nPiece = nField.Piece;

                if (nPiece == null)
                {
                    list.Add(nField);
                    continue;
                }

                if (nPiece.PlayerAffiliation != affiliation)
                {
                    list.Add(nField);
                }

                break;
            }

            for (var i = y - 1; i >= 0; i--)
            {
                var nField = fields[x, i];
                var nPiece = nField.Piece;

                if (nPiece == null)
                {
                    list.Add(nField);
                    continue;
                }

                if (nPiece.PlayerAffiliation != affiliation)
                {
                    list.Add(nField);
                }

                break;
            }

            return(list);
        }
Exemplo n.º 4
0
        public static List <Field> GetAccessibleFieldsKing(BoardController boardController, Piece piece)
        {
            var list = new List <Field>();

            var player      = piece.Player;
            var field       = piece.Field;
            var affiliation = piece.PlayerAffiliation;

            var board = boardController.Board;

            var fields = board.Fields;
            var x      = field.X;
            var y      = field.Y;

            var dX = new[] { 0, -1, -1, -1, 0, 1, 1, 1 };
            var dY = new[] { 1, 1, 0, -1, -1, -1, 0, 1 };

            var attackedFields = BoardController.GetAttackedFields(player, true);

            for (var i = 0; i < 8; i++)
            {
                var nX = x + dX[i];
                var nY = y + dY[i];

                if (nX < 0 || nY < 0 || nX >= board.Width || nY >= board.Height)
                {
                    continue;
                }

                var nField = fields[nX, nY];
                var nPiece = nField.Piece;

                if (nPiece == null || nPiece.PlayerAffiliation != affiliation)
                {
                    if (attackedFields.Contains(nField))
                    {
                        continue;
                    }

                    list.Add(nField);
                }
            }

            if (player.MayCastleLong)
            {
                var field1 = field;
                var field2 = fields[x - 1, y];
                var field3 = fields[x - 2, y];

                if (!(attackedFields.Contains(field1) ||
                      attackedFields.Contains(field2) ||
                      attackedFields.Contains(field3)))
                {
                    if (field2.Piece == null && field3.Piece == null)
                    {
                        list.Add(field3);
                    }
                }
            }

            if (player.MayCastleShort)
            {
                var field1 = field;
                var field2 = fields[x + 1, y];
                var field3 = fields[x + 2, y];

                if (!(attackedFields.Contains(field1) ||
                      attackedFields.Contains(field2) ||
                      attackedFields.Contains(field3)))
                {
                    if (field2.Piece == null && field3.Piece == null)
                    {
                        list.Add(field3);
                    }
                }
            }

            return(list);
        }