Skip to content

Commit

Permalink
fixes according 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
tikitko committed Jul 28, 2024
1 parent 7dbcd3c commit 599d185
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 198 deletions.
2 changes: 1 addition & 1 deletion components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
name = "components"
version = "0.0.1"
authors = ["Tikitko <[email protected]>"]
edition = "2018"
edition = "2021"
2 changes: 1 addition & 1 deletion components/src/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ impl Direction {
Self::Up => Self::Down,
}
}
}
}
10 changes: 4 additions & 6 deletions components/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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(())
}
}
22 changes: 14 additions & 8 deletions components/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::hash::Hash;

pub struct Node<V> where
V: Copy + Hash + Eq {
pub struct Node<V>
where
V: Copy + Hash + Eq,
{
value: V,
next_node: Option<Box<Node<V>>>,
}

impl<V> Node<V> where
V: Copy + Hash + Eq {
impl<V> Node<V>
where
V: Copy + Hash + Eq,
{
pub fn new(value: V) -> Self {
Self {
value,
Expand Down Expand Up @@ -37,12 +41,14 @@ impl<V> Node<V> where
child_nodes_values.push(self.value);
child_nodes_values
}
pub fn recursive_run<F>(&mut self, mut entrance: F) where
F: FnMut(&mut Node<V>) {
pub fn recursive_run<F>(&mut self, mut entrance: F)
where
F: FnMut(&mut Node<V>),
{
entrance(self);
match self.get_next_node_mut() {
Some(next_node) => next_node.recursive_run(entrance),
None => {},
None => {}
}
}
}
}
21 changes: 11 additions & 10 deletions components/src/point.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use std::hash::Hash;
use std::ops::{Add, Sub};
use std::hash::{Hash};

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Point<N> where
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash {
pub struct Point<N>
where
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash,
{
x: N,
y: N,
}

impl<N> Point<N> where
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash {
impl<N> Point<N>
where
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash,
{
pub fn new(x: N, y: N) -> Self {
Self {
x,
y,
}
Self { x, y }
}
pub fn x(&self) -> N {
self.x
}
pub fn y(&self) -> N {
self.y
}
}
}
24 changes: 15 additions & 9 deletions components/src/world.rs
Original file line number Diff line number Diff line change
@@ -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<L, N> where
pub struct World<L, N>
where
L: Hash + Eq,
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash {
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash,
{
layers: HashMap<L, HashSet<Point<N>>>,
}

impl<L, N> World<L, N> where
impl<L, N> World<L, N>
where
L: Hash + Eq + Copy,
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash {
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash,
{
pub fn new() -> Self {
Self {
layers: HashMap::new(),
Expand All @@ -39,11 +43,13 @@ impl<L, N> World<L, N> where
pub fn generate_map<PointM, ObjectM, PointR, ObjectR>(
&self,
point_mapper: PointM,
object_mapper: ObjectM
) -> HashMap<PointR, ObjectR> where
object_mapper: ObjectM,
) -> HashMap<PointR, ObjectR>
where
PointM: Fn(&Point<N>) -> 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 {
Expand All @@ -52,4 +58,4 @@ impl<L, N> World<L, N> where
}
map
}
}
}
2 changes: 1 addition & 1 deletion snake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "snake"
version = "0.0.1"
authors = ["Tikitko <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
components = { path = "../components" }
12 changes: 5 additions & 7 deletions snake/src/game.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -47,9 +47,7 @@ pub struct Game {

impl Game {
pub fn new(config: Config) -> Result<Self, CreateError> {
Ok(Self {
config,
})
Ok(Self { config })
}
pub fn start(&mut self) {
self.start_game_loop();
Expand All @@ -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,
}
}
Expand All @@ -101,4 +99,4 @@ impl Game {
last_world_view = Some(world_view);
}
}
}
}
4 changes: 2 additions & 2 deletions snake/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate components;

pub mod snake;
pub mod game;
pub mod snake;
pub mod world;

pub type Point<N> = components::point::Point<N>;
pub type Direction = components::direction::Direction;
pub type AreaSize = u16;
pub type AreaSize = u16;
65 changes: 39 additions & 26 deletions snake/src/snake.rs
Original file line number Diff line number Diff line change
@@ -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<N> where
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash {
pub struct Snake<N>
where
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash,
{
head_point_node: Box<Node<Point<N>>>,
is_stomach_not_empty: bool,
}

impl<N> Snake<N> where
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash {
impl<N> Snake<N>
where
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash,
{
pub fn new(point: Point<N>) -> Self {
Self {
head_point_node: Box::new(Node::new(point)),
Expand All @@ -37,8 +41,10 @@ impl<N> Snake<N> where
}
}

impl<N> Snake<N> where
N: Add<Output=N> + Sub<Output=N> + Copy + Eq + Hash + From<u8> {
impl<N> Snake<N>
where
N: Add<Output = N> + Sub<Output = N> + Copy + Eq + Hash + From<u8>,
{
pub fn next_head_point(&self, move_direction: Direction) -> Point<N> {
let head_point = self.head_point_node.get_value();
let mut x = head_point.x();
Expand Down Expand Up @@ -66,24 +72,31 @@ impl<N> Snake<N> 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<F>(&mut self, should_remove: F) where
F: Fn(Point<N>) -> 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<F>(&mut self, should_remove: F)
where
F: Fn(Point<N>) -> 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 => {}
});
}
}
}
Loading

0 comments on commit 599d185

Please sign in to comment.