-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.cs
73 lines (62 loc) · 2.2 KB
/
Evaluator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections.Immutable;
namespace Congo.Core
{
public static class CongoEvaluator
{
/// <summary>
/// Shall ensure <b>-/+ Inf</b> for all evaluation functions!
/// </summary>
public static int INF => 1_000_000;
/// <summary>
/// Ternary evaluation either win, lose or 0.
/// </summary>
public static int WinLose(CongoGame game)
{
if (game.IsWin()) { return game.WhitePlayer.HasLion ? 1 : -1; }
return 0;
}
/// <summary>
/// Max. possible score = 5 + 10 + 12 + 100 + 10 + 7 + 7*3 = 165
/// </summary>
private static readonly ImmutableArray<int> materialValues = new int[10] {
0, // ground
0, // river
6, // elephant
7, // zebra
5, // giraffe
10, // crocodile
1, // pawn
3, // superpawn
100, // lion
10 // monkey
}.ToImmutableArray();
private static int ScoreByColor(CongoColor color, CongoBoard board)
{
int score = 0;
var e = board.GetEnumerator(color);
while(e.MoveNext()) {
score += materialValues[(int)board.GetPiece(e.Current).Code];
}
return color.IsWhite() ? score : -score;
}
/// <summary>
/// Count score based on material values of each piece on the board.
/// </summary>
public static int Material(CongoGame game)
{
int score = 0;
score += ScoreByColor(White.Color, game.Board);
score += ScoreByColor(Black.Color, game.Board);
if (!game.HasEnded() &&
game.Predecessor.ActivePlayer.Color != game.ActivePlayer.Color &&
game.Predecessor.ActivePlayer.LionInDanger(game.ActivePlayer.Moves)) {
score += game.ActivePlayer.Color.IsWhite() ? 100 : -100;
}
return score;
}
/// <summary>
/// Default game evaluation, all algorithms should use this method.
/// </summary>
public static int Default(CongoGame game) => Material(game);
}
}