Exemplo n.º 1
0
        /*
         * This method checks whether the king can be killed by the opponent
         */
        public static bool isCheck(bool white)
        {
            // Get the king's position

            Position posKing = null;
            Iterator it;

            if (white)
            {
                it = Board.getInstance().getWhitePieces().getIterator();
            }
            else
            {
                it = Board.getInstance().getBlackPieces().getIterator();
            }

            while (it.MoveNext() && posKing == null)
            {
                if ((it.Current as Piece) is King)
                {
                    posKing = (it.Current as Piece).getPosition();
                }
            }

            //Check "check" :)

            if (white)
            {
                it = Board.getInstance().getBlackPieces().getIterator();
            }
            else
            {
                it = Board.getInstance().getWhitePieces().getIterator();
            }

            while (it.MoveNext())
            {
                Piece    piece       = it.Current as Piece;
                Iterator itPositions = piece.getAllowedMovements().getIterator();

                while (itPositions.MoveNext())
                {
                    Position pos = itPositions.Current as Position;
                    if (pos.Equals(posKing))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
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);
        }