Skip to content

Commit

Permalink
refactor: remove unneeded derives
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Correia committed Dec 30, 2024
1 parent 7bc49e0 commit 32d983a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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!");
Expand Down
18 changes: 9 additions & 9 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,23 +24,23 @@ 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;
}
}
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;
Expand Down Expand Up @@ -161,14 +161,14 @@ fn make_move(board: &mut Board, turn: CellState, row: usize) -> Result<usize, St
}
}

fn handle_game_over(state: GameOver, bot_difficulty: BotDifficulty) -> 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!")
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ 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,
Reset,
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")]
Expand Down
2 changes: 1 addition & 1 deletion src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!("|");
Expand Down

0 comments on commit 32d983a

Please sign in to comment.