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); } }
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 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); }
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 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); }
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 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); }