This commit is contained in:
Cadey Ratio 2019-12-18 01:37:11 +00:00
parent cea23a53fc
commit 4ece6f80a6
2 changed files with 142 additions and 19 deletions

View File

@ -28,16 +28,104 @@ pub struct Game {
#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] #[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct Board { pub struct Board {
pub height: usize, pub height: i8,
pub width: usize, pub width: i8,
pub food: Vec<Coord>, pub food: Vec<Coord>,
pub snakes: Vec<Snake>, pub snakes: Vec<Snake>,
} }
#[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 struct Coord {
pub x: usize, pub x: i8,
pub y: usize, 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> { pub struct Line<'a> {
@ -45,13 +133,40 @@ pub struct Line<'a> {
pub end: &'a Coord, pub end: &'a Coord,
} }
impl Line { impl Line<'_> {
fn manhattan<'a>(self: &'a Line) -> u32 { pub fn manhattan(&self) -> u32 {
let abs_x = (self.end.x as i32 - self.start.y as i32).abs(); 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(); let abs_y = (self.end.y as i32 - self.start.y as i32).abs();
(abs_x - abs_y) as u32 (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)] #[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)]

View File

@ -30,31 +30,39 @@ fn begin(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartRespons
#[post("/move", format = "json", data = "<msg>")] #[post("/move", format = "json", data = "<msg>")]
fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> { fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
let mut g = Grid::new(msg.board.height, msg.board.width); let target = find_target(&msg);
g.fill(); println!("target: {:?}", target);
let target = find_target(&msg, &g); let head = &msg.you.body[0];
println!("{:?}", target); for ne in &msg.board.safe_neighbors(&head) {
println!("{:?} {}", ne.0, battlesnake::Line{
start: &head,
end: &ne.0,
}.direction());
}
pathfinding::directed::astar::astar(
&head,
// do pathfinding? |n| msg.board.safe_neighbors(n).iter(),
|n| (battlesnake::manhattan(n, &target) as usize),
|n| n == &target,
);
Json(battlesnake::MoveResponse { Json(battlesnake::MoveResponse {
move_field: "up".to_string(), 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]; let head = &gs.you.body[0];
if gs.you.health > 30 { 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(); let mut coord: &battlesnake::Coord = &gs.you.body.last().unwrap();
for food in &gs.board.food { for food in &gs.board.food {
let score = battlesnake::Line{start: head, end: food}.manhattan(); let score = battlesnake::manhattan(&head, &food);
if score < lowestScore { if score < lowest_score {
lowestScore = score; lowest_score = score;
coord = food; coord = food;
} }
} }