-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdungeongenerator.html
More file actions
110 lines (81 loc) · 1.92 KB
/
Copy pathdungeongenerator.html
File metadata and controls
110 lines (81 loc) · 1.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DungeonMapper Map Generator</title>
<style>
body{font-family:sans-serif;text-align:center;margin-top:30px}
#grid{display:grid;grid-template-columns:repeat(5,60px);gap:4px;justify-content:center;margin-bottom:20px}
.cell{width:60px;height:60px;border:1px solid #000;display:flex;align-items:center;justify-content:center;font-size:22px}
</style>
</head>
<body>
<h2>DungeonMapper Map Generator</h2>
<p><a href="./dungeon/index.html" target="_blank" rel="noopener noreferrer">play the game here</a> or play with pen and paper</p>
<div id="grid"></div>
<button onclick="generate()">generate map</button>
<script>
const grid=document.getElementById("grid")
for(let i=0;i<25;i++){
let d=document.createElement("div")
d.className="cell"
grid.appendChild(d)
}
function randCell(){
return [Math.floor(Math.random()*4),Math.floor(Math.random()*5)]
}
function generate(){
let map=[...Array(5)].map(()=>Array(5).fill("coin"))
let ogres=0
while(ogres<4){
let [r,c]=randCell()
if(map[r][c]=="coin"){
map[r][c]="ogre"
ogres++
}
}
let treasure=0
while(treasure<2){
let [r,c]=randCell()
if(map[r][c]=="coin"){
map[r][c]="treasure"
treasure++
}
}
for(let r=0;r<5;r++){
for(let c=0;c<5;c++){
if(map[r][c]!="coin") continue
let diag=false
let ortho=false
for(let dr=-1;dr<=1;dr++){
for(let dc=-1;dc<=1;dc++){
if(dr==0 && dc==0) continue
let rr=r+dr
let cc=c+dc
if(rr<0||rr>4||cc<0||cc>4) continue
if(map[rr][cc]=="ogre"){
if(Math.abs(dr)==1 && Math.abs(dc)==1) diag=true
if(dr==0 || dc==0) ortho=true
}
}
}
if(diag) map[r][c]="castle"
else if(ortho) map[r][c]="goblin"
}
}
for(let r=0;r<5;r++){
for(let c=0;c<5;c++){
let v=map[r][c]
let cell=grid.children[r*5+c]
if(v=="ogre") cell.textContent="O"
else if(v=="treasure") cell.textContent="T"
else if(v=="castle") cell.textContent="C"
else if(v=="goblin") cell.textContent="G"
else cell.textContent="$"
}
}
}
generate()
</script>
</body>
</html>