189 lines
5.8 KiB
Rust
189 lines
5.8 KiB
Rust
use actors::*;
|
|
use game::{Action, Controllable, Game, Tickable};
|
|
use graphics::standard_renderer;
|
|
use graphics::COLOR_BACKGROUND;
|
|
use log::{debug, info, trace};
|
|
use rand::SeedableRng;
|
|
use sdl2::event::Event;
|
|
use sdl2::keyboard::Keycode;
|
|
use simple_logger;
|
|
use std::time::Duration;
|
|
use tokio::time::interval;
|
|
|
|
mod actors;
|
|
mod game;
|
|
mod graphics;
|
|
mod playfield;
|
|
mod random;
|
|
mod srs;
|
|
mod tetromino;
|
|
|
|
const TICKS_PER_SECOND: usize = 60;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// simple_logger::init()?;
|
|
simple_logger::init_with_level(log::Level::Info)?;
|
|
|
|
let mut actor = qlearning::QLearningAgent::default();
|
|
let mut rng = rand::rngs::StdRng::seed_from_u64(1337);
|
|
|
|
let mut avg = 0.0;
|
|
|
|
for i in 0..100000 {
|
|
if i % 100 == 0 {
|
|
info!("Last 100 scores avg: {}", avg);
|
|
avg = 0.0;
|
|
}
|
|
let mut game = Game::default();
|
|
while (&game).is_game_over().is_none() {
|
|
let cur_state = (&game).into();
|
|
let cur_score = game.score();
|
|
let action = actor.get_action(&mut rng, &cur_state, &((&game).get_legal_actions()));
|
|
|
|
match action {
|
|
Action::Nothing => (),
|
|
Action::MoveLeft => game.move_left(),
|
|
Action::MoveRight => game.move_right(),
|
|
Action::SoftDrop => game.move_down(),
|
|
Action::HardDrop => game.hard_drop(),
|
|
Action::Hold => game.hold(),
|
|
Action::RotateLeft => game.rotate_left(),
|
|
Action::RotateRight => game.rotate_right(),
|
|
}
|
|
|
|
let new_state = (&game).into();
|
|
let reward = game.score() - cur_score;
|
|
actor.update(cur_state, action, new_state, reward as f64);
|
|
|
|
game.tick();
|
|
}
|
|
|
|
avg += game.score() as f64 / 100.0;
|
|
// info!("Game over with score of {}", game.score());
|
|
}
|
|
|
|
actor.exploration_prob = 0.0;
|
|
|
|
let sdl_context = sdl2::init()?;
|
|
let video_subsystem = sdl_context.video()?;
|
|
let window = video_subsystem
|
|
.window("retris", 800, 800)
|
|
.position_centered()
|
|
.build()?;
|
|
let mut canvas = window.into_canvas().build()?;
|
|
let mut event_pump = sdl_context.event_pump()?;
|
|
let mut game = Game::default();
|
|
let mut interval = interval(Duration::from_millis(1000 / TICKS_PER_SECOND as u64));
|
|
|
|
'running: loop {
|
|
match game.is_game_over() {
|
|
Some(e) => {
|
|
println!("Lost due to: {:?}", e);
|
|
break;
|
|
}
|
|
None => (),
|
|
}
|
|
|
|
let cur_state = (&game).into();
|
|
let action = actor.get_action(&mut rng, &cur_state, &((&game).get_legal_actions()));
|
|
match action {
|
|
Action::Nothing => (),
|
|
Action::MoveLeft => game.move_left(),
|
|
Action::MoveRight => game.move_right(),
|
|
Action::SoftDrop => game.move_down(),
|
|
Action::HardDrop => game.hard_drop(),
|
|
Action::Hold => game.hold(),
|
|
Action::RotateLeft => game.rotate_left(),
|
|
Action::RotateRight => game.rotate_right(),
|
|
}
|
|
|
|
for event in event_pump.poll_iter() {
|
|
match event {
|
|
Event::Quit { .. }
|
|
| Event::KeyDown {
|
|
keycode: Some(Keycode::Escape),
|
|
..
|
|
} => {
|
|
debug!("Escape registered");
|
|
break 'running;
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::Left),
|
|
..
|
|
} => {
|
|
debug!("Move left registered");
|
|
game.move_left();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::Right),
|
|
..
|
|
} => {
|
|
debug!("Move right registered");
|
|
game.move_right();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::Down),
|
|
..
|
|
} => {
|
|
debug!("Soft drop registered");
|
|
game.move_down();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::Z),
|
|
..
|
|
} => {
|
|
debug!("Rotate left registered");
|
|
game.rotate_left();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::X),
|
|
..
|
|
} => {
|
|
debug!("Rotate right registered");
|
|
game.rotate_right();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::Space),
|
|
..
|
|
}
|
|
| Event::KeyDown {
|
|
keycode: Some(Keycode::Up),
|
|
..
|
|
} => {
|
|
debug!("Hard drop registered");
|
|
game.hard_drop();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::LShift),
|
|
..
|
|
} => {
|
|
debug!("Hold registered");
|
|
game.hold();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(Keycode::R),
|
|
..
|
|
} => {
|
|
info!("Restarting game");
|
|
game = Game::default();
|
|
}
|
|
Event::KeyDown {
|
|
keycode: Some(e), ..
|
|
} => trace!("Ignoring keycode {}", e),
|
|
_ => (),
|
|
}
|
|
}
|
|
game.tick();
|
|
canvas.set_draw_color(COLOR_BACKGROUND);
|
|
canvas.clear();
|
|
standard_renderer::render(&mut canvas, &game);
|
|
canvas.present();
|
|
interval.tick().await;
|
|
}
|
|
|
|
dbg!(game);
|
|
|
|
Ok(())
|
|
}
|