feat(js): m03 rules with fixtures and tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas Winther
2026-10-28 23:41:34 +01:00
co-authored by Claude Sonnet 5.5
parent e9c41fed03
commit 2e39f23bb3
7 changed files with 1570 additions and 0 deletions
+716
View File
@@ -0,0 +1,716 @@
{
"function": "advance",
"cases": [
{
"name": "advance-plain-move",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 3,
"y": 2
},
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "advance-moves-in-nextDir-and-records-it-as-dir",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 1
},
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
}
],
"dir": "Up",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "advance-keeps-apple-it-did-not-reach",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": {
"x": 4,
"y": 4
},
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 3,
"y": 2
},
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": {
"x": 4,
"y": 4
},
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "advance-eat-grows-scores-clears-apple",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": {
"x": 3,
"y": 2
},
"seed": 1,
"score": 4,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 3,
"y": 2
},
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 5,
"status": "Playing",
"frame": 0
}
},
{
"name": "advance-into-wall-is-game-over",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 4,
"y": 2
},
{
"x": 3,
"y": 2
},
{
"x": 2,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 4,
"y": 2
},
{
"x": 3,
"y": 2
},
{
"x": 2,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
},
{
"name": "advance-into-top-wall-records-turn",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 0
},
{
"x": 2,
"y": 1
},
{
"x": 2,
"y": 2
}
],
"dir": "Up",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 0
},
{
"x": 2,
"y": 1
},
{
"x": 2,
"y": 2
}
],
"dir": "Up",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
},
{
"name": "advance-into-own-body-is-game-over",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 3,
"y": 2
},
{
"x": 3,
"y": 3
},
{
"x": 2,
"y": 3
},
{
"x": 1,
"y": 3
}
],
"dir": "Left",
"nextDir": "Down",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 3,
"y": 2
},
{
"x": 3,
"y": 3
},
{
"x": 2,
"y": 3
},
{
"x": 1,
"y": 3
}
],
"dir": "Down",
"nextDir": "Down",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
},
{
"name": "advance-into-vacating-tail",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 1,
"y": 1
},
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 2
},
{
"x": 2,
"y": 1
}
],
"dir": "Up",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 1
},
{
"x": 1,
"y": 1
},
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "advance-into-tail-while-eating",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 1,
"y": 1
},
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 2
},
{
"x": 2,
"y": 1
}
],
"dir": "Up",
"nextDir": "Right",
"apple": {
"x": 2,
"y": 1
},
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 1,
"y": 1
},
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 2
},
{
"x": 2,
"y": 1
}
],
"dir": "Right",
"nextDir": "Right",
"apple": {
"x": 2,
"y": 1
},
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
},
{
"name": "advance-paused-unchanged",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Paused",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Paused",
"frame": 0
}
},
{
"name": "advance-game-over-unchanged",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
},
{
"name": "advance-won-unchanged",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Won",
"frame": 0
}
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Won",
"frame": 0
}
}
]
}
+250
View File
@@ -0,0 +1,250 @@
{
"function": "inBounds",
"cases": [
{
"name": "top-left-corner",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": 0,
"y": 0
}
],
"expected": true
},
{
"name": "bottom-right-corner",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": 19,
"y": 19
}
],
"expected": true
},
{
"name": "x-negative",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": -1,
"y": 0
}
],
"expected": false
},
{
"name": "y-negative",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": 0,
"y": -1
}
],
"expected": false
},
{
"name": "x-equals-width",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": 20,
"y": 5
}
],
"expected": false
},
{
"name": "y-equals-height",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": 5,
"y": 20
}
],
"expected": false
},
{
"name": "middle",
"args": [
{
"width": 20,
"height": 20,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
{
"x": 10,
"y": 10
}
],
"expected": true
}
]
}
+75
View File
@@ -0,0 +1,75 @@
{
"function": "move",
"cases": [
{
"name": "move-up",
"args": [
{
"x": 3,
"y": 3
},
"Up"
],
"expected": {
"x": 3,
"y": 2
}
},
{
"name": "move-down",
"args": [
{
"x": 3,
"y": 3
},
"Down"
],
"expected": {
"x": 3,
"y": 4
}
},
{
"name": "move-left",
"args": [
{
"x": 3,
"y": 3
},
"Left"
],
"expected": {
"x": 2,
"y": 3
}
},
{
"name": "move-right",
"args": [
{
"x": 3,
"y": 3
},
"Right"
],
"expected": {
"x": 4,
"y": 3
}
},
{
"name": "move-does-not-wrap-at-the-edge",
"args": [
{
"x": 0,
"y": 0
},
"Up"
],
"expected": {
"x": 0,
"y": -1
}
}
]
}
+453
View File
@@ -0,0 +1,453 @@
{
"function": "turn",
"cases": [
{
"name": "turn-normal-perpendicular",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
"Up"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "turn-same-direction-is-fine",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
"Right"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "turn-reverse-rejected",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
"Left"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "turn-replaces-pending-turn",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
"Down"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Down",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "turn-against-last-moved-dir",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
},
"Left"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Up",
"apple": null,
"seed": 1,
"score": 0,
"status": "Playing",
"frame": 0
}
},
{
"name": "turn-ignored-when-paused",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Paused",
"frame": 0
},
"Up"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Paused",
"frame": 0
}
},
{
"name": "turn-ignored-when-game-over",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
},
"Up"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "GameOver",
"frame": 0
}
},
{
"name": "turn-ignored-when-won",
"args": [
{
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Won",
"frame": 0
},
"Up"
],
"expected": {
"width": 5,
"height": 5,
"snake": [
{
"x": 2,
"y": 2
},
{
"x": 1,
"y": 2
},
{
"x": 0,
"y": 2
}
],
"dir": "Right",
"nextDir": "Right",
"apple": null,
"seed": 1,
"score": 0,
"status": "Won",
"frame": 0
}
}
]
}