-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.mjs
82 lines (69 loc) · 1.99 KB
/
game.mjs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { setupRand, getRandom, shuffle } from "./pseudorandom.mjs";
let colorStyles;
let noOfCheeses;
let grid;
let cheeseList;
function getDistance(cheese, x, y) {
let dx = x - cheese.x;
let dy = y - cheese.y;
let distance = Math.round(Math.sqrt((dx * dx) + (dy * dy)));
return distance;
}
function getDistToNearestCheese(x, y) {
let result;
for (let cheeseNo = 0; cheeseNo < noOfCheeses; cheeseNo++) {
let distance = getDistance(cheeseList[cheeseNo], x, y);
if (result == undefined) {
result = distance;
}
if (distance < result) {
result = distance;
}
}
return result;
}
function getStyle(x, y) {
let distance = getDistToNearestCheese(x, y);
if (distance == 0) {
return "cheese";
}
if (distance >= colorStyles.length) {
distance = colorStyles.length - 1;
}
return colorStyles[distance];
}
function setupGame(req) {
let randSetup = {
startValue: req.startValue,
randMult: req.randMult,
randAdd: req.randAdd,
randModulus: req.randModulus
}
setupRand(randSetup);
colorStyles = req.colorStyles;
shuffle(colorStyles);
// build the grid and cheese list
grid = []
cheeseList = [];
for (let x = 0; x < req.width; x++) {
let column = [];
for (let y = 0; y < req.height; y++) {
let square = { x: x, y: y, style: "empty" };
// put the square into the cheese list
cheeseList.push(square);
// put the square into the column
column.push(square);
}
// put the column into the grid
grid.push(column);
}
shuffle(cheeseList);
noOfCheeses = getRandom(req.minCheeses, req.maxCheeses);
// set the styles for these cheese positions
for (let x = 0; x < req.width; x++) {
for (let y = 0; y < req.height; y++) {
grid[x][y].style = getStyle(x, y);
}
}
}
export {setupGame, grid, noOfCheeses};