Skip to content

Commit 9d26b0f

Browse files
committed
Add tests for CheckSimple()
1 parent 2444d6c commit 9d26b0f

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

engine/generator.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package engine
22

3-
import "fmt"
4-
53
var (
64
nextRank int8 = 16
75
nextFile int8 = 1
@@ -132,9 +130,11 @@ func (g *Generator) GenerateMoves() []Move {
132130
// CheckSimple finds possible check attacks
133131
func (g *Generator) CheckSimple() bool {
134132

133+
g.reset()
134+
135135
// check for knight attacks first
136136
for _, delta := range deltaKnight {
137-
to := g.kingSquare + int8(delta)
137+
to := g.kingSquare + delta
138138
if g.board.legalSquare(to) && g.board.data[to]*opponent(g.board.sideToMove) == Knight {
139139
return true
140140
}
@@ -144,6 +144,7 @@ func (g *Generator) CheckSimple() bool {
144144
for _, delta := range deltaAll {
145145
depth := 0
146146
for to := g.kingSquare + delta; g.board.legalSquare(to); to += delta {
147+
depth++
147148
found := g.board.data[to] * opponent(g.board.sideToMove)
148149

149150
// empty square, go ahead
@@ -153,7 +154,7 @@ func (g *Generator) CheckSimple() bool {
153154

154155
if found == Pawn || found == King {
155156
if depth == 1 {
156-
if g.attackPosible(to, opposite(delta)) {
157+
if g.attackPossible(to, opposite(delta)) {
157158
return true
158159
}
159160
}
@@ -166,7 +167,7 @@ func (g *Generator) CheckSimple() bool {
166167
}
167168

168169
// found opponent
169-
if g.attackPosible(to, delta) {
170+
if g.attackPossible(to, delta) {
170171
return true
171172
}
172173
break
@@ -233,11 +234,6 @@ func (g *Generator) sortMoves() {
233234
// 5. normal moves
234235
sorted = append(sorted, ordinary...)
235236

236-
if len(sorted) != len(g.moves) {
237-
fmt.Printf("len(g.moves): %d, len(sorted): %d\n", len(g.moves), len(sorted))
238-
panic("Sorted moves are missing moves!")
239-
}
240-
241237
g.moves = sorted
242238
}
243239

@@ -471,7 +467,7 @@ func (g *Generator) findThreats(square Square, sideToMove int8, skipKing bool) [
471467
// if there is a pawn/king on next square, check for attack
472468
if content == -Pawn || content == -King {
473469
if depth == 1 {
474-
if g.attackPosible(to, opposite(delta)) {
470+
if g.attackPossible(to, opposite(delta)) {
475471
threats = append(threats, to)
476472
}
477473
}
@@ -493,7 +489,7 @@ func (g *Generator) findThreats(square Square, sideToMove int8, skipKing bool) [
493489
}
494490

495491
// found opponent piece
496-
if g.attackPosible(to, delta) {
492+
if g.attackPossible(to, delta) {
497493
threats = append(threats, to)
498494
}
499495

@@ -545,7 +541,7 @@ func (g *Generator) numberOfCheckThreats() int {
545541
// pawn on the next square, check if attack possible
546542
if piece == Pawn || piece == King {
547543
if depth == 1 {
548-
if g.attackPosible(newSquare, opposite(delta)) {
544+
if g.attackPossible(newSquare, opposite(delta)) {
549545
g.kingUnderCheck = true
550546
threats++
551547
g.legalEnding[newSquare] = true
@@ -565,7 +561,7 @@ func (g *Generator) numberOfCheckThreats() int {
565561
}
566562

567563
// we found an opponents piece; might be a threat for the current square
568-
if g.attackPosible(newSquare, delta) {
564+
if g.attackPossible(newSquare, delta) {
569565
if squareOfGuardingPiece == Invalid {
570566
threats++
571567
g.kingUnderCheck = true
@@ -599,11 +595,8 @@ func (g *Generator) hasDirection(delta []int8, direction int8) bool {
599595

600596
// check for attacks from a diven square in a particular direction
601597
// does not work for knight obviously
602-
func (g *Generator) attackPosible(from int8, direction int8) bool {
603-
absPiece := g.board.data[from]
604-
if absPiece < 0 {
605-
absPiece = -absPiece
606-
}
598+
func (g *Generator) attackPossible(from int8, direction int8) bool {
599+
absPiece := abs(g.board.data[from])
607600

608601
switch absPiece {
609602
case King:

engine/generator_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestGenerateMovesForDefaultBoardPosition(t *testing.T) {
2727
Move{From: G1, To: H3, MovedPiece: WhiteKnight, Special: moveOrdinary, Content: Empty, Promoted: Empty},
2828
}
2929

30-
doTest(defaultFEN, expected, t)
30+
doTestMovesForFEN(defaultFEN, expected, t)
3131

3232
}
3333

@@ -41,10 +41,40 @@ func TestGenerateMovesForCheckPositionToCreateAllEscapeMovesForKing(t *testing.T
4141
Move{From: E6, To: F5, MovedPiece: BlackKing, Special: moveOrdinary, Content: Empty, Promoted: Empty},
4242
}
4343

44-
doTest(fen, expected, t)
44+
doTestMovesForFEN(fen, expected, t)
4545
}
4646

47-
func doTest(fen string, expected []Move, t *testing.T) {
47+
func TestCheckSimpleShouldBeFalseForStartingPosition(t *testing.T) {
48+
doTestCheckSimple(defaultFEN, false, t)
49+
}
50+
51+
func TestCheckSimpleShouldBeFalseForNonCheckPosition(t *testing.T) {
52+
doTestCheckSimple("rnbqkbnr/3p1ppp/p3p3/1pp3B1/3PP3/2N2N2/PPP2PPP/R2QKB1R b KQkq - 1 5", false, t)
53+
}
54+
55+
func TestCheckSimpleShouldBeTrueForCheckPositionFromRook(t *testing.T) {
56+
doTestCheckSimple("k7/8/8/8/8/8/8/RK6 b - - 0 2", true, t)
57+
}
58+
59+
func TestAttackCheckSimpleForSingleRookOnFile(t *testing.T) {
60+
doTestCheckSimple("r7/8/8/8/8/8/8/K7 w - - 0 1", true, t)
61+
}
62+
63+
func TestAttackCheckSimpleForSingleRookOnRank(t *testing.T) {
64+
doTestCheckSimple("r6K/8/8/8/8/8/8/8 w - - 0 1", true, t)
65+
}
66+
67+
/* helper */
68+
69+
func doTestCheckSimple(fen string, expected bool, t *testing.T) {
70+
board, _ := parseFEN(fen)
71+
if NewGenerator(board).CheckSimple() != expected {
72+
t.Errorf("Expected CheckSimple() to be %t but was %t for board\n%s\n",
73+
expected, !expected, board.String())
74+
}
75+
}
76+
77+
func doTestMovesForFEN(fen string, expected []Move, t *testing.T) {
4878
board, _ := parseFEN(fen)
4979
gen := NewGenerator(board)
5080

0 commit comments

Comments
 (0)