added event on key press with crossterm
This commit is contained in:
@@ -6,5 +6,6 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
crossterm = "0.28.1"
|
||||||
left-pad = "1.0.1"
|
left-pad = "1.0.1"
|
||||||
rand = "0.9.0"
|
rand = "0.9.0"
|
||||||
|
|||||||
@@ -1,16 +1,94 @@
|
|||||||
mod models;
|
mod models;
|
||||||
use crate::models::taquin::*;
|
use crate::models::taquin::*;
|
||||||
|
use crossterm::event::{read, Event, KeyCode, KeyEvent};
|
||||||
|
use crossterm::terminal::enable_raw_mode;
|
||||||
use left_pad::leftpad;
|
use left_pad::leftpad;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let _ = enable_raw_mode();
|
||||||
let mut taquin_game: TaquinGame = TaquinGame::new();
|
let mut taquin_game: TaquinGame = TaquinGame::new();
|
||||||
|
let mut playing: bool = true;
|
||||||
|
|
||||||
taquin_game.init_grid();
|
taquin_game.init_grid();
|
||||||
draw_game(taquin_game);
|
draw_game(&taquin_game);
|
||||||
|
|
||||||
|
// enable_raw_mode().expect("Could not enable raw mode");
|
||||||
|
while playing {
|
||||||
|
let mut message = "";
|
||||||
|
let mut user_cheated: bool = false;
|
||||||
|
match read().unwrap() {
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Char('q'),
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
playing = false;
|
||||||
|
message = "Bye bye!";
|
||||||
|
}
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Char('r'),
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
taquin_game.init_grid();
|
||||||
|
user_cheated = false;
|
||||||
|
message = "New game started";
|
||||||
|
}
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Char('s'),
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
taquin_game.resolve();
|
||||||
|
user_cheated = true;
|
||||||
|
message = "Too hard?";
|
||||||
|
}
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Up, ..
|
||||||
|
}) => {
|
||||||
|
if !taquin_game.move_to(Directions::Up) {
|
||||||
|
message = "Cannot move any more up";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Down,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if !taquin_game.move_to(Directions::Down) {
|
||||||
|
message = "Cannot move any more down";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Left,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if !taquin_game.move_to(Directions::Left) {
|
||||||
|
message = "Cannot move any more left";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Right,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if !taquin_game.move_to(Directions::Right) {
|
||||||
|
message = "Cannot move any more right";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
message = "error";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_game(game: TaquinGame) {
|
if taquin_game.is_grid_done() && !user_cheated {
|
||||||
|
message = "Yahoo! Good job!";
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_game(&taquin_game);
|
||||||
|
print!("{}\r\n", message);
|
||||||
|
let _ = io::stdout().flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_game(game: &TaquinGame) {
|
||||||
|
print!("{}[2J", 27 as char);
|
||||||
let grid = game.grid();
|
let grid = game.grid();
|
||||||
|
|
||||||
for i in 0..ROWS * 2 + 1 {
|
for i in 0..ROWS * 2 + 1 {
|
||||||
@@ -60,14 +138,14 @@ fn draw_game(game: TaquinGame) {
|
|||||||
print!("High score: {}", game.high_score());
|
print!("High score: {}", game.high_score());
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = io::stdout().flush();
|
print!("\r\n");
|
||||||
println!("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Arrows to move");
|
print!("Arrows to move\r\n");
|
||||||
println!("Q: Quit");
|
print!("Q: Quit\r\n");
|
||||||
println!("R: Restart");
|
print!("R: Restart\r\n");
|
||||||
println!("S: Solve");
|
print!("S: Solve\r\n");
|
||||||
|
let _ = io::stdout().flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_even(num: usize) -> bool {
|
fn is_even(num: usize) -> bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user