summaryrefslogtreecommitdiff
path: root/tic_tac_toe/src/main.rs
blob: 5bdb7aad932ea93e5539510f62bd9921e0af585a (plain)
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
mod ai;
mod board;
mod console_helpers;

use ai::get_best_move;
use board::{Board, Coord, GameState, Tile, init_board, parse_coord};
use console_helpers::{clear_scrn, write_header};

use std::io;

fn render_board(board: &Board) -> () {
    println!("+-1--2--3-+");
    let mut y = 1;
    for row_i in 0..3 {
        let row: Vec<Tile> = (0..3).map(|col_i| board.state[col_i][row_i]).collect();
        print!("{y}");
        for tile in row {
            print!(" {tile} ")
        }
        println!("|\n|         |");
        y += 1;
    }
    println!("+---------+");
}

fn get_coord_input() -> Coord {
    loop {
        let mut entered = String::new();
        match io::stdin().read_line(&mut entered) {
            Ok(_) => {}
            Err(_) => {
                println!("Couldn't read input!");
                continue;
            }
        };

        let coord = match parse_coord(&entered) {
            Ok(c) => c,
            Err(e) => {
                println!("Couldn't parse input '{entered}'! ({e})");
                continue;
            }
        };

        return coord;
    }
}

fn check_state(board: &Board) -> GameState {
    //check row
    let mut has_space = false;
    for row_i in 0..3 {
        let row: Vec<Tile> = (0..3)
            .map(|col_i| {
                if !has_space && board.state[col_i][row_i] == Tile::Unowned {
                    has_space = true
                }
                return board.state[col_i][row_i];
            })
            .collect();
        if row[0] != Tile::Unowned && row[0] == row[1] && row[1] == row[2] {
            return match row[0] {
                Tile::PlayerOne => GameState::PlayerOneWin,
                Tile::PlayerTwo => GameState::PlayerTwoWin,
                Tile::Unowned => panic!("Impossible state"),
            };
        }
    }

    //check draw
    if !has_space {
        return GameState::Draw;
    }

    //check col
    for col in board.state {
        if col[0] != Tile::Unowned && col[0] == col[1] && col[1] == col[2] {
            return match col[0] {
                Tile::PlayerOne => GameState::PlayerOneWin,
                Tile::PlayerTwo => GameState::PlayerTwoWin,
                Tile::Unowned => panic!("Impossible state"),
            };
        }
    }

    //check diagonal
    if board.state[0][0] != Tile::Unowned
        && board.state[0][0] == board.state[1][1]
        && board.state[1][1] == board.state[2][2]
    {
        return match board.state[0][0] {
            Tile::PlayerOne => GameState::PlayerOneWin,
            Tile::PlayerTwo => GameState::PlayerTwoWin,
            Tile::Unowned => panic!("Impossible state"),
        };
    }

    if board.state[2][0] != Tile::Unowned
        && board.state[2][0] == board.state[1][1]
        && board.state[1][1] == board.state[0][2]
    {
        return match board.state[2][0] {
            Tile::PlayerOne => GameState::PlayerOneWin,
            Tile::PlayerTwo => GameState::PlayerTwoWin,
            Tile::Unowned => panic!("Impossible state"),
        };
    }

    //no wincon
    return GameState::InProgress;
}

fn main() {
    let mut board = init_board();
    loop {
        // clear_scrn();
        write_header("Current state");
        render_board(&board);

        println!("Enter a move (e.g., 1,2) >");
        let coord = get_coord_input();
        board.set_at_coord(coord, Tile::PlayerOne);

        match check_state(&board) {
            GameState::InProgress => (),
            GameState::Draw => {
                println!("Draw!");
                break;
            }
            GameState::PlayerOneWin => {
                println!("Player one wins!");
                break;
            }
            GameState::PlayerTwoWin => {
                println!("Player two wins!");
                break;
            }
        }

        clear_scrn();

        //get AI move
        let ai_move = get_best_move(&board, Tile::PlayerTwo, true);
        board.set_at_coord(ai_move.coord, Tile::PlayerTwo);

        match check_state(&board) {
            GameState::InProgress => (),
            GameState::Draw => {
                println!("Draw!");
                break;
            }
            GameState::PlayerOneWin => {
                println!("Player one wins!");
                break;
            }
            GameState::PlayerTwoWin => {
                println!("Player two wins!");
                break;
            }
        }
    }

    render_board(&board);
    println!("Game over!")
}