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); }
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); }
public void repaint() { Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => { String turn = Chess.WhosTurn(); Turn.Content = char.ToUpper(turn[0]) + turn.Substring(1) + "'s turn"; bool black = false; for (int y = 0; y < 8; ++y) { for (int x = 0; x < 8; ++x) { Label l = FindChild <Label>(ChessBoard, "board_" + x + y); if (l != null) { l.MouseLeftButtonDown -= showMoves; l.MouseLeftButtonDown -= move; l.MouseLeftButtonDown -= repaint; Piece current = Chess.board.at(new Position(x, y)); String type = current.visual(); l.Content = type; l.Background = black ? new SolidColorBrush(Color.FromArgb(150, 0, 0, 0)) : new SolidColorBrush(Color.FromArgb(205, 255, 255, 255)); if (current.getColor() == Chess.WhosTurn()) { if (current.getType() == "king") { if (Chess.IsChecked(Chess.WhosTurn())) { l.Background = Brushes.LightCoral; } } l.Cursor = Cursors.Hand; l.MouseLeftButtonDown += showMoves; } else { l.Cursor = Cursors.Arrow; } } black = !black; } black = !black; } ChessMove latestMove = Chess.computerMove; if (latestMove != null) { Label last = FindChild <Label>(ChessBoard, "board_" + latestMove.from.x + latestMove.from.y); last.Background = Brushes.GreenYellow; last = FindChild <Label>(ChessBoard, "board_" + latestMove.to.x + latestMove.to.y); last.Background = Brushes.GreenYellow; } })); }
public Position findKing(string color) { for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; ++y) { Piece current = at(new Position(x, y)); if (current.getType() == "king" && current.getColor() == color) { return(new Position(x, y)); } } } return(null); }
public static void undo() { Piece last = board.at(lastMove.to); if (last.getType() == "pawn" && (lastMove.from.y == 1 && last.getColor() == "black" || lastMove.from.y == 6 && last.getColor() == "white")) { last.numberOfMoves = 0; } else { last.numberOfMoves--; } last.setPosition(lastMove.from); board.set(lastMove.from, last); board.set(lastMove.to, pieceRemoved); }
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); }
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 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); }
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); }
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 bool IsSame(Piece p, Position pos) { return p.getColor() == board.at(pos).getColor() && !checkingOwn; }
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; }
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; }
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 IsSame(Piece p, Position pos) { return(p.getColor() == board.at(pos).getColor() && !checkingOwn); }