From 32d983a82f8f622ce71140a90850293150578d13 Mon Sep 17 00:00:00 2001 From: Quentin Correia Date: Mon, 30 Dec 2024 23:50:03 +0100 Subject: [PATCH] refactor: remove unneeded derives --- src/bot.rs | 10 +++++----- src/game.rs | 18 +++++++++--------- src/lib.rs | 8 ++++---- src/tui.rs | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 541a993..699189a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,13 +1,13 @@ use crate::{game, Board, BotDifficulty, CellState, WIDTH}; use rand::prelude::*; -pub(crate) fn bot_move(board: &mut Board, difficulty: BotDifficulty) -> usize { - let best_move = get_best_move(&board, difficulty); +pub(crate) fn bot_move(board: &mut Board, difficulty: &BotDifficulty) -> usize { + let best_move = get_best_move(&board, &difficulty); board[best_move] = CellState::PlayerTwo; return best_move; } -fn get_best_move(board: &Board, difficulty: BotDifficulty) -> usize { +fn get_best_move(board: &Board, difficulty: &BotDifficulty) -> usize { // count is always uneven, but div by 2 floors the number let count_moves = board.iter().filter(|c| c != &&CellState::Empty).count() / 2; @@ -119,9 +119,9 @@ mod test { break; } - let index = get_best_move(&board, BotDifficulty::Easy); + let index = get_best_move(&board, &BotDifficulty::Easy); board[index] = CellState::PlayerTwo; - tui::print_ui(&board, BotDifficulty::Easy); + tui::print_ui(&board, &BotDifficulty::Easy); if let Some(_) = game::check_if_game_over(&board, &CellState::PlayerTwo, index) { println!("Player 2 wins!"); diff --git a/src/game.rs b/src/game.rs index 9d81a23..5a7501d 100644 --- a/src/game.rs +++ b/src/game.rs @@ -11,7 +11,7 @@ pub fn start_game() { let mut bot_difficulty: BotDifficulty = BotDifficulty::Off; loop { - tui::print_ui(&board, bot_difficulty); + tui::print_ui(&board, &bot_difficulty); match tui::get_command() { Command::Quit => break, @@ -24,9 +24,9 @@ pub fn start_game() { Ok(index) => { match check_if_game_over(&board, &turn, index) { Some(state) => { - tui::print_ui(&board, bot_difficulty); + tui::print_ui(&board, &bot_difficulty); - if handle_game_over(state, bot_difficulty) { + if handle_game_over(state, &bot_difficulty) { reset_game(&mut board, &mut turn); } else { break; @@ -34,13 +34,13 @@ pub fn start_game() { } None => { if bot_difficulty != BotDifficulty::Off { - let index = bot::bot_move(&mut board, bot_difficulty); + let index = bot::bot_move(&mut board, &bot_difficulty); if let Some(state) = check_if_game_over(&board, &CellState::PlayerTwo, index) { - tui::print_ui(&board, bot_difficulty); + tui::print_ui(&board, &bot_difficulty); - if handle_game_over(state, bot_difficulty) { + if handle_game_over(state, &bot_difficulty) { reset_game(&mut board, &mut turn); } else { break; @@ -161,14 +161,14 @@ fn make_move(board: &mut Board, turn: CellState, row: usize) -> Result bool { +fn handle_game_over(state: GameOver, bot_difficulty: &BotDifficulty) -> bool { match state { GameOver::Draw => println!("It's a draw!"), GameOver::Winner(cell_state) => match cell_state { CellState::Empty => unreachable!(), CellState::PlayerOne => println!("Player one wins!"), CellState::PlayerTwo => { - if bot_difficulty == BotDifficulty::Off { + if bot_difficulty == &BotDifficulty::Off { println!("Player two wins!") } else { println!("The bot wins!") @@ -238,7 +238,7 @@ mod test { for i in 0..SIZE { if board[i] != CellState::Empty { let game_over = check_if_game_over(&board, &board[i], i); - assert!(game_over.is_none(), "index: {}, winner: {:?}", i, game_over); + assert!(game_over.is_none()); } } } diff --git a/src/lib.rs b/src/lib.rs index f32df03..570feb5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,20 +19,20 @@ pub const DIRECTIONS: [(isize, isize); 4] = [ (-1, 1), // Diagonal TR-BL ]; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, PartialEq, Eq)] pub(crate) enum CellState { Empty, PlayerOne, PlayerTwo, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(PartialEq, Eq)] pub(crate) enum GameOver { Draw, Winner(CellState), } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(PartialEq, Eq)] pub(crate) enum Command { Quit, Bot, @@ -40,7 +40,7 @@ pub(crate) enum Command { Move(usize), } -#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::AsRefStr, strum::FromRepr, Default)] +#[derive(Default, PartialEq, Eq, strum::AsRefStr, strum::FromRepr)] #[repr(u8)] pub(crate) enum BotDifficulty { #[strum(serialize = "off")] diff --git a/src/tui.rs b/src/tui.rs index 383e66b..ce67b53 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -32,7 +32,7 @@ fn clear_ui() { print!("\x1B[2J\x1B[1;1H"); } -pub(crate) fn print_ui(board: &Board, bot: BotDifficulty) { +pub(crate) fn print_ui(board: &Board, bot: &BotDifficulty) { clear_ui(); print!("|");