Skip to content

Conversation

@moriz1432-dotcom
Copy link

Author name

Author: moriz1432-dotcom

About your game

What is your game about?
Key Objectives

⭐ Collect all coins in each level

⭐ Navigate the maze without getting trapped

⭐ Advance through progressively harder levels

⭐ Complete all stages to win

How do you play your game?
Gameplay Mechanics

Simple movement using WASD keys

Grid-based maps with solid walls and walkable paths

Coins disappear when the player steps on them

Automatic level progression when all coins are collected

Final victory message after completing the last level

Comment on lines +127 to +137
setTimeout(() => {
clearText()
setMap(levels[level])
}, 500)
} else {
setTimeout(() => {
clearText()
addText("YOU WIN!", { y: 1, color: color`3` })
}, 500)
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: A race condition allows level to be incremented multiple times due to re-entrant afterInput() calls during the 500ms delay after coin collection.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

The afterInput() callback increments the level variable and then schedules setMap() with a 500ms delay. During this 500ms window, if a player input occurs, the afterInput() callback re-executes. Since tilesWith(coin).length is still 0, the level variable is incremented again, and another setTimeout is scheduled. This can cause the game to skip multiple levels, corrupt game state, and trigger the "YOU WIN!" condition prematurely.

💡 Suggested Fix

To prevent re-entry, either load the new map immediately after incrementing level without a setTimeout delay, or introduce a flag to prevent afterInput() from processing level transitions if one is already in progress.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: games/Coin-Collector+.js#L120-L137

Potential issue: The `afterInput()` callback increments the `level` variable and then
schedules `setMap()` with a 500ms delay. During this 500ms window, if a player input
occurs, the `afterInput()` callback re-executes. Since `tilesWith(coin).length` is still
`0`, the `level` variable is incremented again, and another `setTimeout` is scheduled.
This can cause the game to skip multiple levels, corrupt game state, and trigger the
"YOU WIN!" condition prematurely.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 5920927

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant