/** * Evaluate the win change against an enemy piece. */ private static float EvaluateWinChance(Dictionary <Square, AiChessPiece> board, float[,] strength, AiChessPiece initial, AiChessPiece target) { List <AiChessPiece> surroundings = AiMovementUtil.SurroundingPieces(board, initial.Position); bool addOne = false; switch (initial.Type) { case "knight": { if (!surroundings.Contains(target)) { addOne = true; } break; } case "rook": return(10 * EvaluationValues.PieceValues[target.Type]); } float minRoll = CaptureMatrix.GetMin(initial.Type, target.Type, addOne); if (minRoll > 6) { return(-10000); } return(minRoll); }
/* * Check if this move puts the king in a less secure position. * Returns 0 if does not involve king, 1000 if move protects king, and -1000 if move leaves open spot next to king. */ private static float EvaluateKingSafety(Dictionary <Square, AiChessPiece> board, Square initial, Square target, string owner) { AiChessPiece initialPiece = board[initial]; if (initialPiece.Type == "king" && initialPiece.Owner.Name == owner) { return(0); } List <AiChessPiece> initialSurroundings = AiMovementUtil.SurroundingPieces(board, initial); List <AiChessPiece> targetSurroundings = AiMovementUtil.SurroundingPieces(board, target); float score = 0; if (initialSurroundings.Any(piece => piece.Type == "king" && piece.Owner.Name == owner)) { score -= 1000; } if (targetSurroundings.Any(piece => piece.Type == "king" && piece.Owner.Name == owner)) { score += 1000; } return(score); }
// Methods to generate strength array from all pieces on the board public static float[,] InitStrength(Dictionary <Square, AiChessPiece> board) { float[,] strength = new float[8, 8]; foreach (AiChessPiece piece in board.Values) { AiMovementUtil.GetPossibleMoves(board, piece, true, strength); } return(strength); }
// Get all possible moves for a piece on the board private static List <ChessMove> GetMovesForPiece(Dictionary <Square, AiChessPiece> board, float[,] strength, AiChessPiece piece) { List <Square> possibleSquares = AiMovementUtil.GetPossibleMoves(board, piece, false, null); List <ChessMove> possibleMoves = new List <ChessMove>(); foreach (Square s in possibleSquares) { if (piece.Position.Equals(s)) { continue; } ChessMove move = new ChessMove(board, piece.Position, s, EvaluateMove.Instance.EvaluateChessMove(board, strength, piece.Position, s, piece.Owner.Name)); move.AttackOnly = s.AttackOnly; possibleMoves.Add(move); } return(possibleMoves); }
public float EvaluateChessMove(Dictionary <Square, AiChessPiece> board, float[,] strength, Square initialSquare, Square targetSquare, string owner) { AiChessPiece initialPiece = board[initialSquare]; List <AiChessPiece> initialSurroundings = AiMovementUtil.SurroundingPieces(board, initialSquare); List <AiChessPiece> targetSurroundings = AiMovementUtil.SurroundingPieces(board, targetSquare); float initial = EvaluateChessSquare(strength, initialSquare, owner, initialPiece.Type, initialSurroundings); float target = EvaluateChessSquare(strength, targetSquare, owner, initialPiece.Type, targetSurroundings); var score = target - initial; // take into account if the target square is better than initial square if (targetSurroundings.Any(p => p.Type == "king" && p.Owner.Name != owner)) { score += 30; } if (board.ContainsKey(targetSquare)) { score += 20; // be more aggressive AiChessPiece targetPiece = board[targetSquare]; score += weightEvalPriority * EvaluatePiecePriority(targetPiece); score += weightEvalWinChance * EvaluateWinChance(board, strength, initialPiece, targetPiece); } score += weightEvalKingSafety * EvaluateKingSafety(board, initialSquare, targetSquare, owner); return(score * (owner == "player1" ? 1 : -1)); }