Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Orlha committed Oct 16, 2019
1 parent 338dce9 commit 1900336
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
42 changes: 25 additions & 17 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use std::collections::HashSet;
use std::collections::HashMap;

use crate::map::*;
use crate::ext::*;
Expand All @@ -9,13 +10,16 @@ use crate::cell::*;
pub struct Game {
map: Map,
char: Char,
cells: Vec<Cell>,
//cells: Vec<Cell>,
pos: Pos,
turn: i64,
cellsm: HashMap<i32, Cell>,
id_reuse: Vec<i32>,
}

impl Game {
pub fn new(_x: i64, _y: i64) -> Game {
//
Game {..Default::default()}
}
pub fn char_move(&mut self, dir: Direction) {
Expand All @@ -35,13 +39,21 @@ impl Game {
}
}
pub fn spawn_cell(&mut self) -> Result<()> {
let id = (self.cells.len() + 1) as i32;
//let id = (self.cells.len() + 1) as i32;
let id = if self.id_reuse.len() > 0 {
self.id_reuse.pop().unwrap()
} else {
self.cellsm.len() as i32
};
let r = self.map.bind_cell(id);
match r {
Ok(t) => {
self.cellsm.insert(id, Cell::new(id, t));
/*
self.cells.push(Cell::new(id, t));
println!("Created cell:\n{}", self.cells[self.cells.len() - 1]);
println!("Total cells: {}", self.cells.len());
*/
println!("Created cell:\n{}", self.cellsm[&(self.cellsm.len() as i32 - 1)]);
println!("Total cells: {}", self.cellsm.len());
return Ok(());
}
Err(_t) => {
Expand All @@ -62,7 +74,9 @@ impl Game {
let mut x = 0;
let mut deads: HashSet<i32> = HashSet::new();
println!("{:?}", deads);
for cell in self.cells.iter_mut() {
println!("map len {}", self.cellsm.len());
//for cell in self.cells.iter_mut() {
for (id, cell) in self.cellsm.iter_mut() {
println!("calculation cell");
if deads.contains(&cell.get_id()) {
continue;
Expand Down Expand Up @@ -107,22 +121,14 @@ impl Game {
println!("Processed {} cells;", x);
self.turn += 1;

//println!("deads: {:?}", deads);
let mut count_vec: Vec<(&i32)> = deads.iter().collect();
//println!(" vec: {:?}", count_vec);
count_vec.sort_by(|a, b| b.cmp(a));
//println!(" sort: {:?}", count_vec);
/*
for x in deads {
println!("removing cell with id {}", x - 1);
//self.cells.remove(x as usize - 1);
}
*/
println!("deads count: {}", deads.len());
println!("cells count: {}", self.cells.len());
println!("cells count: {}", self.cellsm.len());
for x in count_vec {
println!("removing id {}", *x);
self.cells.remove(*x as usize - 1);
//self.cells.remove(*x as usize - 1);
self.cellsm.remove(&(*x - 1));
}
Ok(())
}
Expand All @@ -143,7 +149,9 @@ impl Default for Game {
map: Map::new(),
char: Char::new(),
pos: Pos::new(),
cells: Vec::new()
//cells: Vec::new(),
cellsm: HashMap::new(),
id_reuse: Vec::new(),
}
}
}
12 changes: 10 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,22 @@ impl Map {
}
}
fn check_spot_status(&mut self, pos: Pos, spot: Spot) -> Result<()> {
println!("look if we are at bad borders here");
let n = self.get_spot_status(pos).unwrap();
/*
match self.get_spot_status(pos).unwrap() {
spot => {
println!("{:?}", self.get_spot_status(pos).unwrap());
//println!("{:?}", self.get_spot_status(pos).unwrap());
println!("still ok!");
Ok(())
}
_ => Err("No valid status on questioned spot;".into()),
_ => {
println!("invalid as intended!");
Err("No valid status on questioned spot;".into())
}
}
*/
/*
let n = self.get_spot_status(pos).unwrap();
println!("spot status: {:?}", n);
println!("expc status: {:?}", spot);
Expand All @@ -129,6 +136,7 @@ impl Map {
} else {
return Err("Err".into());
}
*/
}
pub fn cell_cell(&self, cell: &mut Cell) {
}
Expand Down

0 comments on commit 1900336

Please sign in to comment.