-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (140 loc) · 3.72 KB
/
index.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
const prompt = require('prompt-sync')({sigint: true});
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
class Field {
constructor(inputBoard) {
this.board = inputBoard
this.height = 0
this.width = 0
this.randomHatPosition = [Math.floor(Math.random() * this.board[0].length), Math.floor(Math.random() * this.board.length)]
this.randomPlayerPosition = [Math.floor(Math.random() * this.board[0].length), Math.floor(Math.random() * this.board.length)]
this.randomPosition()
this.end = false //boolean
this.win = false //boolean
}
isFound() {
console.log('this.playerPosition', this.playerPosition)
console.log('this.hatPosition', this.hatPosition)
// console.log(this.playerPosition[0] === this.hatPosition[0] && this.playerPosition[1] === this.hatPosition[1])
const found = this.playerPosition[0] === this.hatPosition[0] && this.playerPosition[1] === this.hatPosition[1]
if(found) {
// console.log('is found', found)
}
console.log('is found', found)
}
randomPosition() {
console.log(this.randomHatPosition)
console.log(this.randomPlayerPosition)
this.board[this.randomHatPosition[1]][this.randomHatPosition[0]] = '^'
this.board[this.randomPlayerPosition[1]][this.randomPlayerPosition[0]] = '*'
this.playerPosition = this.randomPlayerPosition
this.hatPosition = this.randomHatPosition
}
print() {
this.board[this.randomHatPosition[1]][this.randomHatPosition[0]] = '^'
this.board[this.playerPosition[1]][this.playerPosition[0]] = '*'
console.log(this.board.map( r => r.join('')).join('\n'))
console.log(`hat position = ${this.hatPosition}`)
console.log(`player position = ${this.playerPosition}`)
}
moveUp() {
if(this.playerPosition[1] > 0) {
this.playerPosition[1]--
}
this.isFound()
}
moveDown() {
if(this.playerPosition[1] < this.board[0].length - 1) {
this.playerPosition[1]++
}
this.isFound()
}
moveLeft() {
if(this.playerPosition[0] > 0) {
this.playerPosition[0]--
}
this.isFound()
}
moveRight() {
if(this.playerPosition[0] < this.board[0].length - 1) {
this.playerPosition[0]++
}
this.isFound()
}
static generateField(height, width, percentage) {
// if(height >= 3 && height <= 10 && width >= 3 && width <= 10 && percentage >= 0 && percentage <=100) {
this.height = height
this.width = width
const matrix = [];
for(let i=0; i < height; i++) {
matrix[i] = ['░'];
for(let j=0; j <width; j++) {
matrix[i][j] = '░';
}
}
return matrix
}
}
// console.log(Field.generateField(3, 4, 10))
const myField = new Field(Field.generateField(5, 5, 10));
/*const myField = new Field([
['*', '░', 'O'],
['░', 'O', '░'],
['░', '^', '░'],
])
console.log(myField)
*/
const isWin = () => {
const win = myField.win
console.log('win = ', win)
if(win) {
return true
}
return false
}
const checkEnd = () => {
const end = myField.end
console.log('end = ', end)
if(end) {
return true
} else {
return false
}
}
const isGameEnd = () => {
const ended = checkEnd()
if (ended) {
const win = isWin()
if(win) {
console.log('Win')
} else {
console.log('Lose')
}
return true
}
return false
}
while(!isGameEnd()){
myField.print()
console.log('w:up a:left s:down d:right')
const input = prompt('Which way ? ')
switch (input) {
case 'w':
myField.moveUp()
break;
case 's':
myField.moveDown()
break;
case 'a':
myField.moveLeft()
break;
case 'd':
myField.moveRight()
break;
default:
console.log('Only W A S D')
break;
}
}