use enum for the direction
This commit is contained in:
parent
fc123cbd2b
commit
565f2aefb5
@ -15,7 +15,7 @@ use rand::seq::SliceRandom;
|
||||
use serde_json::{json, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{Battlesnake, Board, Coord, Game};
|
||||
use crate::{Battlesnake, Board, Coord, Direction, Game, Move};
|
||||
|
||||
// info is called when you create your Battlesnake on play.battlesnake.com
|
||||
// and controls your Battlesnake's appearance
|
||||
@ -25,7 +25,7 @@ pub fn info() -> Value {
|
||||
|
||||
json!({
|
||||
"apiversion": "1",
|
||||
"author": "Der Informatiker",
|
||||
"author": "der-informatiker",
|
||||
"color": "#00FFEE",
|
||||
"head": "smart-caterpillar",
|
||||
"tail": "mouse",
|
||||
@ -45,13 +45,10 @@ pub fn end(_game: &Game, _turn: i32, _board: &Board, _you: &Battlesnake) {
|
||||
// move is called on every turn and returns your next move
|
||||
// Valid moves are "up", "down", "left", or "right"
|
||||
// See https://docs.battlesnake.com/api/example-move for available data
|
||||
pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Value {
|
||||
let mut is_move_safe: HashMap<_, _> = vec![
|
||||
("up", true),
|
||||
("down", true),
|
||||
("left", true),
|
||||
("right", true),
|
||||
]
|
||||
pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Move {
|
||||
use Direction::{Down, Left, Right, Up};
|
||||
let mut is_move_safe: HashMap<_, _> =
|
||||
vec![(Up, true), (Down, true), (Left, true), (Right, true)]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
@ -61,30 +58,30 @@ pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Va
|
||||
|
||||
if my_neck.x < my_head.x {
|
||||
// Neck is left of head, don't move left
|
||||
is_move_safe.insert("left", false);
|
||||
is_move_safe.insert(Left, false);
|
||||
} else if my_neck.x > my_head.x {
|
||||
// Neck is right of head, don't move right
|
||||
is_move_safe.insert("right", false);
|
||||
is_move_safe.insert(Right, false);
|
||||
} else if my_neck.y < my_head.y {
|
||||
// Neck is below head, don't move down
|
||||
is_move_safe.insert("down", false);
|
||||
is_move_safe.insert(Down, false);
|
||||
} else if my_neck.y > my_head.y {
|
||||
// Neck is above head, don't move up
|
||||
is_move_safe.insert("up", false);
|
||||
is_move_safe.insert(Up, false);
|
||||
}
|
||||
|
||||
// TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
|
||||
if my_head.x == 0 {
|
||||
is_move_safe.insert("left", false);
|
||||
is_move_safe.insert(Left, false);
|
||||
}
|
||||
if my_head.x == board.width - 1 {
|
||||
is_move_safe.insert("right", false);
|
||||
is_move_safe.insert(Right, false);
|
||||
}
|
||||
if my_head.y == 0 {
|
||||
is_move_safe.insert("down", false);
|
||||
is_move_safe.insert(Down, false);
|
||||
}
|
||||
if my_head.y == board.height - 1 {
|
||||
is_move_safe.insert("up", false);
|
||||
is_move_safe.insert(Up, false);
|
||||
}
|
||||
|
||||
// TODO: Step 2 - Prevent your Battlesnake from colliding with itself
|
||||
@ -92,25 +89,25 @@ pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Va
|
||||
x: my_head.x - 1,
|
||||
y: my_head.y,
|
||||
}) {
|
||||
is_move_safe.insert("left", false);
|
||||
is_move_safe.insert(Left, false);
|
||||
}
|
||||
if you.body.contains(&Coord {
|
||||
x: my_head.x + 1,
|
||||
y: my_head.y,
|
||||
}) {
|
||||
is_move_safe.insert("right", false);
|
||||
is_move_safe.insert(Right, false);
|
||||
}
|
||||
if you.body.contains(&Coord {
|
||||
x: my_head.x,
|
||||
y: my_head.y - 1,
|
||||
}) {
|
||||
is_move_safe.insert("down", false);
|
||||
is_move_safe.insert(Down, false);
|
||||
}
|
||||
if you.body.contains(&Coord {
|
||||
x: my_head.x,
|
||||
y: my_head.y + 1,
|
||||
}) {
|
||||
is_move_safe.insert("up", false);
|
||||
is_move_safe.insert(Up, false);
|
||||
}
|
||||
|
||||
// TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
|
||||
@ -124,11 +121,14 @@ pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Va
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Choose a random move from the safe ones
|
||||
let chosen = safe_moves.choose(&mut rand::thread_rng()).unwrap();
|
||||
let chosen = safe_moves.choose(&mut rand::thread_rng()).unwrap_or(&Up);
|
||||
|
||||
// TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
|
||||
// let food = &board.food;
|
||||
|
||||
info!("MOVE {}: {}", turn, chosen);
|
||||
json!({ "move": chosen })
|
||||
info!("MOVE {}: {:?}", turn, chosen);
|
||||
Move {
|
||||
r#move: *chosen,
|
||||
shout: None,
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,29 @@ mod logic;
|
||||
// API and Response Objects
|
||||
// See https://docs.battlesnake.com/api
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize)]
|
||||
pub enum Direction {
|
||||
#[serde(rename = "left")]
|
||||
Left,
|
||||
#[serde(rename = "up")]
|
||||
Up,
|
||||
#[serde(rename = "right")]
|
||||
Right,
|
||||
#[serde(rename = "down")]
|
||||
Down,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Move {
|
||||
r#move: Direction,
|
||||
#[serde(default, skip_serializing_if = "is_default")]
|
||||
shout: Option<String>,
|
||||
}
|
||||
|
||||
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
|
||||
*value == T::default()
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Game {
|
||||
id: String,
|
||||
@ -75,7 +98,7 @@ fn handle_start(start_req: Json<GameState>) -> Status {
|
||||
}
|
||||
|
||||
#[post("/move", format = "json", data = "<move_req>")]
|
||||
fn handle_move(move_req: Json<GameState>) -> Json<Value> {
|
||||
fn handle_move(move_req: Json<GameState>) -> Json<Move> {
|
||||
let response = logic::get_move(
|
||||
&move_req.game,
|
||||
move_req.turn,
|
||||
|
Loading…
Reference in New Issue
Block a user