From 4734c5c3d2b83a46c38bec95efaab94e40f125fa Mon Sep 17 00:00:00 2001 From: Philip Cramer <107579314+PhilipCramer@users.noreply.github.com> Date: Wed, 18 Feb 2026 22:23:07 +0100 Subject: [PATCH] chore: update dependencies --- Cargo.toml | 8 ++++---- benches/othello.rs | 26 ++++++++++++++++++++++---- src/main.rs | 19 ++++++++++++------- src/mcts.rs | 6 +++--- src/othello.rs | 7 +++---- 5 files changed, 44 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7bfbd5..481e942 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,12 +6,12 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ureq = { version = "2.9.6", features = ["json"] } -serde_json = "1.0.114" -rand = "0.8.5" +ureq = { version = "3.2.0", features = ["json"] } +serde_json = "1.0.149" +rand = "0.10.0" [dev-dependencies] -criterion = "0.3.4" +criterion = "0.8.2" [[bench]] name = "othello" diff --git a/benches/othello.rs b/benches/othello.rs index dac330c..90f9b73 100644 --- a/benches/othello.rs +++ b/benches/othello.rs @@ -1,18 +1,36 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{criterion_group, criterion_main, Criterion}; use rusty_othello_ai::{ mcts::MCTS, othello::{simulate_game, State}, }; use std::time::Duration; +pub fn bench_game(c: &mut Criterion) { + let mut group = c.benchmark_group("game"); + group + .sample_size(1000) + .measurement_time(Duration::from_secs(10)); + group.bench_function("play game 1", |b| { + b.iter(|| { + let mut state = State::new(); + let mut actions = state.get_actions(); + while !actions.is_empty() { + state = state.do_action(Some(actions[0].clone())); + actions = state.get_actions(); + } + }) + }); + + group.finish() +} + pub fn bench_simulate_game(c: &mut Criterion) { let mut group = c.benchmark_group("simulate_game"); group .sample_size(1000) .measurement_time(Duration::from_secs(10)); - let game_state = rusty_othello_ai::othello::State::new(); group.bench_function("simulate game 1", |b| { - b.iter(|| simulate_game(black_box(&mut game_state.clone()))) + b.iter(|| simulate_game(&State::new())) }); group.finish() @@ -30,5 +48,5 @@ pub fn bench_mcts_search(c: &mut Criterion) { group.finish() } -criterion_group!(game, bench_simulate_game, bench_mcts_search); +criterion_group!(game, bench_game, bench_simulate_game, bench_mcts_search); criterion_main!(game); diff --git a/src/main.rs b/src/main.rs index 45f4f9e..3a72f85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ use std::process::exit; use std::time::Duration; use std::usize; use std::{borrow::Borrow, thread::sleep}; -use ureq::Response; +use ureq::http::Response; +use ureq::Body; mod console_game; mod mcts; mod othello; @@ -91,8 +92,8 @@ fn is_my_turn(ai: &String) -> Result> { let url = format!("{}/turn", SERVER_URL); match ureq::get(&url).call() { Ok(response) => { - let body = response.into_string()?; - match body.trim() { + let mut body = response.into_body(); + match &body.read_to_string()? { // If the response is "true", it's the AI's turn and the function returns Ok(true) x if x == ai => return Ok(true), // If the response is "false", it's not the AI's turn and the function returns Ok(false) @@ -122,7 +123,11 @@ fn get_game_state() -> State { loop { match get_json() { Ok(resp) => { - return parse_state(resp.into_json().expect("Error parsing response to json")) + return parse_state( + resp.into_body() + .read_json() + .expect("Error parsing response to json"), + ) } Err(_e) => { sleep(delay); @@ -135,14 +140,14 @@ fn get_game_state() -> State { // This function makes a GET request to the server to get the current game board // The response is returned as a Result -fn get_json() -> Result { +fn get_json() -> Result, ureq::Error> { let url = format!("{}/board", SERVER_URL); let resp = ureq::get(&url).call()?; Ok(resp) } // Function to send the AI's move to the server -fn send_move(player: &String, ai_move: Option) -> Result { +fn send_move(player: &String, ai_move: Option) -> Result, ureq::Error> { let resp; let url; // If the AI has a move, format the URL for the setChoice endpoint @@ -168,5 +173,5 @@ fn send_progress(current: usize, total: usize, ai_color: &Color) { Color::WHITE => "true", }; let url = format!("{}/AIStatus/{}/{}/{}", SERVER_URL, current, total, color); - _ = ureq::post(&url).call(); + _ = ureq::post(&url); } diff --git a/src/mcts.rs b/src/mcts.rs index 3de11d2..b7814cc 100644 --- a/src/mcts.rs +++ b/src/mcts.rs @@ -1,5 +1,5 @@ use crate::othello::{simulate_game, Action, Color, State}; -use rand::Rng; +use rand::prelude::*; use std::collections::HashMap; #[derive(Debug, Clone)] @@ -153,8 +153,8 @@ impl MCTS { return self.size - 1; } else { // Pick one random action to expand (not all at once) - let mut rng = rand::thread_rng(); - let action_index = rng.gen_range(0..untried_actions.len()); + let mut rng = rand::rng(); + let action_index = rng.random_range(0..untried_actions.len()); let action = untried_actions[action_index].clone(); // Remove this action from untried_actions in the original node diff --git a/src/othello.rs b/src/othello.rs index c442a2d..d54ac46 100644 --- a/src/othello.rs +++ b/src/othello.rs @@ -1,4 +1,4 @@ -use rand::Rng; +use rand::prelude::*; use std::{fmt, isize, u16, usize}; const BOARD_SIZE: usize = 8; @@ -400,7 +400,6 @@ impl Action { } } -#[inline] pub fn simulate_game(state: &State) -> isize { let mut test_state = state.clone(); let mut consecutive_skips = 0; @@ -419,8 +418,8 @@ pub fn simulate_game(state: &State) -> isize { current_action = None; consecutive_skips += 1; } else { - let mut rng = rand::thread_rng(); - let index = rng.gen_range(0..test_actions.len()); + let mut rng = rand::rng(); + let index = rng.random_range(0..test_actions.len()); current_action = Some(test_actions[index].clone()); consecutive_skips = 0; }