explicit fireworks class

This commit is contained in:
Jeff Wu 2016-03-06 22:00:23 -08:00
parent b4d949ff2f
commit d2928c255d

View file

@ -79,6 +79,64 @@ pub type CardsInfo = Pile<CardInfo>;
pub type Player = u32; pub type Player = u32;
#[derive(Debug)]
pub struct Firework {
pub color: Color,
pub cards: Cards,
}
impl Firework {
fn new(color: Color) -> Firework {
let mut cards = Cards::new();
// have a 0, so it's easier to implement
let card = Card { value: 0, color: color };
cards.place(card);
Firework {
color: color,
cards: cards,
}
}
fn desired_value(&self) -> Option<Value> {
let top_value = self.cards.top().unwrap().value;
if top_value == FINAL_VALUE {
None
} else {
Some(top_value + 1)
}
}
fn score(&self) -> usize {
// subtract one to account for the 0 we pushed
self.cards.size() - 1
}
fn complete(&self) -> bool {
self.desired_value() == None
}
fn place(&mut self, card: Card) {
assert!(
card.color == self.color,
"Attempted to place card on firework of wrong color!"
);
assert!(
Some(card.value) == self.desired_value(),
"Attempted to place card of wrong value on firework!"
);
self.cards.place(card);
}
}
impl fmt::Display for Firework {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(value) = self.desired_value() {
write!(f, "{} firework at {}", self.color, value)
} else {
write!(f, "{} firework complete!", self.color)
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum Hinted { pub enum Hinted {
Color(Color), Color(Color),
@ -201,7 +259,7 @@ fn new_deck() -> Cards {
pub struct BoardState { pub struct BoardState {
deck: Cards, deck: Cards,
pub discard: Cards, pub discard: Cards,
pub fireworks: HashMap<Color, Cards>, pub fireworks: HashMap<Color, Firework>,
pub num_players: u32, pub num_players: u32,
@ -214,17 +272,16 @@ pub struct BoardState {
pub hints_remaining: u32, pub hints_remaining: u32,
pub lives_total: u32, pub lives_total: u32,
pub lives_remaining: u32, pub lives_remaining: u32,
// TODO:
// pub turn_history: Vec<TurnChoice>,
// only relevant when deck runs out // only relevant when deck runs out
deckless_turns_remaining: u32, deckless_turns_remaining: u32,
} }
impl BoardState { impl BoardState {
pub fn new(opts: &GameOptions) -> BoardState { pub fn new(opts: &GameOptions) -> BoardState {
let mut fireworks : HashMap<Color, Cards> = HashMap::new(); let mut fireworks : HashMap<Color, Firework> = HashMap::new();
for color in COLORS.iter() { for color in COLORS.iter() {
let mut firework = Cards::new(); fireworks.insert(color, Firework::new(color));
let card = Card { value: 0, color: color };
firework.place(card);
fireworks.insert(color, firework);
} }
BoardState { BoardState {
@ -252,8 +309,7 @@ impl BoardState {
// returns whether a card would place on a firework // returns whether a card would place on a firework
pub fn is_playable(&self, card: &Card) -> bool { pub fn is_playable(&self, card: &Card) -> bool {
let firework = self.fireworks.get(card.color).unwrap(); let firework = self.fireworks.get(card.color).unwrap();
let under_card = firework.top().unwrap(); Some(card.value) == firework.desired_value()
card.value == under_card.value + 1
} }
pub fn get_players(&self) -> Vec<Player> { pub fn get_players(&self) -> Vec<Player> {
@ -263,8 +319,7 @@ impl BoardState {
pub fn score(&self) -> Score { pub fn score(&self) -> Score {
let mut score = 0; let mut score = 0;
for (_, firework) in &self.fireworks { for (_, firework) in &self.fireworks {
// subtract one to account for the 0 we pushed score += firework.score();
score += firework.size() - 1;
} }
score as u32 score as u32
} }