feat(scala): m01–m06 port with shared fixtures
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
045029639e
commit
1e186733d1
23 files changed
+771
-2
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package snake
|
||||
|
||||
/** m04 spawn: placing apples with the Random from Random.scala. */
|
||||
object Spawn:
|
||||
|
||||
def freeCells(state: GameState): List[Point] =
|
||||
for
|
||||
y <- Util.range(state.height)
|
||||
x <- Util.range(state.width)
|
||||
p = Point(x, y)
|
||||
if !Util.contains(state.snake, p, _ == _)
|
||||
yield p
|
||||
|
||||
def spawnApple(state: GameState): GameState =
|
||||
if state.apple.isDefined || state.status != Status.Playing then state
|
||||
else
|
||||
val cells = freeCells(state)
|
||||
if cells.isEmpty then state.copy(status = Status.Won)
|
||||
else
|
||||
val (i, seed) = Random.intBelow(cells.length).run(state.seed)
|
||||
state.copy(apple = Some(cells(i)), seed = seed)
|
||||
|
||||
def tick(state: GameState): GameState = spawnApple(Rules.advance(state))
|
||||
|
||||
def newGame(seed: Long): GameState = spawnApple(Model.initialState(seed))
|
||||
Reference in new issue
Block a user