Snake · qwen3-8b
Output generated Cost / run $0.0030 Runtime 90.5s
Prompt for this run
Build a playable Snake game on a canvas with a 20 by 20 cell board. Use HTML, CSS, and JavaScript as needed. Write it to `index.html` in the current working directory using your file tools. Inline your own CSS and JavaScript. Do not use external assets, CDNs, or network requests. No third-party libraries are provided. Required behavior: - Use arrow keys to turn a snake that advances one cell at a time. Ignore a direction that would immediately reverse into its own neck, including rapid inputs before the next movement step. - Place one food item in an unoccupied cell. Eating it grows the snake by one segment, increases the score, and spawns new food in another unoccupied cell. - Use solid board boundaries: hitting a wall or the snake's body ends the game. Moving into a tail cell that vacates on that same non-growing step is allowed. - Provide Start, Pause/Resume, and Restart. Restart resets snake length, direction, score, food, and game state. Filling the board shows a win state instead of trying to place impossible food. - Display the controls and score. Prevent arrow keys from scrolling the page. Verify: Start the game and make a few turns to check grid-aligned movement and direction response. Pause and resume, then use Restart and check that snake length and score return to their initial values and food occupies a free cell.
TAKE IT TO CODE
A single self-contained HTML file — copy it and open in any browser, or reuse the prompt with your own pi-agent run.
RUN
METRICS
Transcript · 7 messages
Build a playable Snake game on a canvas with a 20 by 20 cell board. Use HTML, CSS, and JavaScript as needed. Write it to `index.html` in the current working directory using your file tools. Inline your own CSS and JavaScript. Do not use external assets, CDNs, or network requests. No third-party libraries are provided. Required behavior: - Use arrow keys to turn a snake that advances one cell at a time. Ignore a direction that would immediately reverse into its own neck, including rapid inputs before the next movement step. - Place one food item in an unoccupied cell. Eating it grows the snake by one segment, increases the score, and spawns new food in another unoccupied cell. - Use solid board boundaries: hitting a wall or the snake's body ends the game. Moving into a tail cell that vacates on that same non-growing step is allowed. - Provide Start, Pause/Resume, and Restart. Restart resets snake length, direction, score, food, and game state. Filling the board shows a win state instead of trying to place impossible food. - Display the controls and score. Prevent arrow keys from scrolling the page. Verify: Start the game and make a few turns to check grid-aligned movement and direction response. Pause and resume, then use Restart and check that snake length and score return to their initial values and food occupies a free cell.
thinking
Okay, let's tackle this Snake game. The user wants a single HTML file with everything inline. First, I need to structure the HTML with a canvas element for the game area. Then, I'll add CSS for styling and JavaScript for the game logic. Starting with the HTML structure: the canvas should be 20x20 cells. Each cell is probably 20x20 pixels, making the canvas 400x400. I'll set the canvas width and height accordingly. Also, need a div for the score and controls. Next, CSS. The body should center the canvas and have some basic styling. The canvas needs to be styled with border for visibility. The score display should be above the canvas. Controls can be buttons for Start, Pause, Restart. Maybe position them absolutely or use flexbox. JavaScript part is more complex. Let's outline the game variables: snake as an array of coordinates, direction, food position, score, game state (running, paused, etc.), and a grid size. The snake starts at the center, maybe with a few segments. The food is placed randomly in an empty cell. For movement, the snake moves one cell per interval. The direction changes based on arrow keys, but can't reverse immediately. Need to check if the new direction would make the snake collide with itself in the next step. So, when handling key presses, only allow directions that aren't the opposite of the current direction. Handling the game loop: using setInterval to move the snake. Each step, check for collisions with walls or self. If collision, end the game. If food is eaten, increase score, grow snake, and place new food. Food placement: generate random coordinates, check if it's on the snake. If so, generate again. Also, if the board is full (snake occupies all cells), show a win message instead of placing food. Game states: Start, Pause, Resume, Restart. When restarting, reset all variables. The Start button starts the game, Pause toggles the state, and Restart resets everything. Controls: Prevent arrow keys from scrolling the page by adding event listeners to capture keydown events and prevent default behavior. Also, handle key presses to change direction. Testing the required behaviors: Check that the snake moves correctly, direction changes work, food appears in empty cells, game ends on collision, and restart resets everything. Also, verify that the pause/resume works and the win condition is handled when the board fills. Now, putting all together into the index.html. Inline CSS in a style tag, JS in a script tag. Make sure all code is self-contained. Need to handle the game loop, rendering the canvas, collision detection, and state management. Potential issues to watch for: ensuring the snake doesn't reverse immediately, correct collision checks, food placement when the board is full, and proper handling of game states. Also, making sure the canvas is properly sized and the grid is aligned. Let me draft the code step by step. Start with the HTML structure, then CSS, then JS. In the JS, set up the canvas context, define the game variables, handle key events, create the game loop, draw functions, and collision checks. Also, implement the start, pause, restart functionality. Testing the code in the browser would be ideal, but since I can't run it here, I'll have to ensure all parts are covered. Let me write the code now.
/tmp/job-uWzhj7/index.html (3795 bytes)Successfully wrote to /tmp/job-uWzhj7/index.html
thinking
Okay, let's see. The user asked to build a Snake game with specific requirements. I created the index.html file with HTML, CSS, and JavaScript all in one. Now I need to verify if it meets all the criteria. First, the game should use arrow keys for direction. The JavaScript listens for keydown events and changes the direction, but prevents immediate reversal. That seems handled by checking if the new direction is opposite to the current one. Next, the food placement. The getRandomEmptyCell function ensures the food is in an unoccupied cell. If the snake fills the board, the game should show a win state. Wait, the current code doesn't check for a full board. Oh right, the user mentioned that if the board is full, it shows a win instead of trying to place food. But in the code, when the snake grows, it might eventually fill the grid. Hmm, maybe I should add a check in the gameLoop to see if the snake has occupied all cells except the food. If so, trigger a win. Also, the game ends when hitting walls or the body. The collision checks are there for walls and self-collision. But when the snake moves into its own tail cell that vacates on the same step (like when it's not growing), that's allowed. The current code checks for selfCollision by slicing the snake array starting from index 1, which excludes the head. So that should be okay. The controls: Start, Pause/Resume, Restart. The buttons call startGame, togglePause, and restartGame functions. The restart resets everything, including score and snake length. The game state is managed with isPaused and gameOver variables. Testing the required behaviors: Starting the game, making turns, pausing, resuming, restarting. The code should handle these. Also, when the board is full, the game should show a win. Let me check if the code has that. Currently, the code doesn't have a win condition. So I need to add that. Wait, the user specified that filling the board shows a win state instead of trying to place impossible food. So when the snake occupies all cells except the food, the game should end with a win. Let me modify the gameLoop to check if the snake has filled the grid. For example, if the snake's length is tileCount * tileCount - 1 (since food is one cell), then trigger a win. Also, in the initial code, when the snake grows, it adds a segment, but if the board is full, the next food placement would fail. So adding the win condition when the snake can't grow anymore would be better. So I'll need to adjust the code to check for a win condition. Let me update the gameLoop to include that check. Also, ensure that when the game is restarted, the snake starts at the correct position and the food is placed properly. Another thing: the initial draw() call is made after the script, which should render the snake and food. The canvas is 400x400, which is 20x20 cells of 20x20 pixels each. That looks correct. Testing the movement: the snake should move one cell at a time, and direction changes should be blocked if they reverse. The keydown event listener prevents default behavior to avoid page scrolling, which meets the requirement. I think the main missing part is the win condition when the board is filled. Let me add that. Also, make sure that when the game is paused, the interval is cleared, and resuming starts it again. Okay, I'll modify the code to include the win condition check. Let me adjust the gameLoop function to check if the snake has filled all cells except the food. If so, set gameOver to true and show a win message.
{"path":"/tmp/job-uWzhj7/index.html","edits":[{"oldText":"function gameLoop() {\n if (gameOver) return;\n // Move snake\n const heaSuccessfully replaced 1 block(s) in /tmp/job-uWzhj7/index.html.
thinking
<tool_call>
{"name": "browser", "arguments": {"action": "reload"}}
</tool_call>