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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use crate::error::ShogiUtilError::UsiParseError;
use crate::{Board, Color, Move, Piece};
use crate::{Result, Square};

pub struct SfenBoard {
    pub board: Board,
    pub next_turn: Color,
}
impl SfenBoard {
    pub fn parse(sfen_state: &str) -> Result<Self> {
        let e = || UsiParseError(format!("Invalid sfen format: {}", sfen_state));
        let sfen_parts = sfen_state.split(' ').collect::<Vec<_>>();
        if sfen_parts.len() != 4 {
            return Err(e());
        }

        let rows = sfen_parts[0].split('/').collect::<Vec<_>>();
        if rows.len() != 9 {
            return Err(e());
        }

        let mut board = Board::empty();
        for (rank, row) in rows.into_iter().enumerate() {
            let rank = (rank + 1) as u8;
            let mut row = row.chars();
            let mut file = 9;
            while let Some(c) = row.next() {
                if c.is_numeric() {
                    let c = c as u8 - '0' as u8;
                    file -= c - 1;
                } else if c == '+' {
                    let c = row.next().ok_or_else(e)?;
                    let (piece, color) = parse_piece(c)
                        .and_then(|(p, c)| p.promote().map(|p| (p, c)))
                        .ok_or_else(e)?;
                    let pos = Square { rank, file };
                    board.push_piece(&pos, color, piece);
                } else if let Some((piece, color)) = parse_piece(c) {
                    let pos = Square { rank, file };
                    board.push_piece(&pos, color, piece);
                } else {
                    return Err(e());
                }
                file -= 1;
            }
            if file != 0 {
                return Err(e());
            }
        }

        let next_turn;
        match sfen_parts[1] {
            "b" => next_turn = Color::Black,
            "w" => next_turn = Color::White,
            _ => return Err(e()),
        }

        if sfen_parts[2] != "-" {
            let hands = parse_hand(sfen_parts[2]).ok_or_else(e)?;
            for &piece in hands[0].iter() {
                board.push_hand(piece, Color::Black);
            }
            for &piece in hands[1].iter() {
                board.push_hand(piece, Color::White);
            }
        }

        Ok(SfenBoard { board, next_turn })
    }
}

fn parse_hand(hand: &str) -> Option<[Vec<Piece>; 2]> {
    let mut hand = hand.chars();
    let mut stack = String::new();
    let mut result = [vec![], vec![]];
    while let Some(c) = hand.next() {
        if c.is_numeric() {
            stack.push(c);
        } else {
            let (piece, color) = parse_piece(c)?;
            let count = if stack.is_empty() {
                Some(1)
            } else {
                stack.parse::<usize>().ok()
            }?;
            for _ in 0..count {
                result[color.to_usize()].push(piece);
            }
            stack.clear();
        }
    }
    Some(result)
}

fn parse_piece(c: char) -> Option<(Piece, Color)> {
    let color = if c.is_ascii_uppercase() {
        Color::Black
    } else {
        Color::White
    };
    match c.to_ascii_uppercase() {
        'K' => Some((Piece::King, color)),
        'R' => Some((Piece::Rook, color)),
        'B' => Some((Piece::Bishop, color)),
        'G' => Some((Piece::Gold, color)),
        'S' => Some((Piece::Silver, color)),
        'N' => Some((Piece::Knight, color)),
        'L' => Some((Piece::Lance, color)),
        'P' => Some((Piece::Pawn, color)),
        _ => None,
    }
}

pub enum SfenMove {
    DropMove {
        to: Square,
        piece: Piece,
    },
    Travel {
        from: Square,
        to: Square,
        promoted: bool,
    },
}

impl SfenMove {
    pub fn parse(sfen_move: &str) -> Result<SfenMove> {
        let e = || UsiParseError(format!("Invalid sfen move: '{}'", sfen_move));
        if sfen_move.len() < 4 || 5 < sfen_move.len() {
            return Err(e());
        }

        let from = parse_sfen_square(&sfen_move[0..2]);
        let to = parse_sfen_square(&sfen_move[2..4]).ok_or_else(e)?;

        if let Some(from) = from {
            if sfen_move.len() == 5 {
                if &sfen_move[4..5] != "+" {
                    Err(e())
                } else {
                    Ok(SfenMove::Travel {
                        from,
                        to,
                        promoted: true,
                    })
                }
            } else {
                Ok(SfenMove::Travel {
                    from,
                    to,
                    promoted: false,
                })
            }
        } else if &sfen_move[1..2] != "*" {
            Err(e())
        } else {
            let c = sfen_move.chars().next().unwrap();
            let (piece, color) = parse_piece(c).ok_or_else(e)?;
            if color != Color::Black {
                Err(e())
            } else {
                Ok(SfenMove::DropMove { piece, to })
            }
        }
    }
}

fn parse_sfen_square(s: &str) -> Option<Square> {
    assert_eq!(s.len(), 2);
    let mut iter = s.chars();
    let file = iter.next().unwrap();
    let rank = iter.next().unwrap();
    if file < '0' || rank < 'a' {
        return None;
    }
    let file = file as u8 - '0' as u8;
    let rank = rank as u8 - 'a' as u8 + 1;
    if rank > 9 || file > 9 {
        None
    } else {
        Some(Square { rank, file })
    }
}

pub enum UsiRequest {
    Usi,
    IsReady,
    SetOption { id: String, value: String },
    NewGame,
    Position { board: Board, next_turn: Color },
    Go,
    Quit,
}

impl UsiRequest {
    pub fn parse(input: &str) -> Result<UsiRequest> {
        let command = input.split(' ').collect::<Vec<_>>();
        match command[0].trim() {
            "usi" => Ok(UsiRequest::Usi),
            "isready" => Ok(UsiRequest::IsReady),
            "setoption" => {
                if command[1] != "name" {
                    Err(UsiParseError(format!("Invalid command: {}", input)))
                } else {
                    Ok(UsiRequest::SetOption {
                        id: command[2].to_string(),
                        value: command[4].to_string(),
                    })
                }
            }
            "usinewgame" => Ok(UsiRequest::NewGame),
            "position" => match command[1] {
                "sfen" => {
                    let board_sfen = command[2];
                    let next_turn = command[3];
                    let hand_sfen = command[4];
                    let sfen_string = vec![board_sfen, next_turn, hand_sfen, "1"].join(" ");
                    let sfen_board = SfenBoard::parse(&sfen_string)?;
                    let cur_turn = sfen_board.next_turn;
                    let mut board = sfen_board.board;
                    if command[6] != "moves" {
                        return Err(UsiParseError(format!("Invalid command: {}", input)));
                    }
                    push_move_commands(&mut board, &command[7..], cur_turn)?;
                    Ok(UsiRequest::Position {
                        board,
                        next_turn: cur_turn,
                    })
                }
                "startpos" => {
                    if command.len() != 2 && command[2] != "moves" {
                        return Err(UsiParseError(format!("Invalid command: {}", input)));
                    }
                    let cur_turn = Color::Black;
                    let mut board = Board::default();

                    if command.len() >= 4 {
                        push_move_commands(&mut board, &command[3..], cur_turn)?;
                    }

                    Ok(UsiRequest::Position {
                        board,
                        next_turn: cur_turn,
                    })
                }
                _ => Err(UsiParseError(format!("Invalid format: {}", input))),
            },
            "go" => Ok(UsiRequest::Go),
            "quit" => Ok(UsiRequest::Quit),
            _ => Err(UsiParseError(format!("Unsupported option: {}", input))),
        }
    }
}

fn push_move_commands(board: &mut Board, command: &[&str], mut cur_turn: Color) -> Result<()> {
    for &command in command.iter() {
        match SfenMove::parse(command)? {
            SfenMove::DropMove { to, piece } => {
                board.push_move(Move {
                    from: None,
                    to,
                    piece,
                    color: cur_turn,
                })?;
            }
            SfenMove::Travel { from, to, promoted } => {
                board.move_between(&from, &to, promoted, cur_turn)?;
            }
        }

        cur_turn = cur_turn.opponent();
    }
    Ok(())
}

pub enum UsiResponse {
    Id {
        name: String,
    },
    UsiOk,
    ReadyOk,
    TravelMove {
        from: Square,
        to: Square,
        promoted: bool,
    },
    DropMove {
        piece: Piece,
        to: Square,
    },
}

impl ToString for UsiResponse {
    fn to_string(&self) -> String {
        use UsiResponse::*;
        match self {
            Id { name } => format!("id name {}", name),
            UsiOk => "usiok".to_string(),
            ReadyOk => "readyok".to_string(),
            TravelMove { from, to, promoted } => {
                let mut response = "bestmove ".to_string();
                to_sfen_square(&from, &mut response);
                to_sfen_square(&to, &mut response);
                if *promoted {
                    response.push('+');
                }
                response
            }
            DropMove { piece, to } => {
                let mut response = "bestmove ".to_string();
                response.push(piece.to_sfen());
                response.push('*');
                to_sfen_square(&to, &mut response);
                response
            }
        }
    }
}

fn to_sfen_square(sq: &Square, s: &mut String) {
    s.push((sq.file + '0' as u8) as char);
    let rank = (sq.rank - 1 + 'a' as u8) as char;
    s.push(rank);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::debug::dump_board;

    #[test]
    fn test_parse_hand() {
        let result = parse_hand("S2Pb3p").unwrap();
        assert_eq!(
            result,
            [
                vec![Piece::Silver, Piece::Pawn, Piece::Pawn],
                vec![Piece::Bishop, Piece::Pawn, Piece::Pawn, Piece::Pawn]
            ]
        );
    }
    #[test]
    fn test_parse_board() {
        let board =
            SfenBoard::parse("lnsgkgsn1/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1")
                .unwrap()
                .board;
        assert_eq!(
            dump_board(&board),
            r"P1-KY-KE-GI-KI-OU-KI-GI-KE * 
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
P4 *  *  *  *  *  *  *  *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
P+
P-
"
        );
    }

    #[test]
    fn test_parse_sfen_move() {
        if let SfenMove::Travel { from, to, promoted } = SfenMove::parse("8h2b+").unwrap() {
            assert!(promoted);
            assert_eq!(from, Square { file: 8, rank: 8 });
            assert_eq!(to, Square { file: 2, rank: 2 });
        } else {
            unreachable!()
        }

        if let SfenMove::Travel { from, to, promoted } = SfenMove::parse("7g7f").unwrap() {
            assert!(!promoted);
            assert_eq!(from, Square { file: 7, rank: 7 });
            assert_eq!(to, Square { file: 7, rank: 6 });
        } else {
            unreachable!()
        }

        if let SfenMove::DropMove { to, piece } = SfenMove::parse("S*5b").unwrap() {
            assert_eq!(piece, Piece::Silver);
            assert_eq!(to, Square { file: 5, rank: 2 });
        } else {
            unreachable!()
        }
    }
}