Snake daily: first food depends on the previous run, so the board differs per player

Open Beginner friendly
#88 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
Under an hour
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
game-dev

Research direction

Start in games/snake/scenes/GameScene.ts: init() (around lines 105-123) resets the per-run state but not this.food, and create() (line 141) calls randomFreeCell() (lines 462-468), which still sees the previous run's food. Declare food as private food: Cell | undefined and clear it in init(), then check that freeCells in games/snake/logic.ts and the seeded pick in shared/rng.ts behave as the issue describes. Done means the first daily food cell matches across a fresh load, PLAY AGAIN and RESTART, normal mode is unchanged, and npm test passes.

Written by the indexing model from the issue text.

Description

bug good first issue severity: medium

Summary

In Neon Snake's daily challenge, where the first food appears depends on the last food of the previous run played on the same page. The board is supposed to be the same for everyone who steers the same way, but it isn't.

Where

  • games/snake/scenes/GameScene.ts:105-123 (init, never resets food)
  • games/snake/scenes/GameScene.ts:141 (create, this.food = this.randomFreeCell())
  • games/snake/scenes/GameScene.ts:462-468 (randomFreeCell)

Details

Phaser reuses the scene instance, and init() resets bonus, score, eaten and the rest, but not this.food. On the second and later runs on a page, create() calls randomFreeCell() while this.food still holds the previous run's last food cell:

private randomFreeCell(): Cell {
  const taken = this.players.flatMap((p) => p.segments);
  if (this.food) taken.push(this.food);
  if (this.bonus) taken.push(this.bonus.cell);
  const free = freeCells(taken);
  return this.cellRng ? this.cellRng.pick(free) : Phaser.Utils.Array.GetRandom(free);
}

freeCells() lists cells in row-major order, so excluding one extra cell shifts every index after it. The seeded cellRng.pick(free) then lands on a different cell, and every later pick follows from that different board.

Impact

Daily scores are not comparable. A player who played a normal run, a previous daily run, or pressed RESTART before the daily gets a different food sequence from a player who opened the daily fresh. Replaying the daily on the same page is not the same board either.

Steps to reproduce

  1. Open games/snake/?mode=daily, start DAILY, and note the first food cell. Then die.
  2. Choose PLAY AGAIN.
  3. The first food cell is often different (whenever the previous run's last food sat before the picked index).

Suggested fix

Clear this.food in init() (declare it as private food: Cell | undefined), so the first randomFreeCell() of every run sees only the snake.

Acceptance criteria

  • The first daily food cell is the same on a fresh page load and after PLAY AGAIN, RESTART or a normal run.
  • Normal mode behaviour is unchanged.

Getting started

  • Relevant files: games/snake/scenes/GameScene.ts, games/snake/logic.ts (freeCells), shared/rng.ts (dailyRng)
  • How to run tests: npm test
  • Related: #78 (Snake daily mode)

Filed by repo-audit/full-audit at de2b25b. Severity: medium. Effort: S. audit-key: bug|games/snake/scenes/GameScene.ts|randomFreeCell

Dominant language
TypeScript
Stars
3
Forks
3
Avg merge
8h 13m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TMHSDigital/GH-Arcade

All issues in TMHSDigital/GH-Arcade

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.