diff --git a/src/cards.rs b/src/cards.rs index f8cc99a..22afcfe 100644 --- a/src/cards.rs +++ b/src/cards.rs @@ -1,3 +1,4 @@ +pub use super::Game; use serde::{Serialize, Serializer}; use std::fmt; @@ -57,9 +58,33 @@ pub fn curse() -> Card { } } +macro_rules! draw { + ($g:ident, $e:expr) => { + $g.players[$g.active_player].draw($e) + }; +} + +macro_rules! action { + ($g:ident, $e:expr) => { + $g.turn_state.actions += $e + }; +} + +macro_rules! buy { + ($g:ident, $e:expr) => { + $g.turn_state.buys += $e + }; +} + +macro_rules! coin { + ($g:ident, $e:expr) => { + $g.turn_state.coin += $e + }; +} + #[derive(Clone)] pub enum CardType { - Action(fn()), + Action(fn(&mut Game)), Curse, Treasure(u32), Victory(u32), @@ -73,10 +98,10 @@ pub struct Card { } impl Card { - pub fn action(&self) -> Option<()> { + pub fn action(&self) -> Option { for t in &self.types { match t { - CardType::Action(_) => return Some(()), + CardType::Action(effects) => return Some(*effects), _ => (), } } diff --git a/src/main.rs b/src/main.rs index a6c0a68..6360892 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#[macro_use] mod cards; use async_std::{prelude::*, sync::RwLock}; @@ -164,7 +165,7 @@ impl Default for GameSetup { } } -struct Game { +pub struct Game { players: Vec, state: GameState, setup: GameSetup, @@ -216,7 +217,9 @@ impl Game { Card { name: "Cellar".into(), cost: 2, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| { + action!(game, 1); + })], }, 10, ), @@ -224,7 +227,7 @@ impl Game { Card { name: "Moat".into(), cost: 2, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| draw!(game, 2))], }, 10, ), @@ -232,7 +235,10 @@ impl Game { Card { name: "Village".into(), cost: 3, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| { + draw!(game, 1); + action!(game, 2); + })], }, 10, ), @@ -240,7 +246,10 @@ impl Game { Card { name: "Merchant".into(), cost: 3, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| { + draw!(game, 1); + action!(game, 1); + })], }, 10, ), @@ -248,7 +257,7 @@ impl Game { Card { name: "Workshop".into(), cost: 3, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| {})], }, 10, ), @@ -256,7 +265,7 @@ impl Game { Card { name: "Smithy".into(), cost: 4, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| draw!(game, 3))], }, 10, ), @@ -264,7 +273,7 @@ impl Game { Card { name: "Remodel".into(), cost: 4, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| {})], }, 10, ), @@ -272,7 +281,9 @@ impl Game { Card { name: "Militia".into(), cost: 4, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| { + coin!(game, 2); + })], }, 10, ), @@ -280,7 +291,12 @@ impl Game { Card { name: "Market".into(), cost: 5, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| { + draw!(game, 1); + action!(game, 1); + buy!(game, 1); + coin!(game, 1); + })], }, 10, ), @@ -288,7 +304,7 @@ impl Game { Card { name: "Mine".into(), cost: 5, - types: vec![CardType::Action(|| {})], + types: vec![CardType::Action(|game| {})], }, 10, ), @@ -343,24 +359,29 @@ impl Game { } } + fn get_active_player(&mut self) -> &Player { + return &self.players[self.active_player]; + } + pub fn trash_hand(&mut self, player_number: usize, index: usize) { self.trash .push(self.players[player_number].hand.remove(index)); } pub fn play_card(&mut self, player_number: usize, index: usize) { - let player = self.players.get_mut(player_number).unwrap(); - let card = player.hand.remove(index); + let card = self.players[player_number].hand.remove(index); if let Some(coin) = card.treasure() { self.turn_state.coin += coin; } - if let Some(_) = card.action() { + if let Some(effect) = card.action() { self.turn_state.actions -= 1; + + effect(self); } - player.played_cards.push(card); + self.players[player_number].played_cards.push(card); } pub fn buy_card(&mut self, player_number: usize, index: usize) -> bool /*-> Result<(), &'static str>*/