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