-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlow.js
86 lines (73 loc) · 1.98 KB
/
highlow.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
//reference to the readline-sync library
const rs = require("readline-sync");
//variables for the basic gameplay loop
let startingNum = 0;
let aiNum = 0;
let playerStrikes = 0;
let playerPoints = 0;
//functions for logic control
function getRandomNum() {
return Math.floor(Math.random() * 100);
}
function getAINum(startNum) {
num = getRandomNum();
if(num === startNum) {
num = getRandomNum();
}
else{
return num;
}
}
function addPoint() {
playerPoints++;
console.log(`Correct! My number was ${aiNum}. You get 1 point!`);
displayScore();
startGame = rs.keyInYN('Would you like to keep playing?');
}
function addStrike(maxStrikes) {
playerStrikes++;
if(playerStrikes < maxStrikes) {
console.log(`Sorry, you are incorrect. My number was ${aiNum}. That's strike ${playerStrikes}. ${maxStrikes - playerStrikes} more strikes and it's game over!`);
displayScore();
}
else {
gameOver();
}
startGame = rs.keyInYN('Would you like to keep playing?');
}
function displayScore() {
console.log(`You currently have ${playerPoints} points!`);
}
function gameOver() {
console.log('That\'s strike three! GAME OVER.');
console.log(`Final score: ${playerPoints}`);
playerPoints = 0;
playerStrikes = 0;
startGame = false;
}
function goodbye() {
console.log(`Final score: ${playerPoints}`);
console.log('Goodbye!');
}
function playGame() {
startingNum = getRandomNum();
aiNum = getAINum(startingNum);
const prompt = rs.question(`Is the number I am thinking of higher or lower than ${startingNum}? `, {limit: ['higher', 'lower'], limitMessage: 'Sorry, please input \'higher\' or \'lower\' '});
if ((prompt === 'higher' && aiNum > startingNum) || (prompt === 'lower' && aiNum < startingNum)) {
addPoint();
}
else {
addStrike(3);
}
}
//core gameplay loop
let startGame = rs.keyInYN('Would you like to play a game of High or Low?');
while(startGame) {
playGame();
}
if(playerPoints <= 0) {
console.log('Goodbye!');
}
else {
goodbye();
}