rename move to action

This commit is contained in:
Max Känner 2024-09-30 19:40:02 +02:00
parent 46f4e00e50
commit 39e84b3783
2 changed files with 15 additions and 15 deletions

View File

@ -14,12 +14,12 @@ use log::info;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde_json::{json, Value}; use serde_json::{json, Value};
use crate::{Battlesnake, Board, Direction, Game, Move}; use crate::{Action, Battlesnake, Board, Direction, Game};
const MAX_HEALTH: i32 = 100; const MAX_HEALTH: i32 = 100;
impl Battlesnake { impl Battlesnake {
fn possible_moves_without_heads<'a>( fn possible_actions_without_heads<'a>(
&'a self, &'a self,
game: &'a Game, game: &'a Game,
board: &'a Board, board: &'a Board,
@ -53,8 +53,8 @@ impl Battlesnake {
} }
#[must_use] #[must_use]
pub fn possible_moves(&self, game: &Game, board: &Board) -> Vec<Direction> { pub fn possible_actions(&self, game: &Game, board: &Board) -> Vec<Direction> {
self.possible_moves_without_heads(game, board) self.possible_actions_without_heads(game, board)
.filter(|direction| { .filter(|direction| {
// don't go into spots where a bigger snake must go // don't go into spots where a bigger snake must go
let target = self.head.move_to(*direction); let target = self.head.move_to(*direction);
@ -67,11 +67,11 @@ impl Battlesnake {
}) })
.filter_map(|snake| { .filter_map(|snake| {
// get all snakes movement options // get all snakes movement options
let moves = snake let actions = snake
.possible_moves_without_heads(game, board) .possible_actions_without_heads(game, board)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// only snakes that have a single option // only snakes that have a single option
match moves[..] { match actions[..] {
[direction] => Some(snake.head.move_to(direction)), [direction] => Some(snake.head.move_to(direction)),
_ => None, _ => 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 // 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) -> Option<Move> { pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Option<Action> {
let moves = you.possible_moves(game, board); let actions = you.possible_actions(game, board);
if moves.is_empty() { if actions.is_empty() {
return None; return None;
} }
// Choose a random move from the safe ones // 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); info!("DIRECTION {}: {:?}", turn, chosen);
Some(Move { Some(Action {
r#move: *chosen, r#move: *chosen,
shout: None, shout: None,
}) })

View File

@ -29,7 +29,7 @@ pub enum Direction {
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct Move { pub struct Action {
/// In which direction the snake should move /// In which direction the snake should move
r#move: Direction, r#move: Direction,
/// Say something to the other snakes /// Say something to the other snakes
@ -203,7 +203,7 @@ fn handle_start(start_req: Json<GameState>) -> Status {
} }
#[post("/move", format = "json", data = "<move_req>")] #[post("/move", format = "json", data = "<move_req>")]
fn handle_move(move_req: Json<GameState>) -> Option<Json<Move>> { fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> {
let response = logic::get_move( let response = logic::get_move(
&move_req.game, &move_req.game,
move_req.turn, move_req.turn,