Skip to content

Commit

Permalink
Merge pull request #2 from bleuthoot-sven/main
Browse files Browse the repository at this point in the history
Reduce file size by removing/optimizing dependencies
  • Loading branch information
hubble459 authored Jan 1, 2025
2 parents d1ca261 + 98f3250 commit 1e0e329
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/.idea
212 changes: 4 additions & 208 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
color-print = "0.3.7"
getch = "0.3.1"
lazy_static = "1.5.0"
rand = "0.8.5"
strum = { version = "0.26.3", features = ["derive"] }
getch = "0.3"
fastrand = "2.3"

[profile.release]
opt-level = 3
Expand Down
10 changes: 5 additions & 5 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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);
Expand Down Expand Up @@ -39,16 +38,17 @@ fn get_best_move(board: &Board, difficulty: &BotDifficulty) -> usize {
return scores[0].0;
};

let mut rng = rand::thread_rng();
let length = scores.len();

match difficulty {
BotDifficulty::Easy => scores[rng.gen_range(0..length)].0,
BotDifficulty::Easy => scores[fastrand::usize(0..length)].0,
BotDifficulty::Normal => {
scores[rng.gen_range(0..(length.saturating_sub(length / 2).max(1)))].0
let number = fastrand::usize(0..(length.saturating_sub(length / 2).max(1)));
scores[number].0
}
BotDifficulty::Difficult => {
scores[rng.gen_range(0..(length.saturating_sub(length / 4).max(1)))].0
let number = fastrand::usize(0..(length.saturating_sub(length / 4).max(1)));
scores[number].0
}
BotDifficulty::Expert => scores[0].0,
BotDifficulty::Off => unreachable!(),
Expand Down
17 changes: 12 additions & 5 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use color_print::cprintln;

use crate::{
bot, tui, Board, BotDifficulty, CellState, Command, GameOver, DIRECTIONS, I_HEIGHT, I_WIDTH,
SIZE, WIDTH,
Expand All @@ -16,8 +14,7 @@ pub fn start_game() {
match tui::get_command() {
Command::Quit => break,
Command::Bot => {
bot_difficulty =
BotDifficulty::from_repr(bot_difficulty as u8 + 1).unwrap_or_default()
bot_difficulty = change_difficulty(bot_difficulty)
}
Command::Reset => reset_game(&mut board, &mut turn),
Command::Move(row) => match make_move(&mut board, turn, row) {
Expand Down Expand Up @@ -57,7 +54,7 @@ pub fn start_game() {
};
}
Err(message) => {
cprintln!("<red>{}</>", message);
println!("\x1b[31m{}\x1b[0m", message);
println!("Press any key to continue...");
tui::pause();
}
Expand All @@ -66,6 +63,16 @@ pub fn start_game() {
}
}

fn change_difficulty(bot_difficulty: BotDifficulty) -> BotDifficulty {
match bot_difficulty {
BotDifficulty::Off => BotDifficulty::Easy,
BotDifficulty::Easy => BotDifficulty::Normal,
BotDifficulty::Normal => BotDifficulty::Difficult,
BotDifficulty::Difficult => BotDifficulty::Expert,
BotDifficulty::Expert => BotDifficulty::Off
}
}

fn transform_to_index(row: usize, column: usize) -> usize {
column * WIDTH + row
}
Expand Down
31 changes: 21 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_print::cstr;
use std::fmt::Display;
use std::fmt;
use std::fmt::{Display, Formatter};

pub(crate) mod bot;
pub mod game;
Expand Down Expand Up @@ -40,31 +40,42 @@ pub(crate) enum Command {
Move(usize),
}

#[derive(Default, PartialEq, Eq, strum::AsRefStr, strum::FromRepr)]
#[derive(Default, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum BotDifficulty {
#[strum(serialize = "off")]
#[default]
Off = 0,
#[strum(serialize = "easy")]
Easy = 1,
#[strum(serialize = "normal")]
Normal = 2,
#[strum(serialize = "difficult")]
Difficult = 3,
#[strum(serialize = "expert")]
Expert = 4,
}

impl Display for BotDifficulty {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
BotDifficulty::Off => "off",
BotDifficulty::Easy => "easy",
BotDifficulty::Normal => "normal",
BotDifficulty::Difficult => "difficult",
BotDifficulty::Expert => "expert"
}
)
}
}

impl Display for CellState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
CellState::Empty => " ",
CellState::PlayerOne => cstr!("<green>X</green>"),
CellState::PlayerTwo => cstr!("<red>O</red>"),
CellState::PlayerOne => "\x1b[32mX\x1b[0m",
CellState::PlayerTwo => "\x1b[31mO\x1b[0m",
}
)
}
Expand Down
Loading

0 comments on commit 1e0e329

Please sign in to comment.