Skip to main content

threes_simulator/
lib.rs

1pub mod board_state;
2pub mod draw_pile;
3pub mod game_state;
4
5use crossterm::event::{read, Event, KeyCode};
6
7use rng_util::RngType;
8
9use crate::game_state::{Direction, GameState};
10
11pub fn play(rng: &mut RngType) {
12    println!("Press q to quit");
13    println!("Use arrow keys to shift the board");
14
15    let mut game = GameState::initialize(rng);
16
17    println!("{game}");
18    loop {
19        println!("");
20        crossterm::terminal::enable_raw_mode().unwrap();
21        if let Event::Key(event) = read().unwrap() {
22            crossterm::terminal::disable_raw_mode().unwrap();
23            if let Some(dir) = match event.code {
24                KeyCode::Up => Some(Direction::Up),
25                KeyCode::Right => Some(Direction::Right),
26                KeyCode::Down => Some(Direction::Down),
27                KeyCode::Left => Some(Direction::Left),
28                KeyCode::Char('q') => break, // quit on 'q'
29                _ => None,
30            } {
31                println!("You pressed {dir}");
32                if let Some(new_game) = game.shift(dir, true, rng) {
33                    game = new_game;
34                    println!("{game}");
35                } else {
36                    println!("Impossible");
37                }
38            } else {
39                println!("Key not understood. (Press q to quit)");
40            }
41        }
42    }
43    crossterm::terminal::disable_raw_mode().unwrap();
44}