From 4ece6f80a64eb52c95b25a299e65a835d416e323 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 18 Dec 2019 01:37:11 +0000 Subject: [PATCH] what --- src/battlesnake.rs | 129 ++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 32 ++++++----- 2 files changed, 142 insertions(+), 19 deletions(-) diff --git a/src/battlesnake.rs b/src/battlesnake.rs index 1817ff7..18cbd79 100644 --- a/src/battlesnake.rs +++ b/src/battlesnake.rs @@ -28,16 +28,104 @@ pub struct Game { #[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] pub struct Board { - pub height: usize, - pub width: usize, + pub height: i8, + pub width: i8, pub food: Vec, pub snakes: Vec, } -#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] +impl Board { + pub fn inside(&self, val: &Coord) -> bool { + if val.x < 0 || val.y < 0 { + return false; + } + + if val.x > self.width { + return false; + } + + if val.y > self.height { + return false; + } + + true + } + + pub fn is_safe(&self, loc: &Coord) -> bool { + if !self.inside(loc) {return false;} + + for sn in &self.snakes { + for bd in &sn.body { + if loc == bd { + return false; + } + } + } + + true + } + + pub fn safe_neighbors(&self, loc: &Coord) -> Vec<(Coord, usize)> { + let mut result = Vec::<(Coord, usize)>::new(); + let left = loc.left(); + let right = loc.right(); + let up = loc.up(); + let down = loc.down(); + + if self.is_safe(&left) { + result.push((left, 1)); + } + + if self.is_safe(&right) { + result.push((right, 1)); + } + + if self.is_safe(&up) { + result.push((up, 1)); + } + + if self.is_safe(&down) { + result.push((up, 1)); + } + + result + } +} + +#[derive(Default, Debug, Copy, Clone, Eq, Hash, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] pub struct Coord { - pub x: usize, - pub y: usize, + pub x: i8, + pub y: i8, +} + +impl Coord { + pub fn left(&self) -> Coord { + Coord { + x: self.x - 1, + y: self.y, + } + } + + pub fn right(&self) -> Coord { + Coord{ + x: self.x + 1, + y: self.y, + } + } + + pub fn up(&self) -> Coord { + Coord{ + x: self.x, + y: self.y - 1, + } + } + + pub fn down(&self) -> Coord { + Coord{ + x: self.x, + y: self.y + 1, + } + } } pub struct Line<'a> { @@ -45,13 +133,40 @@ pub struct Line<'a> { pub end: &'a Coord, } -impl Line { - fn manhattan<'a>(self: &'a Line) -> u32 { +impl Line<'_> { + pub fn manhattan(&self) -> 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 } + + pub fn direction(&self) -> &str { + if self.start.x > self.end.x { + return "left"; + } + + if self.start.x < self.end.x { + return "right"; + } + + if self.start.y > self.end.y { + return "up"; + } + + if self.start.y < self.end.y{ + return "down"; + } + + "what" + } +} + +pub fn manhattan<'a>(a: &'a Coord, b: &'a Coord) -> u32 { + Line{ + start: &a, + end: &b, + }.manhattan() } #[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] diff --git a/src/main.rs b/src/main.rs index 8bf1b85..0419895 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,31 +30,39 @@ fn begin(msg: Json) -> Json) -> Json { - let mut g = Grid::new(msg.board.height, msg.board.width); - g.fill(); + let target = find_target(&msg); + println!("target: {:?}", target); - let target = find_target(&msg, &g); - println!("{:?}", target); + let head = &msg.you.body[0]; + for ne in &msg.board.safe_neighbors(&head) { + println!("{:?} {}", ne.0, battlesnake::Line{ + start: &head, + end: &ne.0, + }.direction()); + } - - - // do pathfinding? + pathfinding::directed::astar::astar( + &head, + |n| msg.board.safe_neighbors(n).iter(), + |n| (battlesnake::manhattan(n, &target) as usize), + |n| n == &target, + ); Json(battlesnake::MoveResponse { move_field: "up".to_string(), }) } -fn find_target<'a>(gs: &'a battlesnake::SnakeRequest, g: &Grid) -> &'a battlesnake::Coord { +fn find_target<'a>(gs: &'a battlesnake::SnakeRequest) -> &'a battlesnake::Coord { let head = &gs.you.body[0]; if gs.you.health > 30 { - let mut lowestScore: u32 = 99999; + let mut lowest_score: u32 = 99999; let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap(); for food in &gs.board.food { - let score = battlesnake::Line{start: head, end: food}.manhattan(); - if score < lowestScore { - lowestScore = score; + let score = battlesnake::manhattan(&head, &food); + if score < lowest_score { + lowest_score = score; coord = food; } }