-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path134.html
335 lines (272 loc) · 8.57 KB
/
134.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Game</title>
</head>
<body>
<script>
/** 輸入提示 */
function inputPrompt(message, allowedInputs) {
let input = '';
if (allowedInputs) {
do {
input = prompt(message);
} while (!allowedInputs.includes(input) && input !== null);
} else {
do {
input = prompt(message);
} while (!input && input !== null);
}
return input;
}
/** 範圍的隨機數值 */
function randByRange(start, end) {
return parseInt((Math.random() * end - start).toFixed(0)) + start;
}
/** 區域 */
const Area = {
/** 邊界 */
World: '0',
/** 森林 */
Forest: '1',
/** 果園 */
Garden: '2',
/** 河流 */
River: '3',
/** 村莊 */
Village: '4',
};
/** 道具 */
const Item = {
/** 木頭 */
Tree: '0',
/** 果實 */
Furit: '1',
};
/** 行為 */
const Action = {
/** 取得建材 */
Tree: '0',
/** 取得飽足感 */
Food: '1',
};
/** 玩家 */
class Player {
constructor(id, name) {
/** id */
this.id = id;
/** 名稱 */
this.name = name;
/** 經驗值 */
this.exp = 10;
/** 飽足感 */
this.food = 20;
/** 水分 */
this.water = 10;
/** 建材 */
this.tree = 2;
/** 道具欄 */
this.items = [];
}
/** 是否死亡 */
isDead() {
return this.exp <= 0 || (this.food <= 0 && this.water <= 0);
}
/** 是否獲勝 */
isWin() {
return this.exp >= 1000;
}
/** 獲得道具 */
addItem(item, count, unit, itemName) {
for (let i = 0; i < count; i++) {
this.items.push(item);
}
alert(`獲得了${count}${unit}${itemName}`);
}
/** 消耗道具 */
removeItem(item, count, unit, itemName) {
for (let i = 0; i < count; i++) {
const itemIndex = this.items.indexOf(item);
const hasItem = itemIndex !== -1;
if (hasItem) {
this.items.splice(itemIndex, 1);
} else {
count = i;
break;
}
}
alert(`消耗了${count}${unit}${itemName}`);
return count;
}
/** 變動經驗值 */
changeExp(expChange) {
if (expChange < 0) {
this.exp += expChange;
alert(`消耗了經驗值${expChange}點, 目前經驗值為${this.exp}`);
} else {
this.exp += expChange;
alert(`獲得了經驗值${expChange}點, 目前經驗值為${this.exp}`);
}
}
/** 變動建材 */
changeTree(treeChange) {
if (treeChange < 0) {
this.tree += treeChange;
alert(`消耗了建材${treeChange}個, 目前建材為${this.tree}`);
} else {
this.tree += treeChange;
alert(`獲得了建材${treeChange}個, 目前建材為${this.tree}`);
}
}
/** 變動水分 */
changeWater(waterChange) {
if (waterChange < 0) {
this.water += waterChange;
alert(`消耗了水分${waterChange}點, 目前水分為${this.water}`);
} else {
this.water += waterChange;
alert(`獲得了水分${waterChange}點, 目前水分為${this.water}`);
}
}
/** 變動飽足感 */
changeFood(foodChange) {
if (foodChange < 0) {
this.food += foodChange;
alert(`消耗了飽足感${foodChange}點, 目前飽足感為${this.food}`);
} else {
this.food += foodChange;
alert(`獲得了飽足感${foodChange}點, 目前飽足感為${this.food}`);
}
}
/** 選擇區域 */
selectArea() {
const area = inputPrompt(
'要前往哪個區域呢? (' +
`${Area.World}: 邊界, ` +
`${Area.Forest}: 森林, ` +
`${Area.Garden}: 果園, ` +
`${Area.River}: 河流, ` +
`${Area.Village}: 村莊` +
')',
[Area.World, Area.Forest, Area.Garden, Area.River, Area.Village]
);
this.gotoArea(area);
}
/** 前往區域 */
gotoArea(area) {
switch (area) {
case Area.World:
return this.gotoWorld();
case Area.Forest:
return this.gotoForest();
case Area.Garden:
return this.gotoGarden();
case Area.River:
return this.gotoRiver();
case Area.Village:
return this.gotoVillage();
default:
throw new Error('玩家不玩了');
}
}
/** 前往邊界 */
gotoWorld() {
const isWantAttack = confirm('是否砍殺小怪?');
const expChange = randByRange(10, 25);
if (isWantAttack) {
this.changeExp(+expChange);
} else {
this.changeExp(-expChange);
}
this.changeWater(-1);
this.changeFood(-2);
}
/** 前往森林 */
gotoForest() {
this.changeExp(-20);
const isWantTree = confirm('是否要砍伐樹木?');
const treesCount = randByRange(0, 5);
if (isWantTree) {
this.addItem(Item.Tree, +treesCount, '個', '木頭');
}
}
/** 前往果園 */
gotoGarden() {
this.changeTree(-1);
const isWantFurit = confirm('是否採集果實?');
const fruitsCount = randByRange(0, 5);
if (isWantFurit) {
this.addItem(Item.Furit, +fruitsCount, '顆', '果實');
}
}
/** 前往河流 */
gotoRiver() {
this.changeTree(-1);
const isWantWater = confirm('是否採集民生用水?');
const waterCount = randByRange(0, 5);
if (isWantWater) {
this.changeWater(+waterCount);
}
}
/** 前往村莊 */
gotoVillage() {
this.selectActionInVillage();
}
/** 在村莊選擇行為 */
selectActionInVillage() {
const action = inputPrompt(
`查看背包 (${Action.Tree}: 取得建材, ${Action.Food}: 取得飽足感)`,
[Action.Tree, Action.Food]
);
this.doActionInVillage(action);
}
/** 在村莊做出行為 */
doActionInVillage(action) {
switch (action) {
case Action.Tree:
return this.getTreeInVillage();
case Action.Food:
return this.getFoodInVillage();
}
}
/** 在村莊取得建材 */
getTreeInVillage() {
const treesCount = this.removeItem(Item.Tree, 1, '個', '木頭');
const hasTree = treesCount !== 0;
if (hasTree) {
const treeChange = randByRange(1, 5);
this.changeTree(treeChange);
}
}
/** 在村莊取得飽足感 */
getFoodInVillage() {
const furitsCount = this.removeItem(Item.Furit, 1, '顆', '果實');
const hasFurit = furitsCount !== 0;
if (hasFurit) {
this.changeFood(+2);
}
}
}
function gameStart() {
const id = inputPrompt('輸入ID');
const name = inputPrompt('輸入名稱');
const player = new Player(id, name);
try {
while (!(player.isDead() || player.isWin())) {
player.selectArea();
}
if (player.isDead()) {
alert('玩家死亡');
} else if (player.isWin()) {
alert('玩家獲勝');
}
} catch (error) {
alert(error.message);
}
}
gameStart();
</script>
</body>
</html>