more broken

This commit is contained in:
Cadey Ratio 2019-12-18 00:29:00 +00:00
parent 0379f2dc64
commit cea23a53fc
2 changed files with 25 additions and 13 deletions

View File

@ -40,6 +40,20 @@ pub struct Coord {
pub y: usize, pub y: usize,
} }
pub struct Line<'a> {
pub start: &'a Coord,
pub end: &'a Coord,
}
impl Line {
fn manhattan<'a>(self: &'a Line) -> u32 {
let abs_x = (self.end.x as i32 - self.start.y as i32).abs();
let abs_y = (self.end.y as i32 - self.start.y as i32).abs();
(abs_x - abs_y) as u32
}
}
#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] #[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct Snake { pub struct Snake {
pub id: String, pub id: String,

View File

@ -33,29 +33,26 @@ fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResp
let mut g = Grid::new(msg.board.height, msg.board.width); let mut g = Grid::new(msg.board.height, msg.board.width);
g.fill(); g.fill();
for sn in &msg.board.snakes {
for bd in &sn.body {
g.remove_vertex((bd.x, bd.y))
}
}
let target = find_target(&msg, &g); let target = find_target(&msg, &g);
println!("{:?}", target);
// do pathfinding? // do pathfinding?
battlesnake::MoveResponse { Json(battlesnake::MoveResponse {
move_field: "up".to_string(), move_field: "up".to_string(),
} })
} }
fn find_target(gs: &battlesnake::SnakeRequest, g: &Grid) -> &battlesnake::Coord { fn find_target<'a>(gs: &'a battlesnake::SnakeRequest, g: &Grid) -> &'a battlesnake::Coord {
let head = gs.you.body[0]; let head = &gs.you.body[0];
if gs.you.health > 30 { if gs.you.health > 30 {
let mut lowestScore: usize = 99999; let mut lowestScore: u32 = 99999;
let mut coord: &battlesnake::Coord; let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap();
for food in &gs.board.food { for food in &gs.board.food {
let score = g.distance(&(head.x, head.y), &(food.x, food.y)); let score = battlesnake::Line{start: head, end: food}.manhattan();
if score < lowestScore { if score < lowestScore {
lowestScore = score; lowestScore = score;
coord = food; coord = food;
@ -73,5 +70,6 @@ fn main() {
index, index,
begin, begin,
ping, ping,
make_move,
]).launch(); ]).launch();
} }