aboutsummaryrefslogtreecommitdiff
path: root/src/game.rs
diff options
context:
space:
mode:
authorDylan <boss@tehbox.org>2026-11-09 16:56:33 +1300
committerDylan <boss@tehbox.org>2026-11-09 16:56:33 +1300
commit76fde82da0ef09d3635362e1fbf2a289c3d2c6dd (patch)
treef8f03ea61cf339bf76de293de481a34a39679abe /src/game.rs
parent478e7568f5eda9780d1409bb8dccc86221126281 (diff)
downloadtigers-and-sheep-76fde82da0ef09d3635362e1fbf2a289c3d2c6dd.tar.gz
tigers-and-sheep-76fde82da0ef09d3635362e1fbf2a289c3d2c6dd.zip
feat: Colour the game result and show scores to 5 significant figures
The game-over line is bold green for a win, naming the winning player, and bold yellow for a draw. The last-turn panel shows a minimax player's score to 5 significant figures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/game.rs')
-rw-r--r--src/game.rs44
1 files changed, 39 insertions, 5 deletions
diff --git a/src/game.rs b/src/game.rs
index 716446d..74b96e5 100644
--- a/src/game.rs
+++ b/src/game.rs
@@ -212,14 +212,15 @@ impl GameHandler
Some(o) =>
{
let block = panel().title_bottom(" Game over ");
- let result = match o
+ // A win in green, a draw in yellow
+ let (result, colour) = match o
{
- Outcome::Tiger => format!("{} win!", side_name(Turn::Tiger)),
- Outcome::Sheep => format!("{} win!", side_name(Turn::Sheep)),
- Outcome::Draw => "Draw by repetition!".to_string(),
+ Outcome::Tiger => (format!("{} ({}) win!", side_name(Turn::Tiger), players[1].name()), Color::Green),
+ Outcome::Sheep => (format!("{} ({}) win!", side_name(Turn::Sheep), players[0].name()), Color::Green),
+ Outcome::Draw => ("Draw by repetition!".to_string(), Color::Yellow),
};
let text = vec![
- Line::styled(result, Style::new().add_modifier(Modifier::BOLD)),
+ Line::styled(result, Style::new().fg(colour).add_modifier(Modifier::BOLD)),
Line::from("Click a move to revert to it, or press q to quit."),
];
frame.render_widget(Paragraph::new(text).block(block), current_pane);
@@ -498,5 +499,38 @@ mod tests
let terminal = draw(&mut game, &[&mut sheep, &mut tiger]);
let screen = format!("{:?}", terminal.backend().buffer());
assert!(screen.contains("Draw by repetition!"), "{screen}");
+ assert_result_style(&terminal, "Draw by repetition!", Color::Yellow);
+ }
+
+ #[test]
+ fn game_over_panel_shows_the_winner_in_green()
+ {
+ // Only 15 sheep left, so the tigers have won
+ let mut game = movement_game();
+ game.start.set((0, 1), PieceType::Empty);
+ assert_eq!(game.outcome(), Some(Outcome::Tiger));
+ let mut sheep = LocalPlayer::default();
+ let mut tiger = ClaudePlayer::new(std::time::Duration::from_millis(50));
+ let terminal = draw(&mut game, &[&mut sheep, &mut tiger]);
+ assert_result_style(&terminal, "Tigers (Claude) win!", Color::Green);
+ }
+
+ /// Every cell of `text` on screen is bold and in `colour`.
+ fn assert_result_style(terminal: &Terminal<TestBackend>, text: &str, colour: Color)
+ {
+ let buf = terminal.backend().buffer();
+ let area = buf.area;
+ let found = (0..area.height).find_map(|y|
+ {
+ let row: String = (0..area.width).map(|x| buf[(x, y)].symbol()).collect();
+ row.find(text).map(|i| (row[..i].chars().count() as u16, y))
+ });
+ let (x0, y) = found.unwrap_or_else(|| panic!("{text} isn't on screen"));
+ for x in x0..x0 + text.chars().count() as u16
+ {
+ let cell = &buf[(x, y)];
+ assert_eq!(cell.fg, colour, "{text} at column {x}");
+ assert!(cell.modifier.contains(Modifier::BOLD), "{text} at column {x}");
+ }
}
}