-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.cs
185 lines (165 loc) · 6.73 KB
/
GameState.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
namespace omnipede;
using System.Numerics;
public struct GameState
{
public List<Piece> pieces { get; set; } = new();
public int? tempPieceIndex { get; set; }
public BigInteger? promotionRankWhite { get; set; }
public BigInteger? promotionRankBlack { get; set; }
public uint[] promotionAllowedPieces { get; set; } = new uint[0];
public uint currentTurn { get; set; }
public uint slideLimit { get; set; }
public GameState(){}
public string DebugString()
{
string returnString = "";
returnString = returnString+"GameState:";
returnString = returnString+"\n pieces:";
for (int i = 0; i < pieces.Count; i++)
{
returnString = returnString+"\n pieces["+i+"]:";
returnString = returnString+"\n binary: "+Convert.ToString(pieces[i].binary, toBase: 2);
returnString = returnString+"\n hasMoved: "+pieces[i].hasMoved;
returnString = returnString+"\n x: "+pieces[i].x;
returnString = returnString+"\n y: "+pieces[i].y;
}
returnString = returnString+"\n tempPieceIndex: "+tempPieceIndex;
returnString = returnString+"\n currentTurn: "+Convert.ToString(currentTurn, toBase: 2);
returnString = returnString+"\n promotionRankWhite: "+promotionRankWhite.ToString();
returnString = returnString+"\n promotionRankBlack: "+promotionRankBlack.ToString();
return returnString;
}
public int Hash()
{
uint hash = currentTurn + (uint)(tempPieceIndex ?? 0);
foreach (Piece piece in pieces)
{
uint pieceHash = 0;
pieceHash ^= (uint)(piece.x&0b_1111_1111_1111_1111_1111_1111_1111_1111)<< 16;
pieceHash ^= (uint)(piece.y&0b_1111_1111_1111_1111_1111_1111_1111_1111);
pieceHash ^=~(piece.binary&(Piece.Category._MASK|Piece.Color._MASK)) >> 16;
pieceHash ^= piece.binary << 26;
pieceHash ^= (uint)Convert.ToInt32(piece.hasMoved) << 15;
pieceHash ^= currentTurn << 2;
//pieceHash <<= piece.x.Sign+2;
hash ^= pieceHash;
hash ^= hash << 2*(piece.x.Sign+2)+1;
hash ^= hash << 13;
hash ^= hash >> 17;
hash ^= hash << 5;
hash = (hash*6257) % 625341599;
hash ^= hash << 2*(piece.y.Sign+2)+1;
hash ^= hash << 13;
hash ^= hash >> 17;
hash ^= hash << 5;
hash = (hash*6257) % 625341599;
//Console.WriteLine(hash);
}
return (int)(hash&0b_0011_1111_1111_1111_1111_1111_1111_1111);
}
public int[] Hashints()
{
List<int> list = new();
for (int i = 0; i < pieces.Count; i++)
{
list.Add((int)pieces[i].binary);
list.Add(pieces[i].hasMoved ? 1 : 0);
list.Add((int)(pieces[i].x & 0b_0111_1111_1111_1111_1111_1111_1111_1111));
list.Add((int)(pieces[i].y & 0b_0111_1111_1111_1111_1111_1111_1111_1111));
}
if (tempPieceIndex != null)
{
list.Add((int)tempPieceIndex);
}
list.Add((int)currentTurn);
return list.ToArray();
}
public static int Evaluate(GameState gameState)
{
int evauation = 0;
for (int i = 0; i < gameState.pieces.Count; i++)
{
if ((gameState.pieces[i].binary & Piece.Color._MASK) == Piece.Color.White)
{
switch (gameState.pieces[i].binary & Piece.Type._MASK)
{
case Piece.Type.Pawn:
evauation += 100;
//if (gameState.promotionRankWhite != null)
//{
// evauation -= (int)(gameState.promotionRankWhite-gameState.pieces[i].y);
//}
break;
case Piece.Type.Knight:
evauation += 300;
break;
case Piece.Type.Bishop:
evauation += 400;
break;
case Piece.Type.Rook:
evauation += 800;
break;
case Piece.Type.Queen:
evauation += 1500;
break;
case Piece.Type.Guard:
evauation += 200;
break;
case Piece.Type.Hawk:
evauation += 1000;
break;
case Piece.Type.Archbishop:
evauation += 1000;
break;
case Piece.Type.Chancellor:
evauation += 1300;
break;
case Piece.Type.Amazon:
evauation += 2000;
break;
}
}
else
{
switch (gameState.pieces[i].binary & Piece.Type._MASK)
{
case Piece.Type.Pawn:
evauation += -100;
//if (gameState.promotionRankBlack != null)
//{
// evauation -= (int)(gameState.promotionRankBlack-gameState.pieces[i].y);
//}
break;
case Piece.Type.Knight:
evauation += -300;
break;
case Piece.Type.Bishop:
evauation += -400;
break;
case Piece.Type.Rook:
evauation += -800;
break;
case Piece.Type.Queen:
evauation += -1500;
break;
case Piece.Type.Guard:
evauation += -200;
break;
case Piece.Type.Hawk:
evauation += -1000;
break;
case Piece.Type.Archbishop:
evauation += -1000;
break;
case Piece.Type.Chancellor:
evauation += -1300;
break;
case Piece.Type.Amazon:
evauation += -2000;
break;
}
}
}
return evauation;
}
}