-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (99 loc) · 3.66 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
const readlineSync = require('readline-sync');
const logUpdate = require('log-update');
const obstacles = ['*', '=', 'O'];
function getObstacle() {
return obstacles[Math.floor(Math.random() * obstacles.length)];
}
function generateRaceLine(withObstacle) {
const raceLine = ['.', '.', '.'];
if (withObstacle) {
const numObstacles = Math.floor(Math.random() * 2) + 1;
const obstacle = getObstacle();
for (let index = 0; index < numObstacles; index++) {
let positionObstacle = Math.floor(Math.random() * 3);
raceLine[positionObstacle] = obstacle;
}
}
return `|${raceLine.join('')}|`;
}
function generateLines(numberOfLines = 1, withObstacle = true) {
let raceLines = [];
for (let index = 0; index < numberOfLines; index++) {
raceLines.push(generateRaceLine(withObstacle));
}
return raceLines;
}
function displayRaceTrack(raceLines) {
let display = '';
for (let raceLine of raceLines) {
display += `${raceLine}\n`;
}
return display;
}
function doesItHitObstacle(userPositionIndex, nextRaceLine) {
let itHits = false;
let obstacleIndex = nextRaceLine.split('').findIndex((structure) => obstacles.includes(structure));
if (obstacleIndex > -1) {
return userPositionIndex == obstacleIndex;
}
return itHits;
}
function containsObstacles(raceLine) {
return raceLine.split('').some(structure => obstacles.includes(structure));
}
function displayTotalScore(score) {
console.log('Total Score: ', score);
}
(function () {
let totalScore = 0;
let run = true;
let currentUserPosition = 2;
let addNextObstacle = true;
let raceTrack = [...generateLines(1, false), ...generateLines(1, true), ...generateLines(3, false), ...generateLines(1, true), ...generateLines(3, false), '|.X.|'];
console.log('Use A(left), D(right) to steer, W(Move Forward). Quit(Q)');
while (run) {
logUpdate(displayRaceTrack(raceTrack));
let command = readlineSync.prompt();
while (!(command == 'A' || command == 'D' || command == 'Q' || command == 'W')) {
console.log('Enter a valid command');
command = readlineSync.prompt();
}
if (command == 'A') {
currentUserPosition = currentUserPosition - 1;
if (currentUserPosition == 0) {
console.log('You hit the runway edge.');
displayTotalScore(totalScore);
break;
}
} else if (command == 'D') {
currentUserPosition = currentUserPosition + 1;
if (currentUserPosition == 4) {
console.log('You hit the runway edge.');
displayTotalScore(totalScore);
break;
}
} else if (command == 'Q') {
console.log('You left the game.');
displayTotalScore(totalScore);
break;
}
const nextRaceLine = raceTrack[raceTrack.length - 2];
if (doesItHitObstacle(currentUserPosition, nextRaceLine)) {
console.log('GAME OVER');
displayTotalScore(totalScore);
break;
}
if (containsObstacles(nextRaceLine)) {
totalScore += 5;
} else {
totalScore += 1;
}
let nextRaceLineSplits = nextRaceLine.split('');
nextRaceLineSplits.splice(currentUserPosition, 1, 'X');
raceTrack[raceTrack.length - 2] = nextRaceLineSplits.join('');
let newRaceLines = generateLines(addNextObstacle ? 1 : 3, addNextObstacle);
addNextObstacle = !addNextObstacle;
raceTrack.pop();
raceTrack = [...newRaceLines, ...raceTrack];
}
})();