hanabi.rs/src/game.rs

175 lines
4.3 KiB
Rust
Raw Normal View History

2016-03-06 03:49:14 +01:00
use rand::{self, Rng};
2016-03-06 01:54:46 +01:00
use std::collections::HashSet;
use std::collections::HashMap;
use std::fmt;
2016-03-06 03:49:14 +01:00
/*
* Type definitions
*/
2016-03-06 01:54:46 +01:00
pub type Color = &'static str;
2016-03-06 03:49:14 +01:00
const COLORS: [Color; 5] = ["blue", "red", "yellow", "white", "green"];
pub type Value = u32;
// list of (value, count) pairs
const VALUE_COUNTS : [(Value, u32); 5] = [(1, 3), (2, 2), (3, 2), (4, 2), (5, 1)];
2016-03-06 01:54:46 +01:00
pub struct Card {
pub color: Color,
pub value: Value,
}
impl fmt::Debug for Card {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.color, self.value)
}
}
2016-03-06 03:49:14 +01:00
#[derive(Debug)]
pub struct Pile(Vec<Card>);
// basically a stack of cards
impl Pile {
pub fn new() -> Pile {
Pile(Vec::new())
}
pub fn draw(&mut self) -> Option<Card> {
self.0.pop()
}
pub fn place(&mut self, card: Card) {
self.0.push(card);
}
pub fn take(&mut self, index: usize) -> Card {
self.0.remove(index)
}
pub fn shuffle(&mut self) {
rand::thread_rng().shuffle(&mut self.0[..]);
}
}
2016-03-06 01:54:46 +01:00
pub type Hand = Vec<Card>;
2016-03-06 03:49:14 +01:00
pub type Player = u32;
2016-03-06 01:54:46 +01:00
pub struct GameOptions {
2016-03-06 03:49:14 +01:00
pub num_players: u32,
pub hand_size: u32,
// when hits 0, you cannot hint
pub total_hints: u32,
// when hits 0, you lose
pub total_lives: u32,
2016-03-06 01:54:46 +01:00
}
// The state of a given player: all other players may see this
2016-03-06 03:49:14 +01:00
pub struct PlayerState {
2016-03-06 01:54:46 +01:00
hand: Hand,
}
2016-03-06 03:49:14 +01:00
// State of everything except the player's hands
// Is completely common knowledge
pub struct BoardState {
2016-03-06 01:54:46 +01:00
pub deck: Pile,
2016-03-06 03:49:14 +01:00
pub discard: Pile,
pub fireworks: HashMap<Color, Pile>,
2016-03-06 01:54:46 +01:00
// // whose turn is it?
2016-03-06 03:49:14 +01:00
pub next: Player,
pub hints_remaining: u32,
pub lives_remaining: u32,
// only relevant when deck runs out
turns_remaining: u32,
}
// complete game state (known to nobody!)
pub struct GameState {
pub player_states: HashMap<Player, PlayerState>,
pub board_state: BoardState,
}
// complete game view of a given player
pub struct GameStateView {
// not yet implemented
pub other_player_states: HashMap<Player, PlayerState>,
pub board_state: BoardState,
2016-03-06 01:54:46 +01:00
}
impl GameState {
pub fn new(opts: GameOptions) -> GameState {
2016-03-06 03:49:14 +01:00
let mut deck = GameState::make_deck();
let mut player_states : HashMap<Player, PlayerState> = HashMap::new();
for i in 0..opts.num_players {
let hand : Hand = (0..opts.hand_size)
.map(|i| {
// we can assume the deck is big enough to draw initial hands
deck.draw().unwrap()
})
.collect::<Vec<_>>();
let state = PlayerState {
hand: hand,
};
player_states.insert(i, state);
}
let mut fireworks : HashMap<Color, Pile> = HashMap::new();
for color in COLORS.iter() {
let mut pile = Pile::new();
let card = Card { value: 0, color: color };
pile.place(card);
fireworks.insert(color, pile);
}
2016-03-06 01:54:46 +01:00
GameState {
2016-03-06 03:49:14 +01:00
player_states: player_states,
board_state: BoardState {
deck: deck,
fireworks: fireworks,
discard: Pile::new(),
next: 0,
hints_remaining: opts.total_hints,
lives_remaining: opts.total_lives,
// only relevant when deck runs out
turns_remaining: opts.num_players,
}
2016-03-06 01:54:46 +01:00
}
}
fn make_deck() -> Pile {
2016-03-06 03:49:14 +01:00
let mut deck: Pile = Pile(Vec::new());
2016-03-06 01:54:46 +01:00
for color in COLORS.iter() {
2016-03-06 03:49:14 +01:00
for &(value, count) in VALUE_COUNTS.iter() {
for _ in 0..3 {
deck.place(Card {color: color, value: 1});
2016-03-06 01:54:46 +01:00
}
}
};
2016-03-06 03:49:14 +01:00
deck.shuffle();
2016-03-06 01:54:46 +01:00
println!("Created deck: {:?}", deck);
deck
}
}
2016-03-06 03:49:14 +01:00
enum Hint {
Color,
Value,
}
enum Turn {
Hint,
Discard,
Play,
2016-03-06 01:54:46 +01:00
}
2016-03-06 03:49:14 +01:00
// Trait to implement for any valid Hanabi strategy
pub trait Strategy {
fn decide(&mut self, &GameStateView) -> Turn;
fn update(&mut self, Turn);
2016-03-06 01:54:46 +01:00
}
2016-03-06 03:49:14 +01:00
pub fn simulate_symmetric(opts: GameOptions, strategy: &Strategy) {
let strategies = (0..opts.num_players).map(|_| { Box::new(strategy) }).collect();
simulate(opts, strategies)
2016-03-06 01:54:46 +01:00
}
2016-03-06 03:49:14 +01:00
pub fn simulate(opts: GameOptions, strategies: Vec<Box<&Strategy>>) {
2016-03-06 01:54:46 +01:00
}