forked from kaylafortin/javascript-part-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.js
More file actions
60 lines (52 loc) · 1.55 KB
/
maps.js
File metadata and controls
60 lines (52 loc) · 1.55 KB
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
function Tile(type){
this.type = type;
this.isWalkable = function(){
if (this.type === "grass" || this.type === "sand"){
return true;
} else { return false; }
}
}
var beach = new Tile("sand");
console.log(beach.isWalkable());
function randomType(){
var type = Math.floor(Math.random()*3);
var types = ["sand", "grass", "water"];
return types[type];
}
function Map(width, height){
this.width = width;
this.height = height;
this.tiles = new Array(this.width);
for (var i = 0; i < this.width; i++){
this.tiles[i] = new Array(this.height);
}
for (var i = 0; i < this.width; i++){
for (var j = 0; j < this.height; j++){
this.tiles[i][j] = new Tile(randomType());
}
};
this.getWalkableOutput = function(){
for (var i = 0; i < width; i++){
var line = "";
for (var j = 0; j < height; j++){
if (this.tiles[i][j].isWalkable()){line += "o"}
else {line += "x"}
}
console.log(line);
}
}
this.getAsciiOutput = function(){
for (var i = 0; i < width; i++){
var line = "";
for (var j = 0; j < height; j++){
if (this.tiles[i][j].type === "sand"){line += ":"}
else if (this.tiles[i][j].type === "grass"){line += "*"}
else {line += "~"};
}
console.log(line);
}
}
}
var mordor = new Map(100, 100);
mordor.getWalkableOutput();
mordor.getAsciiOutput();