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
//! Solve any Two-player Minimax game using the
//! Minimax algorithm with Alpha-Beta pruning.
//! Also, where possible, a parallel processing
//! implementation is provided.

/// Contains sruct and necessary implementations
/// for `TicTacToe`: a popular two-player game
/// where one player places a symbol - 'X' and another
/// player places a symbol - 'O' on a square grid
/// with the objective of creating a streak of same
/// symbols of length the size of the grid in any direction.
///
/// # Examples
///
/// ```
/// use minimax_alpha_beta::tictactoe::TicTacToe;
/// let mut tic_tac_toe = TicTacToe::create_game(3, None, None, None);
/// tic_tac_toe.print_board();
/// assert_eq!(tic_tac_toe.size, 3);
/// assert_eq!(tic_tac_toe.default_char, '-');
/// ```
mod tests;

/// Contains the concrete implementation of
/// a TicTacToe game.
pub mod tictactoe {

    /// Contains basic data about a
    /// TicTacToe game.
    pub struct TicTacToe {
        pub board: Vec<char>,
        pub size: usize,
        pub default_char: char,
        pub maximizer: char,
        pub minimizer: char,
    }

    /// Implements all necessary
    /// methods to operate a TicTacToe
    /// game.
    impl TicTacToe {
        /// Create a new game of TicTacToe
        /// with a fresh board.
        pub fn create_game(
            size: usize,
            default_char: Option<char>,
            maximizer: Option<char>,
            minimizer: Option<char>,
        ) -> TicTacToe {
            let board: Vec<char> = vec![default_char.unwrap_or('-'); (size * size) as usize];

            TicTacToe {
                size,
                board,
                maximizer: maximizer.unwrap_or('o'),
                minimizer: minimizer.unwrap_or('x'),
                default_char: default_char.unwrap_or('-'),
            }
        }

        /// Pretty print a TicTacToe board
        /// for visualizing a game state.
        pub fn print_board(&self) {
            for idx in 0..self.size {
                let start = self.size * idx;
                let end = self.size * (idx + 1);
                let sub: &[char] = &self.board[start as usize..end as usize];

                for &x in sub.iter() {
                    print!("{}", x);
                }
                println!()
            }
        }

        /// Check the main and anti-diagonals
        /// for a winner.
        pub fn check_diagonals(&self) -> char {
            let mut winner = self.default_char;
            if self.check_diagonal(self.maximizer, true)
                || self.check_diagonal(self.maximizer, false)
            {
                winner = self.maximizer
            } else if self.check_diagonal(self.minimizer, true)
                || self.check_diagonal(self.minimizer, false)
            {
                winner = self.minimizer
            }
            winner
        }

        /// Check the rows of the grid for a winner.
        pub fn check_rows(&self) -> char {
            let mut winner = self.default_char;

            for row in 0..self.size as usize {
                if self.check_row(self.maximizer, row) {
                    winner = self.maximizer;
                    break;
                } else if self.check_row(self.minimizer, row) {
                    winner = self.minimizer;
                    break;
                }
            }
            winner
        }

        /// Check the columns of the grid for a winner.
        pub fn check_cols(&self) -> char {
            let mut winner = self.default_char;

            for col in 0..self.size as usize {
                if self.check_col(self.maximizer, col) {
                    winner = self.maximizer;
                    break;
                } else if self.check_col(self.minimizer, col) {
                    winner = self.minimizer;
                    break;
                }
            }
            winner
        }

        /// Check a given column if a given player has won.
        fn check_col(&self, ch: char, col_num: usize) -> bool {
            for row in 0..self.size as usize {
                if self.board[self.size as usize * row + col_num] != ch {
                    return false;
                }
            }
            true
        }

        /// Check a given row if a given player has won.
        fn check_row(&self, ch: char, row_num: usize) -> bool {
            for col in 0..self.size as usize {
                if self.board[self.size as usize * row_num + col] != ch {
                    return false;
                }
            }
            true
        }

        /// Check the main and anti diagonals if a
        /// given player has won.
        fn check_diagonal(&self, ch: char, diag: bool) -> bool {
            // main diagonal is represented by true.
            if diag {
                for idx in 0..self.size as usize {
                    if self.board[(self.size as usize * idx as usize) + idx] != ch {
                        return false;
                    }
                }
                true
            } else {
                for idx in 0..self.size as usize {
                    if self.board
                        [(self.size as usize * (self.size as usize - 1 - idx as usize)) + idx]
                        != ch
                    {
                        return false;
                    }
                }
                true
            }
        }
    }
}

/// Contains the necessary behaviours for
/// two-player Minimax games.
pub mod strategy {

    /// Any two-player Minimax game must
    /// have this behavior. In other words,
    /// these functions should yield meaningful outputs
    /// for any two-player games.
    pub trait Strategy {
        type Player;
        type Move;
        type Board;

        /// Ability to statically evaluate the current game state.
        fn evaluate(&self) -> f64;
        /// Identify a winner, if exists.
        fn get_winner(&self) -> Self::Player;
        /// Identify if the game is tied.
        fn is_game_tied(&self) -> bool;
        /// Identify if the game is in a completed state.
        fn is_game_complete(&self) -> bool;
        /// Ability to produce a collection of playable legal moves
        /// in the current position.
        fn get_available_moves(&self) -> Vec<Self::Move>;
        /// Modify the game state by playing a given move.
        fn play(&mut self, mv: &Self::Move, maximizer: bool);
        /// Modify the game state by resetting a given move.
        fn clear(&mut self, mv: &Self::Move);
        /// Get the current state of the board.
        fn get_board(&self) -> &Self::Board;
        /// Determine if a given move is valid.
        fn is_a_valid_move(&self, mv: &Self::Move) -> bool;
        /// Ability to produce a sentinel (not-playable) move.
        fn get_a_sentinel_move(&self) -> Self::Move;
    }

    /// The behaviour required of any
    /// minimax game engine.
    pub trait AlphaBetaMiniMaxStrategy: Strategy {
        /// The ability to get the best move
        /// in the current state and for the
        /// current player.
        fn get_best_move(
            &mut self,
            max_depth: i64,
            is_maximizing: bool,
        ) -> <Self as Strategy>::Move;

        /// The ability to produce a best (good enough, sometimes)
        /// evaluation score possible over all
        /// possible moves at the current game state.
        fn minimax_score(
            &mut self,
            depth: i64,
            is_maximizing: bool,
            alpha: f64,
            beta: f64,
            max_depth: i64,
        ) -> f64;
    }
}

/// Contains the concrete implementations
/// for the game-playing strategic behaviour
/// of TicTacToe.
pub mod games {

    use crate::strategy::*;
    use crate::tictactoe::*;

    /// Endow upon TicTacToe the ability to
    /// play games.
    impl Strategy for TicTacToe {
        /// The Player is a char.
        /// Usually one of 'o', 'O', 'x', 'X', '-'.
        type Player = char;

        /// The Move is a single number representing an
        /// index of the Board vector, i.e. in range
        /// `[0, (size * size) - 1]`.
        type Move = usize;

        /// The Board is a single vector of length `size * size`.
        type Board = Vec<char>;

        fn evaluate(&self) -> f64 {
            if self.is_game_tied() {
                0.
            } else {
                let _winner = self.get_winner();
                if _winner == self.maximizer {
                    1000.
                } else {
                    -1000.
                }
            }
        }

        fn get_winner(&self) -> Self::Player {
            let mut winner = self.check_diagonals();

            if winner == self.default_char {
                winner = self.check_rows();
            }
            if winner == self.default_char {
                winner = self.check_cols();
            }
            winner
        }

        fn is_game_tied(&self) -> bool {
            let _winner = self.get_winner();

            _winner == self.default_char && self.get_available_moves().is_empty()
        }

        fn is_game_complete(&self) -> bool {
            let _winner = self.get_winner();

            self.get_available_moves().is_empty() || _winner != '-'
        }

        fn get_available_moves(&self) -> Vec<Self::Move> {
            let mut moves: Vec<usize> = vec![];
            for idx in 0..(self.size * self.size) as usize {
                if self.board[idx] == '-' {
                    moves.push(idx)
                }
            }
            moves
        }

        fn play(&mut self, &mv: &Self::Move, maximizer: bool) {
            // player: true means the maximizer's turn.

            if maximizer {
                self.board[mv] = self.maximizer;
            } else {
                self.board[mv] = self.minimizer;
            }
        }

        fn clear(&mut self, &mv: &Self::Move) {
            self.board[mv] = self.default_char
        }

        fn get_board(&self) -> &Self::Board {
            &self.board
        }

        fn is_a_valid_move(&self, &mv: &Self::Move) -> bool {
            self.board[mv] == self.default_char
        }

        fn get_a_sentinel_move(&self) -> Self::Move {
            self.size * self.size + 1
        }
    }

    pub const INF: f64 = f64::INFINITY;
    pub const NEG_INF: f64 = f64::NEG_INFINITY;

    /// Endow upon anything the ability to
    /// use the AlphaBetaMiniMaxStrategy implementation
    /// of the game engine as long as it understands
    /// how to behave as Strategy.
    impl<T: Strategy> AlphaBetaMiniMaxStrategy for T {
        fn get_best_move(
            &mut self,
            max_depth: i64,
            is_maximizing: bool,
        ) -> <Self as Strategy>::Move {
            let mut best_move: <Self as Strategy>::Move = self.get_a_sentinel_move();

            if self.is_game_complete() {
                return best_move;
            }

            let alpha = NEG_INF;
            let beta = INF;

            if is_maximizing {
                let mut best_move_val: f64 = INF;

                for mv in self.get_available_moves() {
                    self.play(&mv, false);
                    let value = self.minimax_score(max_depth, true, alpha, beta, max_depth);
                    self.clear(&mv);
                    if value <= best_move_val {
                        best_move_val = value;
                        best_move = mv;
                    }
                }

                best_move
            } else {
                let mut best_move_val: f64 = NEG_INF;

                for mv in self.get_available_moves() {
                    self.play(&mv, false);
                    let value = self.minimax_score(max_depth, false, alpha, beta, max_depth);
                    self.clear(&mv);
                    if value >= best_move_val {
                        best_move_val = value;
                        best_move = mv;
                    }
                }
                best_move
            }
        }

        fn minimax_score(
            &mut self,
            depth: i64,
            is_maximizing: bool,
            mut alpha: f64,
            mut beta: f64,
            max_depth: i64,
        ) -> f64 {
            let avail: Vec<<T as Strategy>::Move> = self.get_available_moves();
            if depth == 0 || self.is_game_complete() || avail.is_empty() {
                return self.evaluate();
            }

            if is_maximizing {
                let mut value = NEG_INF;
                for idx in avail {
                    self.play(&idx, true);
                    let score = self.minimax_score(depth - 1, false, alpha, beta, max_depth);
                    if score >= value {
                        value = score;
                    }
                    if score >= alpha {
                        alpha = score;
                    }
                    self.clear(&idx);
                    if beta <= alpha {
                        break;
                    }
                }
                if value != 0. {
                    return value - (max_depth - depth) as f64;
                }
                value
            } else {
                let mut value = INF;
                for idx in avail {
                    self.play(&idx, false);
                    let score = self.minimax_score(depth - 1, true, alpha, beta, max_depth);
                    if score <= value {
                        value = score;
                    }
                    if score <= beta {
                        beta = score;
                    }
                    self.clear(&idx);
                    if beta <= alpha {
                        break;
                    }
                }

                if value != 0. {
                    return value + (max_depth - depth) as f64;
                }
                value
            }
        }
    }
}