more broken
This commit is contained in:
parent
0379f2dc64
commit
cea23a53fc
|
@ -40,6 +40,20 @@ pub struct Coord {
|
|||
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)]
|
||||
pub struct Snake {
|
||||
pub id: String,
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -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);
|
||||
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);
|
||||
println!("{:?}", target);
|
||||
|
||||
|
||||
|
||||
// do pathfinding?
|
||||
|
||||
battlesnake::MoveResponse {
|
||||
Json(battlesnake::MoveResponse {
|
||||
move_field: "up".to_string(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn find_target(gs: &battlesnake::SnakeRequest, g: &Grid) -> &battlesnake::Coord {
|
||||
let head = gs.you.body[0];
|
||||
fn find_target<'a>(gs: &'a battlesnake::SnakeRequest, g: &Grid) -> &'a battlesnake::Coord {
|
||||
let head = &gs.you.body[0];
|
||||
if gs.you.health > 30 {
|
||||
let mut lowestScore: usize = 99999;
|
||||
let mut coord: &battlesnake::Coord;
|
||||
let mut lowestScore: u32 = 99999;
|
||||
let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap();
|
||||
|
||||
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 {
|
||||
lowestScore = score;
|
||||
coord = food;
|
||||
|
@ -73,5 +70,6 @@ fn main() {
|
|||
index,
|
||||
begin,
|
||||
ping,
|
||||
make_move,
|
||||
]).launch();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue