Skip to content

Commit

Permalink
Format and replace magic numbers (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanjameswatson authored Mar 12, 2022
1 parent 7220908 commit 456591a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/game/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,25 @@ const BOARD_MAX_X: i32 = 5;
const BOARD_MIN_Y: i32 = -5;
const BOARD_MAX_Y: i32 = 5;

const EMPTY_FRUIT_PROB: f64 = 0.5;
const NON_EMPTY_FRUIT_PROB: f64 = 0.15;

const STARTS: [[Point2D; 3]; NUM_PLAYERS] = [
[Point2D::new(-3, -2), Point2D::new(-3, -3), Point2D::new(-2, -3)],
[Point2D::new(-2, 3), Point2D::new(-3, 3), Point2D::new(-3, 2)],
[Point2D::new(2, -3), Point2D::new(3, -3), Point2D::new(3, -2)],
[
Point2D::new(-3, -2),
Point2D::new(-3, -3),
Point2D::new(-2, -3),
],
[
Point2D::new(-2, 3),
Point2D::new(-3, 3),
Point2D::new(-3, 2),
],
[
Point2D::new(2, -3),
Point2D::new(3, -3),
Point2D::new(3, -2),
],
[Point2D::new(3, 2), Point2D::new(3, 3), Point2D::new(2, 3)],
];

Expand Down Expand Up @@ -265,10 +280,11 @@ impl Snake {
self.moves.clear();

let fruit_prob = if self.state.fruits.is_empty() {
0.5
EMPTY_FRUIT_PROB
} else {
0.15
NON_EMPTY_FRUIT_PROB
};

if rand::thread_rng().gen_bool(fruit_prob) {
// Attempt to spawn a single new fruit
for _ in 0..10 {
Expand Down

0 comments on commit 456591a

Please sign in to comment.