pub struct Snake {
pub id: usize,
pub head: Position,
pub body: VecDeque<Position>,
pub direction: Direction,
pub length: usize,
pub alive: bool,
}Expand description
Individual snake in the game
Fields§
§id: usizeSnake ID (for multi-agent games)
head: PositionHead position
body: VecDeque<Position>Body positions (deque for efficient head/tail operations)
direction: DirectionCurrent direction
length: usizeLength of snake
alive: boolWhether the snake is alive
Implementations§
Source§impl Snake
impl Snake
Sourcepub fn new(id: usize, start_pos: Position, start_direction: Direction) -> Self
pub fn new(id: usize, start_pos: Position, start_direction: Direction) -> Self
Create new snake at position
Sourcepub fn move_forward(&mut self)
pub fn move_forward(&mut self)
Move snake in current direction
Sourcepub fn move_forward_wrap(&mut self, width: i32, height: i32)
pub fn move_forward_wrap(&mut self, width: i32, height: i32)
Move snake in current direction with wraparound boundaries (torus)
Sourcepub fn change_direction(&mut self, new_direction: Direction)
pub fn change_direction(&mut self, new_direction: Direction)
Change direction (with validation)
Sourcepub fn collides_with_wall(&self, width: i32, height: i32) -> bool
pub fn collides_with_wall(&self, width: i32, height: i32) -> bool
Check if snake collides with walls
Sourcepub fn collides_with_self(&self) -> bool
pub fn collides_with_self(&self) -> bool
Check if snake collides with itself
Sourcepub fn collides_with_other(&self, other: &Snake) -> bool
pub fn collides_with_other(&self, other: &Snake) -> bool
Check if snake collides with another snake
Sourcepub fn get_all_positions(&self) -> Vec<Position>
pub fn get_all_positions(&self) -> Vec<Position>
Get all positions occupied by snake
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Snake
impl RefUnwindSafe for Snake
impl Send for Snake
impl Sync for Snake
impl Unpin for Snake
impl UnsafeUnpin for Snake
impl UnwindSafe for Snake
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more