remove unused code

This commit is contained in:
Max Känner 2024-10-03 04:27:20 +02:00
parent 6123ca177e
commit 1c9d07f2cf
2 changed files with 12 additions and 76 deletions

View File

@ -24,73 +24,9 @@ use serde_json::{json, Value};
use crate::{
simulation::{self, SnakeToken},
Action, Battlesnake, Board, Direction, Game, MAX_HEALTH,
Action, Battlesnake, Board, Direction, Game,
};
impl Battlesnake {
fn possible_actions_without_heads<'a>(
&'a self,
game: &'a Game,
board: &'a Board,
) -> impl Iterator<Item = Direction> + 'a {
enum_iterator::all::<Direction>()
.filter(|direction| {
// filter out directions that would go outside the field
let target = self.head.move_to(*direction);
(0..board.width).contains(&target.x) && (0..board.height).contains(&target.y)
})
.filter(|direction| {
let target = self.head.move_to(*direction);
// don't collide with any snake
!board
.snakes
.iter()
.filter(|snake| {
// filter out snakes that are in our squad if body collisions are allowed
!(game.ruleset.settings.squad.allow_body_collisions
&& self.squad == snake.squad
&& self.id != snake.id)
})
.flat_map(|snake| {
// get all coordinates of the snake body. The tail of the body can be ignored
// if the snake hasn't just eaten, as it will move out of the way
let has_eaten = snake.health == MAX_HEALTH;
snake.body[..snake.body.len() - usize::from(!has_eaten)].iter()
})
.any(|&coord| coord == target)
})
}
#[must_use]
pub fn possible_actions(&self, game: &Game, board: &Board) -> Vec<Direction> {
self.possible_actions_without_heads(game, board)
.filter(|direction| {
// don't go into spots where a bigger snake must go
let target = self.head.move_to(*direction);
!board
.snakes
.iter()
.filter(|snake| {
// The other snake must be bigger than we are
snake.length > self.length
})
.filter_map(|snake| {
// get all snakes movement options
let actions = snake
.possible_actions_without_heads(game, board)
.collect::<Vec<_>>();
// only snakes that have a single option
match actions[..] {
[direction] => Some(snake.head.move_to(direction)),
_ => None,
}
})
.any(|coord| coord == target)
})
.collect()
}
}
// info is called when you create your Battlesnake on play.battlesnake.com
// and controls your Battlesnake's appearance
// TIP: If you open your Battlesnake URL in a browser you should see this data

View File

@ -22,7 +22,7 @@ const MAX_HEALTH: i32 = 100;
Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Deserialize, Serialize, Sequence,
)]
#[serde(rename_all = "lowercase")]
pub enum Direction {
enum Direction {
/// Move left (-x)
Left,
/// Move up (+y)
@ -34,7 +34,7 @@ pub enum Direction {
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Action {
struct Action {
/// In which direction the snake should move
r#move: Direction,
/// Say something to the other snakes
@ -47,7 +47,7 @@ fn is_default<T: Default + PartialEq>(value: &T) -> bool {
}
#[derive(Deserialize, Serialize, Debug)]
pub struct Game {
struct Game {
/// A unique identifier for this Game
id: String,
/// Information about the ruleset being used to run this game
@ -70,7 +70,7 @@ pub struct Game {
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Ruleset {
struct Ruleset {
/// Name of the ruleset being used to run this game.
name: String,
/// The release version of the [Rules](https://github.com/BattlesnakeOfficial/rules) module used in this game.
@ -81,7 +81,7 @@ pub struct Ruleset {
}
#[derive(Debug, Deserialize, Serialize)]
pub struct RulesetSettings {
struct RulesetSettings {
/// Percentage chance of spawning a new food every round.
#[serde(rename = "foodSpawnChance")]
food_spawn_chance: u8,
@ -99,7 +99,7 @@ pub struct RulesetSettings {
}
#[derive(Debug, Deserialize, Serialize)]
pub struct RulesetRoyale {
struct RulesetRoyale {
/// The number of turns between generating new hazards (shrinking the safe board space).
#[serde(rename = "shrinkEveryNTurns")]
shrink_every_n_turns: i32,
@ -107,7 +107,7 @@ pub struct RulesetRoyale {
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Deserialize, Serialize)]
pub struct RulesetSquad {
struct RulesetSquad {
/// Allow members of the same squad to move over each other without dying.
#[serde(rename = "allowBodyCollisions")]
allow_body_collisions: bool,
@ -123,7 +123,7 @@ pub struct RulesetSquad {
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Board {
struct Board {
/// The number of rows in the y-axis of the game board.
height: i32,
/// The number of columns in the x-axis of the game board.
@ -138,7 +138,7 @@ pub struct Board {
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Battlesnake {
struct Battlesnake {
/// Unique identifier for this Battlesnake in the context of the current Game
id: String,
/// Name given to this Battlesnake by its author
@ -165,7 +165,7 @@ pub struct Battlesnake {
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Deserialize, Serialize)]
pub struct Coord {
struct Coord {
x: i32,
y: i32,
}
@ -183,7 +183,7 @@ impl Coord {
}
#[derive(Deserialize, Serialize, Debug)]
pub struct GameState {
struct GameState {
game: Game,
turn: i32,
board: Board,