feat(game): finish prototype game loop

This commit is contained in:
leonhard committed 2026-01-11 16:52:33 +01:00
1 parent d5f1ae56c1
commit a61fac18ed
3 files changed
+76 -34

No files matched your search

+66 -28
View File
@@ -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<dyn Error>> {
fn game_loop(policy: &mut impl Policy<MankallaGame>) {
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;
};
(state, finished) = player_turn(state, action, policy, &mut turn);
while !finished {
match state.get_player_to_move() {
Player::Player2 => {
(state, finished) = bot_turn(state, policy, &mut turn);
}
PlayerRequest::InvalidRequest => {
println!("That is not something you can do");
continue;
Player::Player1 => {
let action;
match get_player_input(&stdin) {
PlayerRequest::Action(a) => {
action = a;
}
PlayerRequest::Quit => {
println!("Ok, goodbye");
return;
}
};
(state, finished) = player_turn(state, action, policy, &mut turn);
}
}
println!("Turn {turn}, you chose {action}");
let (next_state, reward, finished) = MankallaGame::step(&state, &action);
while !finished {
// TODO: Bot turn
turn += 1;
// TODO: Player turn
}
}
enum PlayerRequest {
Action(u8),
Quit,
Noop,
InvalidRequest,
}
fn get_player_input(stdin: &Stdin) -> PlayerRequest {
println!("Choose your action: (0,1,2,3,4,5,q)");
let mut input = String::new();
loop {
stdin
.read_line(&mut input)
.expect("Something with stdin went wrong");
match input.as_str() {
match input.as_str().strip_suffix("\n").unwrap_or("") {
digit @ ("0" | "1" | "2" | "3" | "4" | "5") => {
PlayerRequest::Action(digit.parse().expect("Guaranteed to work"))
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<MankallaGame>,
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<MankallaGame>,
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)
}
+4
View File
@@ -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],
+6 -6
View File
@@ -18,7 +18,7 @@ pub trait Policy<E: Environment> {
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<E: Environment> Policy<E> for GreedyPolicy<E> {
}
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<E: Environment> Policy<E> for GreedyPolicy<E> {
false => 0f32,
};
self.qtable.insert(
(*state, action),
(state, action),
former_value + self.learning_rate * (target - former_value),
);
}
@@ -271,7 +271,7 @@ impl<E: Environment> Policy<E> for EpsilonGreedyPolicy<E> {
fn improve(
&mut self,
state: &E::ActionRelevantState,
state: E::ActionRelevantState,
action: E::Action,
reward: f32,
next_state: E::State,