Move phases definition to mod

This commit is contained in:
Dane Johnson 2022-05-18 11:25:39 -05:00
parent 9f545949a5
commit a846919c88

View File

@ -236,8 +236,7 @@ impl Game {
player.lose(card, &mut self.discard); player.lose(card, &mut self.discard);
} }
pub fn block_challenge(&mut self, block_challenge: phase::BlockChallenge, agents: &[&dyn Agent]) -> CoupResult<Phase> {
pub fn block_challenge(&mut self, block_challenge: BlockChallenge, agents: &[&dyn Agent]) -> CoupResult<Phase> {
if agents[self.turn].should_block_challenge(&block_challenge) { if agents[self.turn].should_block_challenge(&block_challenge) {
if self.players[block_challenge.blocker].holds(block_challenge.block_card) { if self.players[block_challenge.blocker].holds(block_challenge.block_card) {
// Player challenged incorrectly, loses influence and turn is forfeit // Player challenged incorrectly, loses influence and turn is forfeit
@ -247,7 +246,7 @@ impl Game {
// Player challenged correctly, blocker loses a card // Player challenged correctly, blocker loses a card
self.player_lose_influence(block_challenge.blocker, agents); self.player_lose_influence(block_challenge.blocker, agents);
// Game continues // Game continues
Ok(Phase::Resolution(Resolution { Ok(Phase::Resolution(phase::Resolution {
action: block_challenge.action, action: block_challenge.action,
target: block_challenge.target, target: block_challenge.target,
})) }))
@ -259,7 +258,7 @@ impl Game {
} }
} }
pub fn resolution(&mut self, resolution: Resolution, agents: &[&dyn Agent]) -> CoupResult<Phase> { pub fn resolution(&mut self, resolution: phase::Resolution, agents: &[&dyn Agent]) -> CoupResult<Phase> {
let current_player = &mut self.players.get_mut(self.turn).unwrap(); let current_player = &mut self.players.get_mut(self.turn).unwrap();
let current_agent = agents[self.turn]; let current_agent = agents[self.turn];
match resolution.action { match resolution.action {
@ -305,38 +304,42 @@ impl Game {
Ok(Phase::Done) Ok(Phase::Done)
} }
} }
pub mod phase {
use super::{ Card, Action };
#[derive(PartialEq, Debug)]
pub enum Phase {
Action(Action),
//ActionChallenge(ActionChallenge),
Block(Block),
BlockChallenge(BlockChallenge),
Resolution(Resolution),
Done,
}
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub enum Phase { pub struct Block {
Action(Action),
//ActionChallenge(ActionChallenge), }
Block(Block),
BlockChallenge(BlockChallenge), #[derive(PartialEq, Debug)]
Resolution(Resolution), pub struct BlockChallenge {
Done, pub blocker: usize,
pub block_card: Card,
pub action: Action,
pub target: Option<usize>,
}
#[derive(PartialEq, Debug)]
pub struct Resolution {
pub action: Action,
pub target: Option<usize>,
}
} }
#[derive(PartialEq, Debug)] use phase::Phase;
pub struct Block {
}
#[derive(PartialEq, Debug)]
pub struct BlockChallenge {
blocker: usize,
block_card: Card,
action: Action,
target: Option<usize>,
}
#[derive(PartialEq, Debug)]
pub struct Resolution {
action: Action,
target: Option<usize>,
}
pub trait Agent : fmt::Debug { pub trait Agent : fmt::Debug {
fn should_block_challenge(&self, block_challenge: &BlockChallenge) -> bool; fn should_block_challenge(&self, block_challenge: &phase::BlockChallenge) -> bool;
fn exchange(&self, cards: &[Card]) -> [Card; 2]; fn exchange(&self, cards: &[Card]) -> [Card; 2];
fn choose_lost_influence(&self, cards: &[Card]) -> Card; fn choose_lost_influence(&self, cards: &[Card]) -> Card;
} }
@ -351,7 +354,7 @@ mod test {
struct DummyAgent; struct DummyAgent;
impl Agent for DummyAgent { impl Agent for DummyAgent {
#[allow(unused)] #[allow(unused)]
fn should_block_challenge(&self, block_challenge: &BlockChallenge) -> bool { fn should_block_challenge(&self, block_challenge: &phase::BlockChallenge) -> bool {
$sbc $sbc
} }
#[allow(unused)] #[allow(unused)]
@ -389,7 +392,7 @@ mod test {
{ {
let mut game = game.clone(); let mut game = game.clone();
assert_eq!( assert_eq!(
game.block_challenge( BlockChallenge { game.block_challenge( phase::BlockChallenge {
blocker: 1, blocker: 1,
block_card: Contessa, block_card: Contessa,
action: Assassinate, action: Assassinate,
@ -404,7 +407,7 @@ mod test {
{ {
let mut game = game.clone(); let mut game = game.clone();
assert_eq!( assert_eq!(
game.block_challenge( BlockChallenge { game.block_challenge( phase::BlockChallenge {
blocker: 1, blocker: 1,
block_card: Contessa, block_card: Contessa,
action: Assassinate, action: Assassinate,
@ -419,13 +422,13 @@ mod test {
{ {
let mut game = game.clone(); let mut game = game.clone();
assert_eq!( assert_eq!(
game.block_challenge( BlockChallenge { game.block_challenge( phase::BlockChallenge {
blocker: 1, blocker: 1,
block_card: Duke, block_card: Duke,
action: ForeignAid, action: ForeignAid,
target: None, target: None,
}, &[&challenge_agent, &block_agent]), }, &[&challenge_agent, &block_agent]),
Ok(Phase::Resolution(Resolution { Ok(Phase::Resolution(phase::Resolution {
action: ForeignAid, action: ForeignAid,
target: None, target: None,
})) }))
@ -454,7 +457,7 @@ mod test {
// Test income // Test income
{ {
let mut game = game.clone(); let mut game = game.clone();
game.resolution(Resolution { game.resolution(phase::Resolution {
action: Income, action: Income,
target: None, target: None,
}, &[&dummy_agent, &dummy_agent]).unwrap(); }, &[&dummy_agent, &dummy_agent]).unwrap();
@ -464,7 +467,7 @@ mod test {
// Test foreign aid // Test foreign aid
{ {
let mut game = game.clone(); let mut game = game.clone();
game.resolution(Resolution { game.resolution(phase::Resolution {
action: ForeignAid, action: ForeignAid,
target: None, target: None,
}, &[&dummy_agent, &dummy_agent]).unwrap(); }, &[&dummy_agent, &dummy_agent]).unwrap();
@ -474,7 +477,7 @@ mod test {
// Test coup / assassinate // Test coup / assassinate
{ {
let mut game = game.clone(); let mut game = game.clone();
game.resolution(Resolution { game.resolution(phase::Resolution {
action: Coup, action: Coup,
target: Some(1), target: Some(1),
}, &[&dummy_agent, &dummy_agent]).unwrap(); }, &[&dummy_agent, &dummy_agent]).unwrap();
@ -485,7 +488,7 @@ mod test {
// Test steal // Test steal
{ {
let mut game = game.clone(); let mut game = game.clone();
game.resolution(Resolution { game.resolution(phase::Resolution {
action: Steal, action: Steal,
target: Some(1), target: Some(1),
}, &[&dummy_agent, &dummy_agent]).unwrap(); }, &[&dummy_agent, &dummy_agent]).unwrap();
@ -496,7 +499,7 @@ mod test {
// Test exchange // Test exchange
{ {
let mut game = game.clone(); let mut game = game.clone();
game.resolution(Resolution { game.resolution(phase::Resolution {
action: Exchange, action: Exchange,
target: Some(1), target: Some(1),
}, &[&dummy_agent, &dummy_agent]).unwrap(); }, &[&dummy_agent, &dummy_agent]).unwrap();