Workshop › Candy Crush-style Match 3 › Step 4 of 29
There are six gem colors. The board is a list of 8 rows, each a list of 8 numbers from 0 to 5: not the color itself
but its index in COLORS. newBoard() fills it with random gems.
```js
const COLORS = ['#ef4444', '#f59e0b', '#22c55e', '#3b82f6', '#a855f7', '#ec4899']
let board // board[row][col]: a color index
const randomGem = () => Math.floor(Math.random() * COLORS.length)
function newBoard() {
board = []
for (let r = 0; r < N; r++) {
board.push([])
for (let c = 0; c < N; c++) {
board[r].push(randomGem())
}
}
}
newBoard()
requestAnimationFrame(loop)
```randomGem is a short arrow function: Math.random() is a number from 0 up to 1, times 6 and rounded down it is a
whole number 0 to 5.newBoard starts with an empty list, adds an empty row for each r, and pushes 8 random gems into it.board[r][c] is the gem in row r, column c. newBoard() at the bottom makes the first board.TOP write COLORS.function draw() { write let board, randomGem and newBoard.newBoard() above requestAnimationFrame(loop).Skills: Arrays and objects
← The game loop Draw the gems →