Ejemplo n.º 1
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"];
        }
Ejemplo n.º 2
0
        private static int lowestThreathener(Position position, string color)
        {
            List<Piece> pieces;

            if (color == "white")
            {
                pieces = Chess.whitePieces();
            }
            else
            {
                pieces = Chess.blackPieces();
            }

            int value = 123456789;
            Chess.checkingOwn = true;
            foreach (Piece p in pieces)
            {
                if (Rules.validMove(p, position))
                {
                    if (p.getValue() < value)
                    {
                        value = p.getValue();
                    }
                }
            }
            Chess.checkingOwn = false;
            return value;
        }
Ejemplo n.º 3
0
 /*
  * Create a new chess piece.
  * Types available are: King, Queen, Rook, Bishop, Knight and Pawn
  * Colors available are: White, Black
  */
 public Piece(string type, string color, Position position, int numberOfMoves = 0)
 {
     this.type = type;
     this.color = color;
     this.numberOfMoves = numberOfMoves;
     this.position = position;
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 public Piece(Piece piece)
 {
     this.type = piece.getType();
     this.color = piece.getColor();
     this.position = piece.getPosition();
     this.numberOfMoves = piece.numberOfMoves;
 }
Ejemplo n.º 6
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";
            }

        }
Ejemplo n.º 7
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);
         }
     }
 }
Ejemplo n.º 8
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;
     }
 }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
 public static bool hasMoves()
 {
     for (int x = 0; x < 8; ++x)
     {
         for (int y = 0; y < 8; ++y)
         {
             Piece p = board.at(new Position(x, y));
             if (p.getColor() != turn && p.getType() != "blank")
             {
                 Position pos = new Position(x, y);
                 Dictionary<Position, bool?> moves = Chess.validMoves(p);
                 foreach (KeyValuePair<Position, bool?> kvp in moves)
                 {
                     if (kvp.Value != false)
                     {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
Ejemplo n.º 12
0
        public static bool IsChecked(string color)
        {
            Position kingPos = board.findKing(color);
            Piece king = board.at(kingPos);

            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; ++y)
                {
                    Position from = new Position(x, y);
                    Piece p = board.at(from);
                    if (p.getColor() != king.getColor())
                    {
                        if (Rules.validMove(p, kingPos))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
Ejemplo n.º 13
0
 public static bool IsSame(Piece p, Position pos)
 {
     return p.getColor() == board.at(pos).getColor() && !checkingOwn;
 }
Ejemplo n.º 14
0
 public static bool IsEmpty(Position pos)
 {
     Debug.WriteLine(pos.x + "," + pos.y);
     return board.at(pos).getType() == "blank";
 }
Ejemplo n.º 15
0
 private static bool isThreatened(Position position)
 {
     return canMoveTo(position, "white");
 }
Ejemplo n.º 16
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;
        }
Ejemplo n.º 17
0
 private static bool isProtected(Position position)
 {
     return canMoveTo(position, "black");
 }
Ejemplo n.º 18
0
 private static bool queenValid(Piece p, Position from, Position to)
 {
     return rookValid(p, from, to) || bishopValid(p, from, to);
 }
Ejemplo n.º 19
0
        private static bool canMoveTo(Position position, string color){
            List<Piece> pieces;

            if (color == "white")
            {
                pieces = Chess.whitePieces();
            }
            else
            {
                pieces = Chess.blackPieces();
            }

            Chess.checkingOwn = true;
            foreach (Piece p in pieces)
            {
                if (Rules.validMove(p, position))
                {
                    Chess.checkingOwn = false;
                    return true;
                }
            }
            Chess.checkingOwn = false;
            return false;
            
        }
Ejemplo n.º 20
0
        private static List<Piece> getPieces(string color)
        {
            List<Piece> pieces = new List<Piece>();

            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; ++y)
                {
                    Position pos = new Position(x, y);
                    Piece p = board.at(pos);
                    if (p.getColor().Equals(color))
                    {
                        pieces.Add(p);
                    }
                }
            }
            return pieces;
        }
Ejemplo n.º 21
0
 public static void move(Position from, Position to)
 {
     if (!Rules.validMove(board.at(from), to))
     {
         throw new Exception("InvalidMoveError");
     }
     move(from, to, true);
 }
Ejemplo n.º 22
0
 public void set(Position position, Piece piece)
 {
     board[position] = piece;
 }
Ejemplo n.º 23
0
 public Piece at(Position p)
 {
     return board[p];
 }
Ejemplo n.º 24
0
 public static void tempMove(Position from, Position to)
 {
     move(from, to, false);
 }
Ejemplo n.º 25
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;
        }
Ejemplo n.º 26
0
 public ChessMove(Position from, Position to, int value = 0)
 {
     this.from = from;
     this.to = to;
     this.value = value;
 }
Ejemplo n.º 27
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;
        }
Ejemplo n.º 28
0
 public bool Equals(Position other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return other.x == x && other.y == y;
 }
Ejemplo n.º 29
0
 public void setPosition(Position position)
 {
     this.position = position;
 }