-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
70 lines (62 loc) · 1.53 KB
/
map.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This file has methods to make a 2d-array for the game
var mapSize;
// make an m-by-n 2d array
function makeArray(cols, rows) {
var array = [];
for(var i = 0; i < rows; i++){
array[i] = [];
array[i][rows-1] = null;
}
return array;
}
function makeSquareArray(size) {
return makeArray(size, size);
}
function getSurroundings(x, y) {
var surroundings = [[],[],[]];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var posX = getSafeCoordinate(x + i - 1);
var posY = getSafeCoordinate(y + j - 1);
var location = surroundings[i][j] = game.map[posX][posY];
if (location == null) {
surroundings[i][j] == ' ';
} else {
surroundings[i][j] = location.name();
}
}
}
return surroundings;
}
function getSafeCoordinate(coord) {
if (coord < 0) return game.mapSize - 1;
if (coord >= game.mapSize) return 0;
return coord;
}
function Map(mapSize) {
this.mapSize = mapSize;
for (var i = 0; i < mapSize; i++) {
this[i] = [];
}
}
Map.prototype.getSurroundings = function(x, y){
var suroundings = [[],[],[]];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var posX = getSafeCoordinate(x + i - 1);
var posY = getSafeCoordinate(y + j - 1);
var location = surroundings[i][j] = game.map[posX][posY];
if (location == null) {
surroundings[i][j] == ' ';
} else {
surroundings[i][j] = location.name();
}
}
}
return surroundings;
function getSafeCoordinate(coord) {
if (coord < 0) return game.mapSize - 1;
if (coord >= game.mapSize) return 0;
return coord;
}
}