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)]
pub struct Board {
pub height: usize,
pub width: usize,
pub height: i8,
pub width: i8,
pub food: Vec<Coord>,
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 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)]

View File

@ -30,31 +30,39 @@ fn begin(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::StartRespons
#[post("/move", format = "json", data = "<msg>")]
fn make_move(msg: Json<battlesnake::SnakeRequest>) -> Json<battlesnake::MoveResponse> {
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;
}
}