-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindYourHat.js
82 lines (67 loc) · 1.65 KB
/
findYourHat.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
const prompt = require('prompt-sync')({sigint: true});
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
class Field {
constructor(field){
this._field = field;
}
get field(){
return this._field;
}
print(){
for(let i=0; i<this._field.length; i++){
console.log(this._field[i].join(""));
}
}
static generateField(height, width){
}
playGame(){
let i=0, j=0;
for(let k=0; ;k++){
const direction = prompt("Which direction would you like to move?");
this.field[i][j] = fieldCharacter;
// Increase or decrease i, j by direction
if(direction === 'left'){
j--;
} else if(direction === 'up'){
i--;
} else if(direction === 'right'){
j++;
} else if(direction === 'down'){
i++;
}
// Make sure to not get out of the area
if(i > 3) {
i--;
} else if(i < 0){
i++;
} else if(j > 3){
j--;
} else if(j < 0){
j++;
}
// Decide user's state by the location
if(this.field[i][j]===fieldCharacter){
this.field[i][j] = '*';
} else if(this.field[i][j]===hole){
console.log('Game Over. Try Again.');
break;
} else if(this.field[i][j]===hat){
console.log('Congratulations! You did it!');
break;
}
// print field
this.print();
}
}
}
// The Field constructor take a two-dimensional array
const myField = new Field([
[pathCharacter, fieldCharacter, hole],
[fieldCharacter, hole, fieldCharacter],
[fieldCharacter, hat, fieldCharacter],
]);
myField.print();
myField.playGame();