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 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<Direction> {
self.possible_moves_without_heads(game, board)
pub fn possible_actions(&self, game: &Game, board: &Board) -> Vec<Direction> {
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::<Vec<_>>();
// 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<Move> {
let moves = you.possible_moves(game, board);
if moves.is_empty() {
pub fn get_move(game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Option<Action> {
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,
})

View File

@ -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<GameState>) -> Status {
}
#[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(
&move_req.game,
move_req.turn,