Exemple #1
0
        private static bool kingValid(Piece p, Position from, Position to)
        {
            int xDist = to.x - from.x;
            int yDist = to.y - from.y;

            //Check for valid castling
            if (p.numberOfMoves == 0 && yDist == 0 && (xDist == 2 || xDist == -2))
            {
                int   direction = xDist == 2 ? 1 : -2;
                Piece p2        = Chess.board.at(new Position(to.x + direction, from.y));
                if (Chess.IsSame(p, p2.getPosition()) && p2.getType() == "rook" && p2.numberOfMoves == 0 && !Chess.IsChecked(p.getColor()))
                {
                    return(rookValid(p, from, to));
                }
            }

            if (!((xDist * xDist) <= 1 && (yDist * yDist) <= 1))
            {
                return(false);
            }
            return(!Chess.IsSame(p, to));
        }
Exemple #2
0
 public App()
 {
     Chess chess = new Chess(ExitGame);
 }
Exemple #3
0
        private static bool pawnValid(Piece p, Position from, Position to)
        {
            int xDist = from.x - to.x;
            int yDist = from.y - to.y;

            int direction = p.getColor() == "white" ? -1 : 1;

            if (from.y + direction > 7 || from.y + direction < 0)
            {
                return(false);
            }

            if (from.y + direction != to.y && ((from.y + 2 * direction != to.y && p.getNumberOfMoves() == 0) || !Chess.IsEmpty(new Position(from.x, from.y + direction)) || p.getNumberOfMoves() > 0))
            {
                return(false);
            }
            if (xDist < -1 || xDist > 1)
            {
                return(false);
            }
            if (xDist == 0 && !Chess.IsEmpty(to))
            {
                return(false);
            }
            if (xDist * xDist == 1 && yDist * yDist != 1)
            {
                return(false);
            }
            if (xDist != 0 && (Chess.IsEmpty(to) || Chess.IsSame(p, to)))
            {
                return(false);
            }

            return(true);
        }
Exemple #4
0
        public static ChessMove nextDraw()
        {
            ChessMove finalMove = new ChessMove(new Position(0, 0), new Position(0, 0));
            Random    rnd       = new Random();
            double    best      = -10000;

            List <Piece> blackPieces = Chess.blackPieces();

            foreach (Piece piece in blackPieces)
            {
                Piece p = blackPieces.Find(x => x.Equals(piece));
                Dictionary <Position, bool?> moves = Chess.validMoves(p);
                foreach (KeyValuePair <Position, bool?> move in moves)
                {
                    double value = 0;
                    if (move.Value != false)
                    {
                        value = Chess.board.at(move.Key).getValue() + rnd.NextDouble() * 4;
                        if (isThreatened(p.getPosition()))
                        {
                            value += p.getValue();

                            if (isProtected(p.getPosition()))
                            {
                                int low = lowestThreathener(p.getPosition(), "white");
                                int val = p.getValue();
                                value -= low < val ? low : val;
                            }
                        }

                        int previousValue = p.getValue();
                        Chess.tempMove(p.getPosition(), move.Key);
                        value += p.getValue() - previousValue;

                        if (isThreatened(p.getPosition()))
                        {
                            value -= p.getValue();
                            if (isProtected(p.getPosition()))
                            {
                                int low = lowestThreathener(p.getPosition(), "white");
                                int val = p.getValue();
                                value += low < val ? low : val;
                            }
                        }

                        if (Chess.IsChecked("white"))
                        {
                            value += rnd.NextDouble() * 5;
                        }
                        Chess.undo();
                    }
                    else if (move.Value == false)
                    {
                        value = -10000;
                    }

                    if (value > best)
                    {
                        best      = value;
                        finalMove = new ChessMove(piece.getPosition(), move.Key);
                    }
                }
            }

            return(finalMove);
        }