From 39e84b3783b9114574cfdb962bab4ed7c2c2efa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Mon, 30 Sep 2024 19:40:02 +0200 Subject: [PATCH] rename move to action --- battlesnake/src/logic.rs | 26 +++++++++++++------------- battlesnake/src/main.rs | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/battlesnake/src/logic.rs b/battlesnake/src/logic.rs index 5d4dcc4..6e2b0e8 100644 --- a/battlesnake/src/logic.rs +++ b/battlesnake/src/logic.rs @@ -14,12 +14,12 @@ use log::info; use rand::seq::SliceRandom; use serde_json::{json, Value}; -use crate::{Battlesnake, Board, Direction, Game, Move}; +use crate::{Action, Battlesnake, Board, Direction, Game}; const MAX_HEALTH: i32 = 100; impl Battlesnake { - fn possible_moves_without_heads<'a>( + fn possible_actions_without_heads<'a>( &'a self, game: &'a Game, board: &'a Board, @@ -53,8 +53,8 @@ impl Battlesnake { } #[must_use] - pub fn possible_moves(&self, game: &Game, board: &Board) -> Vec { - self.possible_moves_without_heads(game, board) + pub fn possible_actions(&self, game: &Game, board: &Board) -> Vec { + self.possible_actions_without_heads(game, board) .filter(|direction| { // don't go into spots where a bigger snake must go let target = self.head.move_to(*direction); @@ -67,11 +67,11 @@ impl Battlesnake { }) .filter_map(|snake| { // get all snakes movement options - let moves = snake - .possible_moves_without_heads(game, board) + let actions = snake + .possible_actions_without_heads(game, board) .collect::>(); // only snakes that have a single option - match moves[..] { + match actions[..] { [direction] => Some(snake.head.move_to(direction)), _ => None, } @@ -110,17 +110,17 @@ 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) -> Option { - let moves = you.possible_moves(game, board); - if moves.is_empty() { +pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Option { + let actions = you.possible_actions(game, board); + if actions.is_empty() { return None; } // Choose a random move from the safe ones - let chosen = moves.choose(&mut rand::thread_rng())?; + let chosen = actions.choose(&mut rand::thread_rng())?; - info!("MOVE {}: {:?}", turn, chosen); - Some(Move { + info!("DIRECTION {}: {:?}", turn, chosen); + Some(Action { r#move: *chosen, shout: None, }) diff --git a/battlesnake/src/main.rs b/battlesnake/src/main.rs index a3a5c5e..f41bd69 100644 --- a/battlesnake/src/main.rs +++ b/battlesnake/src/main.rs @@ -29,7 +29,7 @@ pub enum Direction { } #[derive(Debug, Deserialize, Serialize)] -pub struct Move { +pub struct Action { /// In which direction the snake should move r#move: Direction, /// Say something to the other snakes @@ -203,7 +203,7 @@ fn handle_start(start_req: Json) -> Status { } #[post("/move", format = "json", data = "")] -fn handle_move(move_req: Json) -> Option> { +fn handle_move(move_req: Json) -> Option> { let response = logic::get_move( &move_req.game, move_req.turn,