fix clippy warnings
This commit is contained in:
parent
bb356427df
commit
d1b0ef3ccf
@ -30,7 +30,8 @@ use crate::{
|
|||||||
// info is called when you create your Battlesnake on play.battlesnake.com
|
// info is called when you create your Battlesnake on play.battlesnake.com
|
||||||
// and controls your Battlesnake's appearance
|
// and controls your Battlesnake's appearance
|
||||||
// TIP: If you open your Battlesnake URL in a browser you should see this data
|
// TIP: If you open your Battlesnake URL in a browser you should see this data
|
||||||
#[must_use] pub fn info() -> Value {
|
#[must_use]
|
||||||
|
pub fn info() -> Value {
|
||||||
info!("INFO");
|
info!("INFO");
|
||||||
|
|
||||||
json!({
|
json!({
|
||||||
|
@ -3,12 +3,7 @@
|
|||||||
use battlesnake::{logic, Action, GameState};
|
use battlesnake::{logic, Action, GameState};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
fairing::AdHoc,
|
fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task,
|
||||||
get,
|
|
||||||
http::Status,
|
|
||||||
launch, post, routes,
|
|
||||||
serde::json::Json,
|
|
||||||
tokio::task,
|
|
||||||
};
|
};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
@ -14,7 +14,13 @@ pub struct SnakeToken {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SnakeToken {
|
impl SnakeToken {
|
||||||
#[must_use] pub fn from_board(board: &crate::Board) -> BTreeMap<String, Self> {
|
/// create a token map from the current game board.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This function panics when there are more than 256 snakes on the board.
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_board(board: &crate::Board) -> BTreeMap<String, Self> {
|
||||||
board
|
board
|
||||||
.snakes
|
.snakes
|
||||||
.iter()
|
.iter()
|
||||||
@ -52,7 +58,8 @@ pub struct Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
#[must_use] pub fn from_game_board(
|
#[must_use]
|
||||||
|
pub fn from_game_board(
|
||||||
board: &crate::Board,
|
board: &crate::Board,
|
||||||
token_map: &BTreeMap<String, SnakeToken>,
|
token_map: &BTreeMap<String, SnakeToken>,
|
||||||
turn: i32,
|
turn: i32,
|
||||||
@ -86,16 +93,19 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub const fn turn(&self) -> i32 {
|
#[must_use]
|
||||||
|
pub const fn turn(&self) -> i32 {
|
||||||
self.turn
|
self.turn
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cast_sign_loss)]
|
#[allow(clippy::cast_sign_loss)]
|
||||||
#[must_use] pub const fn spaces(&self) -> usize {
|
#[must_use]
|
||||||
|
pub const fn spaces(&self) -> usize {
|
||||||
self.height as usize * self.width as usize
|
self.height as usize * self.width as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn alive_snakes(&self) -> usize {
|
#[must_use]
|
||||||
|
pub fn alive_snakes(&self) -> usize {
|
||||||
self.snakes.len()
|
self.snakes.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +113,8 @@ impl Board {
|
|||||||
self.snakes.keys().copied()
|
self.snakes.keys().copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn snake_length(&self, token: SnakeToken) -> Option<usize> {
|
#[must_use]
|
||||||
|
pub fn snake_length(&self, token: SnakeToken) -> Option<usize> {
|
||||||
self.snakes.get(&token).map(|snake| snake.body.len())
|
self.snakes.get(&token).map(|snake| snake.body.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +207,8 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn possible_actions(&self) -> BTreeMap<SnakeToken, BTreeSet<Direction>> {
|
#[must_use]
|
||||||
|
pub fn possible_actions(&self) -> BTreeMap<SnakeToken, BTreeSet<Direction>> {
|
||||||
let mut actions: BTreeMap<_, BTreeSet<_>> = self
|
let mut actions: BTreeMap<_, BTreeSet<_>> = self
|
||||||
.snakes
|
.snakes
|
||||||
.keys()
|
.keys()
|
||||||
@ -236,11 +248,12 @@ impl Board {
|
|||||||
.iter()
|
.iter()
|
||||||
.sorted_unstable_by(|(_, snake1), (_, snake2)| snake2.health.cmp(&snake1.health))
|
.sorted_unstable_by(|(_, snake1), (_, snake2)| snake2.health.cmp(&snake1.health))
|
||||||
.map(|(token, snake)| {
|
.map(|(token, snake)| {
|
||||||
if actions[token].len() == 1 {
|
let actions = &actions[token];
|
||||||
(
|
if actions.len() == 1 {
|
||||||
snake.body.len(),
|
let Some(action) = actions.first() else {
|
||||||
Some(snake.head().move_to(*actions[token].first().unwrap())),
|
unreachable!()
|
||||||
)
|
};
|
||||||
|
(snake.body.len(), Some(snake.head().move_to(*action)))
|
||||||
} else {
|
} else {
|
||||||
(snake.body.len(), None)
|
(snake.body.len(), None)
|
||||||
}
|
}
|
||||||
@ -272,11 +285,14 @@ pub struct Battlesnake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Battlesnake {
|
impl Battlesnake {
|
||||||
#[must_use] pub fn from_game_snake(snake: &crate::Battlesnake) -> Self {
|
#[must_use]
|
||||||
|
pub fn from_game_snake(snake: &crate::Battlesnake) -> Self {
|
||||||
let body: VecDeque<_> = snake.body.iter().copied().collect();
|
let body: VecDeque<_> = snake.body.iter().copied().collect();
|
||||||
debug_assert_eq!(body.len(), usize::try_from(snake.length).unwrap());
|
debug_assert_eq!(Ok(body.len()), usize::try_from(snake.length));
|
||||||
debug_assert!(snake.health <= crate::MAX_HEALTH);
|
debug_assert!(snake.health <= crate::MAX_HEALTH);
|
||||||
let health = u8::try_from(snake.health).expect("max health is 100");
|
let Ok(health) = u8::try_from(snake.health.min(100)) else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
Self { health, body }
|
Self { health, body }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,8 +311,9 @@ impl Battlesnake {
|
|||||||
self.health = self.health.saturating_sub(1);
|
self.health = self.health.saturating_sub(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use] pub fn head(&self) -> &Coord {
|
#[must_use]
|
||||||
|
pub fn head(&self) -> &Coord {
|
||||||
debug_assert!(!self.body.is_empty());
|
debug_assert!(!self.body.is_empty());
|
||||||
self.body.front().expect("not empty")
|
self.body.front().unwrap_or_else(|| unreachable!())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user