feat: add Cell entities class

This class provides a basic structure for creating "player" entities,
storing their neighboring cells and applying them to the grid arena.
This commit is contained in:
2025-05-03 10:00:46 +02:00
parent 40e391b426
commit 3c73ccab96

View File

@@ -1,6 +1,49 @@
use std::{time::Duration, thread};
const WIDTH: usize = 30; const WIDTH: usize = 30;
const HEIGHT: usize = 30; const HEIGHT: usize = 30;
struct Cell {
name: &'static str,
array: Vec<(usize, usize)>
}
impl Cell {
pub fn new(x: usize, y: usize, name: &'static str, directions_array: &[(i8, i8)]) -> Self {
let mut cells_array: Vec<(usize, usize)> = Vec::new();
for i in 0..directions_array.len() {
let dx = x as i8 + directions_array[i].0;
let dy = y as i8 + directions_array[i].1;
if dx > 0 && dy > 0 {
cells_array.push((
dx as usize,
dy as usize
));
}
}
return Cell {name, array: cells_array};
}
pub fn get_array(&self) -> &Vec<(usize, usize)> {
return &self.array;
}
pub fn get_name(&self) -> &'static str {
return self.name;
}
pub fn add_grid(&self, grid: &mut [[bool; WIDTH]; HEIGHT]) {
for (x, y) in &self.array {
if *x < WIDTH && *y < HEIGHT {
grid[*y][*x] = true;
}
}
}
}
fn display(grid: &[[bool; WIDTH]; HEIGHT], buffer: &mut String) { fn display(grid: &[[bool; WIDTH]; HEIGHT], buffer: &mut String) {
buffer.clear(); buffer.clear();
@@ -16,8 +59,7 @@ fn display(grid: &[[bool; WIDTH]; HEIGHT], buffer: &mut String) {
} }
fn count(grid: &[[bool; WIDTH]; HEIGHT], x: usize, y: usize) -> u8 { fn count(grid: &[[bool; WIDTH]; HEIGHT], x: usize, y: usize) -> u8 {
// (i, j) coordinate
let near:[(isize, isize); 8] = [ let near:[(isize, isize); 8] = [
(-1, -1), (-1, 0), (-1, 1), (-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1), (0, -1), (0, 1),
@@ -62,16 +104,13 @@ fn generation(grid: &[[bool; WIDTH]; HEIGHT]) -> [[bool; WIDTH]; HEIGHT] {
fn main() { fn main() {
let mut grid: [[bool; WIDTH]; HEIGHT] = [[false; WIDTH]; HEIGHT]; let mut grid: [[bool; WIDTH]; HEIGHT] = [[false; WIDTH]; HEIGHT];
let mut buffer: String = String::new(); let mut buffer: String = String::new();
// grid[14][13] = true; let glider: Cell = Cell::new(14, 14, "glider", &[(1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]);
// grid[15][14] = true; glider.add_grid(&mut grid);
// grid[13][15] = true;
// grid[14][15] = true;
// grid[15][15] = true;
loop { loop {
display(&grid, &mut buffer); display(&grid, &mut buffer);
grid = generation(&grid); grid = generation(&grid);
std::thread::sleep(std::time::Duration::from_millis(500)); thread::sleep(Duration::from_millis(500));
} }
} }