-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
194 lines (170 loc) · 4.52 KB
/
app.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
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
// set-up user input
const prompt = require("prompt-sync")({ sigint: true });
// game elements
const hat = "^";
const hole = "O";
const fieldCharacter = "░";
const pathCharacter = "*";
// define variables
let gameOver = false;
let myField;
// construct game field here
class Field {
constructor(arr) {
this.field = arr;
}
// helper to return the field
array() {
return this.field;
}
// join array to readable string and log it
print() {
this.string = this.field.map((row) => row.join("")).join("\n");
console.log(this.string);
}
// generate random field
static generateField(w, h) {
// randomly place hole or fieldCharacter
function getRandomChoice() {
const randomNumber = Math.random();
if (randomNumber < 0.3) {
// probability for holes is quite low
return hole;
} else {
return fieldCharacter;
}
}
// generate new field array
let newField = Array.from({ length: h }).map(() =>
Array.from({ length: w }).map(() => getRandomChoice())
);
// intial payer position on top left
newField[0][0] = pathCharacter;
// find random position for hat
let hatRow = Math.floor(Math.random() * h);
let hatCol = Math.floor(Math.random() * w);
// make sure hat is not placed on a hole
while (newField[hatRow][hatCol] !== fieldCharacter) {
hatRow = Math.floor(Math.random() * h);
hatCol = Math.floor(Math.random() * w);
}
// place hat
newField[hatRow][hatCol] = hat;
// output field
return newField;
}
}
const newGame = () => {
// reset player position
let posX = 0;
let posY = 0;
// default field for testing
myField = new Field([
["*", "░", "O"],
["░", "O", "░"],
["░", "^", "░"],
]);
// set up game by letting user specify the field size
let userSetup = prompt(
"Choose the size of the field with a number between 4 and 24: "
);
let userNumber = +userSetup; // Convert the input to a number
// check if the input was is a number between 4 and 24
if (!isNaN(userNumber) && userNumber >= 4 && userNumber <= 24) {
//genrate random field in specified size and replace default field
myField.field = Field.generateField(userSetup, userSetup);
// print field
myField.print();
// start game
gameOver = false;
} else {
// if input is invalid, start over
console.log("Did you pick a number between 2 and 24?");
newGame();
return;
}
// update player position
const updatePath = () => {
// check if player left the field
if (
posY >= 0 &&
posY < myField.array().length &&
posX >= 0 &&
posX < myField.array()[0].length
) {
// check if player fell down a hole
if (myField.array()[posY][posX] === hole) {
gameLost("Game Over *** You fell down a hole!");
}
// check if player found the hat
else if (myField.array()[posY][posX] === hat) {
gameWon();
}
// if player did not find the hat or fall down a hole, update the position and log the updated field
else {
myField.array()[posY][posX] = pathCharacter;
myField.print();
}
// if player left the field, end the game
} else {
gameLost("Game Over *** You moved outside of the field!");
}
};
while (!gameOver) {
let userInput = prompt("Move using W A S D: ");
switch (userInput.toLowerCase()) {
// move up
case "w":
posY--;
updatePath();
break;
// move left
case "a":
posX--;
updatePath();
break;
// move down
case "s":
posY++;
updatePath();
break;
// move right
case "d":
posX++;
updatePath();
break;
// invalid user input
default:
console.log("Did you use the keys W A S D to navigate?");
}
}
};
// start initial game
newGame();
// ends the game and logs the reason for ending it
const gameLost = (str) => {
gameOver = true;
console.log(str);
};
// ends the game and logs a winning message
const gameWon = () => {
gameOver = true;
console.log("You Win *** Found the hat! =)");
};
// ask user if they want to restart
while (gameOver) {
let userInput = prompt("New game - Y/N? ");
switch (userInput.toLowerCase()) {
case "y":
// restart application
newGame();
break;
case "n":
console.log("Quitting app…");
// close application
process.exit();
break;
default:
console.log("Type 'Y' to restart or 'N' to quit");
}
}