-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (79 loc) · 2.32 KB
/
script.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
const strikeButton = document.getElementById("strike");
const resetButton = document.getElementById("reset");
const $team1Score = document.getElementById("score-team1");
const $team1Wickets = document.getElementById("wickets-team1");
const $team2Score = document.getElementById("score-team2");
const $team2Wickets = document.getElementById("wickets-team2");
const strikeAudio = new Audio("http://bit.ly/so-ball-hit");
const gameOverAudio = new Audio("http://bit.ly/so-crowd-cheer");
var team1Score = 0;
var team1Wickets = 0;
var team2Score = 0;
var team2Wickets = 0;
var team1BallsFaced = 0;
var team2BallsFaced = 0;
var turn = 1;
const possibleOutcomes = [0, 1, 2, 3, 4, 6, "W"];
function gameOver() {
gameOverAudio.play();
if (team1Score > team2Score) alert("IND wins");
if (team2Score > team1Score) alert("PAK wins");
if (team2Score === team1Score) alert("It is another superover!");
}
function updateScore() {
$team1Score.textContent = team1Score;
$team1Wickets.textContent = team1Wickets;
$team2Score.textContent = team2Score;
$team2Wickets.textContent = team2Wickets;
}
resetButton.onclick = () => {
window.location.reload();
};
strikeButton.onclick = () => {
//play audio
strikeAudio.pause();
strikeAudio.currentTime = 0;
strikeAudio.play();
//generate random strike value
const randomElement =
possibleOutcomes[Math.floor(Math.random() * possibleOutcomes.length)];
//second batting
if (turn === 2) {
//increase ball count
team2BallsFaced++;
//update score for the ball
document.querySelector(
`#team2-superover div:nth-child(${team2BallsFaced})`
).textContent = randomElement;
// if wicket, update wickets variable
if (randomElement === "W") {
team2Wickets++;
}
// else update score
else {
team2Score += randomElement;
}
// Game over condition
if (
team2BallsFaced === 6 ||
team2Wickets === 2 ||
team2Score > team1Score
) {
turn = 3;
gameOver();
}
}
if (turn === 1) {
team1BallsFaced++;
document.querySelector(
`#team1-superover div:nth-child(${team1BallsFaced})`
).textContent = randomElement;
if (randomElement === "W") {
team1Wickets++;
} else {
team1Score += randomElement;
}
if (team1BallsFaced === 6 || team1Wickets === 2) turn = 2;
}
updateScore();
};