-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticClass.cs
508 lines (422 loc) · 13.3 KB
/
StaticClass.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class StaticClass : MonoBehaviour
{
public static StaticClass m_sTools;
public static MouseCase m_sCase;
public Material blackCase;
public Material whiteCase;
public Material selectorCase;
public Material attackCase;
public Color white;
public Color black;
void Awake ()
{
m_sTools = this;
m_sCase = GetComponent<MouseCase>();
}
public List<Case> RookMoveSet (Case [,] board, int x, int y, bool color, bool limited)
{
List<Case> possibleCase = new List<Case>();
int right = 1;
while(true)
{
if(x + right <= 7)
{
if(board[x + right, y].currPiece != null) { possibleCase.Add(board[x + right, y]); break; }
else if(board[x + right, y].currPiece == null) possibleCase.Add(board[x + right, y]);
else break;
}
else break;
if(limited) break;
right++;
}
int left = -1;
while (true)
{
if(x + left >= 0)
{
if(board[x + left, y].currPiece != null) { possibleCase.Add(board[x + left, y]); break; }
else if(board[x + left, y].currPiece == null) possibleCase.Add(board[x + left, y]);
else break;
}
else break;
if(limited) break;
left--;
}
int top = 1;
while(true)
{
if(y + top <= 7)
{
if(board[x, y + top].currPiece != null) { possibleCase.Add(board[x, y + top]); break; }
else if(board[x, y + top].currPiece == null) possibleCase.Add(board[x, y + top]);
else break;
}
else break;
if(limited) break;
top++;
}
int bot = -1;
while(true)
{
if(y + bot >= 0)
{
if(board[x, y + bot].currPiece != null) { possibleCase.Add(board[x, y + bot]); break; }
else if(board[x, y + bot].currPiece == null) possibleCase.Add(board[x, y + bot]);
else break;
}
else break;
if(limited) break;
bot--;
}
return possibleCase;
}
public List<Case> BishopMoveSet (Case [,] board, int x, int y, bool color, bool limited)
{
List<Case> possibleCase = new List<Case>();
//top right as black
int right = 1;
while(true)
{
if(x + right <= 7 && y + right <= 7)
{
if (board[x + right, y + right].currPiece != null) { possibleCase.Add(board[x + right, y + right]); break; }
else if (board[x + right, y + right].currPiece == null) possibleCase.Add(board[x + right, y + right]);
else break;
}
else break;
if(limited) break;
right++;
}
//top left as black
int left = -1;
while (true)
{
if(x + left >= 0 && y - left <= 7)
{
if (board[x + left, y - left].currPiece != null) { possibleCase.Add(board[x + left, y - left]); break; }
else if (board[x + left, y - left].currPiece == null) possibleCase.Add(board[x + left, y - left]);
else break;
}
else break;
if(limited) break;
left--;
}
//bot right as black
int top = 1;
while(true)
{
if(x + top <= 7 && y - top >= 0)
{
if (board[x + top, y - top].currPiece != null) { possibleCase.Add(board[x + top, y - top]); break; }
else if (board[x + top, y - top].currPiece == null) possibleCase.Add(board[x + top, y - top]);
else break;
}
else break;
if(limited) break;
top++;
}
//bot left as black
int bot = -1;
while(true)
{
if(x + bot >= 0 && y + bot >= 0)
{
if (board[x + bot, y + bot].currPiece != null) { possibleCase.Add(board[x + bot, y + bot]); break; }
else if (board[x + bot, y + bot].currPiece == null) possibleCase.Add(board[x + bot, y + bot]);
else break;
}
else break;
if(limited) break;
bot--;
}
return possibleCase;
}
public List<Case> KnightMoveSet (Case [,] board, int x, int y, bool color)
{
List<Case> possibleCase = new List<Case>();
if (x + 2 <= 7 && y + 1 <= 7) possibleCase.Add(board[x + 2, y + 1]);
if (x - 2 >= 0 && y + 1 <= 7) possibleCase.Add(board[x - 2, y + 1]);
if (x + 2 <= 7 && y - 1 >= 0) possibleCase.Add(board[x + 2, y - 1]);
if (x - 2 >= 0 && y - 1 >= 0) possibleCase.Add(board[x - 2, y - 1]);
if (x + 1 <= 7 && y + 2 <= 7) possibleCase.Add(board[x + 1, y + 2]);
if (x - 1 >= 0 && y + 2 <= 7) possibleCase.Add(board[x - 1, y + 2]);
if (x + 1 <= 7 && y - 2 >= 0) possibleCase.Add(board[x + 1, y - 2]);
if (x - 1 >= 0 && y - 2 >= 0) possibleCase.Add(board[x - 1, y - 2]);
return possibleCase;
}
public void RemoveEchecCase(Case toMove, List<Case> possibleCase, bool color, Case [,] board)
{
//do the plays for each case, check each one out of this, if there is a bad case out of this, delete it
for (int i = possibleCase.Count - 1; i >= 0; i--)
{
if(possibleCase[i].currPiece != null && possibleCase[i].currPiece.color == color) continue;
if(PieceEchec(board, new Simulation(toMove, possibleCase[i]), (color == true ? m_sCase.BlackKing : m_sCase.WhiteKing), true) != null) possibleCase.RemoveAt(i);
}
}
public int GetPossibleMove (bool color)
{
int possible = 0;
foreach(ChessMan chess in m_sCase.Pieces)
{
if(chess.color == color)
{
foreach(Case c in chess.GetPossibleMovement(m_sCase.setupBoard.board))
{
if(c.currPiece == null) possible++;
else if(c.currPiece.color != chess.color) possible++;
}
}
}
return possible;
}
public List<Case> GetListOfPossibleMove (bool color)
{
List<Case> moves = new List<Case>();
foreach(ChessMan chess in m_sCase.Pieces){ if(chess.color == color) { moves.AddRange(chess.GetPossibleMovement(m_sCase.setupBoard.board)); } }
return moves;
}
public EchecThreat PieceEchec(Case [,] board, ChessMan Target, ChessMan deleted = null)
{
if(Target == null) return null;
EchecThreat threat = new EchecThreat(Target);
foreach(ChessMan chess in m_sCase.Pieces)
{
if(deleted != null && deleted == chess) continue;
if(chess.color != Target.color)
{
List<Case> moves = chess.GetPossibleMovement(board, false);
foreach(Case c in moves)
{
//here it means if we have a possible movement case non null with the ennemy king on it
if(c.currPiece != null && c.currPiece == Target)
{
threat.Add(chess);
}
}
}
}
threat.SortThreats();
return threat.threater.Count > 0 ? threat : null;
}
public EchecThreat PieceEchec (Case [,] board, Simulation sim, bool OneShotSimulation = false)
{
sim.Simulate();
EchecThreat threat = new EchecThreat(sim.targetCase.currPiece);
foreach(ChessMan chess in m_sCase.Pieces)
{
if(sim.deletedBackup != null && sim.deletedBackup == chess) continue;
if(chess.color != sim.targetCase.currPiece.color)
{
List<Case> moves = chess.GetPossibleMovement(board, false);
foreach(Case c in moves)
{
//here it means if we have a possible movement case non null with the target on it
if(c.currPiece != null && c.currPiece == sim.targetCase.currPiece)
{
threat.Add(chess);
}
}
}
}
threat.SortThreats();
if(OneShotSimulation) sim.Delete();
return threat.threater.Count > 0 ? threat : null;
}
public EchecThreat PieceEchec (Case [,] board, Simulation sim, ChessMan Target, bool OneShotSimulation = false)
{
sim.Simulate();
EchecThreat threat = new EchecThreat(sim.targetCase.currPiece);
foreach(ChessMan chess in m_sCase.Pieces)
{
if(sim.deletedBackup != null && sim.deletedBackup == chess) continue;
if(chess.color != Target.color)
{
List<Case> moves = chess.GetPossibleMovement(board, false);
foreach(Case c in moves)
{
//here it means if we have a possible movement case non null with the target on it
if(c.currPiece != null && c.currPiece == Target) threat.Add(chess);
}
}
}
threat.SortThreats();
if(OneShotSimulation) sim.Delete();
return threat.threater.Count > 0 ? threat : null;
}
public Decision Evaluate (ChessMan chess, Case [,] board)
{
int x = chess.position.x;
int y = chess.position.y;
List<Case> possibleCase = chess.GetPossibleMovement(board);
List<underDecision> underDecs = new List<underDecision>();
int initialsMoves = GetListOfPossibleMove(chess.color).Count;
EchecThreat isThreatened = PieceEchec(board, chess);
foreach(Case c in possibleCase)
{
underDecision d = new underDecision(0, c);
//here we found out that we can get a special piece with a priority, but take care, it might put us in a bad situation
if(c.currPiece != null && c.currPiece.color != chess.color){ d.value += c.currPiece.Priority; d.SetReason("getting piece with priority : " + c.currPiece.Priority); }
//that's a bad decision and even more, an unaceptable move since its our own piece, dont even listen to this
else if(c.currPiece != null) continue;
//create a simulation that we will simulate later on
Simulation s = new Simulation(board[x,y], c);
//i am already in echec
if(isThreatened != null)
{
//simulate, so we can check if i can find some good situations
//this move can send me out of echec, we should consider it
if(PieceEchec(board, s) == null){ d.value += chess.Priority; d.SetReason("getting out of echec add : " + chess.Priority); Debug.Log(chess.ToString() + " not getting out of echec ??" + d.value); }
//this move cant get me ouf of echec, we delete some priority to it, but it might be usefull on a certain sutaiton
else { d.value -= chess.Priority; d.SetReason("cant get out, del : " + chess.Priority); }
}
else
{
//i was not in an echec move but im going into it? remove some value to this move
if(PieceEchec(board, s) != null) { d.value -= chess.Priority; d.SetReason("going in echec intentionnaly del : " + chess.Priority); }
}
//get all of the move that come from our team after the simulation
List<Case> moves = GetListOfPossibleMove(chess.color);
bool covered = false;
foreach(Case cover in moves) { if(cover == c) covered = true; }
// calculate how many moves we opened up by doing this (because it means we have more possible movements out of this
int difference = (moves.Count - initialsMoves) / 2;
d.value += difference; d.SetReason("add some cases to play" + difference);
if(StaticClass.m_sTools.GetPossibleMove(!chess.color) <= 0){ d.value += 1000; d.SetReason("put ennemy in mat"); }
//now found out if we can put ennemy pieces in echec out of this move
List<Case> SimulatedMove = chess.GetPossibleMovement(m_sCase.setupBoard.board, false);
foreach(Case simulatedCase in SimulatedMove)
{
EchecThreat simulatedThreat = PieceEchec(board,simulatedCase.currPiece);
if(simulatedCase.currPiece != null && simulatedCase.currPiece.color != chess.color)
{
//we can actually put a piece in echec out of this, that's pretty good, even more if we are covered by another piece
d.value += simulatedCase.currPiece.Priority / (covered == true ? 2 : 10);
d.SetReason("put an piece in echec : " + (simulatedCase.currPiece.Priority / (covered == true ? 2 : 10)));
}
//if we can cover an ally piece that is in echec do it
else if(simulatedCase.currPiece != null && simulatedThreat != null)
{
d.value += simulatedCase.currPiece.Priority / 2;
d.SetReason("helped a piece in echec : " + simulatedCase.currPiece.Priority / 2);
}
}
s.Delete();
underDecs.Add(d);
}
underDecision bestUdec = null;
foreach(underDecision d in underDecs)
{
if(bestUdec == null) bestUdec = d;
else if(bestUdec.value < d.value) bestUdec = d;
else if(bestUdec.value == d.value)
{
if(Random.Range(0,2) == 1) bestUdec = d;
}
}
return new Decision(bestUdec, chess);
}
}
public class Decision
{
public ChessMan chess;
public underDecision decision;
public Decision () {}
public Decision(underDecision dec, ChessMan piece)
{
chess = piece;
decision = dec;
}
public Decision (int val, Case deci, ChessMan piece)
{
decision.value = val;
decision.choice = deci;
chess = piece;
}
}
public class underDecision
{
public int value;
public string apparentReason;
public Case choice;
public underDecision(int val, Case c)
{
value = val;
choice = c;
}
public void SetReason (string reason)
{
apparentReason += " and : " + reason;
}
}
public class Simulation
{
public ChessMan deletedBackup;
public Case baseCase;
public Case targetCase;
public Simulation (Case bC, Case tC)
{
targetCase = tC;
deletedBackup = tC.currPiece;
baseCase = bC;
}
public void Simulate ()
{
targetCase.currPiece = baseCase.currPiece;
targetCase.currPiece.position = targetCase.position;
baseCase.currPiece = null;
}
public void Delete ()
{
baseCase.currPiece = targetCase.currPiece;
baseCase.currPiece.position = baseCase.position;
targetCase.currPiece = deletedBackup;
}
}
public class EchecThreat
{
public List<ChessMan> threater = new List<ChessMan>();
public ChessMan threatened;
public EchecThreat (ChessMan _threatened)
{
threatened = _threatened;
}
public void Add(ChessMan _threater)
{
threater.Add(_threater);
}
public void SortThreats ()
{
int ndebut = 0;
int nfin = threater.Count - 1;
while (ndebut < nfin)
{
int debut = ndebut;
int fin = nfin;
ndebut = threater.Count;
for (int i = debut; i < fin; i++)
{
if (threater[i].Priority > threater[i + 1].Priority)
{
ChessMan chess = threater[i];
threater[i] = threater[i + 1];
threater[i + 1] = chess;
if (i < ndebut)
{
ndebut = i - 1;
if (ndebut < 0)
{
ndebut = 0;
}
}
else if (i > nfin)
{
nfin = i + 1;
}
}
}
}
}
}