Exemplo n.º 1
0
        public static IEnumerable<Position> GetRookPositionIterator(Position p)
        {
            for (var x = -BoardRows; x <= BoardRows; x++)
            {
                if (x == 0)
                {
                    continue;
                }

                var newPosition = new Position(p.X + x, p.Y);

                if (IsValid(newPosition))
                {
                    yield return newPosition;
                }
            }

            for (var y = -BoardColumns; y <= BoardColumns; y++)
            {
                if (y == 0)
                {
                    continue;
                }

                var newPosition = new Position(p.X, p.Y + y);

                if (IsValid(newPosition))
                {
                    yield return newPosition;
                }
            }
        }
Exemplo n.º 2
0
 public Pawn(Position p, Color c) : base(p, c)
 {
     if (base.getColor() == Color.Black)
     {
         base.setImage(new Bitmap(Board.getInstance().getPath() + "//BlackPawn.gif"));
     }
     else {
         base.setImage(new Bitmap(Board.getInstance().getPath() + "//WhitePawn.gif"));
     }
 }
Exemplo n.º 3
0
Arquivo: Board.cs Projeto: jsok/Chess
        public override string ToString()
        {
            String repr = String.Empty;
            int col;
            int row;

            for (row = BOARD_DIMENSION - 1; row >= 0; row--)
            {
                repr += "   -";
                for (col = 0; col < BOARD_DIMENSION; col++)
                {
                    repr += "----";
                }
                repr += "\n";

                repr += String.Format(" {0} |", row + 1);
                for (col = 0; col < BOARD_DIMENSION; col++)
                {
                    Position p = new Position(col, row);
                    if (isPieceAtPosition(p))
                    {
                        repr += String.Format(" {0} |", getPieceAtPosition(p).Avatar);
                    }
                    else
                    {
                        repr += "   |";
                    }
                }
                repr += "\n";
            }

            repr += "   -";
            for (col = 0; col < BOARD_DIMENSION; col++)
            {
                repr += "----";
            }
            repr += "\n";

            repr += "    ";
            Char colChar = 'a';
            for (col = 0; col < BOARD_DIMENSION; col++)
            {
                repr += String.Format(" {0}  ", colChar);
                colChar++;
            }

            repr += "\n";
            return repr;
        }
Exemplo n.º 4
0
        /*
         * Checks whether a given position is valid as a next move.
         */
        public bool isvalidMove(Position newPosition)
        {
            Iterator iterator = allowedMovements.getIterator();

            while (iterator.MoveNext())
            {
                Position p = iterator.Current as Position;
                if (p.Equals(newPosition))
                {
                    return true;
                }
            }

            return false;
        }
Exemplo n.º 5
0
        public static Boolean tryParse(String input, out Position result)
        {
            char col;
            int row;

            if ((input.Length >= 2) &&
                (input[0] >= 'a' && input[0] <= 'h') &&
                (Int32.TryParse(input.Substring(1, 1), out row)))
            {
                col = input[0];
                result = new Position(col, row);
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var position = new Position(5, 5);
            Console.WriteLine("Starting position: " + position);

            var knightMoves = Position.GetKnightPositionIterator(position);

            Console.WriteLine("---- Knight moves ----");
            foreach (var p in knightMoves)
            {
                Console.WriteLine(p.ToString());
            }
            Console.WriteLine();

            var rookMoves = Position.GetRookPositionIterator(position);

            Console.WriteLine("---- Rook moves ----");
            foreach (var p in rookMoves)
            {
                Console.WriteLine(p.ToString());
            }
            Console.WriteLine();

            var bishopMoves = Position.GetBishopPositionIterator(position);

            Console.WriteLine("---- Bishop moves ----");
            foreach (var p in bishopMoves)
            {
                Console.WriteLine(p.ToString());
            }
            Console.WriteLine();

            var quennMoves = Position.GetQueenPositionIterator(position);

            Console.WriteLine("---- Queen moves ----");
            foreach (var p in quennMoves)
            {
                Console.WriteLine(p.ToString());
            }
            Console.WriteLine();

            Console.Read();
        }
Exemplo n.º 7
0
        /** return an iterator that will return all positions
          * that a knight may reach from a given starting position.
          */

        public static IEnumerable<Position> GetKnightPositionIterator(Position p)
        {
            for (var x = -2; x <= 2; x++)
            {
                for (var y = -2; y <= 2; y++)
                {
                    if (Math.Abs(x) + Math.Abs(y) != 3)
                    {
                        continue;
                    }

                    var newPosition = new Position(p.X + x, p.Y + y);

                    if (IsValid(newPosition))
                    {
                        yield return newPosition;
                    }
                }
            }
        }
Exemplo n.º 8
0
        /*
         * This constructor is used in the clone method
         */
        public List(ArrayList l)
        {
            list = new ArrayList();

            foreach (Piece p in l)
            {
                Position pos = new Position(p.getPosition().getRow(), p.getPosition().getColumn());
                Color color = p.getColor();
                Image image = p.getImage();
                Piece piece = null;

                if (p is Bishop)
                {
                    piece = new Bishop(pos, color);
                }
                else if (p is Knight)
                {
                    piece = new Knight(pos, color);
                }
                else if (p is Pawn)
                {
                    piece = new Pawn(pos, color);
                }
                else if (p is Queen)
                {
                    piece = new Queen(pos, color);
                }
                else if (p is Rook)
                {
                    piece = new Rook(pos, color);
                }
                else if (p is King)
                {
                    piece = new King(pos, color);
                }

                list.Add(piece);
            }

        }
Exemplo n.º 9
0
        public static IEnumerable<Position> GetBishopPositionIterator(Position p)
        {
            for (var x = -BoardRows; x <= BoardRows; x++)
            {
                if (x == 0)
                {
                    continue;
                }

                var newPosition = new Position(p.X + x, p.Y + x);

                if (IsValid(newPosition))
                {
                    yield return newPosition;
                }

                newPosition = new Position(p.X + x, p.Y - x);

                if (IsValid(newPosition))
                {
                    yield return newPosition;
                }
            }
        }
Exemplo n.º 10
0
 public King(Color color, Position position)
     : base(color, position)
 {
     character = (color == Color.Black) ? '\u265A' : '\u2654';
 }
Exemplo n.º 11
0
 public Bishop(Color color, Position position)
     : base(color, position)
 {
     character = (color == Color.Black) ? '\u265D' : '\u2657';
 }
Exemplo n.º 12
0
 public Square(Position p)
 {
     position = p;
 }
Exemplo n.º 13
0
 public Piece GetPieceAtPosition(Position position)
 {
     return piecesinplay.Where(i => i.Position.Equals(position))
         .Select(i => i.Piece)
         .FirstOrDefault();
 }
Exemplo n.º 14
0
Arquivo: Board.cs Projeto: jsok/Chess
        Boolean isPositionOnBoard(Position position)
        {
            if (position.Column < 0 || position.Column >= BOARD_DIMENSION)
                return false;
            if (position.Row < 0 || position.Row >= BOARD_DIMENSION)
                return false;

            return true;
        }
Exemplo n.º 15
0
 public InPlay(Piece piece, Position position)
 {
     Piece = piece;
     Position = position;
 }
Exemplo n.º 16
0
 public void PutPieceInPlay(Piece piece, Position position)
 {
     piecesinplay.Add(new InPlay(piece,position));
 }
Exemplo n.º 17
0
 void Announce(Piece piece, Position position)
 {
     Message = string.Format("{0} to {1}", piece, position);
 }
Exemplo n.º 18
0
        public void MovePieceTo(Piece piece,Position to)
        {
            var toMove = piecesinplay.Where(p => p.Piece.Equals(piece))
                .FirstOrDefault();

            if (toMove == null) throw new InvalidOperationException("No such piece in play " +  piece);

            var move = new Move(toMove.Position,to,piece,isfirst);

            if(move.Valid())
            {
                move.Apply(piecesinplay);
                Announce(piece, to);
            }
            else
                AnnounceIllegalMove();

            isfirst = false;
        }
Exemplo n.º 19
0
Arquivo: Board.cs Projeto: jsok/Chess
        public Position.Placement validPosition(IPiece piece, Position position)
        {
            if (!isPositionOnBoard(position))
                return Position.Placement.ILLEGAL;

            if (isPieceAtPosition(position))
            {
                IPiece occupier = getPieceAtPosition(position);
                if (piece.isFriendly(occupier))
                    return Position.Placement.BLOCKED;
                else
                    return Position.Placement.CAPTURE;
            }

            return Position.Placement.VALID;
        }
Exemplo n.º 20
0
Arquivo: Board.cs Projeto: jsok/Chess
 public IPiece getPieceAtPosition(Position p)
 {
     return board[p.Column, p.Row];
 }
Exemplo n.º 21
0
        /*
         * Checks whether there is a piece in a given position
         */
        public Piece consultPiece(Position p, bool white)
        {
            Iterator it;

            if (white)
            {
                it = whitePieces.getIterator();
            }
            else {
                it = blackPieces.getIterator();
            }

            while (it.MoveNext())
            {
                Piece piece = (it.Current as Piece);
                if (piece.getPosition().Equals(p))
                {
                    return piece;
                }
            }

            return null;
        }
Exemplo n.º 22
0
        /*
         * Deletes a piece from a given position. In case of "pawn promotion", do not put into deleted pieces set.
         */
        public Piece killPiece(Position pos, bool promotion)
        {
            bool found = false;
            Piece piece = null;

            Iterator it = whitePieces.getIterator();

            while (it.MoveNext())
            {
                Piece p = (it.Current as Piece);

                if (p.getPosition().Equals(pos))
                {

                    piece = p;

                    if (!promotion)
                    {
                        mainForm.getWhiteImages().Images.Add(p.getImage());
                    }

                    whitePieces.remove(p);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                it = blackPieces.getIterator();

                while (it.MoveNext())
                {
                    Piece p = (it.Current as Piece);

                    if (p.getPosition().Equals(pos))
                    {
                        piece = p;

                        if (!promotion)
                        {
                            mainForm.getBlackImages().Images.Add(p.getImage());
                        }

                        blackPieces.remove(p);
                        break;
                    }
                }
            }

            return piece;
        }
Exemplo n.º 23
0
        /*
         * This method updates the set of allowed movements. Implememts an abstract method from Piece.cs
         */
        public override void updateAllowedMovements()
        {
            this.allowedMovements.clear();

            int row = position.getRow();
            int column = position.getColumn();
            int i, j;
            Position currentPos;

            if (this.color == Color.Black) //Black pawn
            { 
                //Down

                i = row + 1;
                j = column;

                if (i <= 7)
                {
                    currentPos = new Position(i, j);

                    if (Board.getInstance().consultPiece(currentPos, true) == null
                            && Board.getInstance().consultPiece(currentPos, false) == null)
                    {

                        this.allowedMovements.insert(currentPos);
                    }
                }

                //2 positions down for first movement

                if (row == 1 && Board.getInstance().consultPiece(new Position(2, column), true) == null
                        && Board.getInstance().consultPiece(new Position(2, column), false) == null
                        && Board.getInstance().consultPiece(new Position(3, column), true) == null)
                {
                    checkSquare(new Position(row + 2, column));
                }

                //Down-right diagonal, only to atack

                i = row + 1;
                j = column + 1;

                if (i < Board.NUM_ROWS && j < Board.NUM_ROWS)
                {
                    currentPos = new Position(i, j);

                    if (Board.getInstance().consultPiece(currentPos, true) != null) //Opponent's piece
                    { 
                        this.allowedMovements.insert(currentPos);
                    }
                }

                //Down-left diagonal, only to atack

                i = row + 1;
                j = column - 1;

                if (i < Board.NUM_ROWS && j >= 0)
                {
                    currentPos = new Position(i, j);

                    if (Board.getInstance().consultPiece(currentPos, true) != null) //Opponent's piece
                    { 
                        this.allowedMovements.insert(currentPos);
                    }
                }

            }

            else { // White pawn

                //Up

                i = row - 1;
                j = column;

                if (i >= 0)
                {
                    currentPos = new Position(i, j);
                    if (Board.getInstance().consultPiece(currentPos, true) == null
                        && Board.getInstance().consultPiece(currentPos, false) == null)
                    {
                        this.allowedMovements.insert(currentPos);
                    }

                }

                //Two positions up for first movement

                if (row == 6 && Board.getInstance().consultPiece(new Position(5, column), true) == null
                    && Board.getInstance().consultPiece(new Position(5, column), false) == null
                    && Board.getInstance().consultPiece(new Position(4, column), false) == null)
                {
                    checkSquare(new Position(row - 2, column));
                }

                //Up-right diagonal, only to attack

                i = row - 1;
                j = column + 1;

                if (i >= 0 && j < Board.NUM_ROWS)
                {
                    currentPos = new Position(i, j);

                    if (Board.getInstance().consultPiece(currentPos, false) != null) //Opponent's piece
                    { 

                        this.allowedMovements.insert(currentPos);
                    }
                }

                //Up-left diagonal, only to attack

                i = row - 1;
                j = column - 1;

                if (i >= 0 && j >= 0)
                {
                    currentPos = new Position(i, j);

                    if (Board.getInstance().consultPiece(currentPos, false) != null) // Opponent's piece
                    { 
                        this.allowedMovements.insert(currentPos);
                    }
                }

            }
        }
Exemplo n.º 24
0
 public bool IsValid(Position from, Position to)
 {
     return (from.y - to.y) < yoffset;
 }
Exemplo n.º 25
0
 public static bool IsValid(Position p)
 {
     return 0 <= p.X && p.X <= BoardRows
            && 0 <= p.Y && p.Y <= BoardColumns;
 }
Exemplo n.º 26
0
 public bool Equals(Position other)
 {
     return other.x == x && other.y == y;
 }
Exemplo n.º 27
0
 public void MoveTo(Position position)
 {
     Position = position;
 }
Exemplo n.º 28
0
Arquivo: Board.cs Projeto: jsok/Chess
 Boolean isPieceAtPosition(Position p)
 {
     return (getPieceAtPosition(p) != null);
 }
Exemplo n.º 29
0
 public static IEnumerable<Position> GetQueenPositionIterator(Position p)
 {
     return GetRookPositionIterator(p).Concat(GetBishopPositionIterator(p));
 }
Exemplo n.º 30
0
 public Move(Position from,Position to, Piece piece,bool isfirst)
 {
     this.from = from;
     this.to = to;
     this.piece = piece;
     this.isfirst = isfirst;
 }