move into unsafe directions as backup
This commit is contained in:
parent
9f7924b24b
commit
290b793b94
@ -58,6 +58,30 @@ impl Battlesnake {
|
|||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_direction_death(&self, direction: Direction, game: &Game, board: &Board) -> bool {
|
||||||
|
let target = self.head.move_to(direction);
|
||||||
|
|
||||||
|
// check if target is out of bounds
|
||||||
|
if !((0..board.width).contains(&target.x) && (0..board.height).contains(&target.y)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if target is inside a snake
|
||||||
|
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| target == coord)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// info is called when you create your Battlesnake on play.battlesnake.com
|
// info is called when you create your Battlesnake on play.battlesnake.com
|
||||||
@ -89,13 +113,19 @@ pub fn end(_game: &Game, _turn: i32, _board: &Board, _you: &Battlesnake) {
|
|||||||
// Valid moves are "up", "down", "left", or "right"
|
// Valid moves are "up", "down", "left", or "right"
|
||||||
// See https://docs.battlesnake.com/api/example-move for available data
|
// See https://docs.battlesnake.com/api/example-move for available data
|
||||||
pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Move {
|
pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Move {
|
||||||
let safe_moves = enum_iterator::all()
|
let moves = enum_iterator::all()
|
||||||
|
.filter(|&direction| !you.is_direction_death(direction, game, board))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let safe_moves = moves
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
.filter(|&direction| you.is_direction_safe(direction, game, board))
|
.filter(|&direction| you.is_direction_safe(direction, game, board))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// Choose a random move from the safe ones
|
// Choose a random move from the safe ones
|
||||||
let chosen = safe_moves
|
let chosen = safe_moves
|
||||||
.choose(&mut rand::thread_rng())
|
.choose(&mut rand::thread_rng())
|
||||||
|
.or_else(|| moves.choose(&mut rand::thread_rng()))
|
||||||
.unwrap_or(&Direction::Up);
|
.unwrap_or(&Direction::Up);
|
||||||
|
|
||||||
info!("MOVE {}: {:?}", turn, chosen);
|
info!("MOVE {}: {:?}", turn, chosen);
|
||||||
|
Loading…
Reference in New Issue
Block a user