|
| 1 | +use std::io::Write; |
| 2 | +use std::isize; |
| 3 | +use std::process::exit; |
| 4 | + |
| 5 | +use crate::mcts::MCTS; |
| 6 | +use crate::othello::{caculate_win, print_state, Action, Color, Position, State}; |
| 7 | + |
| 8 | +enum GameCommand { |
| 9 | + SKIP, |
| 10 | + QUIT, |
| 11 | + INVALID, |
| 12 | + MOVE(usize, usize), |
| 13 | +} |
| 14 | + |
| 15 | +pub fn console_game() { |
| 16 | + let mut win_balance: isize = 0; |
| 17 | + let a = 1.0; |
| 18 | + println!("Game mode: player vs AI\n"); |
| 19 | + let mut state = State::new(); |
| 20 | + let mut mcts = MCTS::new("true", a); |
| 21 | + _ = std::io::stdout().flush(); |
| 22 | + let mut ai_iterations = 20000; |
| 23 | + loop { |
| 24 | + print_state(state); |
| 25 | + state = player_turn(state.clone()); |
| 26 | + if state.remaining_moves == 0 { |
| 27 | + break; |
| 28 | + } |
| 29 | + print_state(state); |
| 30 | + state = ai_turn(&mut mcts, state.clone(), ai_iterations); |
| 31 | + ai_iterations += ai_iterations / 100; |
| 32 | + |
| 33 | + if state.remaining_moves == 0 { |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + //print_state(state); |
| 38 | + win_balance += match caculate_win(state) { |
| 39 | + Some(Color::WHITE) => { |
| 40 | + println!("White wins!"); |
| 41 | + 1 |
| 42 | + } |
| 43 | + Some(Color::BLACK) => { |
| 44 | + println!("Black wins!"); |
| 45 | + -1 |
| 46 | + } |
| 47 | + None => { |
| 48 | + println!("Draw."); |
| 49 | + 0 |
| 50 | + } |
| 51 | + }; |
| 52 | + //println!("\nGAME OVER\n"); |
| 53 | + println!("\nResult: {win_balance}") |
| 54 | +} |
| 55 | + |
| 56 | +fn ai_turn(mcts: &mut MCTS, state: State, iterations: usize) -> State { |
| 57 | + let dev_null = |_a: usize, _b: usize, _c: &Color| -> () { /*println!("Progress: {a}/{b}")*/ }; |
| 58 | + let action = mcts.search(state.clone(), iterations, dev_null); |
| 59 | + if action.is_ok() { |
| 60 | + println!("{:?}", action.clone().unwrap().position); |
| 61 | + state.clone().do_action(Some(action.unwrap().clone())) |
| 62 | + } else { |
| 63 | + state.clone().do_action(None) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn player_turn(state: State) -> State { |
| 68 | + let mut player_choice; |
| 69 | + loop { |
| 70 | + print!("Enter coordinates for desired move: "); |
| 71 | + let _ = std::io::stdout().flush(); |
| 72 | + let cmd = read_command(); |
| 73 | + match cmd { |
| 74 | + GameCommand::QUIT => exit(0), |
| 75 | + GameCommand::INVALID => { |
| 76 | + println!("Please provide a valid command 'quit' 'skip' or 'x,y'") |
| 77 | + } |
| 78 | + GameCommand::SKIP => { |
| 79 | + player_choice = None; |
| 80 | + break; |
| 81 | + } |
| 82 | + GameCommand::MOVE(x_index, y_index) => { |
| 83 | + player_choice = Some(Action { |
| 84 | + color: Color::BLACK, |
| 85 | + position: Position { |
| 86 | + x: x_index, |
| 87 | + y: y_index, |
| 88 | + }, |
| 89 | + }); |
| 90 | + if state |
| 91 | + .get_actions() |
| 92 | + .contains(&player_choice.clone().unwrap()) |
| 93 | + { |
| 94 | + break; |
| 95 | + } else { |
| 96 | + println!("Invalid move."); |
| 97 | + let pos: Vec<(usize, usize)> = state |
| 98 | + .get_actions() |
| 99 | + .iter() |
| 100 | + .map(|a| (a.position.y, a.position.x)) |
| 101 | + .collect(); |
| 102 | + println!("Valid moves: {:?}", pos); |
| 103 | + print_state(state); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + state.clone().do_action(player_choice) |
| 109 | +} |
| 110 | + |
| 111 | +fn read_command() -> GameCommand { |
| 112 | + let mut buf = String::new(); |
| 113 | + let _ = std::io::stdin().read_line(&mut buf); |
| 114 | + match buf.to_lowercase().as_str().trim() { |
| 115 | + "quit" => GameCommand::QUIT, |
| 116 | + "skip" => GameCommand::SKIP, |
| 117 | + line => { |
| 118 | + let cmd: Vec<&str> = line.trim().split(",").clone().collect(); |
| 119 | + match (cmd.get(0), cmd.get(1)) { |
| 120 | + (Some(cmd_1), Some(cmd_2)) => { |
| 121 | + match (cmd_1.parse::<usize>(), cmd_2.parse::<usize>()) { |
| 122 | + (Ok(y_index), Ok(x_index)) => GameCommand::MOVE(x_index, y_index), |
| 123 | + _ => GameCommand::INVALID, |
| 124 | + } |
| 125 | + } |
| 126 | + _ => GameCommand::INVALID, |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments