From bdb7d5faca3c512705c2f13ea7a26069d622719c Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Fri, 13 Dec 2024 13:39:07 -0600 Subject: cleanup and minor spelling mistake Signed-off-by: Matt Strapp --- README.md | 4 +++- wwb/src/main.rs | 59 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 91e6c3c..75c7e25 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ A simulation of WWB has never been done either, the longest known one crashed at ### My current record -Using this program, a computer managed to go at least 15 trillion turns overnight with a high score of 17. The game runs about 150-200 million turns a second on my Ryzen 9 5900X running Arch Linux on WSL. Running it on Windows on the same machine was about the same. +Using this program, a computer managed to go at least 15 trillion turns overnight with a high score of 17. The game runs about 150-200 million turns a second on my AMD Ryzen 9 5900X running Arch Linux on WSL. Running it on Windows on the same machine was about the same. + +This program is single-threaded as the collision detection mechanic requires the two threads to essentially take turns as well as the fact that the game state is shared between the two threads. Performance gains are likely minimal and might even be negative, though I have not tested this. #### Exact info diff --git a/wwb/src/main.rs b/wwb/src/main.rs index ca40d9e..1e69d03 100644 --- a/wwb/src/main.rs +++ b/wwb/src/main.rs @@ -8,24 +8,13 @@ use std::process::exit; use std::sync::{Arc, Mutex}; use wwb::*; -static BINCODE_COFNIG: config::Configuration = config::standard(); +static BINCODE_CONFIG: config::Configuration = config::standard(); /// Roll a d6 and return the result. fn roll_d6() -> u8 { fastrand::u8(1..=6) } -/// Save the game state to a file. -/// -/// Note: If there is no path, the game will not be saved. -fn save_game(game: &Game, path: &str) { - if path.is_empty() { - return; - } - let serialized_game = bincode::encode_to_vec(game, BINCODE_COFNIG).unwrap(); - fs::write(path, serialized_game).unwrap(); -} - /// Load the game state from a file. /// Note: If there is no path, the game will not be loaded. fn load_game(path: &str) -> Game { @@ -35,13 +24,22 @@ fn load_game(path: &str) -> Game { match fs::read(path) { Ok(serialized_game) => { let (game, _): (Game, usize) = - bincode::decode_from_slice(&serialized_game, BINCODE_COFNIG).unwrap(); + bincode::decode_from_slice(&serialized_game, BINCODE_CONFIG).unwrap(); game } - Err(_) => { - Game::default() - } + Err(_) => Game::default(), + } +} + +/// Save the game state to a file. +/// +/// Note: If there is no path, the game will not be saved. +fn save_game(game: &Game, path: &str) { + if path.is_empty() { + return; } + let serialized_game = bincode::encode_to_vec(game, BINCODE_CONFIG).unwrap(); + fs::write(path, serialized_game).unwrap(); } fn main() { @@ -68,10 +66,6 @@ fn main() { .target(Target::Stdout) .init(); - game_loop(path); -} - -fn game_loop(path: String) { let game = Arc::new(Mutex::new(load_game(&path))); // Set up Ctrl-C handler to print the game state before exiting. @@ -86,22 +80,27 @@ fn game_loop(path: String) { }) .expect("Error setting Ctrl-C handler"); + game_loop(&game, &path); +} + +/// ### Game loop: +/// 1. Player rolls d6 +/// 2. If not 5, next player's turn +/// 3. If 5, advance one space +/// 4. If players are on the same space that is NOT 0, move BOTH back to space 0 +/// 5. If there was no collision, roll d6 again +/// 6. If not 5, go back to space 0 +/// 7. If 5 again, nothing happens +/// 8. Next player's turn +/// +fn game_loop(game: &Arc>, path: &str) { loop { let mut game = game.lock().unwrap(); game.turn_count += 1; - // Game loop: - // Player rolls d6 - // If not 5, next player's turn - // If 5, advance one space - // If players are on the same space that is NOT 0, move BOTH back to space 0 - // If there was no collision, roll d6 again - // If not 5, go back to space 0 - // If 5 again, nothing happens - // Next player's turn // This will probably not work properly when the turn count goes above 2^32 on a 32-bit machine and 3^64 on a 64-bit machine. let current_player_number = game.turn_count as usize % PLAYER_COUNT; - let mut current_space = game.players[current_player_number].current_space; + let mut current_space: u16 = game.players[current_player_number].current_space; let roll = roll_d6(); log::debug!( -- cgit v1.2.3