Skip to content

Commit

Permalink
add round and game display
Browse files Browse the repository at this point in the history
  • Loading branch information
evanofslack committed Dec 7, 2024
1 parent 34c05dc commit 8417c16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/core/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use crate::core::error::GameError;
use crate::core::hand::{MadeHand, SelectHand};
use crate::core::stage::{Blind, End, Stage};
use std::collections::HashSet;
use std::fmt;

use itertools::Itertools;

const DEFAULT_ROUND_START: usize = 0;
const DEFAULT_PLAYS: usize = 4;
const DEFAULT_DISCARDS: usize = 4;
const DEFAULT_MONEY: usize = 0;
Expand All @@ -31,6 +33,7 @@ pub struct Game {
pub stage: Stage,
pub ante: Ante,
pub action_history: Vec<Action>,
pub round: usize,

// playing
pub plays: usize,
Expand All @@ -54,6 +57,7 @@ impl Game {
stage: Stage::PreBlind,
ante: Ante::One,
action_history: Vec::new(),
round: DEFAULT_ROUND_START,
plays: DEFAULT_PLAYS,
discards: DEFAULT_DISCARDS,
reward: DEFAULT_REWARD,
Expand Down Expand Up @@ -216,6 +220,7 @@ impl Game {

pub fn next_round(&mut self) -> Result<(), GameError> {
self.stage = Stage::PreBlind;
self.round += 1;
return Ok(());
}

Expand Down Expand Up @@ -449,6 +454,23 @@ impl Game {
}
}

impl fmt::Display for Game {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "deck length: {}", self.deck.len())?;
writeln!(f, "available length: {}", self.available.len())?;
writeln!(f, "discard length: {}", self.discarded.len())?;
writeln!(f, "action history length: {}", self.action_history.len())?;
writeln!(f, "blind: {:?}", self.blind)?;
writeln!(f, "stage: {:?}", self.stage)?;
writeln!(f, "ante: {:?}", self.ante)?;
writeln!(f, "round: {}", self.round)?;
writeln!(f, "hands remaining: {}", self.plays)?;
writeln!(f, "discards remaining: {}", self.discards)?;
writeln!(f, "money: {}", self.money)?;
writeln!(f, "score: {}", self.score)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ mod tests {
// Pick a random move and execute it
let i = rand::thread_rng().gen_range(0..actions.len());
let action = actions[i].clone();
dbg!(action.clone());
let action_res = g.handle_action(action.clone());
println!("game state:\n{}", g.clone());
println!("play action: {}", action.clone());
let action_res = g.handle_action(action);
debug_assert!(action_res.is_ok());
}
let result = g.result();
// Ensure game is over at end
assert!(result.is_some());
// Check game state at end
assert!(matches!(g.stage, Stage::End(_)));
dbg!(g.action_history);
println!("game action history: {:?}", g.action_history);
}
}

0 comments on commit 8417c16

Please sign in to comment.