-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessLevelGenerator.html
More file actions
188 lines (155 loc) · 5.1 KB
/
chessLevelGenerator.html
File metadata and controls
188 lines (155 loc) · 5.1 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<html>
<head>
<title>CHESS - Level Generator</title>
<style>
tr td
{
background-color: #fff;
}
tr:nth-child(even) td:nth-child(odd),
tr:nth-child(odd) td:nth-child(even)
{
background-color: #ccc;
}
</style>
<script>
const KING = 0;
const QUEEN = 1;
const KNIGHT = 2;
const BISHOP = 3;
const ROOK = 4;
const PAWN = 5;
king_w = {name:"king", color:"w", move:[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]};
king_b = {name:"king", color:"b", move:[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]};
queen_w = {name:"queen", color:"w", move:[["x",0],[0,"y"],["x","y"]]};
queen_b = {name:"queen", color:"b", move:[["x",0],[0,"y"],["x","y"]]};
knight_w = {name:"knight", color:"w", move:[[1,2],[2,1],[2,-1],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2]]};
knight_b = {name:"knight", color:"b", move:[[1,2],[2,1],[2,-1],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2]]};
bishop_w = {name:"bishop", color:"w", move:[["x","y"]]};
bishop_b = {name:"bishop", color:"b", move:[["x","y"]]};
rook_w = {name:"rook", color:"w", move:[["x",0],[0,"y"]]};
rook_b = {name:"rook", color:"b", move:[["x",0],[0,"y"]]};
pawn_w = {name:"pawn", color:"w", move:[[-1,-1],[1,-1]]};
pawn_b = {name:"pawn", color:"b", move:[[-1,1],[1,1]]};
figures_w = [king_w, queen_w, knight_w, bishop_w, rook_w, pawn_w];
figures_b = [king_b, queen_b, knight_b, bishop_b, rook_b, pawn_b];
function showMap(map, sx, sy, ex, ey) {
var old = document.getElementById("map");
if (old != null) {
old.parentNode.removeChild(old);
}
var table = document.createElement("table");
table.setAttribute("id", "map");
for (var y = 0;y < map.length; y++) {
var row = document.createElement("tr");
for (var x=0;x<map[y].length;x++) {
var cell = document.createElement("td");
var cellImage = document.createElement("img");
if (map[y][x] != "empty") {
cellImage.setAttribute("src", map[y][x].name + "_" + map[y][x].color + ".png")
cellImage.setAttribute("alt", map[y][x].name + "_" + map[y][x].color)
}
cellImage.setAttribute("width", "100px");
cellImage.setAttribute("height", "100px");
if (x == sx && y == sy) {
cellImage.setAttribute("style", "background-color:green;")
} else if (x == ex && y == ey) {
cellImage.setAttribute("style", "background-color:red;")
}
cell.appendChild(cellImage);
row.appendChild(cell);
}
table.appendChild(row);
}
table.setAttribute("border", "2");
document.getElementsByTagName("body")[0].appendChild(table);
}
function getPossibleMoves(map, x, y, dx, dy) {
var result = [];
var mx = x + dx;
var my = y + dy;
while (mx >= 0 && mx < map[0].length && my >= 0 && my < map.length && map[my][mx] == "empty") {
result.push([mx, my]);
mx = mx + dx;
my = my + dy;
}
return result;
}
function createLevel() {
// get size from input
var size = document.getElementById("size").value;
// init map
var map = Array(size);
for (var i=0;i<size;i++) {
map[i] = new Array(size);
}
// init fields
for (var y=0;y<map.length;y++) {
for (var x=0;x<map.length;x++) {
map[y][x] = "empty";
}
}
// set random start
var sx = Math.floor(Math.random() * size);
var sy = Math.floor(Math.random() * size);
var x = sx;
var y = sy;
var c = "white";
console.log("Start: " + x + "/" + y);
// build path
var steps = 0;
while (steps < 10) {
// get random figure
var fig = "";
if (c == "white") {
fig = figures_w[Math.floor(Math.random() * figures_w.length)];
} else {
fig = figures_b[Math.floor(Math.random() * figures_b.length)];
}
// get list of possible movements
var possibleMoves = [];
for (var mi=0;mi<fig.move.length;mi++) {
var move = fig.move[mi];
if (move[0] == "x" && move[1] == "y") {
possibleMoves.concat(getPossibleMoves(map, x, y, 1, 1));
possibleMoves.concat(getPossibleMoves(map, x, y, -1, -1));
possibleMoves.concat(getPossibleMoves(map, x, y, 1, -1));
possibleMoves.concat(getPossibleMoves(map, x, y, -1, 1));
} else if (move[0] == "x" && move[1] == 0) {
possibleMoves.concat(getPossibleMoves(map, x, y, 1, 0));
possibleMoves.concat(getPossibleMoves(map, x, y, -1, 0));
} else if (move[0] == 0 && move[1] == "y") {
possibleMoves.concat(getPossibleMoves(map, x, y, 0, 1));
possibleMoves.concat(getPossibleMoves(map, x, y, 0, -1));
} else {
var tx = x + move[0];
var ty = y + move[1];
if (tx >= 0 && tx < map[0].length && ty >= 0 && ty < map.length && map[ty][tx] == "empty") {
possibleMoves.push([tx, ty]);
}
}
}
// if possible moves is not empty, set new position
if (possibleMoves.length > 0) {
var move = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
map[y][x] = fig;
x = move[0];
y = move[1];
if (c == "white") {
c = "black";
} else {
c = "white";
}
steps++;
}
}
// show final level
showMap(map, sx, sy, x, y);
}
</script>
</head>
<body>
<input id="size" type="text" />
<button type="button" onclick="createLevel()">Generate</button>
</body>
</html>