Skip to content

Commit

Permalink
Add shorthand function for creating new stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuupertunut committed Aug 10, 2023
1 parent d8e7791 commit 3a4b079
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
37 changes: 11 additions & 26 deletions src/bin/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl BattleSheepApp {
row_length: 1,
},
hover_stack: None,
min_home_stack: Some(Tile::new(TileType::Stack, Player::Min, 16)),
max_home_stack: Some(Tile::new(TileType::Stack, Player::Max, 16)),
min_home_stack: Some(Tile::stack(Player::Min, 16)),
max_home_stack: Some(Tile::stack(Player::Max, 16)),
red_image: RetainedImage::from_image_bytes(
"redsheep.png",
include_bytes!("redsheep.png"),
Expand Down Expand Up @@ -307,15 +307,10 @@ impl eframe::App for BattleSheepApp {
if stack_size > 1 {
let half_size = stack_size / 2;
self.hover_stack = Some(HoverStack {
stack: Tile::new(
TileType::Stack,
clicked_tile.player(),
half_size,
),
stack: Tile::stack(clicked_tile.player(), half_size),
origin: Some(clicked_coords),
});
self.board[clicked_coords] = Tile::new(
TileType::Stack,
self.board[clicked_coords] = Tile::stack(
clicked_tile.player(),
stack_size - half_size,
);
Expand All @@ -326,8 +321,7 @@ impl eframe::App for BattleSheepApp {
origin: hover_origin,
}) => {
if hover_origin == Some(clicked_coords) {
self.board[clicked_coords] = Tile::new(
TileType::Stack,
self.board[clicked_coords] = Tile::stack(
clicked_tile.player(),
stack_size + hover_stack.stack_size(),
);
Expand Down Expand Up @@ -361,16 +355,10 @@ impl eframe::App for BattleSheepApp {
)
};
if new_hover_size >= 1 && new_origin_size >= 1 {
self.hover_stack.as_mut().unwrap().stack = Tile::new(
TileType::Stack,
hover_stack.player(),
new_hover_size,
);
self.board[hover_origin] = Tile::new(
TileType::Stack,
hover_origin_stack.player(),
new_origin_size,
);
self.hover_stack.as_mut().unwrap().stack =
Tile::stack(hover_stack.player(), new_hover_size);
self.board[hover_origin] =
Tile::stack(hover_origin_stack.player(), new_origin_size);
}
}
None => {
Expand All @@ -380,11 +368,8 @@ impl eframe::App for BattleSheepApp {
hover_stack.stack_size() - 1
};
if new_hover_size >= 1 && new_hover_size <= Tile::MAX_STACK_SIZE {
self.hover_stack.as_mut().unwrap().stack = Tile::new(
TileType::Stack,
hover_stack.player(),
new_hover_size,
);
self.hover_stack.as_mut().unwrap().stack =
Tile::stack(hover_stack.player(), new_hover_size);
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Tile {
return Self(bitfield);
}

pub fn stack(player: Player, stack_size: u8) -> Self {
return Self::new(TileType::Stack, player, stack_size);
}

pub fn tile_type(self) -> TileType {
if self.0 < 64 {
return TileType::Stack;
Expand Down Expand Up @@ -379,7 +383,7 @@ impl Board {
return Err("Stack size is 0")?;
}

tiles.push(Tile::new(TileType::Stack, player, stack_size));
tiles.push(Tile::stack(player, stack_size));
}
}
}
Expand Down Expand Up @@ -480,9 +484,9 @@ impl Board {
/* Iterate through all the ways to split the stack. */
(1..stack.stack_size()).map(move |split| {
let mut next_board = self.clone();
next_board[target_coords] = Tile::new(TileType::Stack, player, split);
next_board[target_coords] = Tile::stack(player, split);
next_board[origin_coords] =
Tile::new(TileType::Stack, player, stack.stack_size() - split);
Tile::stack(player, stack.stack_size() - split);

next_board
})
Expand All @@ -494,7 +498,7 @@ impl Board {
fn possible_starting_moves(&self, player: Player) -> impl Iterator<Item = Board> + '_ {
return self.iter_empty_outer_edge().map(move |coords| {
let mut next_board = self.clone();
next_board[coords] = Tile::new(TileType::Stack, player, 16);
next_board[coords] = Tile::stack(player, 16);

next_board
});
Expand Down

0 comments on commit 3a4b079

Please sign in to comment.