don't move out of bounds

This commit is contained in:
Max Känner 2024-09-02 15:33:15 +02:00
parent c605d2887a
commit 81ff66fddf
3 changed files with 27 additions and 10 deletions

View File

@ -5,11 +5,16 @@ version = "1.0.0"
edition = "2021"
readme = "README.md"
repository = "https://git.mkaenner.de/max/battlesnake"
keywords = ["battlesnake"]
description = """
A simple Battlesnake written in Rust
"""
[lints.clippy]
pedantic = "warn"
nursery = "warn"
[dependencies]
rocket = { version = "0.5.0", features = ["json"] }
serde = { version = "1.0.117", features = ["derive"] }

View File

@ -33,19 +33,19 @@ pub fn info() -> Value {
}
// 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");
}
// 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");
}
// 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 {
pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Value {
let mut is_move_safe: HashMap<_, _> = vec![
("up", 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
// let board_width = &board.width;
// let board_height = &board.height;
if my_head.x == 0 {
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
// 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;
info!("MOVE {}: {}", turn, chosen);
return json!({ "move": chosen });
json!({ "move": chosen })
}

View File

@ -1,3 +1,5 @@
#![allow(clippy::needless_pass_by_value)]
use log::info;
use rocket::fairing::AdHoc;
use rocket::http::Status;
@ -22,7 +24,7 @@ pub struct Game {
#[derive(Deserialize, Serialize, Debug)]
pub struct Board {
height: u32,
height: i32,
width: i32,
food: Vec<Coord>,
snakes: Vec<Battlesnake>,
@ -64,7 +66,7 @@ fn handle_index() -> Json<Value> {
fn handle_start(start_req: Json<GameState>) -> Status {
logic::start(
&start_req.game,
&start_req.turn,
start_req.turn,
&start_req.board,
&start_req.you,
);
@ -76,7 +78,7 @@ fn handle_start(start_req: Json<GameState>) -> Status {
fn handle_move(move_req: Json<GameState>) -> Json<Value> {
let response = logic::get_move(
&move_req.game,
&move_req.turn,
move_req.turn,
&move_req.board,
&move_req.you,
);
@ -86,7 +88,7 @@ fn handle_move(move_req: Json<GameState>) -> Json<Value> {
#[post("/end", format = "json", data = "<end_req>")]
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
}