diff --git a/src/gaming.rs b/src/gaming.rs new file mode 100644 index 0000000..f81fa5b --- /dev/null +++ b/src/gaming.rs @@ -0,0 +1,44 @@ +use std::io; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SquareInput { + File(u8), // столбец 0..8 (a..h) + Rank(u8), // строка 0..8 (1..8) + Cell(u8), // конкретная клетка 0..64 +} + +impl SquareInput { + pub fn matches(&self, sq: u8) -> bool { + match *self { + SquareInput::File(f) => (sq % 8) == f, + SquareInput::Rank(r) => (sq / 8) == r, + SquareInput::Cell(c) => sq == c, + } + } +} + +pub fn read_square() -> SquareInput { + loop { + let mut s = String::new(); + io::stdin().read_line(&mut s).unwrap(); + let b = s.trim().to_lowercase().into_bytes(); + + match b.len() { + 1 => { + if (b'a'..=b'h').contains(&b[0]) { + return SquareInput::File(b[0] - b'a'); + } + if (b'1'..=b'8').contains(&b[0]) { + return SquareInput::Rank(b[0] - b'1'); + } + } + 2 => { + if (b'a'..=b'h').contains(&b[0]) && (b'1'..=b'8').contains(&b[1]) { + return SquareInput::Cell((b[1] - b'1') * 8 + (b[0] - b'a')); + } + } + _ => {} + } + println!("не подходит, ещё раз:"); + } +} diff --git a/src/main.rs b/src/main.rs index 14cc56a..57bab9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,15 +4,58 @@ use chess_api::*; mod render_bitmask; use render_bitmask::*; +mod gaming; +use gaming::read_square; + fn main() { - let mut game: Breakthrough = Breakthrough::new(); + let mut game = Breakthrough::new(); - let b = game.get_board(); + loop { + println!("---<>---"); - println!("move: {}", game.legal_moves().len()); + let b = game.get_board(); - render_board(BoardColor::Solid('~'), vec![(b.get_mask(Color::White, 0), 'W'), (0xff << 56, 'f')]); + render_board( + BoardColor::Checkerboard('•', ' '), + &[ + (b.get_mask(Color::White, 0), '●'), + (b.get_mask(Color::Black, 0), '○'), + ], + ); - println!("move: {:?}", game.legal_moves()); + let status = game.status(); + println!("side: {:?}", game.side_to_move()); + if status != GameStatus::Ongoing { + println!("game over: {:?}", status); + break; + } + + let moves = game.legal_moves(); + + let to_input = read_square(); + let candidates: Vec = moves.iter().filter(|m| to_input.matches(m.to)).copied().collect(); + + let chosen = match candidates.len() { + 0 => { + println!("no legal move to that square"); + continue; + } + 1 => candidates[0], + _ => { + let from_input = read_square(); + let exact: Vec = candidates.iter().filter(|m| from_input.matches(m.from)).copied().collect(); + match exact.len() { + 0 => { + println!("no legal move from that square"); + continue; + } + _ => exact[0], + } + } + }; + + game.apply(chosen).unwrap(); + } } + diff --git a/src/render_bitmask/mod.rs b/src/render_bitmask/mod.rs index ba80ddd..2465f60 100644 --- a/src/render_bitmask/mod.rs +++ b/src/render_bitmask/mod.rs @@ -1,4 +1,4 @@ - +#[derive(Clone, Copy)] pub enum BoardColor { Solid(char), Checkerboard(char, char), @@ -21,29 +21,29 @@ pub fn render_u64(a: u64) { -pub fn render_board(background: BoardColor, layers: Vec<(u64, char)>){ - let mut p: u64 = 1 << 56; - for y in 0..8 { - for x in 0..8 { - - if let Some((_, ch)) = layers.iter().find(|(a, _)| (p << x as u64) & a > 0) { - print!("{}", ch) - } else { - match background { - BoardColor::Solid(a) => { +pub fn render_board_row(background: BoardColor, layers: &[(u64, char)], row: u8) { + let p: u64 = 1 << 56 >> (row as u64 * 8); + for x in 0..8 { + if let Some((_, ch)) = layers.iter().find(|(a, _)| (p << x as u64) & a > 0) { + print!("{}", ch) + } else { + match background { + BoardColor::Solid(a) => print!("{}", a), + BoardColor::Checkerboard(a, b) => { + if (x + row as usize) % 2 == 0 { print!("{}", a) - }, - BoardColor::Checkerboard(a, b) => { - if (x + y) % 2 == 0 { - print!("{}", a) - } else { - print!("{}", b) - } + } else { + print!("{}", b) } } } } - p = p >> 8; - print!("\n") + } + print!("\n"); +} + +pub fn render_board(background: BoardColor, layers: &[(u64, char)]) { + for row in 0..8 { + render_board_row(background.clone(), layers, row); } }