forked from makersacademy/bowling-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.js
168 lines (155 loc) · 4.63 KB
/
interface.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
157
158
159
160
161
162
163
164
165
166
167
168
const Frame = require("./frame");
const Scoresheet = require("./scoresheet");
const readLine = require("readline");
const rl = readLine.createInterface({
input: process.stdin,
output: process.stdout,
});
const emptyFrames = [
new Frame(),
new Frame(),
new Frame(),
new Frame(),
new Frame(),
new Frame(),
new Frame(),
new Frame(),
new Frame(),
new Frame(),
];
const scoresheet = new Scoresheet();
let frameCounter = 0;
const input = (frameCounter) => {
if (frameCounter < 9) {
scoresheet.newFrame(emptyFrames[frameCounter]);
scoresheet.addFrame(emptyFrames[frameCounter]);
frameCounter += 1;
rl.question(`Enter your first roll for Frame ${frameCounter}: `, (roll) => {
scoresheet.currentFrame.addRoll(Number(roll));
if (Number(roll) != 10) {
rl.question(
`Enter your second roll for Frame ${frameCounter}: `,
(roll2) => {
scoresheet.currentFrame.addRoll(Number(roll2));
input(frameCounter);
}
);
} else {
input(frameCounter);
}
});
} else if (frameCounter === 9) {
scoresheet.newFrame(emptyFrames[frameCounter]);
scoresheet.addFrame(emptyFrames[frameCounter]);
frameCounter += 1;
rl.question(`Enter your first roll for Frame ${frameCounter}: `, (roll) => {
scoresheet.currentFrame.addRoll(Number(roll));
rl.question(
`Enter your second roll for Frame ${frameCounter}: `,
(roll2) => {
scoresheet.currentFrame.addRoll(Number(roll2));
if (Number(roll) === 10 || Number(roll) + Number(roll2) === 10) {
rl.question(
`Enter your third roll for Frame ${frameCounter}: `,
(roll3) => {
scoresheet.currentFrame.addRoll(Number(roll3));
input(frameCounter);
}
);
} else {
input(frameCounter);
}
}
);
});
} else {
console.log(`Your score is ${scoresheet.totalScore()}.`);
console.log(
` ____________________________________________________________`
);
console.log(
`|__1__|__2__|__3__|__4__|__5__|__6__|__7__|__8__|__9__|__10__|`
);
scoresheetRolls();
scoresheetScore();
console.log(
`|_____|_____|_____|_____|_____|_____|_____|_____|_____|______|`
);
rl.close();
}
};
input(frameCounter);
scoresheetRolls = () => {
string = "|";
for (let i = 0; i < 9; i++) {
if (scoresheet.frames[i].isStrike() === true) {
string = string.concat(` |X|`);
} else if (scoresheet.frames[i].isSpare() === true) {
string = string.concat(` ${scoresheet.frames[i].firstRoll()}|/|`);
} else {
string = string.concat(
` ${scoresheet.frames[i].firstRoll()}|${scoresheet.frames[
i
].secondRoll()}|`
);
}
}
if (scoresheet.frames[9].isStrike() === true) {
if (scoresheet.frames[9].secondRoll() === 10) {
if (scoresheet.frames[9].bonusRoll() === 10) {
string = string.concat(` X|X|X|`);
} else {
string = string.concat(` X|X|${scoresheet.bonusRoll()}|`);
}
} else {
if (
scoresheet.frames[9].secondRoll() + scoresheet.frames[9].bonusRoll() ===
10
) {
string = string.concat(` X|${scoresheet.frames[9].secondRoll()}|/|`);
} else {
string = string.concat(
` X|${scoresheet.frames[9].secondRoll()}|${scoresheet.frames[9].bonusRoll()}|`
);
}
}
} else if (scoresheet.frames[9].isSpare() === true) {
if (scoresheet.frames[9].bonusRoll() === 10) {
string = string.concat(` ${scoresheet.frames[9].firstRoll()}|/|X|`);
} else {
string = string.concat(
` ${scoresheet.frames[9].firstRoll()}|/|${scoresheet.frames[9].bonusRoll()}|`
);
}
} else {
string = string.concat(
` ${scoresheet.frames[9].firstRoll()}|${scoresheet.frames[9].secondRoll()}|`
);
}
console.log(string);
};
scoresheetScore = () => {
let score = 0;
string = "|";
for (let i = 0; i < 9; i++) {
score += scoresheet.frameScore(i);
let scorelength = score.toString().length;
if (scorelength === 1) {
string = string.concat(` ${score} |`);
} else if (scorelength === 2) {
string = string.concat(` ${score} |`);
} else {
string = string.concat(` ${score} |`);
}
}
score += scoresheet.frameScore(9);
let scorelength = score.toString().length;
if (scorelength === 1) {
string = string.concat(` ${score} |`);
} else if (scorelength === 2) {
string = string.concat(` ${score} |`);
} else {
string = string.concat(` ${score} |`);
}
console.log(string);
};