From a16f490c044af8ffaec4290b70c94df1cf32ae61 Mon Sep 17 00:00:00 2001 From: Jeff Wu Date: Sat, 12 Mar 2016 21:27:15 -0800 Subject: [PATCH] simplify stuff --- src/game.rs | 31 ++++++++++++++++-------------- src/info.rs | 55 ++++++++++++----------------------------------------- 2 files changed, 29 insertions(+), 57 deletions(-) diff --git a/src/game.rs b/src/game.rs index 5ad2d01..0e1d22b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,4 @@ - use rand::{self, Rng}; -use std::convert::From; use std::collections::HashMap; use std::fmt; @@ -12,6 +10,9 @@ use info::*; pub type Color = &'static str; pub const COLORS: [Color; 5] = ["blue", "red", "yellow", "white", "green"]; +pub fn display_color(color: Color) -> char { + color.chars().next().unwrap() +} pub type Value = u32; // list of values, assumed to be small to large @@ -34,10 +35,14 @@ pub struct Card { pub color: Color, pub value: Value, } +impl Card { + fn new(color: Color, value: Value) -> Card { + Card { color: color, value: value } + } +} impl fmt::Display for Card { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let colorchar = self.color.chars().next().unwrap(); - write!(f, "{}{}", colorchar, self.value) + write!(f, "{}{}", display_color(self.color), self.value) } } @@ -57,7 +62,7 @@ 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 }; + let card = Card::new(color, 0); cards.push(card); Firework { color: color, @@ -153,13 +158,11 @@ impl fmt::Display for Discard { // "{}", self.cards, // ))); for color in COLORS.iter() { - let colorchar = color.chars().next().unwrap(); try!(f.write_str(&format!( - "{}: ", colorchar, + "{}: ", display_color(color), ))); - let color_count = self.counts.get(color).unwrap(); for value in VALUES.iter() { - let count = color_count.get(value).unwrap(); + let count = self.get_count(&Card::new(color, *value)); let total = get_count_for_value(value); try!(f.write_str(&format!( "{}/{} {}s", count, total, value @@ -248,7 +251,7 @@ impl PlayerState { }).collect::>(); PlayerState { hand: hand, - info: CardsInfo::from(infos), + info: infos, } } @@ -297,7 +300,7 @@ fn new_deck() -> Cards { for value in VALUES.iter() { let count = get_count_for_value(value); for _ in 0..count { - deck.push(Card {color: color, value: value.clone()}); + deck.push(Card::new(color, value.clone())); } } }; @@ -394,7 +397,7 @@ impl BoardState { break } else { // need these cards - let needed_card = Card {color: card.color, value: value.clone()}; + let needed_card = Card::new(card.color, value.clone()); if self.discard.has_all(&needed_card) { // already discarded all of these playable = false; @@ -505,12 +508,12 @@ impl GameState { let mut player_states : HashMap = HashMap::new(); for i in 0..opts.num_players { - let raw_hand = (0..opts.hand_size).map(|_| { + let hand = (0..opts.hand_size).map(|_| { // we can assume the deck is big enough to draw initial hands board.deck.pop().unwrap() }).collect::>(); player_states.insert( - i, PlayerState::new(Cards::from(raw_hand)), + i, PlayerState::new(hand), ); } diff --git a/src/info.rs b/src/info.rs index 0300e19..ee9e4c8 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,11 +1,11 @@ -use std::collections::HashMap; use std::cmp::Eq; -use std::hash::Hash; +use std::collections::HashMap; use std::fmt; +use std::hash::Hash; use game::*; -// Represents a bit of information about T +// Represents information about possible values of type T pub trait Info where T: Hash + Eq + Clone { // get all a-priori possibilities fn get_all_possibilities() -> Vec; @@ -40,12 +40,6 @@ pub trait Info where T: Hash + Eq + Clone { possible_map } - fn merge(&mut self, other: &Self) { - for (value, possible) in self.get_mut_possibility_map().iter_mut() { - *possible = *possible && *other.get_possibility_map().get(value).unwrap(); - } - } - fn mark_true(&mut self, value: &T) { // mark everything else as definitively impossible for (other_value, possible) in self.get_mut_possibility_map().iter_mut() { @@ -73,47 +67,23 @@ pub trait Info where T: Hash + Eq + Clone { #[derive(Debug)] pub struct ColorInfo(HashMap); impl ColorInfo { - pub fn new() -> ColorInfo { - ColorInfo(ColorInfo::initialize()) - } + pub fn new() -> ColorInfo { ColorInfo(ColorInfo::initialize()) } } impl Info for ColorInfo { - fn get_all_possibilities() -> Vec { - let mut possible : Vec = Vec::new(); - for color in COLORS.iter() { - possible.push(*color); - } - possible - } - fn get_possibility_map(&self) -> &HashMap { - &self.0 - } - fn get_mut_possibility_map(&mut self) -> &mut HashMap { - &mut self.0 - } + fn get_all_possibilities() -> Vec { COLORS.to_vec() } + fn get_possibility_map(&self) -> &HashMap { &self.0 } + fn get_mut_possibility_map(&mut self) -> &mut HashMap { &mut self.0 } } #[derive(Debug)] pub struct ValueInfo(HashMap); impl ValueInfo { - pub fn new() -> ValueInfo { - ValueInfo(ValueInfo::initialize()) - } + pub fn new() -> ValueInfo { ValueInfo(ValueInfo::initialize()) } } impl Info for ValueInfo { - fn get_all_possibilities() -> Vec { - let mut possible : Vec = Vec::new(); - for value in VALUES.iter() { - possible.push(*value); - } - possible - } - fn get_possibility_map(&self) -> &HashMap { - &self.0 - } - fn get_mut_possibility_map(&mut self) -> &mut HashMap { - &mut self.0 - } + fn get_all_possibilities() -> Vec { VALUES.to_vec() } + fn get_possibility_map(&self) -> &HashMap { &self.0 } + fn get_mut_possibility_map(&mut self) -> &mut HashMap { &mut self.0 } } #[derive(Debug)] @@ -134,8 +104,7 @@ impl fmt::Display for CardInfo { let mut string = String::new(); for color in &COLORS { if self.color_info.is_possible(color) { - let colorchar = color.chars().next().unwrap(); - string.push(colorchar); + string.push(display_color(color)); } } // while string.len() < COLORS.len() + 1 {