Skip to content

Commit

Permalink
feat: populate grid on first discover
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBANCHEREAU committed Nov 5, 2023
1 parent 6e7e94a commit cb93c18
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COPY server server
WORKDIR /build/server
RUN cargo build --release

FROM alpine as Runner
FROM debian:bookworm as Runner

COPY --from=ClientBuilder /build/client/dist client/dist
COPY --from=ClientBuilder /build/client/images client/images
Expand All @@ -31,4 +31,4 @@ COPY --from=ServerBuilder /build/server/target/release/server server/server

EXPOSE 9000

CMD [ "server/server" ]
CMD [ "/server/server" ]
6 changes: 6 additions & 0 deletions core/src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ use crate::{
pub struct Game {
grid: VecGrid<Tile>,
listeners: Vec<Box<dyn Observer<GameEvent>>>,
populated: bool,
}
#[cfg(feature = "server")]
impl Game {
pub fn new(grid: VecGrid<Tile>) -> Self {
Self {
grid,
listeners: Default::default(),
populated: false,
}
}
pub fn play(&mut self, play: GameInput) {
Expand All @@ -48,6 +50,10 @@ impl Game {
} = play;
match action {
GameAction::Discover { x, y } => {
if (!self.populated) {
self.populated = true;
self.grid.populate(x, y);
}
self.discover_tile(x, y);
}
GameAction::PlaceFlag { x, y } => {
Expand Down
21 changes: 13 additions & 8 deletions core/src/grid/vec_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ impl Default for VecGrid<TileState> {
Self { grid }
}
}

#[cfg(feature = "server")]
impl VecGrid<Tile> {
pub fn new(width: u8, height: u8) -> Self {
let mut grid: Vec<Vec<Tile>> = Default::default();
let mut grid: Vec<Vec<Tile>> = Vec::with_capacity(height.into());
for _y in 0..height {
let mut col = vec![];
let mut col = Vec::with_capacity(width.into());
for _x in 0..width {
col.push(Tile {
content: TileContent::Empty,
Expand All @@ -70,21 +71,26 @@ impl VecGrid<Tile> {
}
grid.push(col);
}
let mut grid = Self { grid };
Self { grid }
}
pub fn populate(&mut self, input_x: i32, input_y: i32) {
let height = self.grid.len();
let width = self.grid.get(0).map(|v| v.len()).unwrap_or(0);
for (y, x) in (0..height)
.flat_map(|y| (0..width).map(move |x| (y, x)))
.filter(|(y, x)| x.abs_diff(input_x as usize) > 2 || y.abs_diff(input_y as usize) > 2)
.collect::<Vec<_>>()
.partial_shuffle(
&mut ThreadRng::default(),
usize::from(u16::from(width) * u16::from(height) / 5u16),
usize::from(u16::from(width as u16) * u16::from(height as u16) / 5u16),
)
.0
{
for dy in -1..=1i16 {
for dx in -1..=1i16 {
let Ok(x) = u8::try_from(dx + i16::from(*x)) else {continue;};
let Ok(y) = u8::try_from(dy + i16::from(*y)) else {continue;};
if let Some(Tile { content, state: _ }) = grid.get_mut(x, y) {
let Ok(x) = u8::try_from(dx + i16::from(*x as u8)) else {continue;};
let Ok(y) = u8::try_from(dy + i16::from(*y as u8)) else {continue;};
if let Some(Tile { content, state: _ }) = self.get_mut(x, y) {
if dx == 0 && dy == 0 {
*content = TileContent::Bomb;
} else {
Expand All @@ -98,7 +104,6 @@ impl VecGrid<Tile> {
}
}
}
grid
}
}
#[cfg(feature = "server")]
Expand Down

0 comments on commit cb93c18

Please sign in to comment.