From a61fac18edf6c22477901e26c622d33d50e5a9a7 Mon Sep 17 00:00:00 2001 From: Leonhard Kost Date: Sun, 11 Jan 2026 16:52:33 +0100 Subject: [PATCH] feat(game): finish prototype game loop --- src/main.rs | 122 ++++++++++++++++++++++++++++++---------------- src/mankalla.rs | 4 ++ src/q_learning.rs | 12 ++--- 3 files changed, 90 insertions(+), 48 deletions(-) diff --git a/src/main.rs b/src/main.rs index 07eae8e..ae7319e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::{ }; use mankalla_rl::{ - mankalla::MankallaGame, + mankalla::{MankallaGame, MankallaGameState, Player}, q_learning::{Deserialize, Environment, EpsilonGreedyPolicy, Policy, QLearning, Serialize}, }; @@ -26,65 +26,103 @@ fn main() -> Result<(), Box> { fn game_loop(policy: &mut impl Policy) { let mut turn: usize = 1; - let state = MankallaGame::new(); + let mut state = MankallaGame::new(); + let mut finished = false; println!("{}", state); - println!("Choose your action: (0,1,2,3,4,5,q)"); - let stdin = io::stdin(); + let action; - loop { - match get_player_input(&stdin) { - PlayerRequest::Action(a) => { - action = a; - break; - } - PlayerRequest::Quit => { - println!("Ok, goodbye"); - return; - } - PlayerRequest::Noop => { - println!("Well you gotta do something"); - continue; - } - PlayerRequest::InvalidRequest => { - println!("That is not something you can do"); - continue; - } - }; - } - println!("Turn {turn}, you chose {action}"); + match get_player_input(&stdin) { + PlayerRequest::Action(a) => { + action = a; + } + PlayerRequest::Quit => { + println!("Ok, goodbye"); + return; + } + }; - let (next_state, reward, finished) = MankallaGame::step(&state, &action); + (state, finished) = player_turn(state, action, policy, &mut turn); while !finished { - // TODO: Bot turn + match state.get_player_to_move() { + Player::Player2 => { + (state, finished) = bot_turn(state, policy, &mut turn); + } + Player::Player1 => { + let action; + match get_player_input(&stdin) { + PlayerRequest::Action(a) => { + action = a; + } + PlayerRequest::Quit => { + println!("Ok, goodbye"); + return; + } + }; - turn += 1; - - // TODO: Player turn + (state, finished) = player_turn(state, action, policy, &mut turn); + } + } } } enum PlayerRequest { Action(u8), Quit, - Noop, - InvalidRequest, } fn get_player_input(stdin: &Stdin) -> PlayerRequest { - let mut input = String::new(); - stdin - .read_line(&mut input) - .expect("Something with stdin went wrong"); + println!("Choose your action: (0,1,2,3,4,5,q)"); - match input.as_str() { - digit @ ("0" | "1" | "2" | "3" | "4" | "5") => { - PlayerRequest::Action(digit.parse().expect("Guaranteed to work")) + let mut input = String::new(); + loop { + stdin + .read_line(&mut input) + .expect("Something with stdin went wrong"); + + match input.as_str().strip_suffix("\n").unwrap_or("") { + digit @ ("0" | "1" | "2" | "3" | "4" | "5") => { + return PlayerRequest::Action(digit.parse().expect("Guaranteed to work")); + } + "q" => return PlayerRequest::Quit, + _ => continue, } - "q" => PlayerRequest::Quit, - "" => PlayerRequest::Noop, - _ => PlayerRequest::InvalidRequest, } } + +fn player_turn( + state: MankallaGameState, + action: u8, + policy: &mut impl Policy, + turn: &mut usize, +) -> (MankallaGameState, bool) { + println!("Turn {turn}, you chose {action}"); + + let (next_state, reward, finished) = MankallaGame::step(&state, &action); + println!("{}", next_state); + policy.improve(state.into(), action, reward, next_state, finished); + + *turn += 1; + + (next_state, finished) +} + +fn bot_turn( + state: MankallaGameState, + policy: &mut impl Policy, + turn: &mut usize, +) -> (MankallaGameState, bool) { + let action = policy.choose_action(state.into()); + + println!("Turn {turn}, bot chose {action}"); + + let (next_state, reward, finished) = MankallaGame::step(&state, &action); + println!("{}", next_state); + policy.improve(state.into(), action, reward, next_state, finished); + + *turn += 1; + + (next_state, finished) +} diff --git a/src/mankalla.rs b/src/mankalla.rs index d755955..ff800c5 100644 --- a/src/mankalla.rs +++ b/src/mankalla.rs @@ -181,6 +181,10 @@ impl Default for MankallaGameState { } impl MankallaGameState { + pub fn get_player_to_move(&self) -> Player { + self.player_to_move + } + fn get_points(&self, player: &Player) -> u8 { match player { Player::Player1 => self.fields[6], diff --git a/src/q_learning.rs b/src/q_learning.rs index e43ed19..2ff4676 100644 --- a/src/q_learning.rs +++ b/src/q_learning.rs @@ -18,7 +18,7 @@ pub trait Policy { fn choose_action(&self, state: E::ActionRelevantState) -> E::Action; fn improve( &mut self, - state: &E::ActionRelevantState, + state: E::ActionRelevantState, action: E::Action, reward: f32, next_state: E::State, @@ -93,7 +93,7 @@ impl QLearning { let action = policy.choose_action(state.into()); let (next_state, reward, finished) = E::step(&state, &action); - policy.improve(&state.into(), action, reward, next_state, finished); + policy.improve(state.into(), action, reward, next_state, finished); (next_state, finished) } } @@ -130,13 +130,13 @@ impl Policy for GreedyPolicy { } fn improve( &mut self, - state: &E::ActionRelevantState, + state: E::ActionRelevantState, action: E::Action, reward: f32, next_state: E::State, finished: bool, ) { - let former_value = *self.qtable.get(&(*state, action)).unwrap_or(&0f32); + let former_value = *self.qtable.get(&(state, action)).unwrap_or(&0f32); let target = match finished { true => { reward @@ -149,7 +149,7 @@ impl Policy for GreedyPolicy { false => 0f32, }; self.qtable.insert( - (*state, action), + (state, action), former_value + self.learning_rate * (target - former_value), ); } @@ -271,7 +271,7 @@ impl Policy for EpsilonGreedyPolicy { fn improve( &mut self, - state: &E::ActionRelevantState, + state: E::ActionRelevantState, action: E::Action, reward: f32, next_state: E::State,