minimax_alpha_beta/drivers/
tic_tac_toe.rs1use crate::games::TicTacToe;
2use crate::strategy::alpha_beta_minimax::AlphaBetaMiniMaxStrategy;
3use crate::strategy::game_strategy::GameStrategy;
4
5fn get_input() -> String {
7 let mut buffer = String::new();
8 std::io::stdin().read_line(&mut buffer).expect("Failed");
9 buffer
10}
11
12pub fn play_tic_tac_toe_against_computer(size: usize) {
16 play_tic_tac_toe_against_computer_with_depth(size, 6)
17}
18
19pub fn play_tic_tac_toe_against_computer_with_depth(size: usize, depth: i64) {
23 let mut ttt = TicTacToe::new(size);
24 loop {
25 println!("Board:\n{}", ttt);
26 println!("\n");
27
28 if ttt.is_game_complete() {
29 println!("Game is complete.");
30 if ttt.is_game_tied() {
31 println!("Game Tied!");
32 break;
33 } else {
34 println!("{} wins!", ttt.get_winner().unwrap());
35 break;
36 }
37 }
38
39 let example_num: i64 = 7;
40 println!(
41 "Enter a move. (e.g. '{}' represents (row: {}, col: {}) : ",
42 example_num,
43 example_num as usize / size,
44 example_num as usize % size
45 );
46 let s = get_input().trim().parse::<usize>();
47 let n = s.unwrap_or(usize::MAX);
48
49 if n == usize::MAX {
50 break;
51 }
52 println!(
53 "Move played by you: {} (i.e. {}, {})",
54 n,
55 n / size,
56 n % size
57 );
58 ttt.play(&n, true);
59 let move_found = ttt.get_best_move(depth as i64, true);
60 if move_found > (ttt.size * ttt.size) {
61 println!("Game is complete.");
62 if ttt.is_game_tied() {
63 println!("Game Tied!");
64 break;
65 } else {
66 println!("{} wins!", ttt.get_winner().unwrap());
67 break;
68 }
69 }
70 println!(
71 "Move played by AI: {} (i.e. {}, {})",
72 move_found,
73 move_found / size,
74 move_found % size
75 );
76 ttt.play(&move_found, false);
77 }
78}