1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
use ratatui::{buffer::Buffer, layout::Rect, style::{Color, Style}, text::{Line, Text}, widgets::{Paragraph, Widget, Wrap}};
use crate::{board::{GameBoard, Move, PieceType, Pos, Turn}, player::{Highlights, Input, Player}};
/// Sheep that start the game: on the board plus in hand.
const TOTAL_SHEEP: u32 = 20;
/// A human player at the terminal, using the mouse: click a piece to select it, then click one
/// of its highlighted destinations. While sheep are in hand, click an empty point to place one.
#[derive(Default)]
pub struct LocalPlayer
{
/// The piece whose moves are highlighted.
selected: Option<Pos>,
/// The legal moves of `selected`, kept for `highlights`.
selected_moves: Vec<Move>,
/// Why the last click didn't do anything, if it didn't.
message: String,
/// The position to play, for the player panel; set by `update`.
status: Option<(GameBoard, Turn)>,
/// The move this player last made, for the last-turn panel.
played: Option<Move>,
}
/// The source and destination of a move. Placements have no source.
fn ends(mv: Move) -> (Option<Pos>, Pos)
{
match mv
{
Move::PlaceSheep { pos } => (None, pos),
Move::MoveSheep { from, to } | Move::MoveTiger { from, to } => (Some(from), to),
}
}
fn is_mine(board: &GameBoard, pos: Pos, turn: Turn) -> bool
{
match turn
{
Turn::Tiger => matches!(board.get(pos), PieceType::Tiger),
Turn::Sheep => matches!(board.get(pos), PieceType::Sheep),
}
}
impl LocalPlayer
{
/// Clear the selection and note `mv` as played.
fn play(&mut self, mv: Move) -> Option<Move>
{
self.selected = None;
self.selected_moves.clear();
self.played = Some(mv);
Some(mv)
}
fn click(&mut self, pos: Pos, board: GameBoard, turn: Turn) -> Option<Move>
{
self.message.clear();
let moves = board.get_moves(turn);
if turn == Turn::Sheep && board.sheep_left() > 0
{
let mv = Move::PlaceSheep { pos };
if moves.contains(&mv)
{
return self.play(mv);
}
if let Err(err) = board.check_move(mv)
{
self.message = err.to_string();
}
return None;
}
if let Some(mv) = self.selected_moves.iter().copied().find(|&m| ends(m).1 == pos)
{
return self.play(mv);
}
self.selected = None;
self.selected_moves.clear();
if is_mine(&board, pos, turn)
{
let from_here: Vec<Move> = moves.into_iter().filter(|&m| ends(m).0 == Some(pos)).collect();
if from_here.is_empty()
{
self.message = "That piece can't move".to_string();
}
else
{
self.selected = Some(pos);
self.selected_moves = from_here;
}
}
None
}
}
impl Player for LocalPlayer
{
fn name(&self) -> String { "You".to_string() }
fn update(&mut self, board: GameBoard, turn: Turn)
{
self.status = Some((board, turn));
}
fn handle_input(&mut self, input: Input, board: GameBoard, turn: Turn) -> Option<Move>
{
match input
{
Input::Click(pos) => self.click(pos, board, turn),
Input::Key(_) => None,
}
}
fn highlights(&self) -> Highlights
{
Highlights
{
selected: self.selected,
targets: self.selected_moves.iter().map(|&m| ends(m).1).collect(),
}
}
fn reset(&mut self)
{
self.selected = None;
self.selected_moves.clear();
self.played = None;
self.message.clear();
}
fn render_last_turn(&self, area: Rect, buf: &mut Buffer)
{
if let Some(mv) = self.played
{
Paragraph::new(format!("Played {mv}")).render(area, buf);
}
}
fn render(&self, area: Rect, buf: &mut Buffer)
{
// The coordinator's panel title already shows the name
let mut text = Text::default();
if let Some((board, turn)) = self.status
{
let dead = TOTAL_SHEEP - board.sheep_count() - board.sheep_left();
text.push_line(format!("Sheep in hand: {} · on board: {} · killed: {dead}",
board.sheep_left(), board.sheep_count()));
let instruction = if turn == Turn::Sheep && board.sheep_left() > 0
{
"Click an empty point to place a sheep".to_string()
}
else if self.selected.is_some()
{
"Click a highlighted point, or another piece".to_string()
}
else
{
format!("Click a {} to move", if turn == Turn::Tiger { "tiger" } else { "sheep" })
};
text.push_line(instruction);
}
if !self.message.is_empty()
{
text.push_line(Line::styled(self.message.clone(), Style::new().fg(Color::Red)));
}
Paragraph::new(text).wrap(Wrap { trim: false }).render(area, buf);
}
}
#[cfg(test)]
mod tests
{
use super::*;
/// The default board with all 20 sheep placed, on every point but (2, 1).
fn moving_board() -> GameBoard
{
let mut board = GameBoard::default();
for y in 0..5
{
for x in 0..5
{
if (x, y) != (2, 1) && matches!(board.get((x, y)), PieceType::Empty)
{
board.make_move(Move::PlaceSheep { pos: (x, y) }).unwrap();
}
}
}
assert_eq!(board.sheep_left(), 0);
board
}
#[test]
fn click_places_a_sheep()
{
let mut player = LocalPlayer::default();
let board = GameBoard::default();
assert_eq!(player.handle_input(Input::Click((2, 2)), board, Turn::Sheep), Some(Move::PlaceSheep { pos: (2, 2) }));
}
#[test]
fn played_move_is_remembered_until_reset()
{
let mut player = LocalPlayer::default();
let mv = player.handle_input(Input::Click((2, 2)), GameBoard::default(), Turn::Sheep).unwrap();
assert_eq!(player.played, Some(mv));
let mut buf = Buffer::empty(Rect::new(0, 0, 20, 2));
player.render_last_turn(buf.area, &mut buf);
assert_eq!((0..20).map(|x| buf[(x, 0)].symbol()).collect::<String>().trim_end(), "Played sc3");
player.reset();
assert_eq!(player.played, None);
}
#[test]
fn click_on_occupied_point_explains_why()
{
let mut player = LocalPlayer::default();
assert_eq!(player.handle_input(Input::Click((0, 0)), GameBoard::default(), Turn::Sheep), None);
assert_eq!(player.message, "Can't place a sheep on an occupied point!");
}
#[test]
fn select_highlight_then_move_tiger()
{
let mut player = LocalPlayer::default();
let board = GameBoard::default();
assert_eq!(player.handle_input(Input::Click((0, 0)), board, Turn::Tiger), None);
let mut targets = player.highlights().targets;
targets.sort();
assert_eq!(player.highlights().selected, Some((0, 0)));
assert_eq!(targets, vec![(0, 1), (1, 0), (1, 1)]);
assert_eq!(player.handle_input(Input::Click((1, 1)), board, Turn::Tiger),
Some(Move::MoveTiger { from: (0, 0), to: (1, 1) }));
}
#[test]
fn clicking_another_piece_reselects()
{
let mut player = LocalPlayer::default();
let board = GameBoard::default();
player.handle_input(Input::Click((0, 0)), board, Turn::Tiger);
player.handle_input(Input::Click((4, 4)), board, Turn::Tiger);
assert_eq!(player.highlights().selected, Some((4, 4)));
// (1, 1) isn't reachable from (4, 4), so it just deselects
assert_eq!(player.handle_input(Input::Click((1, 1)), board, Turn::Tiger), None);
assert_eq!(player.highlights(), Highlights::default());
}
#[test]
fn select_and_move_sheep()
{
let mut player = LocalPlayer::default();
let board = moving_board();
assert_eq!(player.handle_input(Input::Click((2, 2)), board, Turn::Sheep), None);
assert_eq!(player.highlights().targets, vec![(2, 1)]);
assert_eq!(player.handle_input(Input::Click((2, 1)), board, Turn::Sheep),
Some(Move::MoveSheep { from: (2, 2), to: (2, 1) }));
}
#[test]
fn stuck_piece_is_not_selected()
{
let mut player = LocalPlayer::default();
let board = moving_board();
// (0, 4) is a tiger boxed in by sheep with nothing empty behind them
assert_eq!(player.handle_input(Input::Click((0, 4)), board, Turn::Tiger), None);
assert_eq!(player.highlights().selected, None);
assert_eq!(player.message, "That piece can't move");
}
}
|