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 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::<Vec<_>>();
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<Player, PlayerState> = 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::<Vec<_>>();
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::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<T> where T: Hash + Eq + Clone {
// get all a-priori possibilities
fn get_all_possibilities() -> Vec<T>;
@ -40,12 +40,6 @@ pub trait Info<T> 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<T> where T: Hash + Eq + Clone {
#[derive(Debug)]
pub struct ColorInfo(HashMap<Color, bool>);
impl ColorInfo {
pub fn new() -> ColorInfo {
ColorInfo(ColorInfo::initialize())
}
pub fn new() -> ColorInfo { ColorInfo(ColorInfo::initialize()) }
}
impl Info<Color> for ColorInfo {
fn get_all_possibilities() -> Vec<Color> {
let mut possible : Vec<Color> = Vec::new();
for color in COLORS.iter() {
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
}
fn get_all_possibilities() -> Vec<Color> { COLORS.to_vec() }
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)]
pub struct ValueInfo(HashMap<Value, bool>);
impl ValueInfo {
pub fn new() -> ValueInfo {
ValueInfo(ValueInfo::initialize())
}
pub fn new() -> ValueInfo { ValueInfo(ValueInfo::initialize()) }
}
impl Info<Value> for ValueInfo {
fn get_all_possibilities() -> Vec<Value> {
let mut possible : Vec<Value> = Vec::new();
for value in VALUES.iter() {
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
}
fn get_all_possibilities() -> Vec<Value> { VALUES.to_vec() }
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)]
@ -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 {