Skip to content

Commit

Permalink
fixed target check, added test, simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikwilkowski committed May 8, 2024
1 parent b7d77a4 commit 47ac049
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coup"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
authors = ["Dominik Wilkowski <Hi@Dominik-Wilkowski.com>"]
license = "GPL-3.0-or-later"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ bot that does not exist).

## Changelog

### `v1.1.1`
Fixed engine to check if a target bot not just exists but also is in play.
Before a bot could have targeted another bot who is not playing in this round
(where we have more than 6 bots) which could have resulted in a panic as these
bots may not have 2 cards.

### `v1.1.0`
Challenges and counter challenges are not more fair by making sure the first bot
being asked for a challenge is the bot next in line according to the bots
Expand Down
45 changes: 36 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Coup {

fn setup(&mut self) {
// A fresh deck
let mut deck = Coup::new_deck();
self.deck = Coup::new_deck();

// Put the index of all bots into play so we can shuffle them later
self.playing_bots.clear();
Expand All @@ -274,11 +274,10 @@ impl Coup {

// Give all playing bots cards and coins
for bot in self.playing_bots.iter() {
let new_cards = vec![deck.pop().unwrap(), deck.pop().unwrap()];
let new_cards = vec![self.deck.pop().unwrap(), self.deck.pop().unwrap()];
self.bots[*bot].cards = new_cards;
self.bots[*bot].coins = 2;
}
self.deck = deck;

self.discard_pile = vec![];
self.history = vec![];
Expand Down Expand Up @@ -330,15 +329,15 @@ impl Coup {
return;
}
let context = self.get_context(name.clone());
self.bots.iter_mut().enumerate().for_each(|(index, bot)| {
self.bots.iter_mut().for_each(|bot| {
let context = Context {
coins: bot.coins,
cards: bot.cards.clone(),
..context.clone()
};
if !self.playing_bots.contains(&index) {
} else if bot.name == name {
if bot.name == name {
let lost_card = bot.interface.on_card_loss(&context);
// Bot discarded a card it didn't have so now we kill it dead
if !bot.cards.contains(&lost_card) {
Self::log(format_args!("🚨 {} is being penalized because \x1b[33mit discarded a card({:?}) it didn't have\x1b[39m", bot, lost_card), self.log);

Expand Down Expand Up @@ -389,7 +388,12 @@ impl Coup {
}

fn target_not_found(&self, target: String) -> bool {
self.bots.iter().filter(|bot| bot.name == target).count() != 1
self
.playing_bots
.iter()
.filter(|bot| self.bots[**bot].name == target)
.count()
!= 1
}

fn set_score(&mut self, winners: Vec<String>) {
Expand Down Expand Up @@ -649,7 +653,7 @@ impl Coup {
.copied()
.collect::<Vec<usize>>();

// We move to the next turn
// We move to the next turn (turn is the moving index self.playing_bots)
self.turn = if self.playing_bots.is_empty()
|| self.turn >= self.playing_bots.len() - 1
{
Expand Down Expand Up @@ -1650,6 +1654,24 @@ mod tests {
assert_eq!(coup.target_not_found(String::from("StaticBot")), false);
assert_eq!(coup.target_not_found(String::from("StaticBot 3")), true);
assert_eq!(coup.target_not_found(String::from("StaticBot 2")), false);

let mut coup = Coup::new(vec![
Box::new(StaticBot),
Box::new(StaticBot),
Box::new(StaticBot),
Box::new(StaticBot),
Box::new(StaticBot),
Box::new(StaticBot),
Box::new(StaticBot),
]);
coup.setup();
coup.playing_bots = vec![0, 1, 2, 3, 4, 5];

assert_eq!(coup.target_not_found(String::from("StaticBot 7")), true);

coup.playing_bots = vec![1, 2, 3, 4, 5, 6];

assert_eq!(coup.target_not_found(String::from("StaticBot 7")), false);
}

#[test]
Expand Down Expand Up @@ -3907,7 +3929,12 @@ mod tests {
);
}

// TODO: test_looping
#[test]
fn test_test_looping() {
let mut coup = Coup::new(vec![Box::new(StaticBot), Box::new(StaticBot)]);
coup.looping(5000);
// just making sure looping doesn't panic here. Testing it further is hard
}

// *******************************| Actions |****************************** //
#[test]
Expand Down

0 comments on commit 47ac049

Please sign in to comment.