Simulate Candy Crush: crush lines of three or more, drop the candies and repeat until the board is stable

mediumAbout 30 minutesUpdated CodingProgridssimulationmatricesgames
Not solved

Share this question

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

Stuck? Take a hint

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.

Every question and answer7 days, one payment