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
//! Provides functionality for the [Game] struct

use crate::{Board, EndState, Game, Player};

impl Game {
    /// Allows the user to decide who starts the game.
    fn select_start_player() -> Player {
        println!("Who would like to start?");
        println!("Press O or 1 for Player O");
        println!("Press X or 2 for Player X");
        loop {
            let mut play_again_buffer = String::new();
            std::io::stdin().read_line(&mut play_again_buffer).unwrap();
            match play_again_buffer.trim() {
                "O" | "o" | "1" => return Player::O,
                "X" | "x" | "2" => return Player::X,
                _ => {
                    println!("Sorry, please provide a valid selection.");
                    continue;
                }
            }
        }
    }
}

impl Game {
    /// Creates a new [Game]. At game start a player is randomly chosen.
    pub(crate) fn new() -> Game {
        Game {
            board: Board::new(),
            current_player: Game::select_start_player(),
        }
    }

    /// Resets the game.
    pub(crate) fn reset(&mut self) {
        self.board = Board::new();
        self.current_player = Game::select_start_player();
    }

    /// Handles the end state of the game.
    pub(crate) fn on_end(&mut self) -> EndState {
        if !self.board.has_player_won() && !self.board.is_every_cell_set() {
            return EndState::Continue;
        }

        if self.board.has_player_won() {
            println!("{} has won!", self.current_player);
        } else {
            println!("Draw!");
        }
        println!("Would you like to play again?");
        println!("Press Y for Yes, N for No.");

        loop {
            let mut play_again_buffer = String::new();
            std::io::stdin().read_line(&mut play_again_buffer).unwrap();
            match play_again_buffer.trim() {
                "n" | "N" => return EndState::End,
                "y" | "Y" => {
                    self.reset();
                    return EndState::Replay;
                }
                _ => {
                    println!("Please enter a valid argument.");
                    continue;
                }
            }
        }
    }
}