From d5f1ae56c1fd8819728422f74ee234b62e05444a Mon Sep 17 00:00:00 2001 From: Leonhard Kost Date: Sun, 11 Jan 2026 00:34:57 +0100 Subject: [PATCH] feat(game): start prototyping game loop --- src/main.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..07eae8e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,90 @@ +use std::{ + error::Error, + fs, + io::{self, Stdin}, +}; + +use mankalla_rl::{ + mankalla::MankallaGame, + q_learning::{Deserialize, Environment, EpsilonGreedyPolicy, Policy, QLearning, Serialize}, +}; + +fn main() -> Result<(), Box> { + let mut policy = match fs::read_to_string("policy.csv") { + Ok(s) => EpsilonGreedyPolicy::::deserialize(s.as_str())?, + Err(_) => EpsilonGreedyPolicy::::new(0.2, 1., 1., 0.1, -0.01), + }; + + // QLearning::train(&mut policy, 1000, None); + + game_loop(&mut policy); + + fs::write("policy.csv", policy.serialize())?; + + Ok(()) +} + +fn game_loop(policy: &mut impl Policy) { + let mut turn: usize = 1; + let state = MankallaGame::new(); + + 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}"); + + 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 { + let mut input = String::new(); + stdin + .read_line(&mut input) + .expect("Something with stdin went wrong"); + + match input.as_str() { + digit @ ("0" | "1" | "2" | "3" | "4" | "5") => { + PlayerRequest::Action(digit.parse().expect("Guaranteed to work")) + } + "q" => PlayerRequest::Quit, + "" => PlayerRequest::Noop, + _ => PlayerRequest::InvalidRequest, + } +}