forked from makersacademy/bowling-challenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.js
139 lines (120 loc) · 3.02 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
$( document ).ready(function() {
buttons(0);
$('#restart').click(function() {
location.reload();
});
});
var game = new Game();
var frameIndex = 0;
var firstRoll = 0;
var rollIndex = 0;
var rolls = [];
function buttons(pins) {
var buttonStr = '';
for(var i = 0; i < (11-pins); i++) {
buttonStr += '<button type="button" class="button" onclick="roll(' + i + ')">' + i + '</button> ';
};
$('#buttons').html(buttonStr);
};
function roll(pins) {
if (isFinalFrame() && rollIndex == 2) {
updateFinalFrame(pins);
finishGame();
} else if (isFinalFrame() && rollIndex == 1 && (firstRoll + pins < 10)){
updateFinalFrame(pins);
rolls.push(0);
finishGame();
} else if (isFinalFrame()) {
updateFinalFrame(pins);
} else {
updateFrame(pins);
}
};
function addFinal() {
game.addFinalFrame(rolls[0], rolls[1], rolls[2]);
};
function finishGame() {
addFinal();
updateScore(10);
updateButtons();
printFinalScore();
};
function printFinalScore() {
var finalScore = game.score(10);
$('#score').text(`Your final score is ${finalScore}`);
};
function updateFrame(pins) {
if (pins == 10) {
updateWithStrike();
updateScore(game.frames.length);
} else if (rollIndex == 1 && (firstRoll + pins == 10)) {
updateWithSpare(pins);
updateScore(game.frames.length);
} else {
updateWithRoll(pins);
};
}
function isFinalFrame() {
return game.frames.length == 9;
};
function updateFinalFrame(pins) {
if (pins == 10) {
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html('X');
frameIndex++;
rollIndex++;
rolls.push(pins);
updateButtons(0);
} else if (rollIndex == 1 && (firstRoll + pins == 10)) {
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html('/');
frameIndex++;
rollIndex++;
rolls.push(pins);
updateButtons(0);
} else {
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html(pins);
frameIndex++;
rollIndex++;
firstRoll = pins;
rolls.push(pins);
updateButtons(pins);
};
};
function updateScore(index) {
for(var i = 1; i < index + 1; i++) {
var score = game.score(i);
$('#scoresheetTable tr:eq(2) td:eq(' + (i - 1) + ')').html(score);
};
};
function updateWithSpare(pins) {
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html('/');
frameIndex++;
rollIndex = 0;
game.addFrame(firstRoll, pins)
updateButtons(0);
};
function updateWithStrike() {
frameIndex++;
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html('X');
frameIndex++;
game.addFrame(10);
updateButtons(0);
};
function updateWithRoll(pins) {
if (rollIndex == 0) {
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html(pins);
firstRoll = pins;
frameIndex++;
rollIndex++;
updateButtons(pins);
} else {
$('#scoresheetTable tr:eq(1) td:eq(' + frameIndex + ')').html(pins);
frameIndex++;
rollIndex = 0;
game.addFrame(firstRoll, pins);
updateScore(game.frames.length);
updateButtons(0);
};
};
function updateButtons(pins) {
buttons(pins);
};