-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPositionDecoder.cs
236 lines (204 loc) · 7.42 KB
/
PositionDecoder.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
namespace omnipede;
using System.Diagnostics;
using System.Text.Json;
using System.Text.RegularExpressions;
partial class PositionDecoder
{
public static int Test(int x)
{
x += 10;
return x;
}
public static Position DecodePosition(string filePath)
{
string json = File.ReadAllText(filePath);
string[] positonAray;
string[] splitJson = Regex.Split(json, @"{|}");
try
{
positonAray = Regex.Split(splitJson[2], @"\n");
}
catch(IndexOutOfRangeException)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("IndexOutOfRangeException When Loading Position");
Console.WriteLine("Paste Game Data Into omnipede-input.json Then Try Again");
Console.WriteLine("Press Any Key to Close The Program");
Console.ReadKey();
Environment.Exit(0);
positonAray = Regex.Split(splitJson[2], @"\n");
}
List<string> positonList = positonAray.ToList();
positonList.RemoveAt(0);
positonList.RemoveAt(positonList.Count -1 );
List<Piece> startingPosition = new();
for (int i = 0; i < positonList.Count; i++)
{
startingPosition.Add(new Piece(positonList[i]));
}
json = Regex.Replace(json, @"\n", "");
List<string> encodedPlys = Regex.Split(json, "}[^{]*").ToList();
encodedPlys.RemoveAt(encodedPlys.Count-1);
encodedPlys.RemoveAt(0);
List<ExternalPly> plys = new();
for (int i = 0; i < encodedPlys.Count; i++)
{
encodedPlys[i] = encodedPlys[i].Replace(" ","");
plys.Add(new ExternalPly(encodedPlys[i]));
}
return new(startingPosition, plys);
}
}
public class Position{
public List<Piece> pieces { get; set; }
public bool whitesTurn { get; set; } = true;
public Piece? enPassentable { get; set; }
public Position Clone()
{
return new Position(this);
}
public Position(Position positionToClone)
{
whitesTurn = positionToClone.whitesTurn;
if (positionToClone.enPassentable != null)
{
enPassentable = new Piece(positionToClone.enPassentable);
}
else
{
enPassentable = null;
}
pieces = new();
for (int i = 0; i < positionToClone.pieces.Count; i++)
{
pieces.Add(new Piece(positionToClone.pieces[i]));
}
}
public Position(List<Piece> startingPosition, List<ExternalPly> plys)
{
pieces = startingPosition;
for (int i = 0; i < plys.Count; i++)
{
enPassentable = null;
whitesTurn = !whitesTurn;
for (int j = 0; j < pieces.Count; j++)
{
//Console.WriteLine(JsonSerializer.Serialize(plys[i].startCoords, new JsonSerializerOptions { WriteIndented = true }));
//Console.WriteLine(j);
//Console.WriteLine(pieces.Count);
if (plys[i].endCoords[0] == pieces[j].xCoord & plys[i].endCoords[1] + plys[i].enPassentOffset == pieces[j].yCoord)
{
pieces.RemoveAt(j);
if (j == pieces.Count)
{
j-= 1;
}
}
//Console.WriteLine(j);
//Console.WriteLine(pieces.Count);
//Console.WriteLine(JsonSerializer.Serialize(pieces[j], new JsonSerializerOptions { WriteIndented = true }));
if (plys[i].startCoords[0] == pieces[j].xCoord & plys[i].startCoords[1] == pieces[j].yCoord)
{
pieces.RemoveAt(j);
//j--;
if (plys[i].promotionPiece == null)
{
pieces.Insert(0, new Piece(plys[i].endCoords[0], plys[i].endCoords[1], plys[i].type, true));
}
else
{
pieces.Insert(0, new Piece(plys[i].endCoords[0], plys[i].endCoords[1], plys[i].promotionPiece, true));
}
if ((plys[i].type == "pawnsW" | plys[i].type == "pawnsB") & Math.Abs(plys[i].endCoords[1]-plys[i].startCoords[1]) == 2)
{
enPassentable = pieces[0];
}
}
}
}
}
}
public class Piece{
public long xCoord { get; set; }
public long yCoord { get; set; }
public string type { get; set; }
public bool hasMoved { get; set; } = false;
public Piece Clone()
{
return new Piece(this);
}
public Piece(Piece pieceToClone)
{
xCoord = pieceToClone.xCoord;
yCoord = pieceToClone.yCoord;
type = pieceToClone.type;
hasMoved = pieceToClone.hasMoved;
}
public Piece(long xCoordNew, long yCoordNew, string typeNew, bool hasMovedNew)
{
xCoord = xCoordNew;
yCoord = yCoordNew;
type = typeNew;
hasMoved = hasMovedNew;
}
public Piece(string piece)
{
piece = Regex.Replace(piece, @" |""|\n|\u000D", "");
string[] pieceAray = Regex.Split(piece, @",|:");
xCoord = long.Parse(pieceAray[0]);
yCoord = long.Parse(pieceAray[1]);
type = pieceAray[2];
}
}
public partial class ExternalPly{
public string type = "";
public List<long> startCoords = new();
public List<long> endCoords = new();
public long enPassentOffset = 0;
public string? captured;
public string? promotionPiece;
public ExternalPly(string encodedPly)
{
List<string> encodedPlyList = Regex.Split(encodedPly, @" |{|""|\[|\]|,|\n|:").ToList();
for (int i = 0; i < encodedPlyList.Count ; i++)
{
if (encodedPlyList[i] == ""){
encodedPlyList.RemoveAt(i);
i--;
}
else if (encodedPlyList[i].ToCharArray().Length == 0){
encodedPlyList.RemoveAt(i);
i--;
}
else if (Char.GetUnicodeCategory(encodedPlyList[i].ToCharArray()[0]).ToString() == "Control" & !int.TryParse(encodedPlyList[i], out _)){
encodedPlyList.RemoveAt(i);
i--;
}
}
for (int i = 0; i < encodedPlyList.Count; i++)
{
if (encodedPlyList[i] == "type"){
i++;
type = encodedPlyList[i];
i++; i++;
startCoords.Add(long.Parse(encodedPlyList[i]));
i++;
startCoords.Add(long.Parse(encodedPlyList[i]));
i++; i++;
endCoords.Add(long.Parse(encodedPlyList[i]));
i++;
endCoords.Add(long.Parse(encodedPlyList[i]));
}
else if (long.TryParse(encodedPlyList[i], out _)){
enPassentOffset = long.Parse(encodedPlyList[i]);
}
else if (encodedPlyList[i] == "captured"){
i++;
captured = encodedPlyList[i];
}
else{
promotionPiece = encodedPlyList[i];
}
}
}
}