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 void showMoves(object sender, MouseButtonEventArgs e) { Label from = (Label)sender; from.Background = Brushes.LightBlue; int posX = Grid.GetColumn(from); int posY = Grid.GetRow(from) - 2; this.from = new Position(posX, posY); Dictionary <Position, bool?> moves = Chess.validMoves(Chess.board.at(this.from)); for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; ++y) { Label l = FindChild <Label>(ChessBoard, "board_" + x + y); l.MouseLeftButtonDown -= showMoves; bool?validMove = moves[new Position(x, y)]; if (validMove != false) { l.Background = validMove == null ? Brushes.LightCoral : Brushes.LightGreen; l.Cursor = Cursors.Hand; l.MouseLeftButtonDown += move; } else { l.Cursor = Cursors.Arrow; l.MouseLeftButtonDown += repaint; } } } }
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); }