JavaScript Pacman Code: Check Contact Before Collecting

The player and guard exchange neighboring cells, yet a final-position-only check says they never met. Resolve same-cell and exchanged-edge contact before awarding a collectible, especially the final one.

The playable ring collector advances the player and a fixed patrol together on each legal action. Walls reject the whole turn, including the guard’s movement. Three scenario buttons let you try shared-destination contact, exchanged cells and a collision on the final dot; a losing turn cannot become a win by collecting.

JavaScript Pacman Code: Check Contact Before Collecting
JavaScript Pacman Code: Check Contact Before Collecting
Open the full example ↗

Define the map and collectible set

Rows and columns are zero-based. Walls are #, P is the player's initial cell, G is the guard's initial cell and each dot is an initial collectible.

#######
#P...G#
#.###.#
#.....#
#######

The player starts at (1, 1), the guard at (1, 5). There are 12 walkable cells: five on row 1, two on row 2 and five on row 3. Excluding the two starting cells leaves 10 collectible coordinate IDs.

Ania Kubow's repository offers grid movement and collectible/enemy concepts. Its source implementation uses independent enemy timers and additional scoring rules. Here, the player and guard advance together on a legal action, making their contact order easy to follow.

The guard follows this fixed clockwise sequence, looping after the last cell:

(1,5), (2,5), (3,5), (3,4), (3,3), (3,2),
(3,1), (2,1), (1,1), (1,2), (1,3), (1,4)

Each accepted legal player action advances the guard one position. Waiting for wall-clock time does nothing; this is a discrete teaching model, not real-time play.

Reject blocked movement without advancing the guard

A player action chooses one cardinal neighboring cell. A wall or off-board destination rejects the entire turn. Player position, guard position and patrol index, items, score, turn count and confirmed history remain unchanged.

From the initial cell, Up meets a wall and cannot let the guard sneak down to (2, 5). Right is legal: compute player destination (1, 2) and guard destination (2, 5) from the same prior snapshot. They do not collide, so consume the item at (1, 2), producing score 1 and remaining count 9.

A blocked-action message explains the wall without adding a turn to the history. Check the destination before changing either actor or the item set.

Check two different kinds of contact

After validating a legal player destination, calculate both next positions from the old state. Contact occurs if their destinations are equal, or if the old cells are exchanged.

Staged example Player movement Guard movement Expected result
Same destination (3, 4) → (3, 5) (2, 5) → (3, 5) Loss
Exchanged edge (1, 5) → (1, 4) (1, 4) → (1, 5) Loss

The second row matters because the endpoints differ while the actors crossed the same edge. Keep the guard's patrol index consistent with the stated guard cell in each example.

These staged positions can be loaded separately in the example. Each uses the same turn resolver as normal play.

Collect only after surviving the move pair

For a legal move pair, commit the new positions and increment the turn. If contact occurred, enter Lost immediately without collecting the destination item or increasing score. A last item at a collision destination remains in the set and cannot produce win.

Otherwise, check whether the player's destination coordinate is still in the collectible set. If present, remove that ID and add one point. Revisiting a consumed coordinate earns nothing. The invariant score + remaining = 10 stays true for this original seed, including a losing turn that consumes no item.

A win requires an empty item set after a safe turn. Contact takes priority, so reaching the final dot during a collision still loses.

Play the shared clock

Use the direction buttons or focus the board for fresh arrow keys. Right from the original start puts the player at (1, 2), the guard at (2, 5), and awards one dot. Wait without pressing anything: this turn-based guard does not move on a wall-clock timer.

Load Shared destination, then move Right to observe contact at (3, 5). Load Exchanged edge and move Left; the endpoints differ, but the old cells swap, so contact still loses. The final-item scenario begins with nine collected and one remaining at the contact destination; that last item stays uncollected. Reset restores all ten dots and both starting positions. Keep staged scenarios clearly labeled when discussing results.

Copy this prompt

Build an original turn-based grid collector with a player, a guard and ten dots. Return one HTML file containing the board, direction buttons, text status and staged contact examples. Use the map rows #######, #P...G#, #.###.#, #.....#, #######. Coordinates begin at zero. The player starts at (1, 1), and the guard starts at (1, 5). Dots occupy the ten other walkable cells.

The guard patrol repeats this sequence: (1, 5), (2, 5), (3, 5), (3, 4), (3, 3), (3, 2), (3, 1), (2, 1), (1, 1), (1, 2), (1, 3), (1, 4). Each legal player action advances the guard once. Time passing alone does nothing.

Reject a wall or off-board move without changing either actor, the patrol position, turn count, dots or score. For a legal move, calculate both destinations from the old positions. Commit the positions and turn, then lose if the destinations match or the actors exchange their old cells. Resolve contact before collecting. Otherwise, remove the destination dot once, add one point, and win when no dots remain.

Provide direction buttons and fresh focused arrow input. Show coordinates, next guard position, turns, score and remaining dots. Include Shared destination with player (3, 4) and guard (2, 5), where Right loses; Exchanged edge with player (1, 5) and guard (1, 4), where Left loses; and Final-item contact with nine points and only the shared destination dot remaining. Contact leaves that dot uncollected.

Block moves after an ending. Reset restores the original game. Keep the board legible on a small screen.

Watch the final dot

Load Final-item contact and move Right. Both actors arrive at (3, 5), so the game ends with nine points and one dot remaining. NOS can render the HTML collector for this turn-order walkthrough. Reset, move Right from the original start and compare that safe one-point turn with the staged collision.

Keep reading