-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhexchess_engine.cs
1555 lines (1306 loc) · 62 KB
/
hexchess_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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Newtonsoft.Json;
namespace HexC
{
public enum PiecesEnum { Pawn, Elephant, Castle, Queen, King }
public enum ColorsEnum { White, Tan, Black }
// these numbers are the axial coordinate system on a flat-topped hex board.
// let's change them to pointy-topped! (geez, i hope i did this.)
// http://www.redblobgames.com/grids/hexagons/
class PawnStatic : PieceStatic
{
public static BoardLocationList CouldGoIfOmnipotent(BoardLocation fromHere) // SAME AS KING
{
int[,] JumpOptions = {
{ 0, -1 }, { 1, -1 }, {1, 0 }, {0,1 }, {-1, 1 }, {-1,0 }
};
return CookUpLocations(fromHere, JumpOptions);
}
}
class KingStatic : PieceStatic
{
public static BoardLocationList CouldGoIfOmnipotent(BoardLocation fromHere)
{
int[,] JumpOptions = {
{ 0, -1 }, { 1, -1 }, {1, 0 }, {0,1 }, {-1, 1 }, {-1,0 }
};
return CookUpLocations(fromHere, JumpOptions);
}
}
class KnightStatic : PieceStatic
{
public static BoardLocationList CouldGoIfOmnipotent(BoardLocation fromHere)
{
int[,] JumpOptions = {
{ 1, -3 }, { 2, -3 }, { -2, -1 }, { -1, -2 }, { 3, -2 }, { 3, -1 },
{ 2, 1 }, { 1, 2 }, { -2, 3 }, {-1, 3 }, {-3, 1 }, {-3, 2 }
};
return CookUpLocations(fromHere, JumpOptions);
}
}
class CastleStatic : PieceStatic
{
public static List<BoardLocationList> ListOfSequencesOfSpots(BoardLocation loc)
{
List<BoardLocationList> ll = new List<BoardLocationList>();
// SEQUENCE of these matters, radiating out from the piece.
int[,] StarOne = { { 1, -1 }, { 2, -2 }, { 3, -3 }, { 4, -4 }, { 5, -5 }, { 6, -6 }, { 7, -7 }, { 8, -8 }, { 9, -9 }, { 10, -10 } };
int[,] StarTwo = { { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 }, { 5, 0 }, { 6, 0 }, { 7, 0 }, { 8, 0 }, { 9, 0 }, { 10, 0 } };
int[,] StarThr = { { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 }, { 0, 10 } };
int[,] StarFou = { { -1, 1 }, { -2, 2 }, { -3, 3 }, { -4, 4 }, { -5, 5 }, { -6, 6 }, { -7, 7 }, { -8, 8 }, { -9, 9 }, { -10, 10 } };
int[,] StarFiv = { { -1, 0 }, { -2, 0 }, { -3, 0 }, { -4, 0 }, { -5, 0 }, { -6, 0 }, { -7, 0 }, { -8, 0 }, { -9, 0 }, { -10, 0 } };
int[,] StarSix = { { 0, -1 }, { 0, -2 }, { 0, -3 }, { 0, -4 }, { 0, -5 }, { 0, -6 }, { 0, -7 }, { 0, -8 }, { 0, -9 }, { 0, -10 } };
ll.Add(CookUpLocations(loc, StarOne));
ll.Add(CookUpLocations(loc, StarTwo));
ll.Add(CookUpLocations(loc, StarThr));
ll.Add(CookUpLocations(loc, StarFou));
ll.Add(CookUpLocations(loc, StarFiv));
ll.Add(CookUpLocations(loc, StarSix));
return ll;
}
}
class PieceStatic
{
static protected BoardLocationList CookUpLocations(BoardLocation fromHere, int[,] jumpOpts)
{
BoardLocationList spots = new BoardLocationList();
for (int iSet = 0; iSet < jumpOpts.GetLength(0); iSet++)
{
BoardLocation b = new BoardLocation(fromHere, new BoardLocation(jumpOpts[iSet, 0], jumpOpts[iSet, 1]));
spots.Add(b);
}
return spots;
}
}
public class BoardLocation
{
public BoardLocation(int q, int r)
{
this.q = q;
this.r = r;
}
public BoardLocation(BoardLocation b1, BoardLocation b2)
{
this.q = b1.q + b2.q;
this.r = b1.r + b2.r;
}
public bool IsValidLocation()
{
if (q > 5) return false;
if (r > 5) return false;
if (q < -5) return false;
if (r < -5) return false;
if (q + r > 5) return false;
if (q + r < -5) return false;
return true;
}
static public bool IsSameLocation(BoardLocation one, BoardLocation two)
{
if (one.Q == two.Q)
if (one.R == two.R)
return true;
return false;
}
public bool IsPortal
{
get
{
if (q == 0 && r == 0)
return true;
return false;
}
}
public int Q { get { return q; } }
public int R { get { return r; } }
int q;
int r;
}
public class BoardLocationList : List<BoardLocation>
{
public bool ContainsTheLocation(BoardLocation bToMatch)
{
foreach (BoardLocation bl in this)
{
if (bl.Q == bToMatch.Q)
if (bl.R == bToMatch.R)
return true;
}
return false;
}
}
public class Piece
{
public Piece(PiecesEnum pt, ColorsEnum c)
{
this.pieceType = pt;
this.pieceColor = c;
}
protected PiecesEnum pieceType;
protected ColorsEnum pieceColor;
public PiecesEnum PieceType { get { return this.pieceType; } }
public ColorsEnum Color { get { return this.pieceColor; } }
public char ToChar() { return PieceType.ToString()[0]; }
}
public class PieceList : List<Piece>
{
/*
public void RemoveThePiece(PiecesEnum pt, ColorsEnum c)
{
foreach (var item in this)
{
if (item.PieceType == pt)
if (item.Color == c)
{
this.Remove(item);
return;
}
Debug.Assert(false); // why would we ask for a piece that isn't in this set?
}
}
*/
public bool ContainsThePiece(PiecesEnum pt, ColorsEnum c)
{
foreach (var item in this)
{
if (item.PieceType == pt)
if (item.Color == c)
return true;
}
return false;
}
}
public class PlacedPiece : Piece
{
public PlacedPiece(PiecesEnum pt, ColorsEnum c, int q, int r) : base(pt, c)
{
this.q = q;
this.r = r;
}
public PlacedPiece(PlacedPiece p, BoardLocation bl) : base(p.PieceType, p.pieceColor) // clone me but to a new spot
{
this.q = bl.Q;
this.r = bl.R;
}
public BoardLocation Location { get { return new BoardLocation(q, r); } }
public bool DeepEquals(PlacedPiece p)
{
if (this.pieceColor != p.pieceColor) return false;
if (this.pieceType != p.pieceType) return false;
if (this.q != p.q) return false;
if (this.r != p.r) return false;
return true;
}
// my happy variables
int q;
int r;
}
enum EventTypeEnum { Add, Remove };
class PieceEvent
{
public PieceEvent(PlacedPiece p, EventTypeEnum t)
{ this.p = p; this.t = t; }
PlacedPiece p;
EventTypeEnum t;
public PlacedPiece Regarding { get { return p; } }
public EventTypeEnum EventType { get { return t; } }
}
class Board
{
// MEMBERS
List<PlacedPiece> placedPieces = new List<PlacedPiece>();
PieceList sidelined = new PieceList();
BoardLocationList highlightedSpots = new BoardLocationList(); // purely cosmetic, only used by Windows Form
public BoardLocationList HighlightedSpots { get { return highlightedSpots; } }
// PROPERTIES
public List<PlacedPiece> PlacedPieces { get { return placedPieces; } }
// CONSTRUCTORS
public Board() { }
public Board(Board cloneMe)
{
foreach (PlacedPiece p in cloneMe.placedPieces)
{
placedPieces.Add(p);
}
foreach (Piece p in cloneMe.sidelined)
{
sidelined.Add(p);
}
}
// METHODS
public void Add(Piece p) // sidelined piece on board for color
{
sidelined.Add(p);
}
public void Add(PlacedPiece p)
{
// i'm gonna prevent you from adding an impossible number of pieces,
// just cuz i roll that way.
switch (p.PieceType)
{
case PiecesEnum.King:
// if this is the king we're adding, well, let's make sure there isn't one already.
Debug.Assert(null == this.FindPiece(PiecesEnum.King, p.Color));
break;
case PiecesEnum.Queen:
Debug.Assert(null == this.FindPiece(PiecesEnum.Queen, p.Color));
break;
// others need checks
}
// And will I let you place a piece on top of another piece? Fuck no right?
// Debug.Assert(null == this.AnyoneThere(p.Location));
placedPieces.Add(p);
}
public void Remove(PlacedPiece p)
{
foreach (var placed in placedPieces)
{
if (placed.DeepEquals(p))
{
placedPieces.Remove(placed);
return;
}
}
Debug.Assert(false); // hey why remove what isn't there?
}
public List<PlacedPiece> PlacedPiecesThisColor(ColorsEnum col)
{
List<PlacedPiece> myCol = new List<PlacedPiece>();
foreach (PlacedPiece p in placedPieces)
{
if (p.Color == col)
myCol.Add(p);
}
return myCol;
}
public void Highlight(BoardLocation loc)
{
// we just record these locations and flash a designator on them
highlightedSpots.Add(loc);
}
public PlacedPiece AnyoneThere(BoardLocation b)
{
foreach (PlacedPiece pp in placedPieces)
{
if (pp.Location.Q == b.Q)
if (pp.Location.R == b.R)
return pp;
}
return null;
}
BoardLocationList YankSpotsThatArentBoardSpots(BoardLocationList options, bool isAKing = false)
{
BoardLocationList realOptions = new BoardLocationList();
foreach (BoardLocation b in options)
{
if (b.IsValidLocation())
{
if (b.IsPortal)
if (false == isAKing)
continue; // zero zero is only valid for a king.
realOptions.Add(b);
}
}
return realOptions;
}
BoardLocationList YankEmptySpots(BoardLocationList spots)
{
BoardLocationList nonEmptySpots = new BoardLocationList();
foreach (var spot in spots)
{
if (null != AnyoneThere(spot))
nonEmptySpots.Add(spot);
}
return nonEmptySpots;
}
BoardLocationList YankSpotsOccupiedByThisColor(BoardLocationList options, ColorsEnum? c) // can be NULL for ANY color
{
BoardLocationList realOptions = new BoardLocationList();
foreach (BoardLocation b in options)
{
PlacedPiece p = AnyoneThere(b);
if (null != p) // there's a piece
if ((c == null) || // there's a piece and i yank pieces of any color
(p.Color == c)) // there's a piece of a color same as specified
continue;
realOptions.Add(b);
}
return realOptions;
}
PlacedPiece FindPiece(PiecesEnum type, ColorsEnum c)
{
Debug.Assert(type == PiecesEnum.King || type == PiecesEnum.Queen);
foreach (PlacedPiece p in this.placedPieces)
{
if (p.PieceType == type)
if (p.Color == c)
return p;
}
return null;
}
BoardLocationList YankSpotsThatPutMeInCheck(BoardLocationList options, PlacedPiece p)
{
// Console.WriteLine("A {1} {0} removes spots that put their team in check.", p.PieceType.ToString(), p.Color.ToString());
BoardLocationList realOptions = new BoardLocationList();
foreach (BoardLocation bl in options)
{
// Make a hypothetical board
Board bHypothetical = new Board(this); // clone meeeee
bHypothetical.Remove(p); // take me off.
if (null != bHypothetical.AnyoneThere(bl))
bHypothetical.Remove(bHypothetical.AnyoneThere(bl));
bHypothetical.Add(new PlacedPiece(p, bl)); // put me on at the destination
// I wanna see this board.
Program.ShowBoard(bHypothetical);
// WHAT IF THE KING MOVES INTO CHECK BUT ALSO MOVES TO 0,0 FOR THE WIN??
if (false == bHypothetical.InCheck(p.Color)) // see if i'm in check
realOptions.Add(bl);
}
return realOptions;
}
bool CanAttack(PlacedPiece p, PlacedPiece pVictim)
{
if (p.Color == pVictim.Color)
return false;// can't attack my kind.
// Console.WriteLine("A {3} {0} at {1},{2} wonders where it can reach.", p.PieceType, p.Location.Q, p.Location.R, p.Color);
BoardLocationList options = WhereCanIReach(p);
if (options.ContainsTheLocation(pVictim.Location))
return true;
return false;
}
// Does the placed piece threaten the specific king NOW? How can we tell if we don't know that it puts us in check?
bool CanAttackNowNoRecurse(PlacedPiece p, PlacedPiece pVictim)
{
if (p.Color == pVictim.Color)
return false;// can't attack my kind.
// Console.WriteLine("A {3} {0} at {1},{2} wonders where it can reach.", p.PieceType, p.Location.Q, p.Location.R, p.Color);
BoardLocationList options = WhereCanIReach(p, false);
if (options.ContainsTheLocation(pVictim.Location))
return true;
return false;
}
bool InCheck(ColorsEnum c)
{
// if any piece can reach me, I'm in check.
PlacedPiece king = FindPiece(PiecesEnum.King, c);
// ok, sometimes there's no king on the board.
// mostly cuz people are just playing.
// so say they aren't in check in that case.
if (null == king)
{
Console.WriteLine("Some joker has no king on the board, so you can't put that joker into check.");
return false;
}
foreach (PlacedPiece p in placedPieces)
{
// if (CanAttack(p, king))
if (CanAttackNowNoRecurse(p, king))
return true;
}
return false;
}
// a shallow check sees where i can reach without regard to whetehr it puts me into check doing it.
// (a king can't end up in a position where i can reach it, even if me doing so is prevented cuz the move would put me in check)
BoardLocationList WhereCanIReach(PlacedPiece p, bool fShallowCheck = true)
{
switch (p.PieceType)
{
case PiecesEnum.Elephant:
{
BoardLocationList options = KnightStatic.CouldGoIfOmnipotent(p.Location);
options = YankSpotsThatArentBoardSpots(options);
options = YankSpotsOccupiedByThisColor(options, p.Color);
if (fShallowCheck)
options = YankSpotsThatPutMeInCheck(options, p);
return options;
}
case PiecesEnum.King:
{
// THE POSITION OF THE QUEEN IS TECHNICALLY A POTENTIAL DESTINATION OF THE KING
// HOWEVER, THIS CODE WON'T REPORT IT, BECAUSE IT'S handled as a THEORETICAL BOARD
// RE-RUN elsewhere.
BoardLocationList spots = KingStatic.CouldGoIfOmnipotent(p.Location);
spots = YankSpotsThatArentBoardSpots(spots, true); // true for the king!
spots = YankSpotsOccupiedByThisColor(spots, p.Color); // diddily doo is not handled here!
if (fShallowCheck)
spots = YankSpotsThatPutMeInCheck(spots, p);
return spots;
}
case PiecesEnum.Pawn:
{
BoardLocationList stepSpots = PawnStatic.CouldGoIfOmnipotent(p.Location);
stepSpots = YankSpotsThatArentBoardSpots(stepSpots);
stepSpots = YankSpotsOccupiedByThisColor(stepSpots, null); // Yank spots occupied AT ALL
BoardLocationList attackWalkSpots = WalksToStarSpots(p.Location, false);
attackWalkSpots = YankEmptySpots(attackWalkSpots);
attackWalkSpots = YankSpotsOccupiedByThisColor(attackWalkSpots, p.Color);
BoardLocationList allSpots = new BoardLocationList();
foreach (var spot in stepSpots)
allSpots.Add(spot);
foreach (var spot in attackWalkSpots)
allSpots.Add(spot);
if (fShallowCheck)
allSpots = YankSpotsThatPutMeInCheck(allSpots, p);
return allSpots;
}
case PiecesEnum.Queen: // a queen is a castle plus other magic!
case PiecesEnum.Castle:
{
// radiating outward seems to make more sense.
// but how do i represent three directions?
// by changing q, changing r, and changing q&r
// really seeking six potential victims or same-teams.
// victims i can step on, same-team i cannot, but both stop my advance.
// +q, -q, +r, -r, +(q&r), -(q&r)
// just do six
BoardLocationList options = new BoardLocationList();
List<BoardLocationList> listOfRuns = CastleStatic.ListOfSequencesOfSpots(p.Location);
foreach (BoardLocationList ll in listOfRuns)
{
foreach (BoardLocation l in ll)
{
if (l.IsPortal)
break; // run is over!
PlacedPiece pThere = AnyoneThere(l);
if (null == pThere)
{
options.Add(l);
continue;
}
if (pThere.Color == p.Color)
break; // same-color piece, so i can't move here, and run is OVER.
options.Add(pThere.Location);
break; // run is over!
}
}
options = YankSpotsThatArentBoardSpots(options);
if (PiecesEnum.Castle == p.PieceType)
{
if (fShallowCheck)
options = YankSpotsThatPutMeInCheck(options, p);
return options;
}
// IF WE GET THIS FAR, THE PIECE IS A QUEEN.
m_QueenDesties = new BoardLocationList();
BoardLocationList soFar = new BoardLocationList();
soFar.Add(p.Location); // my first location is where I'm at
WalkToSameColorStarSpots(0, soFar);
foreach (BoardLocation b in m_QueenDesties)
{
// ok, the results don't know if we're tryin to attack our own piece. we can't do that.
PlacedPiece pieceInDanger = this.AnyoneThere(b);
if (null != pieceInDanger)
if (p.Color == pieceInDanger.Color)
continue;
// special case: is this the portal? is it empty? then it's not a valid destination.
if (b.IsPortal && null == this.AnyoneThere(b))
continue;
options.Add(b);
}
if (fShallowCheck)
options = YankSpotsThatPutMeInCheck(options, p);
return options;
}
default:
Debug.Assert(false);
return null;
}
}
BoardLocationList WalksToStarSpots(BoardLocation fromHere, bool fSpotMustBeEmpty)
{
BoardLocationList destys = new BoardLocationList();
int[,] WalkOptions = { { 1, -2 }, { 2, -1 }, { 1, 1 }, { -1, 2 }, { -2, 1 }, { -1, -1 } };
int[,] RouteOne = { { 0, -1 }, { 1, -1 }, { 1, 0 }, { 0, 1 }, { -1, 1 }, { -1, 0 } };
int[,] RouteTwo = { { 1, -1 }, { 1, 0 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { 0, -1 } };
for (int iSet = 0; iSet < WalkOptions.GetLength(0); iSet++)
{
BoardLocation to = new BoardLocation(fromHere.Q + WalkOptions[iSet, 0], fromHere.R + WalkOptions[iSet, 1]);
if (true == to.IsValidLocation())
if ((false == fSpotMustBeEmpty) || (null == this.AnyoneThere(to))) /// we can't fucking land on our own piece tho! this is a current bug where it appears i can take my own piece.
{
PlacedPiece one = this.AnyoneThere(new BoardLocation(fromHere.Q + RouteOne[iSet, 0], fromHere.R + RouteOne[iSet, 1]));
PlacedPiece two = this.AnyoneThere(new BoardLocation(fromHere.Q + RouteTwo[iSet, 0], fromHere.R + RouteTwo[iSet, 1]));
if (null == one || null == two)
destys.Add(to);
}
}
return destys;
}
public static BoardLocationList m_QueenDesties = new BoardLocationList();
void WalkToSameColorStarSpots(int level, BoardLocationList pathSoFar)
{
// if (level == -1)
// return;
++level;
if (4 == level)
{
// on our third step, we ask: am i cool with these three steps?
// 1. can't be any duplicates.
int[,] Spots = { { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 2 }, { 1, 3 }, { 2, 3 } };
for (int iSet = 0; iSet < Spots.GetLength(0); iSet++)
{
if (BoardLocation.IsSameLocation(pathSoFar[Spots[iSet, 0]], pathSoFar[Spots[iSet, 1]]))
return;
}
// 2. if no duplicates, then is this destination in our global list of available destinations?
if (false == m_QueenDesties.ContainsTheLocation(pathSoFar[3]))
{
// my last step allows attacks... but never on my color. Still I accidentally include same color landings in this set.
m_QueenDesties.Add(pathSoFar[3]);
}
// level = -1;
return;
}
// The queen speical move path finder is recursive. On the 3rd level, it isn't looking for hop spots,
// it's finding a destination. So it can be occupied. It can't be my piece, but I sort that out in the calling code
bool fEmptyOnly = true;
if (level == 3)
fEmptyOnly = false;
foreach (var spot in WalksToStarSpots(pathSoFar[pathSoFar.Count - 1], fEmptyOnly)) // landing spot must be empty, was true always, now empty only except on last step?
{
pathSoFar.Add(spot);
WalkToSameColorStarSpots(level, pathSoFar);
pathSoFar.RemoveAt(pathSoFar.Count - 1);
}
}
List<PieceEvent> EventsFromAMove(PlacedPiece p, BoardLocation spot)
{
List<PieceEvent> events = new List<PieceEvent>();
events.Add(new PieceEvent(p, EventTypeEnum.Remove)); // I leave this spot
// if anyone was standing where I've landed, they aren't anymore.
// AND if any of my pieces of this type are sidelined, and nobody's blocking the portal, that piece appears at 0,0!
PlacedPiece deadp = this.AnyoneThere(spot);
if (null != deadp)
{
events.Add(new PieceEvent(deadp, EventTypeEnum.Remove));
if (null != this.AnyoneThere(new BoardLocation(0, 0))) // u blockin my portal?
if (sidelined.ContainsThePiece(deadp.PieceType, p.Color))
{
events.Add(new PieceEvent(new PlacedPiece(deadp.PieceType, p.Color, 0, 0), EventTypeEnum.Add));
// we don't remove it here! sidelined.RemoveThePiece (deadp.PieceType, p.Color);
}
}
// if my piece is in the portal, a move of any other piece causes my piece to return to sidelines.
if (p.Location.IsPortal)
{
// i'm moving the piece from the portal... so there's no portal issue involving this move.
}
else
{
// i'm moving a piece that isn't in the portal.
// so is there a piece in the portal?
PlacedPiece portalJoe = this.AnyoneThere(new BoardLocation(0, 0));
if (null != portalJoe)
{
// is the portal piece same color as the mover of this move?
if (p.Color == portalJoe.Color)
{
// same color? then this piece dies in this move.
events.Add(new PieceEvent(portalJoe, EventTypeEnum.Remove));
}
}
}
// if I jump down the hole, i don't leave this turn standing there.
// THIS IS NOW WRONG, BUT HOLE JUMPS SHOULD BE FIGURED OUT EARLIER I *THINK*
if (spot.Q != 0 || spot.R != 0 || p.PieceType == PiecesEnum.King) // nobody EXCEPT A KING gets to jump down the hole.
Console.WriteLine("Debug.Assert due to this problem, but for now just console log.");
PlacedPiece pp = new PlacedPiece(p, spot); // constructor clones me but to a new spot
events.Add(new PieceEvent(pp, EventTypeEnum.Add)); // I appear at this spot
return events;
}
// A Diddily Doo flips queen-king if possible, and determines what the piece can cause in both scenarios.
// which might just be a swapped queen-king.
public List<List<PieceEvent>> WhatCanICauseWithDoo(PlacedPiece p)
{
// First gather events with no diddily-doo.
List<List<PieceEvent>> allPotentialOutcomes = WhatCanICause(p);
// Now see if there's also a sequence of events after diddily-doo
// Is the Queen next to the king?
PlacedPiece king = FindPiece(PiecesEnum.King, p.Color);
// listen, we have loose board constraints right now, and you can move your king off the board.
// If you do, then no, you can't caus enothin.
if (null == king)
return allPotentialOutcomes; // nothin really
PlacedPiece queen = FindPiece(PiecesEnum.Queen, p.Color);
if (null != queen)
{
// shit there are some rules about moving into check. just be super-simple and we'll figure it out later.
BoardLocationList queenCouldBe = KingStatic.CouldGoIfOmnipotent(king.Location);
if (queenCouldBe.ContainsTheLocation(queen.Location))
{
// swap those two pieces on theoretical game board
Board bAfterDooSwap = new Board(this);
bAfterDooSwap.Remove(queen);
bAfterDooSwap.Remove(king);
var newKing = new PlacedPiece(PiecesEnum.King, queen.Color, queen.Location.Q, queen.Location.R);
var newQueen = new PlacedPiece(PiecesEnum.Queen, king.Color, king.Location.Q, king.Location.R);
bAfterDooSwap.Add(newKing);
bAfterDooSwap.Add(newQueen);
// We can (hey, probably) swap king-queen.
// but if this inquiry is about one of them, then we're inquiring about a piece in a different spot.
if (p.PieceType == PiecesEnum.King)
p = newKing;
if (p.PieceType == PiecesEnum.Queen)
p = newQueen;
List<List<PieceEvent>> evenMorePotentialOutcomes = bAfterDooSwap.WhatCanICause(p);
// each event set has one additional set, a swapparoo
// though i bet i'm going to be missing check cases and crying here.
foreach (var set in evenMorePotentialOutcomes)
{
set.Add(new PieceEvent(queen, EventTypeEnum.Remove));
set.Add(new PieceEvent(king, EventTypeEnum.Remove));
set.Add(new PieceEvent(newQueen, EventTypeEnum.Add));
set.Add(new PieceEvent(newKing, EventTypeEnum.Add));
allPotentialOutcomes.Add(set);
}
evenMorePotentialOutcomes = null;
}
}
return allPotentialOutcomes;
}
// Front door for asking "hey, what outcomes can I produce if it's my turn to move now?"
// However, does NOT consider the diddily-doo option.
// Result is a list of a list of changes, one list per scenario.
protected List<List<PieceEvent>> WhatCanICause(PlacedPiece p) // , bool fdiddilydooit = true)
{
List<List<PieceEvent>> allPotentialOutcomes = new List<List<PieceEvent>>();
switch (p.PieceType)
{
case PiecesEnum.Elephant: // no special moves, no pathfinding. easiest!
{
BoardLocationList spots = WhereCanIReach(p);
// we want events associated with each spot
foreach (BoardLocation spot in spots)
{
List<PieceEvent> events = EventsFromAMove(p, spot);
allPotentialOutcomes.Add(events);
}
return allPotentialOutcomes;
}
case PiecesEnum.King:
{
BoardLocationList spots = WhereCanIReach(p);
// did i just land on some unfortunate piece? cuz those are events.
foreach (BoardLocation spot in spots)
{
List<PieceEvent> events = EventsFromAMove(p, spot);
allPotentialOutcomes.Add(events);
}
return allPotentialOutcomes;
}
case PiecesEnum.Castle: // seemingly straightforward straightnesses
{
BoardLocationList spots = WhereCanIReach(p);
// we want events associated with each spot
foreach (BoardLocation spot in spots)
{
List<PieceEvent> events = EventsFromAMove(p, spot);
allPotentialOutcomes.Add(events);
}
return allPotentialOutcomes;
}
case PiecesEnum.Queen:
{
// King and queen have optional "free move" together.
// Must seek paths
BoardLocationList spots = WhereCanIReach(p);
// we want events associated with each spot
foreach (BoardLocation spot in spots)
{
List<PieceEvent> events = EventsFromAMove(p, spot);
allPotentialOutcomes.Add(events);
}
return allPotentialOutcomes;
}
case PiecesEnum.Pawn:
{
// 2. can croass the board and xform... into any piece?!
// 3. Exiting a mob requires two pawn moves.
// 4. electric fence!
// are we a raw lone pawn? for now assume yes.
BoardLocationList spots = WhereCanIReach(p);
// IF I REACH ACROSS THE BOARD, I TRANSFORM, DUDE.
// (this requires establishment of a back/front wall for each color)r
// we want events associated with each spot
foreach (BoardLocation spot in spots)
{
List<PieceEvent> events = EventsFromAMove(p, spot);
allPotentialOutcomes.Add(events);
}
return allPotentialOutcomes;
}
}
return allPotentialOutcomes;
}
}
class Game
{
ColorsEnum m_first;
ColorsEnum m_second;
ColorsEnum m_third;
// ColorsEnum m_current;
public Game(ColorsEnum first, ColorsEnum second, ColorsEnum third)
{
//m_current =
m_first = first;
m_second = second;
m_third = third;
}
public List<ColorsEnum> NextThree
{
get
{
List<ColorsEnum> three = new List<ColorsEnum> { m_first, m_second, m_third };
return three;
}
}
}
class Recommender
{
}
class Program
{
public static void ShowTextBoard(Board b, BoardLocation singleSpot = null, BoardLocationList highlights = null)
{
// Spit sequentially
// the lines grow, then shrink
// 6, then 7, then 8, then 9, up to 11, then back down.
// FIRST: how many in this line.
// SECOND: first of the two coordinates, that increment across the line.
// THIRD: the other coordinate, that does not change across this line.
int[,] Lines = { { 6, 0, -5 },
{ 7, -1, -4 },
{ 8, -2, -3 },
{ 9, -3, -2 },
{10, -4, -1 },
{11, -5, 0 },
{10, -5, 1 },
{ 9, -5, 2 },
{ 8, -5, 3 },
{ 7, -5, 4 },
{ 6, -5, 5 } };
for (int iLine = 0; iLine < Lines.GetLength(0); iLine++)
{
Console.ResetColor();
// First ident this many spaces: 11 minus how-many-this-line
Console.Write(" ".Substring(0, 11 - Lines[iLine, 0]));
// now increment through the line
for (int iPos = 0; iPos < Lines[iLine, 0]; iPos++)
{
BoardLocation spot = new BoardLocation(Lines[iLine, 1] + iPos, Lines[iLine, 2]);
// Console.BackgroundColor = ConsoleColor.DarkYellow;
if (highlights != null)
{
if (highlights.ContainsTheLocation(spot))
{
Console.BackgroundColor = ConsoleColor.DarkYellow;
}
}
if (singleSpot != null)
{
if (BoardLocation.IsSameLocation(singleSpot, spot))
Console.BackgroundColor = ConsoleColor.DarkRed;
}
PlacedPiece p = b.AnyoneThere(spot);
if (null == p)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(spot.IsPortal ? "X" : "·");
Console.ResetColor();
Console.Write(" ");
}
else
{
switch (p.Color)
{
case ColorsEnum.Black:
Console.ForegroundColor = ConsoleColor.Cyan;
break;
case ColorsEnum.White:
Console.ForegroundColor = ConsoleColor.White;
break;
case ColorsEnum.Tan:
Console.ForegroundColor = ConsoleColor.Red;
break;
}
Console.Write(p.ToChar());
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
}
}
Console.WriteLine();
}
}