don't move out of bounds
This commit is contained in:
parent
c605d2887a
commit
81ff66fddf
@ -5,11 +5,16 @@ version = "1.0.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
repository = "https://git.mkaenner.de/max/battlesnake"
|
||||||
keywords = ["battlesnake"]
|
keywords = ["battlesnake"]
|
||||||
description = """
|
description = """
|
||||||
A simple Battlesnake written in Rust
|
A simple Battlesnake written in Rust
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
pedantic = "warn"
|
||||||
|
nursery = "warn"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = { version = "0.5.0", features = ["json"] }
|
rocket = { version = "0.5.0", features = ["json"] }
|
||||||
serde = { version = "1.0.117", features = ["derive"] }
|
serde = { version = "1.0.117", features = ["derive"] }
|
||||||
|
@ -33,19 +33,19 @@ pub fn info() -> Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start is called when your Battlesnake begins a game
|
// start is called when your Battlesnake begins a game
|
||||||
pub fn start(_game: &Game, _turn: &i32, _board: &Board, _you: &Battlesnake) {
|
pub fn start(_game: &Game, _turn: i32, _board: &Board, _you: &Battlesnake) {
|
||||||
info!("GAME START");
|
info!("GAME START");
|
||||||
}
|
}
|
||||||
|
|
||||||
// end is called when your Battlesnake finishes a game
|
// end is called when your Battlesnake finishes a game
|
||||||
pub fn end(_game: &Game, _turn: &i32, _board: &Board, _you: &Battlesnake) {
|
pub fn end(_game: &Game, _turn: i32, _board: &Board, _you: &Battlesnake) {
|
||||||
info!("GAME OVER");
|
info!("GAME OVER");
|
||||||
}
|
}
|
||||||
|
|
||||||
// move is called on every turn and returns your next move
|
// move is called on every turn and returns your next move
|
||||||
// Valid moves are "up", "down", "left", or "right"
|
// Valid moves are "up", "down", "left", or "right"
|
||||||
// See https://docs.battlesnake.com/api/example-move for available data
|
// See https://docs.battlesnake.com/api/example-move for available data
|
||||||
pub fn get_move(_game: &Game, turn: &i32, _board: &Board, you: &Battlesnake) -> Value {
|
pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Value {
|
||||||
let mut is_move_safe: HashMap<_, _> = vec![
|
let mut is_move_safe: HashMap<_, _> = vec![
|
||||||
("up", true),
|
("up", true),
|
||||||
("down", true),
|
("down", true),
|
||||||
@ -74,8 +74,18 @@ pub fn get_move(_game: &Game, turn: &i32, _board: &Board, you: &Battlesnake) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
|
// TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
|
||||||
// let board_width = &board.width;
|
if my_head.x == 0 {
|
||||||
// let board_height = &board.height;
|
is_move_safe.insert("left", false);
|
||||||
|
}
|
||||||
|
if my_head.x == board.width - 1 {
|
||||||
|
is_move_safe.insert("right", false);
|
||||||
|
}
|
||||||
|
if my_head.y == 0 {
|
||||||
|
is_move_safe.insert("down", false);
|
||||||
|
}
|
||||||
|
if my_head.y == board.height - 1 {
|
||||||
|
is_move_safe.insert("up", false);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Step 2 - Prevent your Battlesnake from colliding with itself
|
// TODO: Step 2 - Prevent your Battlesnake from colliding with itself
|
||||||
// let my_body = &you.body;
|
// let my_body = &you.body;
|
||||||
@ -97,5 +107,5 @@ pub fn get_move(_game: &Game, turn: &i32, _board: &Board, you: &Battlesnake) ->
|
|||||||
// let food = &board.food;
|
// let food = &board.food;
|
||||||
|
|
||||||
info!("MOVE {}: {}", turn, chosen);
|
info!("MOVE {}: {}", turn, chosen);
|
||||||
return json!({ "move": chosen });
|
json!({ "move": chosen })
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![allow(clippy::needless_pass_by_value)]
|
||||||
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use rocket::fairing::AdHoc;
|
use rocket::fairing::AdHoc;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
@ -22,7 +24,7 @@ pub struct Game {
|
|||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
height: u32,
|
height: i32,
|
||||||
width: i32,
|
width: i32,
|
||||||
food: Vec<Coord>,
|
food: Vec<Coord>,
|
||||||
snakes: Vec<Battlesnake>,
|
snakes: Vec<Battlesnake>,
|
||||||
@ -64,7 +66,7 @@ fn handle_index() -> Json<Value> {
|
|||||||
fn handle_start(start_req: Json<GameState>) -> Status {
|
fn handle_start(start_req: Json<GameState>) -> Status {
|
||||||
logic::start(
|
logic::start(
|
||||||
&start_req.game,
|
&start_req.game,
|
||||||
&start_req.turn,
|
start_req.turn,
|
||||||
&start_req.board,
|
&start_req.board,
|
||||||
&start_req.you,
|
&start_req.you,
|
||||||
);
|
);
|
||||||
@ -76,7 +78,7 @@ fn handle_start(start_req: Json<GameState>) -> Status {
|
|||||||
fn handle_move(move_req: Json<GameState>) -> Json<Value> {
|
fn handle_move(move_req: Json<GameState>) -> Json<Value> {
|
||||||
let response = logic::get_move(
|
let response = logic::get_move(
|
||||||
&move_req.game,
|
&move_req.game,
|
||||||
&move_req.turn,
|
move_req.turn,
|
||||||
&move_req.board,
|
&move_req.board,
|
||||||
&move_req.you,
|
&move_req.you,
|
||||||
);
|
);
|
||||||
@ -86,7 +88,7 @@ fn handle_move(move_req: Json<GameState>) -> Json<Value> {
|
|||||||
|
|
||||||
#[post("/end", format = "json", data = "<end_req>")]
|
#[post("/end", format = "json", data = "<end_req>")]
|
||||||
fn handle_end(end_req: Json<GameState>) -> Status {
|
fn handle_end(end_req: Json<GameState>) -> Status {
|
||||||
logic::end(&end_req.game, &end_req.turn, &end_req.board, &end_req.you);
|
logic::end(&end_req.game, end_req.turn, &end_req.board, &end_req.you);
|
||||||
|
|
||||||
Status::Ok
|
Status::Ok
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user