]> git.scottworley.com Git - pluta-lesnura/blobdiff - src/lib.rs
Coordinate to grant momentum to the next player
[pluta-lesnura] / src / lib.rs
index c73c146dceef08cb2142ec86418cd76f5d847980..45b7b4527aa64f55b499f662a27e4ace0e86ce1a 100644 (file)
@@ -178,10 +178,31 @@ impl Hand {
     fn len(&self) -> usize {
         self.cards.len()
     }
-    #[cfg(test)]
     fn random(&self) -> Option<&Card> {
         self.cards.choose(&mut rand::thread_rng())
     }
+    /// Which suits are in this Hand?
+    fn suits(&self) -> Vec<Suit> {
+        self.cards
+            .iter()
+            .map(|c| c.suit().expect("I shouldn't have jokers in my hand"))
+            .collect()
+    }
+    /// Make a new Hand that contains only cards of the requested suit
+    fn filter_by_suit(&self, suit: Suit) -> Self {
+        self.filter_by_suits(&[suit])
+    }
+    /// Make a new Hand that contains only cards of the requested suits
+    fn filter_by_suits(&self, suits: &[Suit]) -> Self {
+        Self {
+            cards: self
+                .cards
+                .iter()
+                .filter(|c| suits.contains(&c.suit().expect("I shouldn't have jokers in my hand")))
+                .copied()
+                .collect(),
+        }
+    }
 }
 
 #[derive(Copy, Clone)]
@@ -204,6 +225,7 @@ pub enum Phase {
     Momentum,
 }
 
+#[derive(Debug)]
 pub enum GameOutcome {
     Loss,
     Win,
@@ -247,9 +269,15 @@ impl Game {
     pub fn current_player_hand(&self) -> &Hand {
         &self.hands[self.turn.0]
     }
+    #[must_use]
+    pub fn next_player_hand(&self) -> &Hand {
+        &self.hands[self.turn.next(self.hands.len()).0]
+    }
+    #[must_use]
     fn player_hand_mut(&mut self, pi: PlayerIndex) -> &mut Hand {
         &mut self.hands[pi.0]
     }
+    #[must_use]
     fn current_player_hand_mut(&mut self) -> &mut Hand {
         self.player_hand_mut(self.turn)
     }
@@ -347,7 +375,7 @@ impl Game {
             .progress
             .iter()
             .zip(self.path_lengths.iter())
-            .any(|(&prog, len)| prog >= len.0 .0.try_into().expect("wat?"))
+            .any(|(&prog, len)| prog >= len.0.value().try_into().expect("wat?"))
         {
             GameOutcome::Win
         } else {
@@ -395,27 +423,68 @@ impl Default for Game {
 }
 
 pub struct Player(Box<dyn FnMut(&Game) -> Play>);
+impl Player {
+    #[must_use]
+    pub fn new<T>(f: T) -> Self
+    where
+        T: FnMut(&Game) -> Play + 'static,
+    {
+        Self(Box::new(f))
+    }
+}
 
-#[cfg(test)]
-fn random_player(game: &Game) -> Play {
-    match game.phase {
-        Phase::Play => Play::Play(
-            *game
-                .current_player_hand()
-                .random()
-                .expect("I always have a card to play because I just drew one"),
-        ),
-        Phase::Momentum => {
-            if rand::thread_rng().gen_bool(0.5) {
-                Play::Draw
-            } else {
-                match game.current_player_hand().random() {
-                    Some(card) => Play::Play(*card),
-                    None => Play::Draw,
+#[must_use]
+pub fn random_player(draw_chance: f64) -> Player {
+    Player(Box::new(move |game: &Game| -> Play {
+        match game.phase {
+            Phase::Play => Play::Play(
+                *game
+                    .current_player_hand()
+                    .random()
+                    .expect("I always have a card to play because I just drew one"),
+            ),
+            Phase::Momentum => {
+                if rand::thread_rng().gen_bool(draw_chance) {
+                    Play::Draw
+                } else {
+                    match game.current_player_hand().random() {
+                        Some(card) => Play::Play(*card),
+                        None => Play::Draw,
+                    }
                 }
             }
         }
-    }
+    }))
+}
+
+/// When available, make plays that grant momentum.
+#[must_use]
+pub fn momentum_player(mut fallback: Player) -> Player {
+    Player(Box::new(move |game: &Game| -> Play {
+        if game.phase == Phase::Play {
+            if let Some(suit) = game.discard.top().and_then(Card::suit) {
+                if let Some(card) = game.current_player_hand().filter_by_suit(suit).random() {
+                    return Play::Play(*card);
+                }
+            }
+        }
+        fallback.0(game)
+    }))
+}
+
+/// Try to coordinate to give the next player a momentum opportunity.
+#[must_use]
+pub fn coordinating_player(mut fallback: Player) -> Player {
+    Player(Box::new(move |game: &Game| -> Play {
+        if let Some(card) = game
+            .current_player_hand()
+            .filter_by_suits(&game.next_player_hand().suits())
+            .random()
+        {
+            return Play::Play(*card);
+        }
+        fallback.0(game)
+    }))
 }
 
 /// # Errors
@@ -490,8 +559,17 @@ mod tests {
 
     #[test]
     fn test_game() {
-        let mut game = Game::default();
-        game.add_player();
-        assert!(play(game, vec![Player(Box::new(random_player))]).is_ok());
+        for num_players in 1..10 {
+            let players: Vec<_> = std::iter::from_fn(|| {
+                Some(momentum_player(coordinating_player(random_player(0.5))))
+            })
+            .take(num_players)
+            .collect();
+            let mut game = Game::default();
+            for _ in 0..num_players {
+                game.add_player();
+            }
+            assert!(play(game, players).is_ok());
+        }
     }
 }