Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
330 lines (303 loc) · 11.1 KB
/
Copy pathscript.js
File metadata and controls
330 lines (303 loc) · 11.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// audio files
const clickAudio = new Audio("./audio/click.mp3");
const bgMusic = new Audio('./audio/bgMusic.mp3');
const child = new Audio('./audio/child.mp3');
const lostGame = new Audio('./audio/lostGame.mp3');
const lostToss = new Audio('./audio/lostToss.mp3');
const wonGame = new Audio('./audio/wonGame.mp3');
const wonToss = new Audio('./audio/wonToss.mp3');
const outAudio = new Audio('./audio/out.mp3');
const outAudio2 = new Audio('./audio/out2.mp3');
var musicPlaying = false;
// the homepage elements
const homePageParagraph = document.getElementById("homePageParagraph");
const homePageHeading = document.getElementById("homePageHeading");
const homePageButton = document.getElementById("homePageButton");
const startGameSectionH2 = document.querySelector("p.start-game-section-content");
// function to play music
function playMusic(){
if(musicPlaying == true){
musicPlaying = false;
bgMusic.pause();
document.getElementById('musicButton').style.textDecoration = "line-through";
}
else if (musicPlaying == false){
musicPlaying = true;
bgMusic.currentTime = 0;
bgMusic.play();
document.getElementById('musicButton').style.textDecoration = "none";
}
}
var rulesCount = 0;
// function to change the homepage content to display the rules
function changeRule() {
clickAudio.currentTime = 0;
clickAudio.play();
switch(rulesCount){
//rule one
case 0:
homePageHeading.innerText = "How to Start the Game";
homePageParagraph.innerText = `Begin with a toss.
The winner of the toss decides who does the batting at first.`
homePageButton.innerText = "Next"
break;
//rule two
case 1:
homePageHeading.innerText = "Understanding how to represent numbers";
homePageParagraph.innerText = `The number of fingers raised represents the number chosen.
Six is represented by a closed fist.`
homePageButton.innerText = "Next"
break;
//rule three
case 2:
homePageHeading.innerText = "Batting and Scoring";
homePageParagraph.innerText = `In batting, both players throw hand signals, numbers in this case simultaneously.
Calculate the runs based on the numbers thrown.
For example, if computer throws a 6, and you throw a 4, you score 4 runs. If you throw a 1 and the computer throws a 5, computer gets 5 runs.`
homePageButton.innerText = "Next"
break;
//rule four
case 3:
homePageHeading.innerText = "Wickets and Winning";
homePageParagraph.innerText = `If both players throw out the same hand sign, the batter is out.
The bowler gets to bat and at the end, who has the highest runs wins.`
var anchorTag = document.createElement('a');
anchorTag.innerText = "START GAME";
anchorTag.setAttribute('href','gamePage.html');
homePageButton.innerText = "";
homePageButton.append(anchorTag);
startGameSectionH2.innerText = "Thats it!";
break;
}
rulesCount++;
}
// gamePage elements
const gameH2 = document.getElementById('gameH2');
const choice1 = document.getElementById('choice1');
const choice2 = document.getElementById('choice2');
const buttonDiv = document.getElementById('buttonDiv');
const numberDiv = document.getElementById("numberDiv");
const okDiv = document.getElementById("okDiv");
const okButton = document.getElementById("okButton");
//function to decide the toss
function decideToss(){
buttonDiv.style.visibility = 'visible';
var randomNumber = Math.random();
var result;
if (randomNumber < 0.5) {
child.currentTime = 0;
child.play();
gameH2.innerText = "You Won the toss! Choose what you want";
choice1.innerText = "Batting";
choice2.innerText = "Balling";
choice1.onclick = () =>{
batting();
}
choice2.onclick = () =>{
balling();
}
}
else {
lostToss.currentTime = 0;
lostToss.play();
randomNumber = Math.random();
if (randomNumber < 0.5) {
result = 2;
gameH2.innerText = "You lost the toss and the computer chose Batting";
}
else {
result = 1;
gameH2.innerText = "You lost the toss and the computer chose Balling";
}
okDiv.style.visibility = 'visible';
buttonDiv.style.visibility = 'hidden';
okButton.onclick = () =>{
okDiv.style.visibility = 'hidden';
if(result == 1){
batting();
}
else if(result == 2){
balling();
}
}
}
}
// function to display that its out batting turn
function batting(){
console.log("BATTING");
gameH2.innerText = "It is your turn to Bat";
document.getElementById('numberDiv').dataset.batOrBall = 'bat';
startGame();
}
// function to display that its our balling turn
function balling(){
console.log("BALLING");
gameH2.innerText = "It is your turn to Ball";
document.getElementById('numberDiv').dataset.batOrBall = 'ball';
startGame();
}
// function to display the number buttons
function startGame(){
numberDiv.style.visibility = "visible";
buttonDiv.style.visibility = "hidden";
}
// score boards
const yourScoreBoard = document.getElementById('yourScore');
const computerScoreBoard = document.getElementById('computerScore');
const handImage = document.getElementsByClassName('hand-img');
var yourScore = 0;
var computerScore = 0;
var turn = 0;
var notOut = true;
var computerNotOut = true;
// function to update the score, onclick is used on number buttons
function updateScore(run){
clickAudio.currentTime = 0;
clickAudio.play();
var batOrBall = document.getElementById('numberDiv').dataset.batOrBall;
const possibleOutcomes = [1,2,3,4,5,6];
var computerRun = possibleOutcomes[Math.floor(Math.random() * possibleOutcomes.length)];
handImage[0].setAttribute('src',`./images/l${run}.png`);
handImage[1].setAttribute('src',`./images/r${computerRun}.png`);
// to update our score during our batting
if(batOrBall === 'bat'){
if(notOut){
// if its the second turn, update turn message
if(turn == 1){
gameH2.innerText = "It is your turn to Bat"
if(computerScore == 0) {
endGame();
}
}
// if out
if(computerRun == run){
// if out on first try, atleast update the score
if(yourScore == 0){
yourScoreBoard.innerText = computerScore;
}
notOut = false;
outAudio.currentTime = 0;
outAudio.play();
gameH2.innerText = "OUT!!";
turn++;
document.getElementById('numberDiv').dataset.batOrBall = 'ball';
var parent = document.getElementById("buttonDiv");
numberDiv.style.visibility = 'hidden';
okDiv.style.visibility = 'visible';
okButton.onclick = () =>{
if(turn == 1) gameH2.innerText = "It is your turn to Ball";
okDiv.style.visibility = 'hidden';
numberDiv.style.visibility = 'visible';
}
}
// if not out
else{
yourScore += run;
yourScoreBoard.innerText = yourScore;
if(turn == 1){
console.log('out');
if(computerScore < yourScore || computerScore == 0){
endGame();
}
}
}
}
}
// to update computer's score during our balling
else if(batOrBall === 'ball'){
if(computerNotOut){
// if its the second turn, update turn message
if(turn == 1){
gameH2.innerText = "It is your turn to Ball"
if(yourScore == 0) {
endGame();
}
}
// if out
if(computerRun == run){
// if out on first try, atleast update the score
if(computerScore == 0){
computerScoreBoard.innerText = computerScore;
}
computerNotOut = false;
outAudio2.currentTime = 0;
outAudio2.play();
gameH2.innerText = "OUT!!";
turn++;
document.getElementById('numberDiv').dataset.batOrBall = 'bat';
numberDiv.style.visibility = 'hidden';
okDiv.style.visibility = 'visible';
okButton.onclick = () =>{
if(turn == 1) gameH2.innerText = "It is your turn to Bat";
okDiv.style.visibility = 'hidden';
numberDiv.style.visibility = 'visible';
}
}
// if not out
else{
computerScore += computerRun;
computerScoreBoard.innerText = computerScore;
if(turn == 1){
if(computerScore > yourScore || yourScore == 0){
endGame();
}
}
}
}
}
if(turn == 2){
endGame();
}
// function to display results
function endGame(){
if(yourScore > computerScore){
wonGame.currentTime = 0;
wonGame.play();
gameH2.innerText = "You Won!!!!";
}
else if(yourScore < computerScore){
lostGame.currentTime = 0;
lostGame.play();
gameH2.innerText = "You Lost!!!!";
}
else if(yourScore == computerScore){
lostGame.currentTime = 0;
lostGame.play();
gameH2.innerText = "Its a Draw!!!!";
}
// displaying newgame button
console.log(okDiv);
okDiv.style.visibility = 'visible';
okButton.innerText = "NEW GAME";
numberDiv.style.visibility = 'hidden';
buttonDiv.style.visibility = 'hidden';
// clicking starts new game
okButton.onclick = () =>{
newGame();
}
}
}
//function to start a new game
function newGame(){
yourScore = 0;
computerScore = 0;
turn = 0;
notOut = true;
computerNotOut = true;
okButton.innerText = "OK";
choice1.innerText = "Heads";
choice2.innerText = "Tails";
yourScoreBoard.innerText = "Waiting Turn...";
computerScoreBoard.innerText = "Waiting Turn...";
gameH2.innerText = "New Game";
okDiv.style.visibility='hidden';
buttonDiv.style.visibility = 'visible';
handImage[0].setAttribute('src',`./images/l0.png`);
handImage[1].setAttribute('src',`./images/r0.png`);
choice1.onclick = () =>{
decideToss();
}
choice1.onclick = () =>{
decideToss();
}
}