From 81ff66fddf7be1c0b247f968471ecd925c68b423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Mon, 2 Sep 2024 15:33:15 +0200 Subject: [PATCH] don't move out of bounds --- battlesnake/Cargo.toml | 5 +++++ battlesnake/src/logic.rs | 22 ++++++++++++++++------ battlesnake/src/main.rs | 10 ++++++---- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/battlesnake/Cargo.toml b/battlesnake/Cargo.toml index 773f77a..adde98a 100644 --- a/battlesnake/Cargo.toml +++ b/battlesnake/Cargo.toml @@ -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"] } diff --git a/battlesnake/src/logic.rs b/battlesnake/src/logic.rs index ca1d215..4c45878 100644 --- a/battlesnake/src/logic.rs +++ b/battlesnake/src/logic.rs @@ -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 }) } diff --git a/battlesnake/src/main.rs b/battlesnake/src/main.rs index c534a32..b54626a 100644 --- a/battlesnake/src/main.rs +++ b/battlesnake/src/main.rs @@ -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, snakes: Vec, @@ -64,7 +66,7 @@ fn handle_index() -> Json { fn handle_start(start_req: Json) -> 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) -> Status { fn handle_move(move_req: Json) -> Json { 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) -> Json { #[post("/end", format = "json", data = "")] fn handle_end(end_req: Json) -> 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 }