-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cs
118 lines (94 loc) · 3.73 KB
/
Engine.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices;
using System.Text.Json;
namespace omnipede;
public class Engine
{
public static Tuple<Ply?, int> Normal(ref int maxTime, ref System.Diagnostics.Stopwatch totalTime, ref GameState gameState, int depth, int alpha, int beta, bool debug)
{
List<Ply> testPlyList;
List<Ply> principleVariation = new();
Ply bestPly;
int bestPlyValue;
Ply testPly;
int testPlyValue;
if (depth == 0)
{
return new(null, GameState.Evaluate(gameState));
}
testPlyList = ListPlies.PseudoLegal(gameState);
bestPly = testPlyList[0];
//Console.WriteLine(gameState.currentTurn);
if (gameState.currentTurn == Piece.Color.White)
{
bestPlyValue = -2147483648;
for (int i = 0; i < testPlyList.Count; i++)
{
if (totalTime.ElapsedMilliseconds > maxTime)
{
return new(null, GameState.Evaluate(gameState));
}
testPly = testPlyList[i];
Tuple<Ply?, int> testTuple = new(new(), 0);
gameState = Ply.DoPly(gameState, testPly);
if (testPly.piecesToRemove.Count > 0 && (testPly.piecesToRemove[0].Item1.binary & Piece.Type._MASK) == Piece.Type.King)
{
testPlyValue = 2147483647;
}
else
{
testTuple = Engine.Normal(ref maxTime, ref totalTime, ref gameState, depth-1, alpha, beta, debug);
testPlyValue = testTuple.Item2;
}
alpha = Math.Max(alpha, testPlyValue);
if (beta <= alpha)
{
gameState = Ply.UndoPly(gameState, testPly);
return new(null, testPlyValue);
}
if (testPlyValue > bestPlyValue)
{
bestPly = testPly;
bestPlyValue = testPlyValue;
}
gameState = Ply.UndoPly(gameState, testPly);
}
}
else
{
bestPlyValue = 2147483647;
for (int i = 0; i < testPlyList.Count; i++)
{
if (totalTime.ElapsedMilliseconds > maxTime)
{
return new(null, GameState.Evaluate(gameState));
}
testPly = testPlyList[i];
Tuple<Ply?, int> testTuple = new(new(), 0);
gameState = Ply.DoPly(gameState, testPly);
if (testPly.piecesToRemove.Count > 0 && (testPly.piecesToRemove[0].Item1.binary & Piece.Type._MASK) == Piece.Type.King)
{
testPlyValue = -2147483648;
}
else
{
testTuple = Engine.Normal(ref maxTime, ref totalTime, ref gameState, depth-1, alpha, beta, debug);
testPlyValue = testTuple.Item2;
}
beta = Math.Min(beta, testPlyValue);
if (beta <= alpha)
{
gameState = Ply.UndoPly(gameState, testPly);
return new(null, testPlyValue);
}
if (testPlyValue < bestPlyValue)
{
bestPly = testPly;
bestPlyValue = testPlyValue;
}
gameState = Ply.UndoPly(gameState, testPly);
}
}
return new(bestPly, bestPlyValue);
}
}