Play locally with two people, starting with piles of 1, 2, and 3 objects and Player A's turn. Each valid move removes a positive whole quantity from one pile and records an independent before/after snapshot. The four-move sample ends with Player B taking the final object, so B loses and A wins. Invalid actions leave piles, turn and history unchanged.

Declare the rule and the initial state
This local misère variant starts with piles [1, 2, 3], stable pile IDs 0, 1 and 2, and Player A to move. A player removes a positive whole number of objects from exactly one nonempty pile. Whoever takes the last object loses.
Harvard CS50's Nim project supplies useful pile/action and last-object-loses context. Its listed available-action example uses positive quantities, although its background wording includes zero. This original specification explicitly requires at least one object, avoiding a no-removal turn. The page provides two local players.
| Pile ID | Initial count | Available removal quantities |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 1, 2 |
| 2 | 3 | 1, 2, 3 |
There are six initial actions, not three: each pile can offer several quantities. Choosing a pile is not yet a committed move.
Validate the whole action before mutation
An action contains a pile ID and quantity. Require an in-range stable ID, a nonempty pile, and an integer quantity from 1 through that pile's current count. Missing selections, zero, negative values, fractions and excessive removal are rejected.
For pile 1 with count 2, a request to remove 3 cannot be clamped to 2. Reject it without altering piles, turn, confirmed history or winner. The same applies to a request against an empty pile. Rejected requests appear as feedback without adding a move-history entry.
A valid action changes exactly one pile. Its total decreases by exactly the requested quantity, and all counts remain nonnegative integers. Record the actor, selected ID, quantity, before/after state and resulting phase. Snapshot these values rather than pointing every history row at the same mutable array.
Read the terminal trace from the actor
| Move | Actor and action | After-state | Result |
|---|---|---|---|
| 1 | A removes 2 from pile 2 | [1, 2, 1] | B's turn |
| 2 | B removes 2 from pile 1 | [1, 0, 1] | A's turn |
| 3 | A removes 1 from pile 0 | [0, 0, 1] | B's turn |
| 4 | B removes 1 from pile 2 | [0, 0, 0] | B loses; A wins |
Resolve that last row using actor B before any nonterminal turn switch. The terminal banner records the loser and winner; it should not imply that A now has an active turn on an empty board.
For a code review, also consider A taking the last object from [0, 0, 1]. The same rule makes A lose and B win. There is no preset button for this separate state; it is a reasoning check on the winner logic.
Use the four-move sequence to follow how the displayed turn and result change. The page has no computer opponent, so both players choose their own moves.
Confirm each selected move once
Choose a pile button, then type how many objects to remove. The selection text shows the allowed range for that pile. Choosing a different pile clears the quantity so you can enter a fresh move. Blank quantity remains unanswered.
After a successful removal, the game clears the selected pile and quantity. Choose both again for the next player. Pressing Remove again without a new selection leaves the board unchanged, which prevents the previous move from being reused.
Terminal play rejects further Remove requests and retains the winner until Reset. Reset restores [1, 2, 3], A's turn, blank selections, empty history, no winner and cleared errors. Keep counts and stable IDs readable without relying only on a row of decorative objects.
Play the supplied trace
Follow the table's first two turns: remove 2 from pile 2 for A, then 2 from pile 1 for B. Pile 1 becomes empty and its button is unavailable. Remove pile 0's final object for A, leaving one in pile 2 for B. When B removes it, compare the last history entry with the banner: B took the last object, so A wins.
Copy a prompt for this example
Build a single self-contained HTML two-player Nim game with embedded CSS and JavaScript. Use a compact tabletop layout with large pile buttons, readable counts, a labeled quantity field, Remove, Reset, and a move history.
Start with piles [1, 2, 3], identified as pile 0, pile 1, and pile 2. Player A moves first. Each turn removes a positive whole number of objects from exactly one nonempty pile. State prominently that the player who takes the last object loses.
Disable empty-pile buttons and display removal ranges only for nonempty piles. Choosing a pile must display its current allowable removal range and clear any previous quantity. Require a selected nonempty pile and a whole quantity from 1 through its count. Reject blanks, zero, negative values, fractions, and excessive quantities without changing the piles, turn, or history. Do not clamp an invalid quantity.
After a valid removal, store an independent before-and-after snapshot with actor, pile, quantity, and result. Clear the selection and quantity before the next move. Determine whether the acting player took the last object before switching turns.
Support this sequence: A removes 2 from pile 2; B removes 2 from pile 1; A removes 1 from pile 0; B removes 1 from pile 2. The final result must say B loses and A wins. Further removal must leave that result unchanged.
Reset restores the original piles, Player A, blank controls, and empty history. Keep the layout responsive and do not add a computer opponent. Return only the complete HTML file.
For a review of how the final move determines the winner, NOS can display this prepared HTML Nim game. Play the four recorded moves and inspect the final B action before changing the rules or adding any computer opponent.