feat(game): finish prototype game loop
This commit is contained in:
1 parent
d5f1ae56c1
commit
a61fac18ed
3 files changed
+76
-34
No files matched your search
+66
-28
@@ -5,7 +5,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use mankalla_rl::{
|
use mankalla_rl::{
|
||||||
mankalla::MankallaGame,
|
mankalla::{MankallaGame, MankallaGameState, Player},
|
||||||
q_learning::{Deserialize, Environment, EpsilonGreedyPolicy, Policy, QLearning, Serialize},
|
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>) {
|
fn game_loop(policy: &mut impl Policy<MankallaGame>) {
|
||||||
let mut turn: usize = 1;
|
let mut turn: usize = 1;
|
||||||
let state = MankallaGame::new();
|
let mut state = MankallaGame::new();
|
||||||
|
let mut finished = false;
|
||||||
|
|
||||||
println!("{}", state);
|
println!("{}", state);
|
||||||
|
|
||||||
println!("Choose your action: (0,1,2,3,4,5,q)");
|
|
||||||
|
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
|
|
||||||
let action;
|
let action;
|
||||||
loop {
|
|
||||||
match get_player_input(&stdin) {
|
match get_player_input(&stdin) {
|
||||||
PlayerRequest::Action(a) => {
|
PlayerRequest::Action(a) => {
|
||||||
action = a;
|
action = a;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
PlayerRequest::Quit => {
|
PlayerRequest::Quit => {
|
||||||
println!("Ok, goodbye");
|
println!("Ok, goodbye");
|
||||||
return;
|
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 => {
|
Player::Player1 => {
|
||||||
println!("That is not something you can do");
|
let action;
|
||||||
continue;
|
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 {
|
enum PlayerRequest {
|
||||||
Action(u8),
|
Action(u8),
|
||||||
Quit,
|
Quit,
|
||||||
Noop,
|
|
||||||
InvalidRequest,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_player_input(stdin: &Stdin) -> PlayerRequest {
|
fn get_player_input(stdin: &Stdin) -> PlayerRequest {
|
||||||
|
println!("Choose your action: (0,1,2,3,4,5,q)");
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
|
loop {
|
||||||
stdin
|
stdin
|
||||||
.read_line(&mut input)
|
.read_line(&mut input)
|
||||||
.expect("Something with stdin went wrong");
|
.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") => {
|
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)
|
||||||
|
}
|
||||||
@@ -181,6 +181,10 @@ impl Default for MankallaGameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MankallaGameState {
|
impl MankallaGameState {
|
||||||
|
pub fn get_player_to_move(&self) -> Player {
|
||||||
|
self.player_to_move
|
||||||
|
}
|
||||||
|
|
||||||
fn get_points(&self, player: &Player) -> u8 {
|
fn get_points(&self, player: &Player) -> u8 {
|
||||||
match player {
|
match player {
|
||||||
Player::Player1 => self.fields[6],
|
Player::Player1 => self.fields[6],
|
||||||
|
|||||||
+6
-6
@@ -18,7 +18,7 @@ pub trait Policy<E: Environment> {
|
|||||||
fn choose_action(&self, state: E::ActionRelevantState) -> E::Action;
|
fn choose_action(&self, state: E::ActionRelevantState) -> E::Action;
|
||||||
fn improve(
|
fn improve(
|
||||||
&mut self,
|
&mut self,
|
||||||
state: &E::ActionRelevantState,
|
state: E::ActionRelevantState,
|
||||||
action: E::Action,
|
action: E::Action,
|
||||||
reward: f32,
|
reward: f32,
|
||||||
next_state: E::State,
|
next_state: E::State,
|
||||||
@@ -93,7 +93,7 @@ impl QLearning {
|
|||||||
let action = policy.choose_action(state.into());
|
let action = policy.choose_action(state.into());
|
||||||
|
|
||||||
let (next_state, reward, finished) = E::step(&state, &action);
|
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)
|
(next_state, finished)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,13 +130,13 @@ impl<E: Environment> Policy<E> for GreedyPolicy<E> {
|
|||||||
}
|
}
|
||||||
fn improve(
|
fn improve(
|
||||||
&mut self,
|
&mut self,
|
||||||
state: &E::ActionRelevantState,
|
state: E::ActionRelevantState,
|
||||||
action: E::Action,
|
action: E::Action,
|
||||||
reward: f32,
|
reward: f32,
|
||||||
next_state: E::State,
|
next_state: E::State,
|
||||||
finished: bool,
|
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 {
|
let target = match finished {
|
||||||
true => {
|
true => {
|
||||||
reward
|
reward
|
||||||
@@ -149,7 +149,7 @@ impl<E: Environment> Policy<E> for GreedyPolicy<E> {
|
|||||||
false => 0f32,
|
false => 0f32,
|
||||||
};
|
};
|
||||||
self.qtable.insert(
|
self.qtable.insert(
|
||||||
(*state, action),
|
(state, action),
|
||||||
former_value + self.learning_rate * (target - former_value),
|
former_value + self.learning_rate * (target - former_value),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,7 @@ impl<E: Environment> Policy<E> for EpsilonGreedyPolicy<E> {
|
|||||||
|
|
||||||
fn improve(
|
fn improve(
|
||||||
&mut self,
|
&mut self,
|
||||||
state: &E::ActionRelevantState,
|
state: E::ActionRelevantState,
|
||||||
action: E::Action,
|
action: E::Action,
|
||||||
reward: f32,
|
reward: f32,
|
||||||
next_state: E::State,
|
next_state: E::State,
|
||||||
|
|||||||
Reference in new issue
Block a user