simplify stuff

This commit is contained in:
Jeff Wu 2016-03-12 21:27:15 -08:00
parent b63c850484
commit a16f490c04
2 changed files with 29 additions and 57 deletions

View File

@ -1,6 +1,4 @@
use rand::{self, Rng}; use rand::{self, Rng};
use std::convert::From;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
@ -12,6 +10,9 @@ use info::*;
pub type Color = &'static str; pub type Color = &'static str;
pub const COLORS: [Color; 5] = ["blue", "red", "yellow", "white", "green"]; 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; pub type Value = u32;
// list of values, assumed to be small to large // list of values, assumed to be small to large
@ -34,10 +35,14 @@ pub struct Card {
pub color: Color, pub color: Color,
pub value: Value, pub value: Value,
} }
impl Card {
fn new(color: Color, value: Value) -> Card {
Card { color: color, value: value }
}
}
impl fmt::Display for Card { impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let colorchar = self.color.chars().next().unwrap(); write!(f, "{}{}", display_color(self.color), self.value)
write!(f, "{}{}", colorchar, self.value)
} }
} }
@ -57,7 +62,7 @@ impl Firework {
fn new(color: Color) -> Firework { fn new(color: Color) -> Firework {
let mut cards = Cards::new(); let mut cards = Cards::new();
// have a 0, so it's easier to implement // 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); cards.push(card);
Firework { Firework {
color: color, color: color,
@ -153,13 +158,11 @@ impl fmt::Display for Discard {
// "{}", self.cards, // "{}", self.cards,
// ))); // )));
for color in COLORS.iter() { for color in COLORS.iter() {
let colorchar = color.chars().next().unwrap();
try!(f.write_str(&format!( try!(f.write_str(&format!(
"{}: ", colorchar, "{}: ", display_color(color),
))); )));
let color_count = self.counts.get(color).unwrap();
for value in VALUES.iter() { 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); let total = get_count_for_value(value);
try!(f.write_str(&format!( try!(f.write_str(&format!(
"{}/{} {}s", count, total, value "{}/{} {}s", count, total, value
@ -248,7 +251,7 @@ impl PlayerState {
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
PlayerState { PlayerState {
hand: hand, hand: hand,
info: CardsInfo::from(infos), info: infos,
} }
} }
@ -297,7 +300,7 @@ fn new_deck() -> Cards {
for value in VALUES.iter() { for value in VALUES.iter() {
let count = get_count_for_value(value); let count = get_count_for_value(value);
for _ in 0..count { 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 break
} else { } else {
// need these cards // 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) { if self.discard.has_all(&needed_card) {
// already discarded all of these // already discarded all of these
playable = false; playable = false;
@ -505,12 +508,12 @@ impl GameState {
let mut player_states : HashMap<Player, PlayerState> = HashMap::new(); let mut player_states : HashMap<Player, PlayerState> = HashMap::new();
for i in 0..opts.num_players { 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 // we can assume the deck is big enough to draw initial hands
board.deck.pop().unwrap() board.deck.pop().unwrap()
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
player_states.insert( player_states.insert(
i, PlayerState::new(Cards::from(raw_hand)), i, PlayerState::new(hand),
); );
} }

View File

@ -1,11 +1,11 @@
use std::collections::HashMap;
use std::cmp::Eq; use std::cmp::Eq;
use std::hash::Hash; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::hash::Hash;
use game::*; use game::*;
// Represents a bit of information about T // Represents information about possible values of type T
pub trait Info<T> where T: Hash + Eq + Clone { pub trait Info<T> where T: Hash + Eq + Clone {
// get all a-priori possibilities // get all a-priori possibilities
fn get_all_possibilities() -> Vec<T>; fn get_all_possibilities() -> Vec<T>;
@ -40,12 +40,6 @@ pub trait Info<T> where T: Hash + Eq + Clone {
possible_map 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) { fn mark_true(&mut self, value: &T) {
// mark everything else as definitively impossible // mark everything else as definitively impossible
for (other_value, possible) in self.get_mut_possibility_map().iter_mut() { for (other_value, possible) in self.get_mut_possibility_map().iter_mut() {
@ -73,47 +67,23 @@ pub trait Info<T> where T: Hash + Eq + Clone {
#[derive(Debug)] #[derive(Debug)]
pub struct ColorInfo(HashMap<Color, bool>); pub struct ColorInfo(HashMap<Color, bool>);
impl ColorInfo { impl ColorInfo {
pub fn new() -> ColorInfo { pub fn new() -> ColorInfo { ColorInfo(ColorInfo::initialize()) }
ColorInfo(ColorInfo::initialize())
}
} }
impl Info<Color> for ColorInfo { impl Info<Color> for ColorInfo {
fn get_all_possibilities() -> Vec<Color> { fn get_all_possibilities() -> Vec<Color> { COLORS.to_vec() }
let mut possible : Vec<Color> = Vec::new(); fn get_possibility_map(&self) -> &HashMap<Color, bool> { &self.0 }
for color in COLORS.iter() { fn get_mut_possibility_map(&mut self) -> &mut HashMap<Color, bool> { &mut self.0 }
possible.push(*color);
}
possible
}
fn get_possibility_map(&self) -> &HashMap<Color, bool> {
&self.0
}
fn get_mut_possibility_map(&mut self) -> &mut HashMap<Color, bool> {
&mut self.0
}
} }
#[derive(Debug)] #[derive(Debug)]
pub struct ValueInfo(HashMap<Value, bool>); pub struct ValueInfo(HashMap<Value, bool>);
impl ValueInfo { impl ValueInfo {
pub fn new() -> ValueInfo { pub fn new() -> ValueInfo { ValueInfo(ValueInfo::initialize()) }
ValueInfo(ValueInfo::initialize())
}
} }
impl Info<Value> for ValueInfo { impl Info<Value> for ValueInfo {
fn get_all_possibilities() -> Vec<Value> { fn get_all_possibilities() -> Vec<Value> { VALUES.to_vec() }
let mut possible : Vec<Value> = Vec::new(); fn get_possibility_map(&self) -> &HashMap<Value, bool> { &self.0 }
for value in VALUES.iter() { fn get_mut_possibility_map(&mut self) -> &mut HashMap<Value, bool> { &mut self.0 }
possible.push(*value);
}
possible
}
fn get_possibility_map(&self) -> &HashMap<Value, bool> {
&self.0
}
fn get_mut_possibility_map(&mut self) -> &mut HashMap<Value, bool> {
&mut self.0
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -134,8 +104,7 @@ impl fmt::Display for CardInfo {
let mut string = String::new(); let mut string = String::new();
for color in &COLORS { for color in &COLORS {
if self.color_info.is_possible(color) { if self.color_info.is_possible(color) {
let colorchar = color.chars().next().unwrap(); string.push(display_color(color));
string.push(colorchar);
} }
} }
// while string.len() < COLORS.len() + 1 { // while string.len() < COLORS.len() + 1 {