-
Notifications
You must be signed in to change notification settings - Fork 0
/
fen.odin
130 lines (109 loc) · 2.26 KB
/
fen.odin
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
package bbc
import "core:fmt"
// piece :: enum i32 { P,N,B,R,Q,K,p,n,b,r,q,k }
char_pieces: map[u8]Piece = {
'P' = Piece.P,
'N' = Piece.N,
'B' = Piece.B,
'R' = Piece.R,
'Q' = Piece.Q,
'K' = Piece.K,
'p' = Piece.p,
'n' = Piece.n,
'b' = Piece.b,
'r' = Piece.r,
'q' = Piece.q,
'k' = Piece.k,
}
parse_fen :: proc(fen: string) {
// RESET boards
bitboards = {}
occupancies = {}
side = 0
enpassant = i32(board_square.no_sq)
castle = 0
index := 0
fen_str := transmute([]u8)fen
fen_char := fen_str[index]
for rank in 0 ..< 8 {
for file := 0; file < 8; file += 1 {
square := rank * 8 + file
// match pieces
if (fen_char >= 'a' && fen_char <= 'z') || (fen_char >= 'A' && fen_char <= 'Z') {
piece := char_pieces[fen_char]
set_bit(&bitboards[piece], i32(square))
index += 1
fen_char = fen_str[index]
}
// match empty square number
if fen_char >= '0' && fen_char <= '9' {
offset := fen_char - '0'
piece := -1
for bb_piece in 0 ..< 12 {
if get_bit(bitboards[bb_piece], i32(square)) {
piece = bb_piece
}
}
if piece == -1 {
file -= 1
}
file += int(offset)
index += 1
fen_char = fen_str[index]
}
// match rank separator
if fen_char == '/' {
index += 1
fen_char = fen_str[index]
}
}
}
// skip space
index += 1
fen_char = fen_str[index]
// parse side
if fen_char == 'w' {
side = white
} else {
side = black
}
//skip side and space
index += 2
fen_char = fen_str[index]
for fen_char != ' ' {
switch fen_char {
case 'K':
castle |= wk
case 'Q':
castle |= wq
case 'k':
castle |= bk
case 'q':
castle |= bq
case '-':
}
index += 1
fen_char = fen_str[index]
}
// skip space
index += 1
fen_char = fen_str[index]
if fen_char != '-' {
file := fen_char - 'a'
rank := 8 - (fen_str[index + 1] - '0')
enpassant = i32(rank * 8 + file)
} else {
enpassant = i32(board_square.no_sq)
}
// white pieces
for piece in i32(Piece.P) ..= i32(Piece.K) {
occupancies[white] |= bitboards[piece]
}
// black pieces
for piece in i32(Piece.p) ..= i32(Piece.k) {
occupancies[black] |= bitboards[piece]
}
occupancies[both] |= occupancies[white]
occupancies[both] |= occupancies[black]
//fmt.printf("fen: '%s'\n\n\n", fen_str[index:])
}