Compare commits

...

2 Commits

Author SHA1 Message Date
af0baffde0 whatever I did there 2024-09-30 18:26:22 +02:00
157ea2aaad use serdes rename_all 2024-09-30 18:25:59 +02:00
2 changed files with 42 additions and 5 deletions

View File

@ -14,7 +14,7 @@ use log::info;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde_json::{json, Value}; use serde_json::{json, Value};
use crate::{Battlesnake, Board, Direction, Game, Move}; use crate::{Battlesnake, Board, Coord, Direction, Game, Move};
impl Battlesnake { impl Battlesnake {
/// Check if moving the snake in `direction` is safe. /// Check if moving the snake in `direction` is safe.
@ -68,6 +68,46 @@ impl Battlesnake {
} }
false false
} }
#[must_use]
pub fn health_cost(&self, tile: Coord, game: &Game, board: &Board) -> i32 {
// we can't leave the board
if !((0..board.width).contains(&tile.x) && (0..board.height).contains(&tile.y)) {
return self.health;
}
// we can't move into other snakes
if board
.snakes
.iter()
.filter(|snake| {
!(game.ruleset.settings.squad.allow_body_collisions && self.squad == snake.squad)
})
.flat_map(|snake| snake.body.iter())
.any(|&coord| tile == coord)
{
return self.health;
}
// every step costs us one health point
let mut cost = 1;
// hazards increase the health loss
if board.hazards.iter().any(|&hazard| tile == hazard) {
cost += game.ruleset.settings.hazard_damage_per_turn;
}
// check for bigger snakes that can move into this square
for snake in board
.snakes
.iter()
.filter(|snake| snake.id != self.id && self.length <= snake.length)
{
let moves = enum_iterator::all().map(|dir| snake.head.move_to(dir));
}
cost
}
} }
// info is called when you create your Battlesnake on play.battlesnake.com // info is called when you create your Battlesnake on play.battlesnake.com

View File

@ -16,18 +16,15 @@ mod logic;
// See https://docs.battlesnake.com/api // See https://docs.battlesnake.com/api
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize, Sequence)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize, Sequence)]
#[serde(rename_all = "lowercase")]
pub enum Direction { pub enum Direction {
/// Move left (-x) /// Move left (-x)
#[serde(rename = "left")]
Left, Left,
/// Move up (+y) /// Move up (+y)
#[serde(rename = "up")]
Up, Up,
/// Move right (+x) /// Move right (+x)
#[serde(rename = "right")]
Right, Right,
/// Move down (-y) /// Move down (-y)
#[serde(rename = "down")]
Down, Down,
} }