From 599d185e2dd20b65f3f816945dc0ab0c52683e71 Mon Sep 17 00:00:00 2001 From: tikitko Date: Sun, 28 Jul 2024 18:48:16 +0200 Subject: [PATCH] fixes according 2024 --- components/Cargo.toml | 2 +- components/src/direction.rs | 2 +- components/src/lib.rs | 10 +- components/src/node.rs | 22 +-- components/src/point.rs | 21 +-- components/src/world.rs | 24 ++-- snake/Cargo.toml | 2 +- snake/src/game.rs | 12 +- snake/src/lib.rs | 4 +- snake/src/snake.rs | 65 +++++---- snake/src/world.rs | 220 ++++++++++++++++++------------ terminal-snake/Cargo.toml | 2 +- terminal-snake/src/game_config.rs | 48 +++---- terminal-snake/src/main.rs | 2 +- terminal/Cargo.toml | 4 +- terminal/src/lib.rs | 15 +- 16 files changed, 257 insertions(+), 198 deletions(-) diff --git a/components/Cargo.toml b/components/Cargo.toml index dc63508..658a6dc 100644 --- a/components/Cargo.toml +++ b/components/Cargo.toml @@ -2,4 +2,4 @@ name = "components" version = "0.0.1" authors = ["Tikitko "] -edition = "2018" +edition = "2021" diff --git a/components/src/direction.rs b/components/src/direction.rs index cf7487b..749e41d 100644 --- a/components/src/direction.rs +++ b/components/src/direction.rs @@ -15,4 +15,4 @@ impl Direction { Self::Up => Self::Down, } } -} \ No newline at end of file +} diff --git a/components/src/lib.rs b/components/src/lib.rs index cf526b8..3ed1f6a 100644 --- a/components/src/lib.rs +++ b/components/src/lib.rs @@ -1,5 +1,5 @@ -use std::os::raw::{c_uint, c_int}; -use std::time::{UNIX_EPOCH, SystemTime, SystemTimeError}; +use std::os::raw::{c_int, c_uint}; +use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; extern "C" { fn srand(seed: c_uint); @@ -17,9 +17,7 @@ pub unsafe fn get_rand_in_range(a: c_int, b: c_int) -> c_int { } pub unsafe fn set_rand_current_time_seed() -> Result<(), SystemTimeError> { - let nanos = SystemTime::now() - .duration_since(UNIX_EPOCH)? - .subsec_nanos(); + let nanos = SystemTime::now().duration_since(UNIX_EPOCH)?.subsec_nanos(); srand(nanos); Ok(()) -} \ No newline at end of file +} diff --git a/components/src/node.rs b/components/src/node.rs index ff747cc..c88518d 100644 --- a/components/src/node.rs +++ b/components/src/node.rs @@ -1,13 +1,17 @@ use std::hash::Hash; -pub struct Node where - V: Copy + Hash + Eq { +pub struct Node +where + V: Copy + Hash + Eq, +{ value: V, next_node: Option>>, } -impl Node where - V: Copy + Hash + Eq { +impl Node +where + V: Copy + Hash + Eq, +{ pub fn new(value: V) -> Self { Self { value, @@ -37,12 +41,14 @@ impl Node where child_nodes_values.push(self.value); child_nodes_values } - pub fn recursive_run(&mut self, mut entrance: F) where - F: FnMut(&mut Node) { + pub fn recursive_run(&mut self, mut entrance: F) + where + F: FnMut(&mut Node), + { entrance(self); match self.get_next_node_mut() { Some(next_node) => next_node.recursive_run(entrance), - None => {}, + None => {} } } -} \ No newline at end of file +} diff --git a/components/src/point.rs b/components/src/point.rs index 34f3a61..5374998 100644 --- a/components/src/point.rs +++ b/components/src/point.rs @@ -1,20 +1,21 @@ +use std::hash::Hash; use std::ops::{Add, Sub}; -use std::hash::{Hash}; #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub struct Point where - N: Add + Sub + Copy + Eq + Hash { +pub struct Point +where + N: Add + Sub + Copy + Eq + Hash, +{ x: N, y: N, } -impl Point where - N: Add + Sub + Copy + Eq + Hash { +impl Point +where + N: Add + Sub + Copy + Eq + Hash, +{ pub fn new(x: N, y: N) -> Self { - Self { - x, - y, - } + Self { x, y } } pub fn x(&self) -> N { self.x @@ -22,4 +23,4 @@ impl Point where pub fn y(&self) -> N { self.y } -} \ No newline at end of file +} diff --git a/components/src/world.rs b/components/src/world.rs index 02b9401..77fc52e 100644 --- a/components/src/world.rs +++ b/components/src/world.rs @@ -1,18 +1,22 @@ +use crate::point::Point; use std::collections::{HashMap, HashSet}; use std::hash::Hash; use std::ops::{Add, Sub}; -use crate::point::Point; #[derive(Clone)] -pub struct World where +pub struct World +where L: Hash + Eq, - N: Add + Sub + Copy + Eq + Hash { + N: Add + Sub + Copy + Eq + Hash, +{ layers: HashMap>>, } -impl World where +impl World +where L: Hash + Eq + Copy, - N: Add + Sub + Copy + Eq + Hash { + N: Add + Sub + Copy + Eq + Hash, +{ pub fn new() -> Self { Self { layers: HashMap::new(), @@ -39,11 +43,13 @@ impl World where pub fn generate_map( &self, point_mapper: PointM, - object_mapper: ObjectM - ) -> HashMap where + object_mapper: ObjectM, + ) -> HashMap + where PointM: Fn(&Point) -> PointR, ObjectM: Fn(&L) -> ObjectR, - PointR: Eq + Hash { + PointR: Eq + Hash, + { let mut map = HashMap::new(); for (layer_key, layer) in &self.layers { for point in layer { @@ -52,4 +58,4 @@ impl World where } map } -} \ No newline at end of file +} diff --git a/snake/Cargo.toml b/snake/Cargo.toml index 456dc43..511551f 100644 --- a/snake/Cargo.toml +++ b/snake/Cargo.toml @@ -2,7 +2,7 @@ name = "snake" version = "0.0.1" authors = ["Tikitko "] -edition = "2018" +edition = "2021" [dependencies] components = { path = "../components" } \ No newline at end of file diff --git a/snake/src/game.rs b/snake/src/game.rs index 1393194..0fe0a42 100644 --- a/snake/src/game.rs +++ b/snake/src/game.rs @@ -1,7 +1,7 @@ use super::world; -use std::hash::Hash; use std::cell::{RefCell, RefMut}; +use std::hash::Hash; use std::rc::Rc; pub struct Config { @@ -47,9 +47,7 @@ pub struct Game { impl Game { pub fn new(config: Config) -> Result { - Ok(Self { - config, - }) + Ok(Self { config }) } pub fn start(&mut self) { self.start_game_loop(); @@ -70,14 +68,14 @@ impl Game { Ok(mut world) => { self.start_tick_loop(&mut world); Ok(()) - }, + } Err(err) => Err(err), }; match self.config.game_controller() { Some(mut controller) => controller.game_end(start_result), None => break, } - }, + } ActionType::Exit => break, } } @@ -101,4 +99,4 @@ impl Game { last_world_view = Some(world_view); } } -} \ No newline at end of file +} diff --git a/snake/src/lib.rs b/snake/src/lib.rs index 716c4a5..e7ea3c1 100644 --- a/snake/src/lib.rs +++ b/snake/src/lib.rs @@ -1,9 +1,9 @@ extern crate components; -pub mod snake; pub mod game; +pub mod snake; pub mod world; pub type Point = components::point::Point; pub type Direction = components::direction::Direction; -pub type AreaSize = u16; \ No newline at end of file +pub type AreaSize = u16; diff --git a/snake/src/snake.rs b/snake/src/snake.rs index d40738a..0c2cbcd 100644 --- a/snake/src/snake.rs +++ b/snake/src/snake.rs @@ -1,18 +1,22 @@ -use super::components::point::Point; -use super::components::node::Node; use super::components::direction::Direction; +use super::components::node::Node; +use super::components::point::Point; -use std::ops::{Add, Sub}; use std::hash::Hash; +use std::ops::{Add, Sub}; -pub struct Snake where - N: Add + Sub + Copy + Eq + Hash { +pub struct Snake +where + N: Add + Sub + Copy + Eq + Hash, +{ head_point_node: Box>>, is_stomach_not_empty: bool, } -impl Snake where - N: Add + Sub + Copy + Eq + Hash { +impl Snake +where + N: Add + Sub + Copy + Eq + Hash, +{ pub fn new(point: Point) -> Self { Self { head_point_node: Box::new(Node::new(point)), @@ -37,8 +41,10 @@ impl Snake where } } -impl Snake where - N: Add + Sub + Copy + Eq + Hash + From { +impl Snake +where + N: Add + Sub + Copy + Eq + Hash + From, +{ pub fn next_head_point(&self, move_direction: Direction) -> Point { let head_point = self.head_point_node.get_value(); let mut x = head_point.x(); @@ -66,24 +72,31 @@ impl Snake where node.set_value(point); match node.get_next_node() { Some(_) => next_point = Some(current_point), - None => if add_body_to_end { - node.set_next_node(Some(Node::new(current_point))); - next_point = None; - } else { - next_point = Some(current_point); - }, + None => { + if add_body_to_end { + node.set_next_node(Some(Node::new(current_point))); + next_point = None; + } else { + next_point = Some(current_point); + } + } } - }, - None => {}, + } + None => {} }); } - pub fn recursive_remove_tail(&mut self, should_remove: F) where - F: Fn(Point) -> bool { - self.head_point_node.recursive_run(|node| match node.get_next_node_mut() { - Some(next_node) => if should_remove(next_node.get_value()) { - node.set_next_node(None); - }, - None => {}, - }); + pub fn recursive_remove_tail(&mut self, should_remove: F) + where + F: Fn(Point) -> bool, + { + self.head_point_node + .recursive_run(|node| match node.get_next_node_mut() { + Some(next_node) => { + if should_remove(next_node.get_value()) { + node.set_next_node(None); + } + } + None => {} + }); } -} \ No newline at end of file +} diff --git a/snake/src/world.rs b/snake/src/world.rs index afea620..38beb57 100644 --- a/snake/src/world.rs +++ b/snake/src/world.rs @@ -1,14 +1,14 @@ -use super::snake::Snake; +use super::components::direction::Direction; use super::components::point::Point; use super::components::world::World as GenericWorld; -use super::components::direction::Direction; -use super::components::{set_rand_current_time_seed, get_rand_in_range}; +use super::components::{get_rand_in_range, set_rand_current_time_seed}; +use super::snake::Snake; use super::AreaSize; -use std::collections::{HashSet, HashMap}; -use std::iter::FromIterator; -use std::hash::Hash; use std::cell::{RefCell, RefMut}; +use std::collections::{HashMap, HashSet}; +use std::hash::Hash; +use std::iter::FromIterator; use std::rc::Rc; pub struct Config { @@ -110,6 +110,12 @@ pub struct World { config: Config, } +struct SnakesInteractionsDetectResult { + snakes_to_remove: HashSet, + snakes_that_ate_food: HashMap>, + snakes_that_bit_tail: HashMap)>, +} + impl World { pub fn new(config: Config) -> Result { if config.world_size.0 < 10 || config.world_size.1 < 10 { @@ -141,63 +147,61 @@ impl World { config, }) } - pub fn tick(&mut self, reset: bool) -> WorldView { - // Border - if reset { - let border_points: HashSet> = { - let mut border_points = HashSet::new(); - for x in 0..self.config.world_size.0 { - for y in 0..self.config.world_size.1 { - let max_x = self.config.world_size.0 - 1; - let max_y = self.config.world_size.1 - 1; - if x == 0 || y == 0 || x == max_x || y == max_y { - border_points.insert(Point::new(x, y)); - } + fn spawn_border(&mut self) { + let border_points: HashSet> = { + let mut border_points = HashSet::new(); + for x in 0..self.config.world_size.0 { + for y in 0..self.config.world_size.1 { + let max_x = self.config.world_size.0 - 1; + let max_y = self.config.world_size.1 - 1; + if x == 0 || y == 0 || x == max_x || y == max_y { + border_points.insert(Point::new(x, y)); } } - border_points - }; - self.border_points = border_points; - self.world_mask.set_layer(ObjectType::Border, self.border_points.clone()); - } - // Snakes - if reset { - let snakes: HashMap> = { - let mut snakes = HashMap::new(); - for snake_number in 0..self.config.snakes_controllers.len() as AreaSize { - let real_snake_number = snake_number + 1; - let snake_number = snake_number as usize; - let mut snake = Snake::new(Point::new(3, real_snake_number * 3)); - for _ in 0..self.config.base_snake_tail_size { - snake.fill_stomach_if_empty(); - snake.move_to(Direction::Right); - } - snakes.insert(snake_number, snake); + } + border_points + }; + self.border_points = border_points; + self.world_mask + .set_layer(ObjectType::Border, self.border_points.clone()); + } + fn spawn_snakes(&mut self) { + let snakes: HashMap> = { + let mut snakes = HashMap::new(); + for snake_number in 0..self.config.snakes_controllers.len() as AreaSize { + let real_snake_number = snake_number + 1; + let snake_number = snake_number as usize; + let mut snake = Snake::new(Point::new(3, real_snake_number * 3)); + for _ in 0..self.config.base_snake_tail_size { + snake.fill_stomach_if_empty(); + snake.move_to(Direction::Right); } - snakes + snakes.insert(snake_number, snake); + } + snakes + }; + for (snake_number, snake) in snakes { + if let Some(mut controller) = self.config.snake_controller(&snake_number) { + let world_view = WorldView::new(self); + controller.snake_will_burn(&world_view); + } + let points = HashSet::from_iter(snake.body_parts_points(true).clone()); + let snake_info = SnakeInfo { + snake, + direction: None, }; - // Snakes: snakes_spawn - for (snake_number, snake) in snakes { + self.snakes_info.insert(snake_number, snake_info); + self.world_mask + .set_layer(ObjectType::Snake(snake_number.clone()), points); + if let Some(snake_info) = self.snakes_info.get(&snake_number) { if let Some(mut controller) = self.config.snake_controller(&snake_number) { let world_view = WorldView::new(self); - controller.snake_will_burn(&world_view); - } - let points = HashSet::from_iter(snake.body_parts_points(true).clone()); - let snake_info = SnakeInfo { - snake, - direction: None, - }; - self.snakes_info.insert(snake_number, snake_info); - self.world_mask.set_layer(ObjectType::Snake(snake_number.clone()), points); - if let Some(snake_info) = self.snakes_info.get(&snake_number) { - if let Some(mut controller) = self.config.snake_controller(&snake_number) { - let world_view = WorldView::new(self); - controller.snake_did_burn(snake_info, &world_view); - } + controller.snake_did_burn(snake_info, &world_view); } } } - // Snakes: snakes_move + } + fn snakes_move(&mut self) -> HashMap, HashSet> { let mut points_move_vectors = HashMap::, HashSet>::new(); let snakes_numbers = { let mut snakes_numbers = Vec::::new(); @@ -241,14 +245,20 @@ impl World { } if let Some(snake_info) = self.snakes_info.get(&snake_number) { let points = HashSet::from_iter(snake_info.snake.body_parts_points(true).clone()); - self.world_mask.set_layer(ObjectType::Snake(snake_number.clone()), points); + self.world_mask + .set_layer(ObjectType::Snake(snake_number.clone()), points); if let Some(mut controller) = self.config.snake_controller(&snake_number) { let world_view = WorldView::new(self); controller.snake_did_move(snake_info, &world_view); } } } - // Snakes: snakes_interactions_detect + points_move_vectors + } + fn snakes_interactions_detect( + &mut self, + points_move_vectors: &HashMap, HashSet>, + ) -> SnakesInteractionsDetectResult { let mut snakes_to_remove = HashSet::::new(); let mut snakes_that_ate_food = HashMap::>::new(); let mut snakes_that_bit_tail = HashMap::)>::new(); @@ -279,35 +289,47 @@ impl World { } for object in self.world_mask.point_occurrences(&body_point) { match object { - ObjectType::Snake(number) => if number != *snake_number { - if self.config.cut_tails { - if let Some(other_snake_info) = self.snakes_info.get(&number) { - if other_snake_info.snake.head_point() == head_point { - snakes_to_remove.insert(snake_number.clone()); - continue; + ObjectType::Snake(number) => { + if number != *snake_number { + if self.config.cut_tails { + if let Some(other_snake_info) = self.snakes_info.get(&number) { + if other_snake_info.snake.head_point() == head_point { + snakes_to_remove.insert(snake_number.clone()); + continue; + } } + if body_point == head_point { + let tail_info = (number.clone(), body_point.clone()); + snakes_that_bit_tail + .insert(snake_number.clone(), tail_info); + } + } else { + snakes_to_remove.insert(snake_number.clone()); } - if body_point == head_point { - let tail_info = (number.clone(), body_point.clone()); - snakes_that_bit_tail.insert(snake_number.clone(), tail_info); - } + } + } + ObjectType::Eat => { + if body_point == head_point { + snakes_that_ate_food + .insert(snake_number.clone(), body_point.clone()); } else { snakes_to_remove.insert(snake_number.clone()); } - }, - ObjectType::Eat => if body_point == head_point { - snakes_that_ate_food.insert(snake_number.clone(), body_point.clone()); - } else { - snakes_to_remove.insert(snake_number.clone()); - }, + } ObjectType::Border => { snakes_to_remove.insert(snake_number.clone()); - }, + } } } } } - // Snakes: snakes_remove + SnakesInteractionsDetectResult { + snakes_to_remove, + snakes_that_ate_food, + snakes_that_bit_tail, + } + } + fn handle_snakes_to_remove(&mut self, snakes_to_remove: HashSet) { for snake_remove_number in snakes_to_remove { if let Some(to_remove_snake_info) = self.snakes_info.get(&snake_remove_number) { if let Some(mut controller) = self.config.snake_controller(&snake_remove_number) { @@ -316,13 +338,18 @@ impl World { } } self.snakes_info.remove(&snake_remove_number); - self.world_mask.remove_layer(&ObjectType::Snake(snake_remove_number)); + self.world_mask + .remove_layer(&ObjectType::Snake(snake_remove_number)); if let Some(mut controller) = self.config.snake_controller(&snake_remove_number) { let world_view = WorldView::new(self); controller.snake_did_died(&world_view); } } - // Snakes: snakes_bit_tail + } + fn handle_snakes_that_bit_tail( + &mut self, + snakes_that_bit_tail: HashMap)>, + ) { for (snake, (cut_snake, body_point)) in snakes_that_bit_tail { if let Some(snake_info) = self.snakes_info.get(&snake) { if let Some(mut controller) = self.config.snake_controller(&snake) { @@ -334,10 +361,13 @@ impl World { snake_info.snake.fill_stomach_if_empty(); } if let Some(cut_snake_info) = self.snakes_info.get_mut(&cut_snake) { - cut_snake_info.snake.recursive_remove_tail(|p| p == body_point); + cut_snake_info + .snake + .recursive_remove_tail(|p| p == body_point); let body_points = cut_snake_info.snake.body_parts_points(true).clone(); let points = HashSet::from_iter(body_points); - self.world_mask.set_layer(ObjectType::Snake(cut_snake.clone()), points); + self.world_mask + .set_layer(ObjectType::Snake(cut_snake.clone()), points); } if let Some(snake_info) = self.snakes_info.get(&snake) { if let Some(mut controller) = self.config.snake_controller(&snake) { @@ -346,7 +376,11 @@ impl World { } } } - // Snakes: snakes_feeding + } + fn handle_snakes_that_ate_food( + &mut self, + snakes_that_ate_food: HashMap>, + ) { for (snakes_feeding, eat_point) in snakes_that_ate_food { if let Some(snake_info) = self.snakes_info.get(&snakes_feeding) { if let Some(mut controller) = self.config.snake_controller(&snakes_feeding) { @@ -357,7 +391,8 @@ impl World { if let Some(snake_info) = self.snakes_info.get_mut(&snakes_feeding) { snake_info.snake.fill_stomach_if_empty(); if self.eat_points.remove(&eat_point) { - self.world_mask.set_layer(ObjectType::Eat, self.eat_points.clone()); + self.world_mask + .set_layer(ObjectType::Eat, self.eat_points.clone()); } } if let Some(snake_info) = self.snakes_info.get(&snakes_feeding) { @@ -367,7 +402,8 @@ impl World { } } } - // Eat + } + fn spawn_eat(&mut self) { let eat_to_spawn = self.config.eat_count - self.eat_points.len() as AreaSize; unsafe { let _ = set_rand_current_time_seed(); @@ -385,8 +421,24 @@ impl World { } } } - self.world_mask.set_layer(ObjectType::Eat, self.eat_points.clone()); - // Other + self.world_mask + .set_layer(ObjectType::Eat, self.eat_points.clone()); + } + pub fn tick(&mut self, reset: bool) -> WorldView { + if reset { + self.spawn_border(); + self.spawn_snakes() + } + let points_move_vectors = self.snakes_move(); + let SnakesInteractionsDetectResult { + snakes_to_remove, + snakes_that_ate_food, + snakes_that_bit_tail, + } = self.snakes_interactions_detect(&points_move_vectors); + self.handle_snakes_to_remove(snakes_to_remove); + self.handle_snakes_that_bit_tail(snakes_that_bit_tail); + self.handle_snakes_that_ate_food(snakes_that_ate_food); + self.spawn_eat(); WorldView::new(self) } -} \ No newline at end of file +} diff --git a/terminal-snake/Cargo.toml b/terminal-snake/Cargo.toml index cc1dbe8..04b43e1 100644 --- a/terminal-snake/Cargo.toml +++ b/terminal-snake/Cargo.toml @@ -2,7 +2,7 @@ name = "terminal-snake" version = "0.0.1" authors = ["Tikitko "] -edition = "2018" +edition = "2021" [dependencies] snake = { path = "../snake" } diff --git a/terminal-snake/src/game_config.rs b/terminal-snake/src/game_config.rs index 8a7405d..76871a7 100644 --- a/terminal-snake/src/game_config.rs +++ b/terminal-snake/src/game_config.rs @@ -1,32 +1,18 @@ -use super::terminal::{ - TerminalSize, - Terminal, - KeyCode -}; -use super::snake::{ - Direction, - Point +use super::snake::game::{ + ActionType as GameActionType, Config as GameConfig, GameController, TickType as GameTickType, }; use super::snake::world::{ - SnakeController, - ObjectType as WorldObjectType, - Config as WorldConfig, - CreateError as WorldCreateError, - WorldView, - SnakeInfo -}; -use super::snake::game::{ - GameController, - ActionType as GameActionType, - TickType as GameTickType, - Config as GameConfig + Config as WorldConfig, CreateError as WorldCreateError, ObjectType as WorldObjectType, + SnakeController, SnakeInfo, WorldView, }; +use super::snake::{Direction, Point}; +use super::terminal::{KeyCode, Terminal, TerminalSize}; -use std::time::{Duration, SystemTime}; -use std::thread; -use std::collections::HashMap; use std::cell::RefCell; +use std::collections::HashMap; use std::rc::Rc; +use std::thread; +use std::time::{Duration, SystemTime}; pub fn new() -> GameConfig { GameConfig { @@ -63,7 +49,7 @@ impl TerminalGameController { let delay_time = MINIMUM_DELAY_MILLIS - after_time; thread::sleep(Duration::from_millis(delay_time)); } - }, + } None => thread::sleep(Duration::from_millis(MINIMUM_DELAY_MILLIS)), } self.last_tick_start = Some(SystemTime::now()); @@ -121,7 +107,7 @@ impl GameController for TerminalGameController { KeyCode::Char('s') => Direction::Down, _ => first_snake.next_direction, }; - }, + } Err(_) => return GameTickType::Break, } match self.second_snake.try_borrow_mut() { @@ -133,14 +119,14 @@ impl GameController for TerminalGameController { KeyCode::Down => Direction::Down, _ => second_snake.next_direction, }; - }, + } Err(_) => return GameTickType::Break, } GameTickType::Common - }, + } None => GameTickType::Common, } - }, + } None => GameTickType::Initial, } } @@ -155,7 +141,9 @@ impl GameController for TerminalGameController { }, WorldObjectType::Eat => '@', }; - let map = world_view.get_world_mask().generate_map(points_mapper, objects_mapper); + let map = world_view + .get_world_mask() + .generate_map(points_mapper, objects_mapper); let _ = self.terminal.render(&map); } fn game_end(&mut self, _: Result<(), WorldCreateError>) { @@ -179,4 +167,4 @@ impl SnakeController for DirectionSnakeController { fn snake_did_eat(&mut self, _: bool, _: &SnakeInfo, _: &WorldView) {} fn snake_will_died(&mut self, _: &SnakeInfo, _: &WorldView) {} fn snake_did_died(&mut self, _: &WorldView) {} -} \ No newline at end of file +} diff --git a/terminal-snake/src/main.rs b/terminal-snake/src/main.rs index c81c2b9..d6c77d8 100644 --- a/terminal-snake/src/main.rs +++ b/terminal-snake/src/main.rs @@ -8,4 +8,4 @@ fn main() { Ok(mut game) => game.start(), Err(err) => println!("{:?}", err), } -} \ No newline at end of file +} diff --git a/terminal/Cargo.toml b/terminal/Cargo.toml index 5735e74..f507191 100644 --- a/terminal/Cargo.toml +++ b/terminal/Cargo.toml @@ -2,7 +2,7 @@ name = "terminal" version = "0.0.1" authors = ["Tikitko "] -edition = "2018" +edition = "2021" [dependencies] -crossterm = "0.17.6" \ No newline at end of file +crossterm = "0.27.0" \ No newline at end of file diff --git a/terminal/src/lib.rs b/terminal/src/lib.rs index 463dffa..86987dc 100644 --- a/terminal/src/lib.rs +++ b/terminal/src/lib.rs @@ -1,16 +1,14 @@ -use std::io::{stdout, Write, Stdout}; +use crossterm::event::{poll, read, Event}; +use crossterm::terminal::{disable_raw_mode, enable_raw_mode, size}; +use crossterm::{cursor, style, terminal, ExecutableCommand, QueueableCommand}; use std::collections::HashMap; +use std::io::{stdout, Result, Stdout, Write}; use std::time::Duration; -use crossterm::{cursor, style, QueueableCommand, terminal, ExecutableCommand}; -use crossterm::event::{read, Event, poll}; -use crossterm::terminal::{disable_raw_mode, enable_raw_mode, size}; pub type TerminalSize = u16; pub type TerminalPoint = (TerminalSize, TerminalSize); pub type TerminalPixel = char; pub type KeyCode = crossterm::event::KeyCode; -pub type ErrorKind = crossterm::ErrorKind; -pub type Result = crossterm::Result; pub struct Terminal { stdout: Stdout, @@ -76,8 +74,7 @@ impl Terminal { .queue(cursor_move_to_command(point.clone()))? .queue(print_styled_content_command(SPACE_CHAR))?; } - self.stdout - .queue(cursor_move_to_command((0, 0)))?; + self.stdout.queue(cursor_move_to_command((0, 0)))?; self.stdout.flush()?; Ok(()) } @@ -89,4 +86,4 @@ fn cursor_move_to_command(point: TerminalPoint) -> cursor::MoveTo { fn print_styled_content_command(char: char) -> style::PrintStyledContent { style::PrintStyledContent(style::StyledContent::new(style::ContentStyle::new(), char)) -} \ No newline at end of file +}