-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.js
86 lines (75 loc) · 2.08 KB
/
Field.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
class Field {
constructor(grid) {
this.grid = grid;
this.playerPosition = { x: 0, y: 0 };
this.fieldWidth = grid[0].length;
this.fieldHeight = grid.length;
this.isGameOver = false;
this.fieldCharacter = "░";
this.hole = "O";
this.hat = "^";
this.pathCharacter = "*";
}
print() {
console.log(this.grid.map((row) => row.join("")).join("\n"));
}
move(direction) {
if (this.isGameOver) return false;
let { x, y } = this.playerPosition;
switch (direction) {
case "N":
y--;
break;
case "S":
y++;
break;
case "E":
x++;
break;
case "W":
x--;
break;
default:
console.log("Invalid move. Please use N, S, E, or W.");
return true;
}
if (x < 0 || x >= this.fieldWidth || y < 0 || y >= this.fieldHeight) {
console.log("Out of bounds. Game Over!");
this.isGameOver = true;
return false;
}
this.playerPosition = { x, y };
if (this.grid[y][x] === this.hat) {
console.log("Congratulations! You found your hat!");
this.isGameOver = true;
return false;
}
if (this.grid[y][x] === this.hole) {
console.log("Oops! You fell into a hole. Game Over!");
this.isGameOver = true;
return false;
}
this.grid[this.playerPosition.y][this.playerPosition.x] =
this.pathCharacter;
return true;
}
static generateField(height, width, holePercentage = 0.2) {
const field = Array.from({ length: height }, () => Array(width).fill("░"));
const hatPosition = {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height),
};
field[hatPosition.y][hatPosition.x] = "^";
let holesToPlace = Math.floor(height * width * holePercentage);
while (holesToPlace > 0) {
const holeX = Math.floor(Math.random() * width);
const holeY = Math.floor(Math.random() * height);
if (field[holeY][holeX] === "░") {
field[holeY][holeX] = "O";
holesToPlace--;
}
}
return field;
}
}
module.exports = Field;