hanabi.rs/src/helpers.rs

409 lines
12 KiB
Rust
Raw Normal View History

2016-03-06 11:13:08 +01:00
use std::cmp::Eq;
use std::collections::{HashMap, HashSet};
2016-03-09 19:27:40 +01:00
use std::fmt;
use std::ops::{Index,IndexMut};
2016-03-13 06:27:15 +01:00
use std::hash::Hash;
2016-03-27 19:47:58 +02:00
use std::convert::From;
use std::slice;
2016-03-06 11:13:08 +01:00
use game::*;
// trait representing information about a card
pub trait CardInfo {
fn new() -> Self;
// whether the card is possible
fn is_possible(&self, card: &Card) -> bool;
// mark all current possibilities for the card
2016-03-31 19:17:22 +02:00
// this should generally be overridden, for efficiency
fn get_possibilities(&self) -> Vec<Card> {
let mut v = Vec::new();
for &color in COLORS.iter() {
for &value in VALUES.iter() {
let card = Card::new(color, value);
if self.is_possible(&card) {
v.push(card);
}
}
}
v
}
2016-03-30 09:00:01 +02:00
// get probability weight for the card
#[allow(unused_variables)]
2016-03-27 19:47:58 +02:00
fn get_weight(&self, card: &Card) -> f32 {
1 as f32
}
2016-03-30 09:00:01 +02:00
2016-03-27 19:47:58 +02:00
fn get_weighted_possibilities(&self) -> Vec<(Card, f32)> {
self.get_possibilities().into_iter()
.map(|card| {
let weight = self.get_weight(&card);
(card, weight)
}).collect::<Vec<_>>()
}
fn total_weight(&self) -> f32 {
self.get_possibilities().iter()
.map(|card| self.get_weight(&card))
.fold(0.0, |a, b| a+b)
}
2016-03-30 09:00:01 +02:00
2016-03-27 19:47:58 +02:00
fn weighted_score<T>(&self, score_fn: &Fn(&Card) -> T) -> f32
where f32: From<T>
{
let mut total_score = 0.;
let mut total_weight = 0.;
for card in self.get_possibilities() {
let weight = self.get_weight(&card);
let score = f32::from(score_fn(&card));
total_weight += weight;
total_score += weight * score;
}
total_score / total_weight
}
2016-03-30 09:00:01 +02:00
2016-03-30 07:24:29 +02:00
fn average_value(&self) -> f32 {
self.weighted_score(&|card| card.value as f32 )
}
2016-03-30 09:00:01 +02:00
2016-03-27 19:47:58 +02:00
fn probability_of_predicate(&self, predicate: &Fn(&Card) -> bool) -> f32 {
let f = |card: &Card| {
if predicate(card) { 1.0 } else { 0.0 }
};
self.weighted_score(&f)
}
2016-03-30 09:00:01 +02:00
fn probability_is_playable(&self, board: &BoardState) -> f32 {
self.probability_of_predicate(&|card| board.is_playable(card))
}
fn probability_is_dead(&self, board: &BoardState) -> f32 {
self.probability_of_predicate(&|card| board.is_dead(card))
}
fn probability_is_dispensable(&self, board: &BoardState) -> f32 {
self.probability_of_predicate(&|card| board.is_dispensable(card))
}
// mark a whole color as false
2016-03-31 19:17:22 +02:00
fn mark_color_false(&mut self, color: Color);
// mark a color as correct
2016-03-31 19:17:22 +02:00
fn mark_color_true(&mut self, color: Color) {
for &other_color in COLORS.iter() {
if other_color != color {
self.mark_color_false(other_color);
}
}
}
2016-03-31 19:17:22 +02:00
fn mark_color(&mut self, color: Color, is_color: bool) {
if is_color {
self.mark_color_true(color);
} else {
self.mark_color_false(color);
}
}
// mark a whole value as false
2016-03-31 19:17:22 +02:00
fn mark_value_false(&mut self, value: Value);
// mark a value as correct
2016-03-31 19:17:22 +02:00
fn mark_value_true(&mut self, value: Value) {
for &other_value in VALUES.iter() {
if other_value != value {
self.mark_value_false(other_value);
}
}
}
2016-03-31 19:17:22 +02:00
fn mark_value(&mut self, value: Value, is_value: bool) {
if is_value {
self.mark_value_true(value);
} else {
self.mark_value_false(value);
}
}
}
2016-03-06 11:13:08 +01:00
2016-03-19 07:34:07 +01:00
// Represents hinted information about possible values of type T
2016-03-31 19:17:22 +02:00
pub trait Info<T> where T: Hash + Eq + Clone + Copy {
2016-03-06 11:13:08 +01:00
// get all a-priori possibilities
2016-03-09 19:27:40 +01:00
fn get_all_possibilities() -> Vec<T>;
2016-03-06 11:13:08 +01:00
// get map from values to whether it's possible
// true means maybe, false means no
fn get_possibility_set(&self) -> &HashSet<T>;
fn get_mut_possibility_set(&mut self) -> &mut HashSet<T>;
2016-03-06 11:13:08 +01:00
2016-03-09 19:27:40 +01:00
// get what is now possible
fn get_possibilities(&self) -> Vec<T> {
self.get_possibility_set().iter().map(|t| t.clone()).collect::<Vec<T>>()
2016-03-09 19:27:40 +01:00
}
2016-03-31 19:17:22 +02:00
fn is_possible(&self, value: T) -> bool {
self.get_possibility_set().contains(&value)
2016-03-09 19:27:40 +01:00
}
fn initialize() -> HashSet<T> {
Self::get_all_possibilities().iter()
.map(|val| val.clone()).collect::<HashSet<_>>()
2016-03-06 11:13:08 +01:00
}
2016-03-31 19:17:22 +02:00
fn mark_true(&mut self, value: T) {
let possible = self.get_mut_possibility_set();
possible.clear();
possible.insert(value.clone());
2016-03-06 11:13:08 +01:00
}
2016-03-31 19:17:22 +02:00
fn mark_false(&mut self, value: T) {
self.get_mut_possibility_set().remove(&value);
2016-03-06 11:13:08 +01:00
}
2016-03-07 01:14:47 +01:00
2016-03-31 19:17:22 +02:00
fn mark(&mut self, value: T, info: bool) {
if info { self.mark_true(value); } else { self.mark_false(value); }
2016-03-07 01:14:47 +01:00
}
2016-03-06 11:13:08 +01:00
}
2016-03-29 07:16:58 +02:00
#[derive(Debug,Clone)]
pub struct ColorInfo(HashSet<Color>);
2016-03-06 11:13:08 +01:00
impl ColorInfo {
2016-03-13 06:27:15 +01:00
pub fn new() -> ColorInfo { ColorInfo(ColorInfo::initialize()) }
2016-03-06 11:13:08 +01:00
}
impl Info<Color> for ColorInfo {
2016-03-13 06:27:15 +01:00
fn get_all_possibilities() -> Vec<Color> { COLORS.to_vec() }
fn get_possibility_set(&self) -> &HashSet<Color> { &self.0 }
fn get_mut_possibility_set(&mut self) -> &mut HashSet<Color> { &mut self.0 }
2016-03-06 11:13:08 +01:00
}
2016-03-29 07:16:58 +02:00
#[derive(Debug,Clone)]
pub struct ValueInfo(HashSet<Value>);
2016-03-06 11:13:08 +01:00
impl ValueInfo {
2016-03-13 06:27:15 +01:00
pub fn new() -> ValueInfo { ValueInfo(ValueInfo::initialize()) }
2016-03-06 11:13:08 +01:00
}
impl Info<Value> for ValueInfo {
2016-03-13 06:27:15 +01:00
fn get_all_possibilities() -> Vec<Value> { VALUES.to_vec() }
fn get_possibility_set(&self) -> &HashSet<Value> { &self.0 }
fn get_mut_possibility_set(&mut self) -> &mut HashSet<Value> { &mut self.0 }
2016-03-06 11:13:08 +01:00
}
// represents information only of the form:
// this color is/isn't possible, this value is/isn't possible
2016-03-29 07:16:58 +02:00
#[derive(Debug,Clone)]
pub struct SimpleCardInfo {
2016-03-06 11:13:08 +01:00
pub color_info: ColorInfo,
pub value_info: ValueInfo,
}
impl CardInfo for SimpleCardInfo {
fn new() -> SimpleCardInfo {
SimpleCardInfo {
2016-03-06 11:13:08 +01:00
color_info: ColorInfo::new(),
value_info: ValueInfo::new(),
}
}
fn get_possibilities(&self) -> Vec<Card> {
let mut v = Vec::new();
for &color in self.color_info.get_possibilities().iter() {
for &value in self.value_info.get_possibilities().iter() {
v.push(Card::new(color, value));
}
}
v
}
fn is_possible(&self, card: &Card) -> bool {
2016-03-31 19:17:22 +02:00
self.color_info.is_possible(card.color) &&
self.value_info.is_possible(card.value)
}
2016-03-31 19:17:22 +02:00
fn mark_color_false(&mut self, color: Color) {
self.color_info.mark_false(color);
}
2016-03-31 19:17:22 +02:00
fn mark_value_false(&mut self, value: Value) {
self.value_info.mark_false(value);
}
}
impl fmt::Display for SimpleCardInfo {
2016-03-09 19:27:40 +01:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut string = String::new();
2016-03-31 19:17:22 +02:00
for &color in &COLORS {
2016-03-11 07:49:20 +01:00
if self.color_info.is_possible(color) {
2016-03-31 19:17:22 +02:00
string.push(color);
2016-03-11 07:49:20 +01:00
}
2016-03-09 19:27:40 +01:00
}
// while string.len() < COLORS.len() + 1 {
string.push(' ');
//}
2016-03-31 19:17:22 +02:00
for &value in &VALUES {
2016-03-11 07:49:20 +01:00
if self.value_info.is_possible(value) {
string.push_str(&format!("{}", value));
}
2016-03-09 19:27:40 +01:00
}
f.pad(&string)
}
}
// Can represent information of the form:
// this card is/isn't possible
2016-03-27 19:47:58 +02:00
// also, maintains integer weights for the cards
2016-03-30 09:00:01 +02:00
#[derive(Clone,Debug)]
pub struct CardPossibilityTable {
possible: HashMap<Card, u32>,
}
impl CardPossibilityTable {
2016-03-27 19:47:58 +02:00
// mark a possible card as false
pub fn mark_false(&mut self, card: &Card) {
self.possible.remove(card);
}
// a bit more efficient
// pub fn borrow_possibilities<'a>(&'a self) -> Vec<&'a Card> {
// self.possible.keys().collect::<Vec<_>>()
// }
2016-03-27 19:47:58 +02:00
pub fn decrement_weight_if_possible(&mut self, card: &Card) {
if self.is_possible(card) {
self.decrement_weight(card);
}
}
pub fn decrement_weight(&mut self, card: &Card) {
2016-03-29 07:16:58 +02:00
let remove = {
let weight =
self.possible.get_mut(card)
.expect(&format!("Decrementing weight for impossible card: {}", card));
*weight -= 1;
*weight == 0
};
if remove {
self.possible.remove(card);
}
}
pub fn get_card(&self) -> Option<Card> {
let possibilities = self.get_possibilities();
if possibilities.len() == 1 {
Some(possibilities[0].clone())
} else {
None
}
2016-03-27 19:47:58 +02:00
}
pub fn is_determined(&self) -> bool {
self.get_possibilities().len() == 1
}
pub fn color_determined(&self) -> bool {
self.get_possibilities()
.iter().map(|card| card.color)
.collect::<HashSet<_>>()
.len() == 1
}
pub fn value_determined(&self) -> bool {
self.get_possibilities()
.iter().map(|card| card.value)
.collect::<HashSet<_>>()
.len() == 1
}
2016-03-27 19:47:58 +02:00
}
impl <'a> From<&'a CardCounts> for CardPossibilityTable {
fn from(counts: &'a CardCounts) -> CardPossibilityTable {
let mut possible = HashMap::new();
for &color in COLORS.iter() {
for &value in VALUES.iter() {
2016-03-27 19:47:58 +02:00
let card = Card::new(color, value);
let count = counts.remaining(&card);
if count > 0 {
possible.insert(card, count);
}
}
}
CardPossibilityTable {
possible: possible,
}
}
}
impl CardInfo for CardPossibilityTable {
fn new() -> CardPossibilityTable {
Self::from(&CardCounts::new())
}
fn is_possible(&self, card: &Card) -> bool {
self.possible.contains_key(card)
}
fn get_possibilities(&self) -> Vec<Card> {
let mut cards = self.possible.keys().map(|card| {card.clone() }).collect::<Vec<_>>();
cards.sort();
cards
}
2016-03-31 19:17:22 +02:00
fn mark_color_false(&mut self, color: Color) {
for &value in VALUES.iter() {
self.mark_false(&Card::new(color, value));
}
}
2016-03-31 19:17:22 +02:00
fn mark_value_false(&mut self, value: Value) {
for &color in COLORS.iter() {
2016-03-31 19:17:22 +02:00
self.mark_false(&Card::new(color, value));
}
}
2016-03-27 19:47:58 +02:00
fn get_weight(&self, card: &Card) -> f32 {
*self.possible.get(card).unwrap_or(&0) as f32
}
}
impl fmt::Display for CardPossibilityTable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2016-03-27 19:47:58 +02:00
for (card, weight) in &self.possible {
try!(f.write_str(&format!("{} {}, ", weight, card)));
}
Ok(())
}
}
#[derive(Clone)]
pub struct HandInfo<T> where T: CardInfo {
pub hand_info: Vec<T>
}
impl <T> HandInfo<T> where T: CardInfo {
pub fn new(hand_size: u32) -> Self {
let hand_info = (0..hand_size).map(|_| T::new()).collect::<Vec<_>>();
HandInfo {
hand_info: hand_info,
}
}
// update for hint to me
pub fn update_for_hint(&mut self, hinted: &Hinted, matches: &Vec<bool>) {
match hinted {
&Hinted::Color(color) => {
for (card_info, &matched) in self.hand_info.iter_mut().zip(matches.iter()) {
card_info.mark_color(color, matched);
}
}
&Hinted::Value(value) => {
for (card_info, &matched) in self.hand_info.iter_mut().zip(matches.iter()) {
card_info.mark_value(value, matched);
}
}
}
}
pub fn remove(&mut self, index: usize) -> T { self.hand_info.remove(index) }
pub fn push(&mut self, card_info: T) { self.hand_info.push(card_info) }
pub fn iter_mut(&mut self) -> slice::IterMut<T> { self.hand_info.iter_mut() }
pub fn iter(&self) -> slice::Iter<T> { self.hand_info.iter() }
pub fn len(&self) -> usize { self.hand_info.len() }
}
impl <T> Index<usize> for HandInfo<T> where T: CardInfo {
type Output = T;
fn index(&self, index: usize) -> &T {
&self.hand_info[index]
}
}
impl <T> IndexMut<usize> for HandInfo<T> where T: CardInfo {
fn index_mut(&mut self, index: usize) -> &mut T {
&mut self.hand_info[index]
}
}