don't move into self

This commit is contained in:
Max Känner 2024-09-02 15:42:33 +02:00
parent c54eb6b574
commit fc123cbd2b
2 changed files with 26 additions and 3 deletions

View File

@ -15,7 +15,7 @@ use rand::seq::SliceRandom;
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::collections::HashMap; use std::collections::HashMap;
use crate::{Battlesnake, Board, Game}; use crate::{Battlesnake, Board, Coord, Game};
// info is called when you create your Battlesnake on play.battlesnake.com // info is called when you create your Battlesnake on play.battlesnake.com
// and controls your Battlesnake's appearance // and controls your Battlesnake's appearance
@ -88,7 +88,30 @@ pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Va
} }
// TODO: Step 2 - Prevent your Battlesnake from colliding with itself // TODO: Step 2 - Prevent your Battlesnake from colliding with itself
// let my_body = &you.body; if you.body.contains(&Coord {
x: my_head.x - 1,
y: my_head.y,
}) {
is_move_safe.insert("left", false);
}
if you.body.contains(&Coord {
x: my_head.x + 1,
y: my_head.y,
}) {
is_move_safe.insert("right", false);
}
if you.body.contains(&Coord {
x: my_head.x,
y: my_head.y - 1,
}) {
is_move_safe.insert("down", false);
}
if you.body.contains(&Coord {
x: my_head.x,
y: my_head.y + 1,
}) {
is_move_safe.insert("up", false);
}
// TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes // TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
// let opponents = &board.snakes; // let opponents = &board.snakes;

View File

@ -43,7 +43,7 @@ pub struct Battlesnake {
shout: Option<String>, shout: Option<String>,
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Debug, PartialEq, Eq, Clone, Copy,Deserialize, Serialize)]
pub struct Coord { pub struct Coord {
x: i32, x: i32,
y: i32, y: i32,