mancala 0.1.0

Play a variant of mancala
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Mancala Game related definitions
//!
//! [Mancala](https://en.wikipedia.org/wiki/Mancala) is a game with many variants.
//! Here we focus on one variant, but we allow different number of bowls.
//!
//! The code below shows how one would build a standard mancala game.
//!
//! ```rust
//! # use mancala::game::GameBuilder;
//! let game =
//!   GameBuilder::new()
//!     .bowls(6)
//!     .stones(4)
//!     .build();
//! ```

use std::fmt::{self, Display, Formatter};

/// Representation of a Bowl
pub type Bowl = usize;

/// Representation of a number of stones in a bowl
pub type Stones = u8;

/// Score a finished game;
pub type Score = i8;

/// GameBuilder is used to create a Mancala game.
pub struct GameBuilder {
    bowls: u8,
    stones: Stones,
}

impl GameBuilder {
    /// Creates a new GameBuilder
    ///
    /// The default number of bowls is 6 and the default number of stones per bowl is 4.
    pub fn new() -> Self {
        GameBuilder {
            bowls: 6,
            stones: 4,
        }
    }

    /// Sets the number of bowls for this GameBuilder
    pub fn bowls(self, bowls: u8) -> Self {
        GameBuilder { bowls, ..self }
    }

    /// Sets the number of stones for this GameBuilder
    pub fn stones(self, stones: Stones) -> Self {
        GameBuilder { stones, ..self }
    }

    /// Creates a Game with the required number of bowls and stones per bowl
    pub fn build(self) -> Game {
        let current = Position::new(self.bowls, self.stones);
        Game {
            current,
            history: vec![],
        }
    }
}

impl Default for GameBuilder {
    fn default() -> Self {
        GameBuilder::new()
    }
}

/// Game is an sequence of Positions.
///
/// A Game is created with a GameBuilder.
#[derive(Debug, PartialEq)]
pub struct Game {
    /// The current position of this game
    pub current: Position,
    history: Vec<(Player, Bowl)>,
}

impl Game {
    /// Determine if this game is finished
    pub fn finished(&self) -> bool {
        self.current.finished()
    }

    /// Determine which bowls are playable.
    pub fn options(&self) -> Vec<Bowl> {
        self.current.options()
    }

    /// Play a certain bowl.
    ///
    /// Fails if the bowl does not contain any stones.
    pub fn play(&mut self, bowl: Bowl) -> Result<(), FoulPlay> {
        match self.current.play(bowl) {
            Some(position) => {
                self.history.push((self.current.player, bowl));
                self.current = position;
                Ok(())
            }

            None => Err(FoulPlay::NoStonesInBowl),
        }
    }

    /// Determine the score of a game.
    ///
    /// None if the game is not finished
    pub fn score(&self) -> Option<Score> {
        self.current.score()
    }

    /// Return which players turn it is
    pub fn turn(&self) -> Player {
        self.current.turn()
    }
}

/// Discriminates between all the ways a play can go wrong.
#[derive(Debug)]
pub enum FoulPlay {
    /// Playing a bowl when there are no stones in the bowl, is foul play.
    NoStonesInBowl,
}

/// Position is a instance of the board.
#[derive(Debug, PartialEq)]
pub struct Position {
    player: Player,
    size: usize,
    capture: [Stones; 2],
    bowls: Vec<Stones>,
}

/// The names for the player.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Player {
    /// The starting player
    Red,
    /// The other player
    Blue,
}

impl Player {
    /// The opposite player
    pub fn other(self) -> Self {
        match self {
            Player::Red => Player::Blue,
            Player::Blue => Player::Red,
        }
    }
}

impl Position {
    /// Create a position with a number of bowls and a number of stones per bowl.
    pub fn new(bowls: u8, stones: Stones) -> Self {
        let size = bowls as usize;
        let bowls = vec![stones; 2 * size];
        Position {
            player: Player::Red,
            size,
            capture: [0, 0],
            bowls,
        }
    }

    /// Determine which bowls are playable.
    pub fn options(&self) -> Vec<Bowl> {
        self.bowls[0..self.size]
            .iter()
            .cloned()
            .enumerate()
            .filter_map(|(bowl, stones)| if stones > 0 { Some(bowl) } else { None })
            .collect()
    }

    /// Play a certain bowl.
    ///
    /// If the bowl is empty, returns nothing.
    pub fn play(&self, bowl: Bowl) -> Option<Self> {
        if self.bowls[bowl] > 0 {
            Some(self.sow(bowl))
        } else {
            None
        }
    }

    fn sow(&self, bowl: Bowl) -> Self {
        let mut player = self.player;
        let mut bowls = self.bowls.clone();
        let mut stones = bowls[bowl];
        let mut index = bowl;
        let mut store_offset = 0;
        let mut stored = 0;
        let mut change_player = true;
        bowls[index] = 0;
        while stones > 0 {
            index += 1;
            if index - store_offset == 2 * self.size {
                index = 0;
                store_offset = 0;
            }
            if index == self.size {
                stored += 1;
                store_offset = 1;
            } else {
                bowls[index - store_offset] += 1;
            }
            if stones == 1 && index == self.size {
                change_player = false
            }
            if stones == 1 && store_offset == 0 && bowls[index] == 1 {
                let capture_index = 2 * self.size - 1 - index;
                stored += bowls[capture_index];
                bowls[capture_index] = 0;
            }
            stones -= 1;
        }
        let mut capture = [self.capture[0] + stored as Stones, self.capture[1]];
        if change_player {
            player = player.other();
            capture = [capture[1], capture[0]];
            bowls.rotate_left(self.size);
        }
        Position {
            player,
            size: self.size,
            capture,
            bowls,
        }
    }

    /// Determine if a position is finished
    pub fn finished(&self) -> bool {
        self.bowls[0..self.size].iter().all(|&stones| stones == 0)
            || self.bowls[self.size..(2 * self.size)]
                .iter()
                .all(|&stones| stones == 0)
    }

    /// Which player is allowed to make a play
    pub fn active_player(&self) -> Player {
        self.player
    }

    /// Determine the score after the game is finished.
    ///
    /// Scores are awarded to the current player. Positive scores are a win, negative scores are a loss.
    pub fn score(&self) -> Option<Score> {
        if self.finished() {
            let mut first: Stones = self.bowls[0..self.size].iter().cloned().sum();
            first += self.capture[0];
            let mut second: Stones = self.bowls[self.size..2 * self.size].iter().cloned().sum();
            second += self.capture[1];

            let score = first as Score - second as Score;
            Some(score)
        } else {
            None
        }
    }

    /// Difference between the actual captured stones
    pub fn delta(&self) -> Score {
        self.capture[0] as Score - self.capture[1] as Score
    }

    /// Return which players turn it is
    pub fn turn(&self) -> Player {
        self.player
    }
}

impl Display for Position {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let current_side = &self.bowls[0..self.size];
        let opposite_side: &Vec<Stones> = &self.bowls[self.size..].iter().cloned().rev().collect();

        write!(f, "{:<3}", self.capture[1])?;
        for stones in opposite_side {
            write!(f, "| {:<3} ", stones)?
        }
        writeln!(f, "|")?;
        write!(f, "{:<3}", "")?;
        for stones in current_side {
            write!(f, "| {:<3} ", stones)?
        }
        writeln!(f, "| {:<3}", self.capture[0])
    }
}

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

    fn from_position<P>(position: P) -> PlayedGameBuilder
    where
        P: Into<Position>,
    {
        PlayedGameBuilder {
            current: position.into(),
        }
    }

    struct PlayedGameBuilder {
        current: Position,
    }

    impl PlayedGameBuilder {
        fn with_history(self, history: Vec<(Player, Bowl)>) -> Game {
            Game {
                current: self.current,
                history,
            }
        }
    }

    #[test]
    fn fresh_game_is_not_finished() {
        let game = GameBuilder::new().bowls(6).stones(4).build();

        assert!(!game.finished());
    }

    #[test]
    fn game_knows_options_to_play() {
        let game = GameBuilder::new().bowls(3).stones(2).build();

        let options = game.options();

        assert_eq!(options, vec!(0, 1, 2));
    }

    #[test]
    fn game_records_history_of_what_is_played() -> Result<(), FoulPlay> {
        let mut actual = GameBuilder::new().bowls(3).stones(2).build();

        actual.play(0)?;

        let position = (Player::Blue, [2, 2, 2, 0, 3, 3]);
        let expected = from_position(position).with_history(vec![(Player::Red, 0)]);
        assert_eq!(actual, expected);
        Ok(())
    }

    #[test]
    fn play_that_goes_over_store_should_capture_stone() {
        let start = Position::from([2, 2, 2, 2]);

        let actual = start.play(1);

        let expected = Position::from((Player::Blue, 0, 1, [3, 2, 2, 0]));
        assert_eq!(actual, Some(expected))
    }

    #[test]
    fn play_that_cycles_should_start_over() {
        let start = Position::from([6, 6, 6, 6]);

        let actual = start.play(0);

        let expected = Position::from((Player::Blue, 0, 1, [7, 7, 1, 8]));
        assert_eq!(actual, Some(expected))
    }

    #[test]
    fn play_into_your_store_allows_an_other_turn() {
        let start = Position::from([2, 2, 2, 2, 2, 2]);

        let actual = start.play(1);

        let expected = Position::from((1, 0, [2, 0, 3, 2, 2, 2]));
        assert_eq!(actual, Some(expected))
    }

    #[test]
    fn play_into_empty_bowl_captures_opposite_bowl() {
        let start = Position::from([2, 2, 0, 2, 2, 2, 2, 2]);

        let actual = start.play(0);

        let expected = Position::from((Player::Blue, 0, 2, [2, 0, 2, 2, 0, 3, 1, 2]));
        assert_eq!(actual, Some(expected))
    }

    #[test]
    fn positions_with_no_stones_on_one_side_is_finished() {
        let start = Position::from([0, 0, 2, 2]);

        assert!(start.finished());
        assert_eq!(start.score(), Some(-4));
    }

    #[test]
    fn play_changes_player() {
        let start = Position::from([1, 0, 1, 0]);

        let actual = start.play(0).unwrap();

        let expected = Position::from((Player::Blue, 0, 1, [0, 0, 0, 1]));
        assert_eq!(actual, expected);
        assert_eq!(expected.score(), Some(-2));
    }
}

macro_rules! position_from_array_for_sizes {
    ( $($n : expr),* ) => {
        $(
            impl From<[Stones; $n]> for Position {
                fn from(bowls: [Stones; $n]) -> Self {
                    Position {
                        player: Player::Red,
                        size: $n/2,
                        capture: [0, 0],
                        bowls: bowls.to_vec(),
                    }
                }
            }
        )*
    }
}

macro_rules! position_with_player_from_array_for_sizes {
    ( $($n : expr),* ) => {
        $(
        impl From<(Player, [Stones; $n])> for Position {
            fn from(data: (Player, [Stones; $n])) -> Self {
                Position {
                    player: data.0,
                    size: $n/2,
                    capture: [0, 0],
                    bowls: data.1.to_vec(),
                }
            }
        }
        )*
    }
}

macro_rules! position_with_capture_from_array_for_sizes {
    ( $($n : expr),* ) => {
        $(
            impl From<(Stones, Stones, [Stones; $n])> for Position {
                fn from(data: (Stones, Stones, [Stones; $n])) -> Self {
                    Position {
                        player: Player::Red,
                        size: $n/2,
                        capture: [data.0, data.1],
                        bowls: data.2.to_vec(),
                    }
                }
            }
        )*
    }
}

macro_rules! position_with_player_with_capture_from_array_for_sizes {
    ( $($n : expr),* ) => {
        $(
            impl From<(Player, Stones, Stones, [Stones; $n])> for Position {
                fn from(data: (Player, Stones, Stones, [Stones; $n])) -> Self {
                    Position {
                        player: data.0,
                        size: $n/2,
                        capture: [data.1, data.2],
                        bowls: data.3.to_vec(),
                    }
                }
            }
        )*
    }
}

position_from_array_for_sizes!(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32);
position_with_player_from_array_for_sizes!(
    2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32
);
position_with_capture_from_array_for_sizes!(
    2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32
);
position_with_player_with_capture_from_array_for_sizes!(
    2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32
);