Compare commits
2 Commits
27d60845b5
...
af0baffde0
Author | SHA1 | Date | |
---|---|---|---|
af0baffde0 | |||
157ea2aaad |
@ -14,7 +14,7 @@ use log::info;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::{Battlesnake, Board, Direction, Game, Move};
|
||||
use crate::{Battlesnake, Board, Coord, Direction, Game, Move};
|
||||
|
||||
impl Battlesnake {
|
||||
/// Check if moving the snake in `direction` is safe.
|
||||
@ -68,6 +68,46 @@ impl Battlesnake {
|
||||
}
|
||||
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
|
||||
|
@ -16,18 +16,15 @@ mod logic;
|
||||
// See https://docs.battlesnake.com/api
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize, Sequence)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Direction {
|
||||
/// Move left (-x)
|
||||
#[serde(rename = "left")]
|
||||
Left,
|
||||
/// Move up (+y)
|
||||
#[serde(rename = "up")]
|
||||
Up,
|
||||
/// Move right (+x)
|
||||
#[serde(rename = "right")]
|
||||
Right,
|
||||
/// Move down (-y)
|
||||
#[serde(rename = "down")]
|
||||
Down,
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user