66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
namespace CheckersSpielBot
|
|
{
|
|
public static class BoardEvaluator
|
|
{
|
|
private const int PieceValue = 100;
|
|
private const int KingValue = 160;
|
|
|
|
//Weights (Musst be Balanced To have a good Ai)
|
|
private const int AdvancementWeight = 5;
|
|
private const int BackRowWeight = 15;
|
|
private const int CenterWeight = 10;
|
|
private const int MobilityWeight = 8;
|
|
|
|
public static int Evaluate(int[,] board, int aiPlayer)
|
|
{
|
|
int score = 0;
|
|
int humanPlayer = aiPlayer == 1 ? 2 : 1;
|
|
|
|
MoveGeneratorService moveGen = new MoveGeneratorService(board);
|
|
|
|
int aiMobility = 0;
|
|
int humanMobility = 0;
|
|
|
|
for (int row = 0; row < 8; row++)
|
|
{
|
|
for (int col = 0; col < 8; col++)
|
|
{
|
|
int piece = board[row, col];
|
|
if (piece == 0) continue;
|
|
|
|
//Reward For who will play first
|
|
bool isAi = BoardHelper.BelongsToPlayer(piece, aiPlayer);
|
|
bool isKing = BoardHelper.IsKing(piece);
|
|
|
|
int sign = isAi ? 1 : -1;
|
|
|
|
// Material
|
|
score += sign * (isKing ? KingValue : PieceValue);
|
|
|
|
// Advancement — how far the piece has advanced toward promotion
|
|
int advancement = isAi ? (aiPlayer == 1 ? 7 - row : row) : (aiPlayer == 1 ? row : 7 - row); // Red moves up, Blue moves down
|
|
|
|
score += sign * advancement * AdvancementWeight;
|
|
|
|
// Back row protection — pieces on starting back row
|
|
bool onBackRow = isAi ? (aiPlayer == 1 && row == 7) || (aiPlayer == 2 && row == 0) : (aiPlayer == 1 && row == 0) || (aiPlayer == 2 && row == 7);
|
|
if (onBackRow && !isKing) { score += sign * BackRowWeight; }
|
|
|
|
// Center control — [columns:2,3,4,5 && rows:2,3,4,5]
|
|
if (col >= 2 && col <= 5 && row >= 2 && row <= 5) { score += sign * CenterWeight; }
|
|
|
|
// Mobility
|
|
int moves = moveGen.GetLegalMoves(row, col, capturesOnly: false).Count;
|
|
|
|
if (isAi) { aiMobility += moves; }
|
|
else { humanMobility += moves; }
|
|
}
|
|
}
|
|
|
|
// Mobility difference
|
|
score += (aiMobility - humanMobility) * MobilityWeight;
|
|
|
|
return score;
|
|
}
|
|
}
|
|
} |