-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.go
199 lines (177 loc) · 3.67 KB
/
evaluation.go
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
// Package ghess Evaluation scores a certain position.
package ghess
import (
"errors"
"math/rand"
"time"
)
/*
State Functions ##################################3
*/
/*
Evaluation is HERE: #######################################
### Ideas for Evaluation:
- tension
- mobility (possible moves)
- Double/isolated pawns
- Open file Rook
- Outpost knight
- Center control
*/
// pawnProtect returns true if piece is protected by a pawn.
func (b *Board) pawnProtect(dest int, isWhite bool) bool {
var pot1, pot2 int
var pawn byte
if !isWhite {
pot1 = dest + 9
pot2 = dest + 11
pawn = 'p'
} else {
pot1 = dest - 9
pot2 = dest - 11
pawn = 'P'
}
switch pawn {
case b.board[pot1]:
return true
case b.board[pot2]:
return true
default:
return false
}
}
// pawnThreat returns true if square is attacked
// by enemy pawn. According to turn
func (b *Board) pawnThreat(dest int, isWhite bool) bool {
//isWhite := b.toMove == "w"
var pot1, pot2 int
var pawn byte
if isWhite {
pot1 = dest + 9
pot2 = dest + 11
pawn = 'p'
} else {
pot1 = dest - 9
pot2 = dest - 11
pawn = 'P'
}
switch pawn {
case b.board[pot1]:
return true
case b.board[pot2]:
return true
default:
return false
}
}
// threatenQueen checks if the piece threatens the enemy queen.
func (b *Board) queenThreaten(pos int, piece byte, isWhite bool) bool {
// find queen
var queen int
var e error
for idx, val := range b.board {
// Only look for 64 squares
if idx%10 == 0 || (idx+1)%10 == 0 ||
idx > 88 || idx < 11 {
continue
}
if isWhite && val == 'q' {
queen = idx
} else if !isWhite && val == 'Q' {
queen = idx
}
}
switch piece {
case 'P': // Cause pawns are weird
return false
case 'N':
e = b.validKnight(pos, queen)
case 'B':
e = b.validBishop(pos, queen)
case 'R':
e = b.validRook(pos, queen)
case 'Q':
return false
case 'K':
e = b.validKing(pos, queen, false)
}
if e == nil {
return true
}
return false
}
// TODO: implement, pawnThreatensPiece
// See chess programming wiki:
// http://chessprogramming.wikispaces.com/Simplified+evaluation+function
// Evaluate returns score based on position.
// When evaluating individual pieces, the boolean to pass
// in does not mean WHOSE turn it is but rather who owns the piece.
func (b *Board) Evaluate() int {
// For position, if piece,
var score int
if b.Checkmate {
if b.Score == "0-1" {
score -= 1000000000
} else if b.Score == "1-0" {
score += 1000000000
}
} else if b.Draw {
score -= 50000
}
for idx, val := range b.board {
// only look at 64 squares:
if val == '.' || val == ' ' {
continue
}
isWhitePiece := b.isUpper(idx)
if isWhitePiece {
score += matMap[val]
} else {
score -= matMap[val]
}
switch val {
case 'P', 'p':
if isWhitePiece {
score += whitePawnMap[idx]
} else {
score -= blackPawnMap[idx]
}
case 'N', 'n':
if isWhitePiece {
score += whiteKnightMap[idx]
} else {
score -= blackKnightMap[idx]
}
case 'B', 'b':
if isWhitePiece {
score += whiteBishopMap[idx]
} else {
score -= blackBishopMap[idx]
}
case 'R', 'r':
if isWhitePiece {
score += whiteRookMap[idx]
} else {
score -= blackRookMap[idx]
}
}
}
return score
}
/*
LULZ EVALUATION ISN'T NECESSARY!!!1!#######################
*/
// MoveRandom picks move from lists of valid moves.
// Return an error, such as checkmate or draw.
func (b *Board) MoveRandom(origs, dests []int) error {
if len(origs) < 1 {
return errors.New("There are no valid moves left")
}
rand.Seed(time.Now().UTC().UnixNano())
randomMove := rand.Intn(len(origs))
e := b.Move(origs[randomMove], dests[randomMove])
if e != nil {
return e
}
return nil
}