gamie 0.12.0

A Rust library provides abstractions for several classic tiny games
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Reversi (Othello) game implementation
//!
//! Reversi is a strategy board game for two players, played on an 8×8 board.
//! Players take turns placing pieces on the board, flipping opponent pieces
//! that are surrounded by the current player's pieces in any direction
//! (horizontal, vertical, or diagonal).
//!
//! See [`Game`] for the main game interface.

use core::{cmp::Ordering, convert::Infallible};
use thiserror::Error;

const BOARD_WIDTH: usize = 8;
const BOARD_HEIGHT: usize = 8;

/// A Reversi (Othello) game instance
///
/// The game is played on an 8×8 board where two players alternate placing their pieces.
/// Player0 always goes first. The game ends when neither player can make a valid move,
/// and the player with the most pieces wins.
///
/// # Examples
///
/// ```rust
/// # use gamie::reversi::{Player, Game};
/// let mut game = Game::new().unwrap();
///
/// // Player0's turn
/// game.place(2, 4).unwrap();
///
/// // Player1's turn
/// game.place(2, 3).unwrap();
///
/// // Continue playing...
/// ```
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Game {
    board: [[Option<Player>; BOARD_HEIGHT]; BOARD_WIDTH],
    next_player: Player,
    status: Status,
}

/// Represents a player in the game
///
/// There are two players in Reversi. Player0 always makes the first move,
/// followed by Player1, and they continue alternating turns throughout the game.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Player {
    Player0,
    Player1,
}

/// The current status of the game
///
/// The game can be in one of three states:
/// - [`Ongoing`](Status::Ongoing): The game is still in progress
/// - [`Win`](Status::Win): A player has won by having the most pieces
/// - [`Draw`](Status::Draw): Both players have the same number of pieces
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Status {
    Ongoing,
    Draw,
    Win(Player),
}

/// Errors that can occur when placing a piece on the board
///
/// These errors prevent invalid moves from being made during the game.
#[derive(Debug, Error)]
pub enum Error {
    /// The position is already occupied by a piece
    #[error("position occupied")]
    PositionOccupied,
    /// The move would not flip any opponent pieces
    #[error("invalid position")]
    InvalidPosition,
    /// The game has already ended (either in a win or draw)
    #[error("game ended")]
    GameEnded,
}

impl Game {
    /// Creates a new Reversi game with the standard starting position
    ///
    /// The board is initialized with 4 pieces in the center:
    /// - Player0 at positions (3,3) and (4,4)
    /// - Player1 at positions (3,4) and (4,3)
    ///
    /// Player0 will make the first move.
    pub const fn new() -> Result<Self, Infallible> {
        let mut board = [[None; BOARD_HEIGHT]; BOARD_WIDTH];

        board[3][3] = Some(Player::Player0);
        board[4][4] = Some(Player::Player0);
        board[3][4] = Some(Player::Player1);
        board[4][3] = Some(Player::Player1);

        Ok(Self {
            board,
            next_player: Player::Player0,
            status: Status::Ongoing,
        })
    }

    /// Gets the piece at the specified position
    ///
    /// Returns `Some(Player)` if a piece is present at the given position,
    /// or `None` if the position is empty.
    ///
    /// # Parameters
    ///
    /// - `row`: The row index (0-7)
    /// - `col`: The column index (0-7)
    ///
    /// # Panics
    ///
    /// Panics if `row` or `col` is out of bounds (greater than or equal to 8)
    pub const fn get(&self, row: usize, col: usize) -> Option<Player> {
        self.board[row][col]
    }

    /// Places a piece for the current player at the specified position
    ///
    /// The current player's piece (returned by [`next_player()`](Self::next_player))
    /// is placed at the given position and flips all opponent pieces that are captured
    /// in any direction (horizontal, vertical, or diagonal). After a successful placement,
    /// the turn automatically switches to the other player (if they have valid moves).
    ///
    /// # Parameters
    ///
    /// - `row`: The row index (0-7)
    /// - `col`: The column index (0-7)
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The position is already occupied ([`Error::PositionOccupied`])
    /// - The move would not flip any opponent pieces ([`Error::InvalidPosition`])
    /// - The game has already ended ([`Error::GameEnded`])
    ///
    /// # Panics
    ///
    /// Panics if `row` or `col` is out of bounds (greater than or equal to 8)
    pub fn place(&mut self, row: usize, col: usize) -> Result<(), Error> {
        if matches!(self.status, Status::Win(_) | Status::Draw) {
            return Err(Error::GameEnded);
        }

        if self.board[row][col].is_some() {
            return Err(Error::PositionOccupied);
        }

        let flipping_left_range = (0..col).rev();
        let flipping_right_range = col + 1..BOARD_WIDTH;
        let flipping_up_range = (0..row).rev();
        let flipping_down_range = row + 1..BOARD_HEIGHT;

        let mut any_flipped = false;

        // flip left
        any_flipped |= self.try_flip_line(flipping_left_range.clone().map(|col| (row, col)));

        // flip right
        any_flipped |= self.try_flip_line(flipping_right_range.clone().map(|col| (row, col)));

        // flip up
        any_flipped |= self.try_flip_line(flipping_up_range.clone().map(|row| (row, col)));

        // flip down
        any_flipped |= self.try_flip_line(flipping_down_range.clone().map(|row| (row, col)));

        // flip upper left
        any_flipped |=
            self.try_flip_line(flipping_up_range.clone().zip(flipping_left_range.clone()));

        // flip upper right
        any_flipped |=
            self.try_flip_line(flipping_up_range.clone().zip(flipping_right_range.clone()));

        // flip lower left
        any_flipped |=
            self.try_flip_line(flipping_down_range.clone().zip(flipping_left_range.clone()));

        // flip lower right
        any_flipped |= self.try_flip_line(flipping_down_range.zip(flipping_right_range));

        if !any_flipped {
            return Err(Error::InvalidPosition);
        }

        // place the piece
        self.board[row][col] = Some(self.next_player);

        self.next_player = self.next_player.other();
        if self.can_current_player_move() {
            return Ok(());
        }

        self.next_player = self.next_player.other();
        if self.can_current_player_move() {
            return Ok(());
        }

        // Neither player can move; the game ends
        let mut player0_count = 0u8;
        let mut player1_count = 0u8;

        for row in 0..BOARD_HEIGHT {
            for col in 0..BOARD_WIDTH {
                match self.get(row, col) {
                    Some(Player::Player0) => player0_count += 1,
                    Some(Player::Player1) => player1_count += 1,
                    None => {}
                }
            }
        }

        match player0_count.cmp(&player1_count) {
            Ordering::Greater => self.status = Status::Win(Player::Player0),
            Ordering::Less => self.status = Status::Win(Player::Player1),
            Ordering::Equal => self.status = Status::Draw,
        }

        Ok(())
    }

    /// Checks if the current player can place a piece at the specified position
    ///
    /// This method validates whether placing a piece at the given position would
    /// be a legal move for the current player (without actually making the move).
    ///
    /// # Parameters
    ///
    /// - `row`: The row index (0-7)
    /// - `col`: The column index (0-7)
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The position is already occupied ([`Error::PositionOccupied`])
    /// - The move would not flip any opponent pieces ([`Error::InvalidPosition`])
    /// - The game has already ended ([`Error::GameEnded`])
    ///
    /// # Panics
    ///
    /// Panics if `row` or `col` is out of bounds (greater than or equal to 8)
    pub fn can_place_at(&self, row: usize, col: usize) -> Result<(), Error> {
        if matches!(self.status, Status::Win(_) | Status::Draw) {
            return Err(Error::GameEnded);
        }

        if self.board[row][col].is_some() {
            return Err(Error::PositionOccupied);
        }

        // check each direction for a valid move

        let checking_left_range = (0..col).rev();
        let checking_right_range = col + 1..BOARD_WIDTH;
        let checking_up_range = (0..row).rev();
        let checking_down_range = row + 1..BOARD_HEIGHT;

        // check left
        if self.can_flip_line(checking_left_range.clone().map(|col| (row, col))) {
            return Ok(());
        }

        // check right
        if self.can_flip_line(checking_right_range.clone().map(|col| (row, col))) {
            return Ok(());
        }

        // check up
        if self.can_flip_line(checking_up_range.clone().map(|row| (row, col))) {
            return Ok(());
        }

        // check down
        if self.can_flip_line(checking_down_range.clone().map(|row| (row, col))) {
            return Ok(());
        }

        // check upper left
        if self.can_flip_line(checking_up_range.clone().zip(checking_left_range.clone())) {
            return Ok(());
        }

        // check upper right
        if self.can_flip_line(checking_up_range.clone().zip(checking_right_range.clone())) {
            return Ok(());
        }

        // check lower left
        if self.can_flip_line(checking_down_range.clone().zip(checking_left_range.clone())) {
            return Ok(());
        }

        // check lower right
        if self.can_flip_line(checking_down_range.zip(checking_right_range)) {
            return Ok(());
        }

        Err(Error::InvalidPosition)
    }

    /// Gets the next player whose turn it is to move
    ///
    /// Returns the player who should make the next move. This changes
    /// after each successful call to [`place()`](Self::place).
    pub const fn next_player(&self) -> Player {
        self.next_player
    }

    /// Gets the current game status
    ///
    /// Returns whether the game is ongoing, ended in a draw, or won by a player.
    /// The status is automatically updated after each move.
    pub const fn status(&self) -> &Status {
        &self.status
    }

    fn can_current_player_move(&self) -> bool {
        for row in 0..BOARD_HEIGHT {
            for col in 0..BOARD_WIDTH {
                match self.can_place_at(row, col) {
                    Err(Error::PositionOccupied | Error::InvalidPosition) => continue,
                    Ok(()) => return true,
                    Err(Error::GameEnded) => unreachable!(),
                }
            }
        }

        false
    }

    fn try_flip_line(&mut self, line: impl Iterator<Item = (usize, usize)> + Clone) -> bool {
        let mut skipped = 0;

        let Some((row, col)) = line
            .clone()
            .skip_while(|(row, col)| {
                let is_other_player = self.get(*row, *col) == Some(self.next_player().other());
                skipped += is_other_player as usize;
                is_other_player
            })
            .next()
        else {
            return false;
        };

        if skipped == 0 || self.get(row, col) != Some(self.next_player()) {
            return false;
        }

        for (row, col) in line.take(skipped) {
            self.board[row][col] = Some(self.next_player());
        }

        true
    }

    fn can_flip_line(&self, line: impl Iterator<Item = (usize, usize)>) -> bool {
        let mut skipped = false;

        let Some((row, col)) = line
            .skip_while(|(row, col)| {
                let is_other_player = self.get(*row, *col) == Some(self.next_player().other());
                skipped |= is_other_player;
                is_other_player
            })
            .next()
        else {
            return false;
        };

        skipped && self.get(row, col) == Some(self.next_player())
    }
}

impl Player {
    /// Returns the opposite player
    ///
    /// If this player is Player0, returns Player1, and vice versa.
    pub const fn other(self) -> Self {
        match self {
            Player::Player0 => Player::Player1,
            Player::Player1 => Player::Player0,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::reversi::*;

    #[test]
    fn test() {
        let mut game = Game::new().unwrap();

        game.can_place_at(2, 4).unwrap();

        game.place(2, 4).unwrap();
        game.place(2, 3).unwrap();

        assert!(matches!(game.place(2, 3), Err(Error::PositionOccupied)));
        assert!(matches!(game.place(2, 6), Err(Error::InvalidPosition)));
    }
}