-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateFood.js
51 lines (49 loc) · 1.18 KB
/
generateFood.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
let applePositionI = 0;
let applePositionJ = 0;
let score = 0;
let apple = 0;
const generateFood = (map) => {
let sizeY = map.length;
let sizeX = map[0].length;
let i = Math.floor(Math.random() * sizeY);
let j = Math.floor(Math.random() * sizeX);
if ((map[i][j] === '═') ||
(map[i][j] === '║') ||
(map[i][j] === '╝') ||
(map[i][j] === '╗') ||
(map[i][j] === '╚') ||
(map[i][j] === '╔')) {
generateFood(map);
} else {
map[i][j] = '$';
applePositionI = i;
applePositionJ = j;
}
};
const food = (counter, map) => {
console.log('Money earned: $' + score);
// first round, first apple generates
if (counter === 1) {
generateFood(map);
// after first round, this runs
} else {
// every other rounds:
switch (map[applePositionI][applePositionJ]) {
case '═':
generateFood(map);
score++;
apple = 1;
return apple;
case '║':
generateFood(map);
score++;
apple = 1;
return apple;
// if the apple was not picked up, this regenerates it
case ' ':
map[applePositionI][applePositionJ] = '$';
break;
}
}
};
module.exports = food;