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
use crate::{Move, MutBoard, MoveComputeType};
use crate::parse::patterns::uci_move;
use myopic_core::Square;
use myopic_core::pieces::Piece;
pub fn uci(moves: &String) -> Result<Vec<Move>, String> {
return partial_uci(&crate::start_position(), moves);
}
pub fn partial_uci<B: MutBoard>(start: &B, moves: &String) -> Result<Vec<Move>, String> {
let mut mutator_board = start.clone();
let mut dest: Vec<Move> = Vec::new();
for evolve in uci_move().find_iter(moves) {
match parse_single_move(&mut mutator_board, evolve.as_str()) {
Ok(result) => dest.push(result.clone()),
Err(_) => return Err(format!("Failed at {} in: {}", evolve.as_str(), moves)),
};
mutator_board.evolve(dest.last().unwrap());
}
Ok(dest)
}
fn parse_single_move<B: MutBoard>(start: &mut B, uci_move: &str) -> Result<Move, String> {
let (src, dest, promoting) = extract_uci_component(uci_move)?;
start
.compute_moves(MoveComputeType::All)
.into_iter()
.find(|mv| match mv {
&Move::Standard(_, s, d) => s == src && d == dest,
&Move::Enpassant(s, d) => s == src && d == dest,
&Move::Promotion(s, d, p) => {
s == src
&& d == dest
&& promoting.map(|c| piece_char(p) == c).unwrap_or(false)
},
&Move::Castle(zone) => {
let (_, king_src, king_dest) = zone.king_data();
src == king_src && dest == king_dest
}
}
)
.ok_or(format!("No moves matching {}", uci_move))
}
fn extract_uci_component(pgn_move: &str) -> Result<(Square, Square, Option<char>), String> {
let src = Square::from_string(
pgn_move.chars().take(2).collect::<String>().as_str()
)?;
let dest = Square::from_string(
pgn_move.chars().skip(2).take(2).collect::<String>().as_str()
)?;
Ok((src, dest, pgn_move.chars().skip(4).next()))
}
fn piece_char(piece: Piece) -> char {
match piece {
Piece::WQ | Piece::BQ => 'q',
Piece::WR | Piece::BR => 'r',
Piece::WB | Piece::BB => 'b',
Piece::WN | Piece::BN => 'n',
_ => 'x'
}
}
#[cfg(test)]
mod test {
fn execute_success_test(expected_finish: &'static str, uci: &'static str) {
let finish = crate::fen_position(expected_finish).unwrap();
let mut board = crate::start_position();
for evolve in super::partial_uci(&board, &String::from(uci)).unwrap() {
board.evolve(&evolve);
}
assert_eq!(finish, board);
}
#[test]
fn case_zero() {
execute_success_test(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
""
)
}
#[test]
fn case_one() {
execute_success_test(
"r2qkb1r/pp1n1pp1/2p1pnp1/8/3PN3/P3P1P1/1P3PBP/1nBQK2R w Kkq - 0 13",
"1. d2d4 d7d5 2. c2c4 c7c6 3. g1f3 g8f6 4. e2e3 c8f5 5. b1c3 e7e6 6. f3h4 f5g6
7. h4g6 h7g6 8. g2g3 b8d7 9. f1g2 d5c4 10. c3e4 c4c3 11. a2a3 c3c2 12. a1b1 c2b1n",
);
}
}
#[cfg(test)]
mod test_single_move {
use super::*;
use myopic_core::Square::*;
use myopic_core::castlezone::CastleZone;
fn execute_success_test(expected: Move, start_fen: &'static str, uci: &'static str) {
let mut board = crate::fen_position(start_fen).unwrap();
let uci_parse = parse_single_move(&mut board, &String::from(uci)).unwrap();
assert_eq!(expected, uci_parse);
}
#[test]
fn case_one() {
execute_success_test(
Move::Standard(Piece::BB, G4, F3),
"rn1qkbnr/pp2pppp/2p5/3p4/4P1b1/2N2N1P/PPPP1PP1/R1BQKB1R b KQkq - 0 4",
"g4f3",
)
}
#[test]
fn case_two() {
execute_success_test(
Move::Enpassant(E5, Square::F6),
"r2qkbnr/pp1np1pp/2p5/3pPp2/8/2N2Q1P/PPPP1PP1/R1B1KB1R w KQkq f6 0 7",
"e5f6",
)
}
#[test]
fn case_three() {
execute_success_test(
Move::Promotion(F7, G8, Piece::WN),
"r2q1bnr/pp1nkPpp/2p1p3/3p4/8/2N2Q1P/PPPP1PP1/R1B1KB1R w KQ - 1 9",
"f7g8n",
)
}
#[test]
fn case_four() {
execute_success_test(
Move::Promotion(F7, G8, Piece::WQ),
"r2q1bnr/pp1nkPpp/2p1p3/3p4/8/2N2Q1P/PPPP1PP1/R1B1KB1R w KQ - 1 9",
"f7g8q",
)
}
#[test]
fn case_five() {
execute_success_test(
Move::Standard(Piece::BR, A8, E8),
"r5r1/ppqkb1pp/2p1pn2/3p2B1/3P4/2NB1Q1P/PPP2PP1/4RRK1 b - - 8 14",
"a8e8",
)
}
#[test]
fn case_six() {
execute_success_test(
Move::Standard(Piece::WR, E1, E2),
"4rr2/ppqkb1p1/2p1p2p/3p4/3Pn2B/2NBRQ1P/PPP2PP1/4R1K1 w - - 2 18",
"e1e2",
)
}
#[test]
fn case_seven() {
execute_success_test(
Move::Standard(Piece::BR, F3, F6),
"5r2/ppqkb1p1/2p1pB1p/3p4/3Pn2P/2NBRr2/PPP1RPP1/6K1 b - - 0 20",
"f3f6",
)
}
#[test]
fn case_eight() {
execute_success_test(
Move::Standard(Piece::BN, E4, F2),
"5r2/ppqkb1p1/2p1pr1p/3p4/3Pn2P/2NBR3/PPP1RPP1/7K b - - 1 21",
"e4f2",
)
}
#[test]
fn case_nine() {
execute_success_test(
Move::Standard(Piece::BR, F8, F1),
"5r2/ppqkb1p1/2p1p2p/3p4/P2P3P/2N1R3/1PP3P1/5B1K b - - 0 24",
"f8f1",
)
}
#[test]
fn case_ten() {
execute_success_test(
Move::Castle(CastleZone::WK),
"r3k2r/pp1q1ppp/n1p2n2/4p3/3pP2P/3P1QP1/PPPN1PB1/R3K2R w KQkq - 1 13",
"e1g1",
)
}
#[test]
fn case_eleven() {
execute_success_test(
Move::Castle(CastleZone::BQ),
"r3k2r/pp1q1ppp/n1p2n2/4p3/3pP2P/3P1QP1/PPPN1PB1/R4RK1 b kq - 2 13",
"e8c8",
)
}
}