Simulate Candy Crush: crush lines of three or more, drop the candies and repeat until the board is stable
Problem
Match-3 games (Candy Crush, Bejeweled) run the same loop after every swap: remove lines of matching candies, let the rest fall, and repeat while new lines form. Write candyCrush(board), which takes a grid of positive integers (candy types; 0 is an empty cell) and returns the stable board.
candyCrush([
[1, 1, 1],
[2, 3, 4],
]);
// [[0, 0, 0],
// [2, 3, 4]]
candyCrush([[4], [4], [1], [1], [1], [4]]);
// [[0], [0], [0], [0], [0], [0]] (the three 1s go, the 4s fall together and go too)Ref. UR-1430 · © UIRound · uiround.com/questions/simulate-candy-crush-crush-lines-of-three-drop-candies-and-repeat-until-stable
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 5
- edge cases
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Do one round in two phases: first find every candy that is part of a horizontal or vertical run of three or more, and only then remove them all. Removing while scanning would break runs that cross (an L or T shape).
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.