-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindHat.js.js
123 lines (110 loc) · 3.53 KB
/
findHat.js.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
const prompt = require('prompt-sync')({sigint: true});
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
//define class
class Field {
constructor(hatAndHoles, field) {
this._field = field;
this._hatAndHoles = hatAndHoles;
}
//play game method
playGame() {
let y = 0;
let x = 0;
this.print(this._field);
while (this._hatAndHoles[y][x] === pathCharacter || this._hatAndHoles[y][x] === fieldCharacter) {
const direction = prompt('Please choose a direction, enter U for Up, D for Down, L for Left or R for Right. \n');
if (direction.toUpperCase() === 'U') {
if (y === 0) {
console.log('Invalid Up movement. Please choose another direction')
} else {
y -=1
}
} else if (direction.toUpperCase() === 'D') {
if (y >= this._field.length) {
console.log('Invalid Down movement. Please choose another direction')
} else {
y +=1
}
} else if (direction.toUpperCase() === 'L') {
if (x === 0) {
console.log('Invalid Left movement. Please choose another direction')
} else {
x -= 1
}
} else if (direction.toUpperCase() === 'R') {
if (x >= this._field[y].length) {
console.log('Invalid Right movement. Please choose another direction')
} else {
x += 1
}
} else {
console.log('Invalid entry. Please enter U, D, L or R')
}
if (this._hatAndHoles[y][x] === hat) {
console.log('You win, found a Hat!')
} else if (this._hatAndHoles[y][x] === hole) {
console.log('Game Over, you fell in a hole!')
} else {
this._field[y][x] = pathCharacter;
this.print(this._field);
}
}
}
//print field method
print() {
for (let row of this._field){
console.log(row.join(' '));
}
}
//generate field with hat and holes
static generateField(height, width, holes) {
let newField = [];
for (let i = 0; i < height; i++) {
newField.push([]);
for (let j = 0; j < height; j++) {
newField[i].push(fieldCharacter)
};
};
newField[0][0] = pathCharacter;
let hatX = Math.floor(Math.random() * width);
let hatY = Math.floor(Math.random() * height);
newField[hatY][hatX] = hat;
for (let k = holes; k > 0; k--) {
let holeX = hatX;
let holeY = hatY;
while (holeX === hatX) {
holeX = Math.floor(Math.random() * width)
};
while (holeY === hatY) {
holeY = Math.floor(Math.random() * height)
};
newField[holeY][holeX] = hole;
}
return newField;
}
//generate blank field for the user to traverse without seeing the hat and holes
static generateBlankField(height, width){
let newField = [];
for (let i = 0; i < height; i++) {
newField.push([]);
for (let j = 0; j < height; j++) {
newField[i].push(fieldCharacter)
};
};
newField[0][0] = pathCharacter;
return newField;
}
}
let myField
//create the blank field for the user to see
const blankField = Field.generateBlankField(5, 5)
//created the field with the hat and holes
const newField = Field.generateField(5, 5, 1);
console.log(blankField);
//instantiate a Field object using newField = hatAndHoles and field = blankField
myField = new Field (newField, blankField);
//call playGame method
myField.playGame();