Skip to content

Commit dde4efa

Browse files
committed
18 Three rules about check
1 parent 549c0fd commit dde4efa

File tree

14 files changed

+302
-223
lines changed

14 files changed

+302
-223
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"_config.yml": true,
1313
"diverese": true,
1414
"pm.txt": true,
15+
"GoBit.exe~": true,
1516
".gitignore": true,
17+
"temp.go": true,
1618
"engine-interface.txt": true
1719
},
1820
"cSpell.words": [

bitboard.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ func (b bitBoard) count() int {
1212
return bits.OnesCount64(uint64(b))
1313
}
1414

15-
func (b *bitBoard) set(pos uint) {
16-
*b |= bitBoard(uint64(1) << pos)
15+
func (b *bitBoard) set(pos int) {
16+
*b |= bitBoard(uint64(1) << uint(pos))
1717
}
1818

19-
func (b bitBoard) test(pos uint) bool {
20-
return (b & bitBoard(uint64(1)<<pos)) != 0
19+
func (b bitBoard) test(pos int) bool {
20+
return (b & bitBoard(uint64(1)<<uint(pos))) != 0
2121
}
2222

23-
func (b *bitBoard) clr(pos uint) {
24-
*b &= bitBoard(^(uint64(1) << pos))
23+
func (b *bitBoard) clr(pos int) {
24+
*b &= bitBoard(^(uint64(1) << uint(pos)))
2525
}
2626

2727
func (b *bitBoard) firstOne() int {

bitboard_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func Test_bitBoard_some(t *testing.T) {
1010
tests := []struct {
1111
name string
1212
b bitBoard
13-
pos uint
13+
pos int
1414
}{
1515
{"", 0xF, 63},
1616
{"", 0x0, 0},

castlings.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ const (
1212
longW = uint(0x2) // white can castle long
1313
shortB = uint(0x4) // black can castle short
1414
longB = uint(0x8) // black can castle short
15-
16-
// squares between rook and king
17-
betweenWSh = bitBoard(uint64(1)<<G1) | bitBoard(uint64(1)<<F1)
18-
betweenWL = bitBoard(uint64(1)<<B1) | bitBoard(uint64(1)<<C1) | bitBoard(uint64(1)<<D1)
19-
betweenBSh = bitBoard(uint64(1)<<G8) | bitBoard(uint64(1)<<F8)
20-
betweenBL = bitBoard(uint64(1)<<B8) | bitBoard(uint64(1)<<C8) | bitBoard(uint64(1)<<D8)
2115
)
2216

2317
type castlOptions struct {
@@ -33,18 +27,18 @@ type castlOptions struct {
3327
}
3428

3529
var castl = [2]castlOptions{
36-
{shortW, longW, wR, E1, H1, A1, betweenWSh, betweenWL, 0x0, 0x0, 0x0, 0x0},
37-
{shortB, longB, bR, E8, H8, A8, betweenBSh, betweenBL, 0x0, 0x0, 0x0, 0x0},
30+
{shortW, longW, wR, E1, H1, A1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
31+
{shortB, longB, bR, E8, H8, A8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
3832
}
3933

40-
// only castling privileges (not if it is legal on board)
41-
func (c castlings) canCastle(sd color) bool {
42-
return c.canCastleShort(sd) || c.canCastleLong(sd)
34+
// castling privileges
35+
func (c castlings) flags(sd color) bool {
36+
return c.shortFlag(sd) || c.longFlag(sd)
4337
}
44-
func (c castlings) canCastleShort(sd color) bool {
38+
func (c castlings) shortFlag(sd color) bool {
4539
return (castl[sd].short & uint(c)) != 0
4640
}
47-
func (c castlings) canCastleLong(sd color) bool {
41+
func (c castlings) longFlag(sd color) bool {
4842
return (castl[sd].long & uint(c)) != 0
4943
}
5044

@@ -101,33 +95,47 @@ func parseCastlings(fenCastl string) castlings {
10195

10296
func initCastlings() {
10397
fmt.Println("init castlings")
104-
// pawns stops short castling W
98+
// squares between K and R short castling
99+
castl[WHITE].betweenSh.set(F1)
100+
castl[WHITE].betweenSh.set(G1)
101+
castl[BLACK].betweenSh.set(F8)
102+
castl[BLACK].betweenSh.set(G8)
103+
104+
// squares between K and R long castling
105+
castl[WHITE].betweenL.set(B1)
106+
castl[WHITE].betweenL.set(C1)
107+
castl[WHITE].betweenL.set(D1)
108+
castl[BLACK].betweenL.set(B8)
109+
castl[BLACK].betweenL.set(C8)
110+
castl[BLACK].betweenL.set(D8)
111+
112+
// pawns stop short castling W
105113
castl[WHITE].pawnsSh.set(D2)
106114
castl[WHITE].pawnsSh.set(E2)
107115
castl[WHITE].pawnsSh.set(F2)
108116
castl[WHITE].pawnsSh.set(G2)
109117
castl[WHITE].pawnsSh.set(H2)
110-
// pawns stops long castling W
118+
// pawns stop long castling W
111119
castl[WHITE].pawnsL.set(B2)
112120
castl[WHITE].pawnsL.set(C2)
113121
castl[WHITE].pawnsL.set(D2)
114122
castl[WHITE].pawnsL.set(E2)
115123
castl[WHITE].pawnsL.set(F2)
116124

117-
// pawns stops short castling B
125+
// pawns stop short castling B
118126
castl[BLACK].pawnsSh.set(D7)
119127
castl[BLACK].pawnsSh.set(E7)
120128
castl[BLACK].pawnsSh.set(F7)
121129
castl[BLACK].pawnsSh.set(G7)
122130
castl[BLACK].pawnsSh.set(H7)
123-
// pawns stops long castling B
131+
// pawns stop long castling B
124132
castl[BLACK].pawnsL.set(B7)
125133
castl[BLACK].pawnsL.set(C7)
126134
castl[BLACK].pawnsL.set(D7)
127135
castl[BLACK].pawnsL.set(E7)
128136
castl[BLACK].pawnsL.set(F7)
129137

130-
// knights stops short castling W
138+
// knights stop short castling W
131139
castl[WHITE].knightsSh.set(C2)
132140
castl[WHITE].knightsSh.set(D2)
133141
castl[WHITE].knightsSh.set(E2)
@@ -138,7 +146,7 @@ func initCastlings() {
138146
castl[WHITE].knightsSh.set(F3)
139147
castl[WHITE].knightsSh.set(G3)
140148
castl[WHITE].knightsSh.set(H3)
141-
// knights stops long castling W
149+
// knights stop long castling W
142150
castl[WHITE].knightsL.set(A2)
143151
castl[WHITE].knightsL.set(B2)
144152
castl[WHITE].knightsL.set(C2)
@@ -151,7 +159,7 @@ func initCastlings() {
151159
castl[WHITE].knightsL.set(E3)
152160
castl[WHITE].knightsL.set(F3)
153161

154-
// knights stops short castling B
162+
// knights stop short castling B
155163
castl[BLACK].knightsSh.set(C7)
156164
castl[BLACK].knightsSh.set(D7)
157165
castl[BLACK].knightsSh.set(E7)
@@ -162,7 +170,7 @@ func initCastlings() {
162170
castl[BLACK].knightsSh.set(F6)
163171
castl[BLACK].knightsSh.set(G6)
164172
castl[BLACK].knightsSh.set(H6)
165-
// knights stops long castling B
173+
// knights stop long castling B
166174
castl[BLACK].knightsL.set(A7)
167175
castl[BLACK].knightsL.set(B7)
168176
castl[BLACK].knightsL.set(C7)

engine.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type searchLimits struct {
1010
nodes uint64
1111
moveTime int // in milliseconds
1212
infinite bool
13+
//////////////// current //////////
14+
stop bool
1315
}
1416

1517
var limits searchLimits
@@ -21,30 +23,43 @@ func (s *searchLimits) init() {
2123
s.infinite = false
2224
}
2325

26+
func (s *searchLimits) setStop(st bool) {
27+
s.stop = st
28+
}
2429
func (s *searchLimits) setDepth(d int) {
2530
s.depth = d
2631
}
2732
func (s *searchLimits) setMoveTime(m int) {
2833
s.moveTime = m
2934
}
30-
func engine() (toEngine chan string, frEngine chan string) {
35+
func (s *searchLimits) setInfinite(b bool) {
36+
s.infinite = b
37+
}
38+
39+
func engine() (toEngine chan bool, frEngine chan string) {
3140
fmt.Println("info string Hello from engine")
3241
frEngine = make(chan string)
33-
toEngine = make(chan string)
34-
go func() {
35-
for cmd := range toEngine {
36-
tell("info string engine got ", cmd)
37-
switch cmd {
38-
case "stop":
39-
case "quit":
40-
case "go":
41-
tell("info string Im thinking")
42-
// TODO start the thinking process in the engine from "go"
43-
44-
}
45-
}
46-
}()
42+
toEngine = make(chan bool)
43+
go rootx(toEngine, frEngine)
4744

4845
return
4946
}
5047

48+
func root(toEngine chan bool, frEngine chan string) {
49+
for _ = range toEngine {
50+
tell("info string engine got go!")
51+
// genAllMoves
52+
// evaluate and sort
53+
// for each move{
54+
// score := search()
55+
// store score in move
56+
//}
57+
// reply to GUI with the best move
58+
}
59+
}
60+
61+
func search(b *boardStruct) int {
62+
63+
return b.evaluate()
64+
}
65+

engine_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ import "testing"
55
func Test_evaluate(t *testing.T) {
66
tests := []struct {
77
name string
8-
pos string
8+
pos string
99
want int
1010
}{
11-
{"","position startpos",0},
12-
{"","position startpos moves e2e4 d7d5 e4d5",100},
11+
{"", "position startpos", 0},
12+
{"e4", "position startpos moves e2e4", 24},
13+
{"Nf3", "position startpos moves g1f3", 22},
14+
{"d4", "position startpos moves d2d4", 20},
15+
{"c4", "position startpos moves c2c4", 11},
16+
{"Nc3", "position startpos moves b1c3", 21},
17+
{"e4+d4", "position startpos moves e2e4 e7e5 d2d4",20},
18+
{"e4+Nf3", "position startpos moves e2e4 e7e5 g1f3", 22},
19+
{"e4+Be2", "position startpos moves e2e4 e7e5 f1e2", 15},
20+
{"e4+Bd3", "position startpos moves e2e4 e7e5 f1d3", 17},
21+
{"e4+Bc4", "position startpos moves e2e4 e7e5 f1c4", 18},
22+
{"e4+Bb5", "position startpos moves e2e4 e7e5 f1b5", 17},
23+
{"e4+Ba6", "position startpos moves e2e4 e7e5 f1a6", 8},
1324
}
25+
1426
pSqInit()
1527
for _, tt := range tests {
1628
handlePosition(tt.pos)

magic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ func bitCombs(wBits bitBoard, fr, currSq, currIx int, maxM *int, mTabEntry *sMag
109109
// 1
110110
//
111111
// wBits |= (uint64(1) << uint(currSq))
112-
wBits.set(uint(currSq))
112+
wBits.set(currSq)
113113
cnt += bitCombs(wBits, fr, currSq, currIx, maxM, mTabEntry, dirs)
114114

115115
// 0
116116
//
117117
// wBits &= ^(uint64(1) << uint(currSq))
118-
wBits.clr(uint(currSq))
118+
wBits.clr(currSq)
119119
cnt += bitCombs(wBits, fr, currSq, currIx, maxM, mTabEntry, dirs)
120120

121121
return cnt

magic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Test_sMagic_atks(t *testing.T) {
2929
}
3030
}
3131

32-
func createBitBoard(bits ...uint) bitBoard {
32+
func createBitBoard(bits ...int) bitBoard {
3333
BB := bitBoard(0)
3434
for _, b := range bits {
3535
BB.set(b)

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ func init(){
1414
initAtksKings()
1515
initAtksKnights()
1616
initCastlings()
17+
pSqInit()
1718
board.newGame()
1819
}

moves.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,36 @@ func (m move) StringFull() string {
4848
return fmt.Sprintf("%v%v-%v%v%v", p, fr, cp[:1], to, pr)
4949
}
5050

51-
func (m *move) packMove(fr, to, p12, cp, pr, epSq, castl uint) {
51+
func (m *move) packMove(fr, to, p12, cp, pr, epSq int, castl castlings) {
5252
// 6 bits fr, 6 bits to, 4 bits p12, 4 bits cp, 4 bits prom, 4 bits ep, 4 bits castl = 32 bits
5353
*m = move(fr | (to << toShift) | (p12 << p12Shift) |
54-
(cp << cpShift) | (pr << prShift) | (epSq << epShift) | (castl << castlShift))
54+
(cp << cpShift) | (pr << prShift) | (epSq << epShift) | int(castl << castlShift) )
55+
}
56+
func (m *move) packEval(score int) {
57+
(*m) |= move(score + 30000) << evalShift
5558
}
5659

57-
func (m move) fr() uint {
58-
return uint(m & frMask)
60+
func (m move) eval() int {
61+
return int((uint(m) & uint(evalMask)) >>evalShift) -30000
62+
}
63+
func (m move) fr() int {
64+
return int(m & frMask)
5965
}
60-
func (m move) to() uint {
61-
return uint(m&toMask) >> toShift
66+
func (m move) to() int {
67+
return int(m&toMask) >> toShift
6268
}
6369

64-
func (m move) p12() uint {
65-
return uint(m&p12Mask) >> p12Shift
70+
func (m move) p12() int {
71+
return int(m&p12Mask) >> p12Shift
6672
}
67-
func (m move) cp() uint {
68-
return uint(m&cpMask) >> cpShift
73+
func (m move) cp() int {
74+
return int(m&cpMask) >> cpShift
6975
}
70-
func (m move) pr() uint {
71-
return uint(m&prMask) >> prShift
76+
func (m move) pr() int {
77+
return int(m&prMask) >> prShift
7278
}
73-
func (m move) ep() uint {
74-
return uint(m&epMask) >> epShift
79+
func (m move) ep() int {
80+
return int(m&epMask) >> epShift
7581
}
7682
func (m move) castl() castlings {
7783
return castlings(m&castlMask) >> castlShift
@@ -89,6 +95,19 @@ func (ml *moveList) remove(ix int){
8995
}
9096
}
9197

98+
// Sort is sorting the moves in the Score/Move list according to the score per move
99+
func (ml *moveList) sort(){
100+
bSwap := true
101+
for bSwap {
102+
bSwap = false
103+
for i := 0; i < len(*ml)-1; i++ {
104+
if (*ml)[i+1].eval() > (*ml)[i].eval() {
105+
(*ml)[i],(*ml)[i+1] = (*ml)[i+1], (*ml)[i]
106+
bSwap = true
107+
}
108+
}
109+
}
110+
}
92111

93112
func (ml moveList) String() string {
94113
theString := ""

0 commit comments

Comments
 (0)