-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_add.js
More file actions
32 lines (25 loc) · 790 Bytes
/
Copy pathrandom_add.js
File metadata and controls
32 lines (25 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
// looking for free cells and count
function getAmountFreeCells() {
let freeCellsArray = [];
for (let i = 0; i < fields.length; i++) {
for (let j = 0; j < fields[i].length; j++) {
if (fields[i][j] === 0) {
freeCellsArray.push([i, j]);
}
}
}
return freeCellsArray;
}
// put new random number into free cells
function addNewNumberForFreePlace() {
let freeCells = getAmountFreeCells();
const count = freeCells.length;
const randomPosition = randomNumberPlace(0, count - 1);
fields[freeCells[randomPosition][0]][freeCells[randomPosition][1]] = 2;
}
//just random
function randomNumberPlace(min, max) {
const rand = min + Math.random() * (max - min);
return Math.round(rand);
}