-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
168 lines (147 loc) · 4.59 KB
/
main.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
const prompt = require("prompt-sync")({ sigint: true });
const hat = "^";
const hole = "O";
const fieldCharacter = "░";
const pathCharacter = "*";
class Field {
constructor(field) {
this._field = field;
this._fielWidth = field[0].length;
this._fieldHeight = field.length;
this._playerX = 0;
this._playerY = 0;
this._gameEnded = false;
}
print() {
for (let i = 0; i < this._fieldHeight; i++) {
const fieldRow = this._field[i];
console.log(fieldRow.join(""));
}
}
static generateField(height, width, percentage) {
const randomizedField = [];
const fieldSize = height * width;
let totalHoles = Math.floor(fieldSize * (percentage / 100));
//Generate empty field
for (let i = 0; i < height; i++) {
let helperArray = [];
for (let j = 0; j < width; j++) {
helperArray.push(fieldCharacter);
}
randomizedField.push(helperArray);
}
//Add player on first position
randomizedField[0][0] = pathCharacter;
//Add hat on random position
const hatHeight = Math.floor(Math.random() * height);
const hatWidth = Math.floor(Math.random() * height);
randomizedField[hatHeight][hatWidth] = hat;
//Add holes to field
while (totalHoles > 0) {
const randomHeight = Math.floor(Math.random() * height);
const randomWidth = Math.floor(Math.random() * width);
if (randomizedField[randomHeight][randomWidth] === fieldCharacter) {
randomizedField[randomHeight][randomWidth] = hole;
totalHoles--;
}
}
return randomizedField;
}
playerMove() {
const availableMoves = ["w", "s", "a", "d"];
//Ask for user input
console.log("Which direction would you like to move?");
let input = prompt("'↑W ↓S →D ←A'");
while (!availableMoves.includes(input.toLowerCase())) {
console.log("Incorrect command. Please use WASD to move");
console.log("Which direction would you like to move?");
let input = prompt("'↑W ↓S →D ←A'");
}
switch (input.toLowerCase()) {
case "w":
this._playerY -= 1;
break;
case "s":
this._playerY += 1;
break;
case "d":
this._playerX += 1;
break;
case "a":
this._playerX -= 1;
break;
default:
break;
}
}
checkPosition() {
const x = this._playerX;
const y = this._playerY;
//Checking if player position is out of bounds
if (x < 0 || x >= this._fieldWidth || y < 0 || y >= this._fieldHeight) {
console.log("**********************************");
console.log("YOU'RE OUT OF BOUNDS - GAME LOST!");
console.log("**********************************");
this._gameEnded = true;
}
//Checking if player fell in a hole
if (this._field[y][x] === hole) {
console.log("**********************************");
console.log("YOU'VE FELL IN A HOLE - GAME LOST!");
console.log("**********************************");
this._gameEnded = true;
}
//Checking if player found hat
if (this._field[y][x] === hat) {
console.log("**********************************");
console.log("YOU'VE FOUND THE HAT - GAME WON!");
console.log("**********************************");
this._gameEnded = true;
}
//Checking if position is free and marking the path
if (
this._field[y][x] === fieldCharacter ||
this._field[y][x] === pathCharacter
) {
this._field[y][x] = pathCharacter;
}
}
newGame() {
console.log("**********************************");
console.log("A NEW GAME STARTED - FIND YOUR HAT!");
console.log("**********************************");
//Repeat turns until game has ended
do {
this.print();
this.playerMove();
this.checkPosition();
} while (this._gameEnded === false);
}
playAgain() {
console.log("Would you like to play again?");
let input = prompt(" Yes (Y) - No (N)");
switch (input.toLowerCase()) {
case "y":
this._gameEnded = false;
this.newGame();
break;
case "n":
console.log("BEGONE LOSER!");
default:
break;
}
}
}
//Percentage of holes in field - random between 25 and 50
const holesPercentage = Math.floor(Math.random() * (50 - 25)) + 25;
//Generate random height and width
const fieldHeight = Math.floor(Math.random() * (10 - 5)) + 5;
const fieldWidth = Math.floor(Math.random() * (18 - 6)) + 6;
//Generate randomField
const randomField = Field.generateField(
fieldHeight,
fieldWidth,
holesPercentage
);
const newField = new Field(randomField);
newField.newGame();