chess_game 0.2.0

Simple Chess game
Documentation
use std::{thread, time::Duration};
use chess::{ChessMove, Board, MoveGen, Square, EMPTY, BoardStatus};
use method_shorthands::methods::*;
use rand::prelude::*;

use super::Player;

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct MateInOneBot;

impl Player for MateInOneBot {
    fn get_move(board: Board, num_moves: usize, bot_wait_ms: u64) -> Result<ChessMove, ()> {
        if bot_wait_ms > 0 { thread::sleep(Duration::from_millis(bot_wait_ms)) };
        
        let move_gen = MoveGen::new_legal(&board);
        let move_list: Vec<ChessMove> = move_gen.collect();

        let mut m;

        // King's Pawn opener
        m = match num_moves {
            0 => ChessMove::new(Square::E2, Square::E4, None),
            1 => ChessMove::new(Square::E7, Square::E5, None),
            _ => *move_list.choose(&mut rand::thread_rng()).uw()
        };

        // try checking enemy King
        for j in &move_list {
            let temp_board = board.make_move_new(*j);
            if *temp_board.checkers() != EMPTY {
                m = *j;
            }
        }

        // take checkmate if there is one
        for j in move_list {
            let temp_board = board.make_move_new(j);
            if temp_board.status() == BoardStatus::Checkmate {
                m = j;
            }
        }

        Ok(m)
    }
}