Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dominikwilkowski/coup
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.0
Choose a base ref
...
head repository: dominikwilkowski/coup
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on May 8, 2024

  1. fixed target check, added test, simplified code

    dominikwilkowski committed May 8, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    47ac049 View commit details
Showing with 44 additions and 11 deletions.
  1. +1 −1 Cargo.lock
  2. +1 −1 Cargo.toml
  3. +6 −0 README.md
  4. +36 −9 src/lib.rs
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"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
45 changes: 36 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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();
@@ -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![];
@@ -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);

@@ -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>) {
@@ -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
{
@@ -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]
@@ -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]