Lose influence helper function, encoding and line endings

This commit is contained in:
Dane Johnson 2022-05-18 11:14:27 -05:00
parent 4101157e5f
commit d0f9054a1a

View File

@ -229,22 +229,23 @@ impl Game {
} }
} }
fn player_lose_influence(&mut self, id: usize, agents: &[&dyn Agent]) {
let player = &mut self.players[id];
let card = agents[id]
.choose_lost_influence(&player.cards);
player.lose(card, &mut self.discard);
}
pub fn block_challenge(&mut self, block_challenge: 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
let current_player = &mut self.players[self.turn]; self.player_lose_influence(self.turn, agents);
let card = agents[self.turn]
.choose_lost_influence(&current_player.cards);
current_player.lose(card, &mut self.discard);
Ok(Phase::Done) Ok(Phase::Done)
} else { } else {
// Player challenged correctly, blocker loses a card // Player challenged correctly, blocker loses a card
let blocking_player = &mut self.players[block_challenge.blocker]; self.player_lose_influence(block_challenge.blocker, agents);
let card = agents[block_challenge.blocker]
.choose_lost_influence(&blocking_player.cards);
blocking_player.lose(card, &mut self.discard);
// Game continues // Game continues
Ok(Phase::Resolution(Resolution { Ok(Phase::Resolution(Resolution {
action: block_challenge.action, action: block_challenge.action,
@ -266,10 +267,10 @@ impl Game {
ForeignAid => current_player.coins += 2, ForeignAid => current_player.coins += 2,
Coup | Assassinate => match resolution.target { Coup | Assassinate => match resolution.target {
Some(target) => { Some(target) => {
let target_player = &self.players[target]; // Target may have died from challenge
if target_player.is_alive() { // Target may have died from challenge let target_alive = self.players[target].is_alive();
let card = agents[target].choose_lost_influence(&target_player.cards); if target_alive {
self.players[target].lose(card, &mut self.discard); self.player_lose_influence(target, agents);
} }
} }
_ => return Err("Coup/Assassinate resolution has no target"), _ => return Err("Coup/Assassinate resolution has no target"),
@ -309,12 +310,17 @@ impl Game {
pub enum Phase { pub enum Phase {
Action(Action), Action(Action),
//ActionChallenge(ActionChallenge), //ActionChallenge(ActionChallenge),
//Block(Block), Block(Block),
BlockChallenge(BlockChallenge), BlockChallenge(BlockChallenge),
Resolution(Resolution), Resolution(Resolution),
Done, Done,
} }
#[derive(PartialEq, Debug)]
pub struct Block {
}
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct BlockChallenge { pub struct BlockChallenge {
blocker: usize, blocker: usize,
@ -429,9 +435,6 @@ mod test {
} }
#[test] #[test]
fn test_resolution() { fn test_resolution() {
let dummy_agent = make_dummy_agent!(unimplemented!(), [Contessa, Duke], Captain); let dummy_agent = make_dummy_agent!(unimplemented!(), [Contessa, Duke], Captain);