public Piece(Piece piece) { this.type = piece.getType(); this.color = piece.getColor(); this.position = piece.getPosition(); this.numberOfMoves = piece.numberOfMoves; }
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); }
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); } }
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)); }
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; } }
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); }
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; }
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); }