aboutsummaryrefslogtreecommitdiffstats
path: root/wwb/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'wwb/src/main.rs')
-rw-r--r--wwb/src/main.rs61
1 files changed, 26 insertions, 35 deletions
diff --git a/wwb/src/main.rs b/wwb/src/main.rs
index b09c7c4..963fecb 100644
--- a/wwb/src/main.rs
+++ b/wwb/src/main.rs
@@ -10,16 +10,6 @@ fn roll_d6() -> u8 {
fastrand::u8(1..=6)
}
-fn current_space_pretty(current_space: u16) -> String {
- if current_space == 0 {
- "Start".to_string()
- } else if current_space == BOARD_SIZE + 1 {
- "Finish".to_string()
- } else {
- current_space.to_string()
- }
-}
-
fn main() {
let env = Env::new().filter_or("RUST_LOG", "info");
// Create logger
@@ -41,6 +31,7 @@ fn main() {
let game_clone = Arc::clone(&game);
ctrlc::set_handler(move || {
let game = game_clone.lock().unwrap();
+ println!("Game state at exit:");
log::error!("{:#?}", *game);
exit(0);
})
@@ -62,24 +53,14 @@ fn main() {
// 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 current_turn_pretty = game
- .turn_count
- .to_string()
- .as_bytes()
- .rchunks(3)
- .rev()
- .map(std::str::from_utf8)
- .collect::<Result<Vec<&str>, _>>()
- .unwrap()
- .join(",");
let roll = roll_d6();
log::debug!(
"Player {} rolled a {} on turn {} from space {}",
current_player_number,
roll,
- current_turn_pretty,
- current_space_pretty(current_space)
+ game.turn_count,
+ current_space
);
if roll != 5 {
@@ -88,7 +69,10 @@ fn main() {
current_space += 1;
// Check if the player has won by reaching the space after the last space.
if current_space == BOARD_SIZE + 1 {
- println!("Player {} has reached the Finish space and won on turn {}!", current_player_number, current_turn_pretty);
+ log::error!(
+ "Player {} has reached the Finish space and won on turn {}!",
+ current_player_number, game.turn_count
+ );
break;
}
@@ -96,8 +80,16 @@ fn main() {
log::info!(
"Player {} has a new high score of {} on turn {}",
current_player_number,
- current_space_pretty(current_space),
- current_turn_pretty
+ current_space,
+ game.turn_count
+ .to_string()
+ .as_bytes()
+ .rchunks(3)
+ .rev()
+ .map(std::str::from_utf8)
+ .collect::<Result<Vec<&str>, _>>()
+ .unwrap()
+ .join(",")
);
game.players[current_player_number].high_score = current_space;
}
@@ -106,11 +98,10 @@ fn main() {
for player in game.players.iter_mut() {
if player.current_space == current_space && player.current_space != 0 {
- log::debug!(
- "Two players collided on space {} on turn {}",
- current_space_pretty(current_space),
- current_turn_pretty
- );
+ log::debug!(
+ "Two players collided on space {}!",
+ current_space
+ );
player.current_space = 0;
current_space = 0;
collision = true;
@@ -123,14 +114,14 @@ fn main() {
log::debug!(
"Player {} rolled a 5 again on turn {} gets to stay on space {}",
current_player_number,
- current_turn_pretty,
- current_space_pretty(current_space)
+ game.turn_count,
+ current_space
);
} else {
log::debug!(
"Player {} rolled a non-5 after rolling a 5 on turn {} and goes back to Start",
current_player_number,
- current_turn_pretty
+ game.turn_count
);
current_space = 0;
}
@@ -140,8 +131,8 @@ fn main() {
log::debug!(
"Player {} ends turn {} on space {}",
current_player_number,
- current_turn_pretty,
- current_space_pretty(current_space)
+ game.turn_count,
+ current_space
);
}
}