Esempio n. 1
0
        private static bool rookValid(Piece p, Position from, Position to)
        {
            int xDist = to.x - from.x;
            int yDist = to.y - from.y;

            if (!(xDist == 0 || yDist == 0))
            {
                return false;
            }

            int yDirection = yDist == 0 ? 0 : yDist < 0 ? -1 : 1;
            int xDirection = xDist == 0 ? 0 : xDist < 0 ? -1 : 1;

            int x = from.x + xDirection;
            int y = from.y + yDirection;
            Position current = new Position(x, y);

            while (!current.Equals(to))
            {
                if (!Chess.IsEmpty(current))
                {
                    return false;
                }
                current.x += xDirection;
                current.y += yDirection;
            }

            return !Chess.IsSame(p, to);
        }
Esempio n. 2
0
 public Piece(Piece piece)
 {
     this.type = piece.getType();
     this.color = piece.getColor();
     this.position = piece.getPosition();
     this.numberOfMoves = piece.numberOfMoves;
 }
Esempio n. 3
0
        public Board()
        {
            board = new Dictionary<Position, Piece>();

            for (int y = 0; y < 8; y++)
            {
                for (int x = 0; x < 8; ++x)
                {
                    Position p = new Position(x, y);
                    board[p] = new Piece("blank", "none", p);
                }
            }

            string color = "black";
            for (int y = 1; y < 7; y += 5)
            {
                for (int x = 0; x < 8; ++x)
                {
                    Position p = new Position(x, y);
                    board[p] = new Piece("pawn", color, p);
                }
                color = "white";
            }

            color = "black";
            for (int y = 0; y < 8; y += 7)
            {

                Position p = new Position(0, y);
                board[p] = new Piece("rook", color, p);

                p = new Position(1, y);
                board[p] = new Piece("knight", color, p);

                p = new Position(2, y);
                board[p] = new Piece("bishop", color, p);

                p = new Position(3, y);
                board[p] = new Piece("queen", color, p);

                p = new Position(4, y);
                board[p] = new Piece("king", color, p);

                p = new Position(5, y);
                board[p] = new Piece("bishop", color, p);

                p = new Position(6, y);
                board[p] = new Piece("knight", color, p);

                p = new Position(7, y);
                board[p] = new Piece("rook", color, p);

                color = "white";
            }

        }
Esempio n. 4
0
 public void clear()
 {
     for (int y = 0; y < 8; y++)
     {
         for (int x = 0; x < 8; ++x)
         {
             Position p = new Position(x, y);
             board[p] = new Piece("blank", "none", p);
         }
     }
 }
Esempio n. 5
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);
        }
Esempio n. 6
0
 public static bool validMove(Piece p, Position to)
 {
     if (p.getPosition().Equals(to)) return false;
     String type = p.getType();
     switch (type)
     {
         case "pawn":
             return pawnValid(p, p.getPosition(), to);
         case "knight":
             return knightValid(p, p.getPosition(), to);
         case "bishop":
             return bishopValid(p, p.getPosition(), to);
         case "rook":
             return rookValid(p, p.getPosition(), to);
         case "queen":
             return queenValid(p, p.getPosition(), to);
         case "king":
             return kingValid(p, p.getPosition(), to);
         default:
             return false;
     }
 }
Esempio n. 7
0
        public static void loadFromJSON()
        {
            if (failedToWrite)
            {
                return;
            }
            Chess.board.clear();

            string jsonString = File.ReadAllText(Chess.currentGameFile);

            var json = Json.Decode(jsonString);

            var listOfPieces = json["chess"]["pieces"];

            foreach (var piece in listOfPieces)
            {
                String color = piece.color;
                String type = piece.type;
                Position position = new Position(piece.Position.x, piece.Position.y);
                int moves = piece.moves;

                Piece p = new Piece(type, color, position, moves);

                Chess.board.set(position, p);
            }
            Chess.turn = json["chess"]["turn"];
        }
Esempio n. 8
0
        private static void move(Position from, Position to, bool realMove){
            lastMove = new ChessMove(from, to);
            Piece p1 = board.at(from);
            Piece p2 = board.at(to);
            pieceRemoved = p2;

            int xDiff = to.x - from.x;
            if (p1.getType() == "king" && Math.Abs(xDiff) > 1 && realMove)
            {
                int xDir = xDiff < 0 ? -2 : 1;
                Position rook = new Position(to.x + xDir, to.y);
                xDir = xDiff < 0 ? 1 : -1;
                board.at(rook).debug();
                move(rook, new Position(to.x + xDir, to.y));
            }

            board.set(from, new Piece("blank", "none", from));

            p1.numberOfMoves++;
            if (p1.getType() == "pawn" && Math.Abs(to.y - from.y) == 2)
            {
                p1.numberOfMoves++;
            }

            p1.setPosition(to);

            if (p1.getType() == "pawn" && (to.y == 0 || to.y == 7))
            {
                p1 = new Piece("queen", p1.getColor(), p1.getPosition(), p1.getNumberOfMoves());
            }

            board.set(to, p1);
        }
Esempio n. 9
0
 public static bool IsSame(Piece p, Position pos)
 {
     return p.getColor() == board.at(pos).getColor() && !checkingOwn;
 }
Esempio n. 10
0
        public static Dictionary<Position, bool?> validMoves(Piece toBeMoved)
        {
            Dictionary<Position, bool?> moves = new Dictionary<Position, bool?>();
            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; ++y)
                {
                    Position to = new Position(x, y);
                    Piece moveTo = board.at(to);
                    bool? valid = Rules.validMove(toBeMoved, to);

                    if (valid == true && moveTo.getType() != "blank")
                    {
                        valid = null;
                    }
                    if (valid != false)
                    {
                        move(toBeMoved.getPosition(), moveTo.getPosition(), false);
                        if (IsChecked(toBeMoved.getColor()))
                        {
                            valid = false;
                        }

                        undo();
                    }
                    moves.Add(to, valid);
                }
            }

            return moves;
        }
Esempio n. 11
0
 private static bool queenValid(Piece p, Position from, Position to)
 {
     return rookValid(p, from, to) || bishopValid(p, from, to);
 }
Esempio n. 12
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;
        }
Esempio n. 13
0
        private static bool knightValid(Piece p, Position from, Position to)
        {
            int xDist = from.x - to.x;
            int yDist = from.y - to.y;

            return !Chess.IsSame(p, to) && xDist * xDist + yDist * yDist == 5;
        }
Esempio n. 14
0
 public void set(Position position, Piece piece)
 {
     board[position] = piece;
 }
Esempio n. 15
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);
        }
Esempio n. 16
0
 public bool Equals(Piece other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return other.type == this.type && other.position.Equals(this.position) && other.color == this.color && other.numberOfMoves == this.numberOfMoves;
 }