-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coverage.html
949 lines (811 loc) · 36.3 KB
/
test_coverage.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/aditya43/chess/engine/boundries.go (100.0%)</option>
<option value="file1">github.com/aditya43/chess/engine/chessboard.go (42.9%)</option>
<option value="file2">github.com/aditya43/chess/engine/pieces.go (100.0%)</option>
<option value="file3">github.com/aditya43/chess/engine/positions.go (100.0%)</option>
<option value="file4">github.com/aditya43/chess/main.go (23.7%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package engine
// Boundries in horizontal direction for a piece
type XBoundry struct {
left int
right int
}
// Boundries in vertical direction for a piece
type YBoundry struct {
top int
bottom int
}
// Diagonal boundries for a piece
type DiagonalBoundry struct {
topLeft int
topRight int
bottomLeft int
bottomRight int
}
// Calculate left boundry based off current position of a piece
func (p *Piece) setLeftBoundry() <span class="cov8" title="1">{
pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos <= 1 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos >= 1 </span><span class="cov8" title="1">{
switch pos </span>{
case 1, 2, 3, 4, 5, 6, 7, 8:<span class="cov8" title="1"> // Left boundry numeric positions on board
p.xBoundry.left = pos
return</span>
default:<span class="cov8" title="1">
pos -= 8</span>
}
}
}
}
// Calculate right boundry based off current position of a piece
func (p *Piece) setRightBoundry() <span class="cov8" title="1">{
pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos >= 64 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos <= 64 </span><span class="cov8" title="1">{
switch pos </span>{
case 57, 58, 59, 60, 61, 62, 63, 64:<span class="cov8" title="1"> // Right boundry numeric positions on board
p.xBoundry.right = pos
return</span>
default:<span class="cov8" title="1">
pos += 8</span>
}
}
}
}
// Calculate top boundry based off current position of a piece
func (p *Piece) setTopBoundry() <span class="cov8" title="1">{
pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos >= 64 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos <= 64 </span><span class="cov8" title="1">{
switch pos </span>{
case 8, 16, 24, 32, 40, 48, 56, 64:<span class="cov8" title="1"> // Top boundry numeric positions on board
p.yBoundry.top = pos
return</span>
default:<span class="cov8" title="1">
pos++</span>
}
}
}
}
// Calculate bottom boundry based off current position of a piece
func (p *Piece) setBottomBoundry() <span class="cov8" title="1">{
pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos <= 1 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos >= 1 </span><span class="cov8" title="1">{
switch pos </span>{
case 1, 9, 17, 25, 33, 41, 49, 57:<span class="cov8" title="1"> // Bottom boundry numeric positions on board
p.yBoundry.bottom = pos
return</span>
default:<span class="cov8" title="1">
pos--</span>
}
}
}
}
// Calculate top-left boundry based off current position of a piece
func (p *Piece) setTopLeftBoundry() <span class="cov8" title="1">{
pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
// fmt.Println(pos)
if pos < 2 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos >= 1 </span><span class="cov8" title="1">{
switch pos </span>{
case 8, 16, 24, 32, 40, 48, 56, 64:<span class="cov8" title="1"> // Top boundry numeric positions on board
p.diagonalBoundry.topLeft = pos
return</span>
case 1, 2, 3, 4, 5, 6, 7:<span class="cov8" title="1"> // Left boundry numeric positions on board
p.diagonalBoundry.topLeft = pos
return</span>
default:<span class="cov8" title="1">
if p.isPawn </span><span class="cov8" title="1">{
// Pawn is not on left boundry or top boundry
// Calculate and set its top-left position
p.diagonalBoundry.topLeft = pos - 7
return
}</span>
<span class="cov8" title="1">pos -= 7</span>
}
}
}
}
// Calculate top-right boundry based off current position of a piece
func (p *Piece) setTopRightBoundry() <span class="cov8" title="1">{
pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos >= 64 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos <= 64 </span><span class="cov8" title="1">{
switch pos </span>{
case 8, 16, 24, 32, 40, 48, 56:<span class="cov8" title="1"> // Top boundry numeric positions on board
p.diagonalBoundry.topRight = pos
return</span>
case 57, 58, 59, 60, 61, 62, 63, 64:<span class="cov8" title="1"> // Right boundry numeric positions on board
p.diagonalBoundry.topRight = pos
return</span>
default:<span class="cov8" title="1">
if p.isPawn </span><span class="cov8" title="1">{
// Pawn is not on right boundry or top boundry
// Calculate and set its top-right position
p.diagonalBoundry.topRight = pos + 9
return
}</span>
<span class="cov8" title="1">pos += 9</span>
}
}
}
}
// Calculate bottom-right boundry based off current position of a piece
func (p *Piece) setBottomRightBoundry() <span class="cov8" title="1">{
if p.isPawn </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos < 9 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos >= 9 </span><span class="cov8" title="1">{
switch pos </span>{
case 1, 9, 17, 25, 33, 41, 49, 57:<span class="cov8" title="1"> // Bottom boundry numeric positions on board
p.diagonalBoundry.bottomRight = pos
return</span>
case 58, 59, 60, 61, 62, 63, 64:<span class="cov8" title="1"> // Right boundry numeric positions on board
p.diagonalBoundry.bottomRight = pos
return</span>
default:<span class="cov8" title="1">
pos += 7</span>
}
}
}
}
// Calculate bottom-left boundry based off current position of a piece
func (p *Piece) setBottomLeftBoundry() <span class="cov8" title="1">{
if p.isPawn </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">pos := p.CurPos
for i := 0; i < 8; i++ </span><span class="cov8" title="1">{
if pos <= 1 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">if pos >= 1 </span><span class="cov8" title="1">{
switch pos </span>{
case 1, 9, 17, 25, 33, 41, 49, 57:<span class="cov8" title="1"> // Bottom boundry numeric positions on board
p.diagonalBoundry.bottomLeft = pos
return</span>
case 2, 3, 4, 5, 6, 7, 8:<span class="cov8" title="1"> // Left boundry numeric positions on board
p.diagonalBoundry.bottomLeft = pos
return</span>
default:<span class="cov8" title="1">
pos -= 9</span>
}
}
}
}
// Set boundries for a piece
func (p *Piece) setBoundries() <span class="cov8" title="1">{
if p.xAllow </span><span class="cov8" title="1">{
p.setLeftBoundry()
p.setRightBoundry()
}</span>
<span class="cov8" title="1">if p.yAllow </span><span class="cov8" title="1">{
p.setTopBoundry()
p.setBottomBoundry()
}</span>
<span class="cov8" title="1">if p.crossAllow </span><span class="cov8" title="1">{
p.setTopLeftBoundry()
p.setTopRightBoundry()
p.setBottomRightBoundry()
p.setBottomLeftBoundry()
}</span>
// fmt.Printf("%v %8v: %v %v %v %v %v %v %v %v \n", p.Symbol, p.Kind, p.xBoundry.left, p.diagonalBoundry.topLeft, p.yBoundry.top, p.diagonalBoundry.topRight, p.xBoundry.right, p.diagonalBoundry.bottomRight, p.yBoundry.bottom, p.diagonalBoundry.bottomLeft)
}
</pre>
<pre class="file" id="file1" style="display: none">package engine
import (
"fmt"
"strings"
"github.com/gookit/color"
)
type ChessBoard struct {
Cells map[int]string // Numeric Position to Text Representation. For e.g. 10 -> B2
Pieces map[int]*Piece // Numeric Position --> Piece. For printing purposes only
StrCells map[string]int // Text Representation to Numeric Position of a cell
}
// Create new chessboard
// Create cells with their numeric identifiers
func CreateChessBoard() *ChessBoard <span class="cov8" title="1">{
b := &ChessBoard{}
b.Cells = make(map[int]string)
b.StrCells = make(map[string]int)
l := [8]string{"a", "b", "c", "d", "e", "f", "g", "h"} // Columns
num := 1
for _, c := range l </span><span class="cov8" title="1">{
for i := 1; i < 9; i++ </span><span class="cov8" title="1">{
str := fmt.Sprintf("%v%v", c, i) // Generate string position e.g. a1, h8 etc..
b.Cells[num] = str
b.StrCells[str] = num
num++
}</span>
}
<span class="cov8" title="1">return b</span>
}
// Place piece on a chessboard and update
// available move positions for a piece.
//
// pos numeric position on board
// p Piece
func (b *ChessBoard) PlacePiece(pos int, p *Piece) <span class="cov8" title="1">{
b.Pieces = make(map[int]*Piece)
b.Pieces[pos] = p
p.updateMovePositions(b)
}</span>
// Render chessboard with a piece
func (b *ChessBoard) Print(p *Piece) <span class="cov0" title="0">{
row := 8
cnt := 0
cell := 8
for i := 0; i < 64; i++ </span><span class="cov0" title="0">{
if cnt == 8 </span><span class="cov0" title="0">{
cnt = 0
row -= 1
cell = row
fmt.Println()
}</span>
// Print piece symbol if we are at current position of a piece
<span class="cov0" title="0">if p.CurPos == cell </span><span class="cov0" title="0">{
s := color.New(color.FgWhite, color.BgRed, color.OpBold).Sprintf(" %v ", p.Symbol)
// fmt.Printf("%6v %v ", b.Cells[cell], s)
color.Red.Printf("%6v", strings.ToUpper(b.Cells[cell]))
fmt.Printf(" %v ", s)
// color.BgRed.Printf("%6v %v ", b.Cells[cell], p.Symbol)
}</span>
// If a piece can move to this cell, print it in green
<span class="cov0" title="0">if _, ok := p.AvailPos[cell]; ok </span><span class="cov0" title="0">{
// color.Green.Printf("%6v (%2v)", strings.ToUpper(b.Cells[cell]), cell)
color.Green.Printf("%6v %2v ", strings.ToUpper(b.Cells[cell]), "")
}</span>
// Piece is not at this cell and neither can move to this location
<span class="cov0" title="0">if _, ok := p.AvailPos[cell]; !ok && p.CurPos != cell </span><span class="cov0" title="0">{
// fmt.Printf("%6v (%2v)", strings.ToUpper(b.Cells[cell]), cell)
fmt.Printf("%6v %2v ", strings.ToUpper(b.Cells[cell]), "")
}</span>
<span class="cov0" title="0">cell += 8
cnt++</span>
}
<span class="cov0" title="0">fmt.Println()</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package engine
// Chessboard piece
type Piece struct {
Kind string // Kind of piece. e.g. king, queen, horse, pawn, rook, bishop
CurPos int // Numeric position on board
Symbol string // e.g. ♚, ♛, ♜, ♝, ♞
isHorse bool // Is current piece a Horse
isPawn bool // Is current piece a Pawn
xAllow bool // Is piece allowed to traverse in X direction
yAllow bool // Is piece allowed to traverse in Y direction
crossAllow bool // Is piece allowed to traverse in diagonal direction
maxX int // Max number of cells a piece can traverse in X direction
maxY int // Max number of cells a piece can traverse in Y direction
maxCross int // Max number of cells a piece can traverse in diagonal direction
AvailPos map[int]bool // Available move positions for a piece
xBoundry XBoundry // Horizontal boundries
yBoundry YBoundry // Vertical boundries
diagonalBoundry DiagonalBoundry // Diagonal boundries
}
// Calculate and add available positions for a piece based off it's current position
func (p *Piece) updateMovePositions(b *ChessBoard) <span class="cov8" title="1">{
p.AvailPos = make(map[int]bool)
if !p.isHorse </span><span class="cov8" title="1">{
if p.yAllow </span><span class="cov8" title="1">{ // Check if a piece can move on y axis
if p.isPawn </span><span class="cov8" title="1">{
// Y axis
// Get positions in top direction for a pawn piece
p.addPositions(b, p.yBoundry.top, p.maxY, Positive, 1)
}</span>
<span class="cov8" title="1">if !p.isPawn </span><span class="cov8" title="1">{
// Y axis
// Get positions in top direction for a non pawn piece
p.addPositions(b, p.yBoundry.top, p.maxY, Positive, 1)
// Y axis
// Get positions in bottom direction for a non pawn piece
p.addPositions(b, p.yBoundry.bottom, p.maxY, Negative, 1)
}</span>
}
// Check if a piece can move on x axis
<span class="cov8" title="1">if p.xAllow </span><span class="cov8" title="1">{
// X axis
// Get positions in right direction for a non pawn piece
p.addPositions(b, p.xBoundry.right, p.maxX, Positive, 8)
// X axis
// Get positions in left direction for a non pawn piece
p.addPositions(b, p.xBoundry.left, p.maxX, Negative, 8)
}</span>
// Check if a piece can move in diagonal direction
<span class="cov8" title="1">if p.crossAllow </span><span class="cov8" title="1">{
// Diagonal
// Get diagonal move positions in top-right direction for a non pawn piece
p.addPositions(b, p.diagonalBoundry.topRight, p.maxCross, Positive, 9)
// Diagonal
// Get diagonal move positions in top-left direction for a non pawn piece
p.addPositions(b, p.diagonalBoundry.topLeft, p.maxCross, Negative, 7)
// Diagonal
// Get diagonal move positions in bottom-right direction for a non pawn piece
p.addPositions(b, p.diagonalBoundry.bottomRight, p.maxCross, Positive, 7)
// Diagonal
// Get diagonal move positions in bottom-left direction for a non pawn piece
p.addPositions(b, p.diagonalBoundry.bottomLeft, p.maxCross, Negative, 9)
}</span>
}
<span class="cov8" title="1">if p.isHorse </span><span class="cov8" title="1">{
p.addHorsePositions(b) // Add move positions for a horse piece
}</span>
}
// Create piece
//
// pos: Numeric position of a piece on board
// n: Piece type
func CreatePiece(pos int, n string) *Piece <span class="cov8" title="1">{
var p *Piece
switch n </span>{
case "rook":<span class="cov8" title="1">
p = &Piece{
Kind: n,
CurPos: pos,
Symbol: "♖",
xAllow: true,
yAllow: true,
crossAllow: false,
maxX: 8,
maxY: 8,
}</span>
case "horse":<span class="cov8" title="1">
p = &Piece{
Kind: n,
CurPos: pos,
Symbol: "♘",
isHorse: true,
xAllow: false,
yAllow: false,
crossAllow: false,
}</span>
case "bishop":<span class="cov8" title="1">
p = &Piece{
Kind: n,
CurPos: pos,
Symbol: "♗",
xAllow: false,
yAllow: false,
crossAllow: true,
maxCross: 8,
}</span>
case "queen":<span class="cov8" title="1">
p = &Piece{
Kind: n,
CurPos: pos,
Symbol: "♕",
xAllow: true,
yAllow: true,
crossAllow: true,
maxX: 8,
maxY: 8,
maxCross: 8,
}</span>
case "king":<span class="cov8" title="1">
p = &Piece{
Kind: n,
CurPos: pos,
Symbol: "♔",
xAllow: true,
yAllow: true,
crossAllow: true,
maxX: 1,
maxY: 1,
maxCross: 1,
}</span>
case "pawn":<span class="cov8" title="1">
p = &Piece{
Kind: n,
CurPos: pos,
Symbol: "♙",
isPawn: true,
yAllow: true,
maxY: 1,
crossAllow: true,
maxCross: 1,
}</span>
}
// Set boundries for a piece if it can move on X, Y axis as well as diagonally
<span class="cov8" title="1">p.setBoundries()
return p</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package engine
type Direction int // Move direction of a piece
const (
Positive Direction = iota
Negative
)
// Calculate and add available move positions for any piece except horse
//
// b ChessBoard
// l Limit
// max Maximum distance a piece can travel
// d Positive or Negative direction
// f Increment/Decrement factor
func (p *Piece) addPositions(b *ChessBoard, l int, max int, d Direction, f int) <span class="cov8" title="1">{
if l < 1 </span><span class="cov8" title="1">{
return // Piece doesn't have any space to move in d Direction
}</span>
<span class="cov8" title="1">pos := p.CurPos
for i := 0; i < max; i++ </span><span class="cov8" title="1">{
if l > 0 && pos == l </span><span class="cov8" title="1">{ // Position has reached boundry
if pos != p.CurPos </span><span class="cov8" title="1">{
p.AvailPos[pos] = true // Add move position
}</span>
<span class="cov8" title="1">break</span> // Reached boundry, break!
}
// For calculating positive move positions
<span class="cov8" title="1">if d == Positive </span><span class="cov8" title="1">{
pos += f // Increment by factor
}</span>
// For calculating negative move positions
<span class="cov8" title="1">if d == Negative </span><span class="cov8" title="1">{
pos -= f // Decrement by factor
}</span>
<span class="cov8" title="1">p.AvailPos[pos] = true</span> // Add move position
}
}
// Add move position for a horse
func (p *Piece) addHorsePositions(b *ChessBoard) <span class="cov8" title="1">{
// 2 left, 1 top move
if canMakeTwoLeftOneTop(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos-15] = true
}</span>
// 2 left, 1 down move
<span class="cov8" title="1">if canMakeTwoLeftOneDown(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos-17] = true
}</span>
// 2 top, 1 left move
<span class="cov8" title="1">if canMakeTwoTopOneLeft(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos-6] = true
}</span>
// 2 down, 1 left move
<span class="cov8" title="1">if canMakeTwoDownOneLeft(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos-10] = true
}</span>
// 2 down, 1 right move
<span class="cov8" title="1">if canMakeTwoDownOneRight(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos+6] = true
}</span>
// 2 right, 1 down move
<span class="cov8" title="1">if canMakeTwoRightOneDown(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos+15] = true
}</span>
// 2 right, 1 top move
<span class="cov8" title="1">if canMakeTwoRightOneTop(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos+17] = true
}</span>
// 2 top, 1 right move
<span class="cov8" title="1">if canMakeTwoTopOneRight(p.CurPos) </span><span class="cov8" title="1">{
p.AvailPos[p.CurPos+10] = true
}</span>
}
// Can a piece move 2 left, 1 top
// p current position of a piece
func canMakeTwoLeftOneTop(p int) bool <span class="cov8" title="1">{
if p <= 16 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 24, 32, 40, 48, 56, 64:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 left, 1 down
// p current position of a piece
func canMakeTwoLeftOneDown(p int) bool <span class="cov8" title="1">{
if p <= 17 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 25, 33, 41, 49, 57:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 down, 1 right
// p current position of a piece
func canMakeTwoDownOneRight(p int) bool <span class="cov8" title="1">{
if p >= 57 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 1, 2, 10, 18, 26, 34, 42, 50, 58, 9, 17, 25, 33, 41, 49:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 top, 1 left
// p current position of a piece
func canMakeTwoTopOneLeft(p int) bool <span class="cov8" title="1">{
if p <= 8 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 16, 24, 32, 40, 48, 56, 64, 15, 23, 31, 39, 47, 55, 63:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 down, 1 left
// p current position of a piece
func canMakeTwoDownOneLeft(p int) bool <span class="cov8" title="1">{
if p <= 10 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 10, 18, 26, 34, 42, 50, 58, 9, 17, 25, 33, 41, 49, 57:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 right, 1 down
// p current position of a piece
func canMakeTwoRightOneDown(p int) bool <span class="cov8" title="1">{
if p >= 49 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 1, 9, 17, 25, 33, 41:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 right, 1 top
// p current position of a piece
func canMakeTwoRightOneTop(p int) bool <span class="cov8" title="1">{
if p >= 49 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 8, 16, 24, 32, 40, 48, 56, 64:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
// Can a piece move 2 top, 1 right
// p current position of a piece
func canMakeTwoTopOneRight(p int) bool <span class="cov8" title="1">{
if p >= 57 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">switch p </span>{
case 7, 15, 23, 31, 39, 47, 55, 63, 8, 16, 24, 32, 40, 48, 56, 64:<span class="cov8" title="1">
return false</span>
}
<span class="cov8" title="1">return true</span>
}
</pre>
<pre class="file" id="file4" style="display: none">package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
game "github.com/aditya43/chess/engine"
"github.com/gookit/color" // For printing colorful text in terminal
)
// Execute program
func main() <span class="cov0" title="0">{
for </span><span class="cov0" title="0">{
cr := bufio.NewReader(os.Stdin)
printInfo()
s, _ := cr.ReadString('\n')
w := strings.Fields(strings.ToLower(s))
checkExit(s)
takeAction(w, cr)
}</span>
}
// Take action based off user input
func takeAction(w []string, cr *bufio.Reader) <span class="cov0" title="0">{
err := validateInput(w)
if err != nil </span><span class="cov0" title="0">{
color.Error.Println(err)
restartProgram(cr)
return
}</span>
<span class="cov0" title="0">cb := game.CreateChessBoard() // Create chessboard
pos := cb.StrCells[w[1]] // Get numeric position for a cell
p := game.CreatePiece(pos, w[0]) // Create a piece
cb.PlacePiece(pos, p) // Place a piece on chessboard
printOutput(cb, p) // Print output
restartProgram(cr)</span> // Restart program
}
// Validate user inputs
func validateInput(w []string) error <span class="cov8" title="1">{
var pieceErr, posErr bool
if len(w) < 2 </span><span class="cov8" title="1">{
return errors.New("Invalid input. Please enter piece type and position.")
}</span>
<span class="cov8" title="1">switch strings.ToLower(w[0]) </span>{
case "king", "queen", "bishop", "horse", "rook", "pawn":<span class="cov8" title="1"></span>
default:<span class="cov8" title="1">
pieceErr = true</span> // User didn't input any of the above piece type
}
<span class="cov8" title="1">switch strings.ToLower(w[1]) </span>{
case "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8",
"b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8",
"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8",
"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8",
"e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8",
"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8",
"g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8",
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8":<span class="cov8" title="1"></span>
default:<span class="cov8" title="1">
posErr = true</span> // User didn't input any of the above cell positions
}
<span class="cov8" title="1">if pieceErr && posErr </span><span class="cov8" title="1">{
return errors.New("Invalid piece type and position value.")
}</span>
<span class="cov8" title="1">if pieceErr </span><span class="cov8" title="1">{
return errors.New("Invalid piece type.")
}</span>
<span class="cov8" title="1">if posErr </span><span class="cov8" title="1">{
return errors.New("Invalid position value.")
}</span>
<span class="cov8" title="1">return nil</span>
}
// Print information about how to use this program
func printInfo() <span class="cov0" title="0">{
print("\033[H\033[2J") // Ansi escape sequence to clear the screen
fmt.Println("Enter a piece type and it's position")
fmt.Println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
color.New(color.FgGreen, color.BgBlack, color.OpBold).Println("👍 Program input is case-insensitive.")
color.New(color.FgGreen, color.BgBlack, color.OpBold).Print("👍 Type ")
color.BgRed.Print("exit")
color.New(color.FgGreen, color.BgBlack, color.OpBold).Println(" to quit program.")
fmt.Print("\n")
fmt.Print("Piece types: ")
color.Yellow.Println("King, Queen, Bishop, Horse, Rook, Pawn")
fmt.Print("Example input: ")
color.Style{color.FgGreen, color.OpBold}.Println("King D5")
fmt.Println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
fmt.Print("Input: ")
}</span>
// Exit program if user typed 'exit'
func checkExit(s string) <span class="cov0" title="0">{
if strings.HasPrefix(strings.ToLower(s), "exit") </span><span class="cov0" title="0">{
fmt.Println("Good bye!🍺")
os.Exit(0)
}</span>
}
// Ask user to hit Enter key to restart program
func restartProgram(cr *bufio.Reader) <span class="cov0" title="0">{
color.Yellow.Println("Hit Enter key to continue")
s, _ := cr.ReadString('\n')
checkExit(s) // User can quit program by typing 'exit'
}</span>
// Print available move positions and render
// chessboard with a piece on it
func printOutput(cb *game.ChessBoard, p *game.Piece) <span class="cov0" title="0">{
m := ""
color.Yellow.Print("Available Moves: ")
for pos := range p.AvailPos </span><span class="cov0" title="0">{
m += strings.ToUpper(cb.Cells[pos]) + ", "
}</span>
<span class="cov0" title="0">color.New(color.FgGreen, color.BgBlack, color.OpBold).Printf("%v\n\n\n", strings.TrimSuffix(m, ", "))
cb.Print(p)
fmt.Print("\n")</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>