-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
136 lines (114 loc) · 4.15 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Game</title>
<link rel="stylesheet" href="index.css">
</head>
<body class="body" >
<div class="background-image">
<h1>Bat Ball Stump Game</h1>
<!-- For Bat Button -->
<button class="choicebutton" onclick = "
let computerchoice = generatecomputerchoice();
let result;
result = getresult('Bat' , computerchoice);
let show = print('Bat', computerchoice, result);
"><img src="images/bat.png" alt="Bat image" class="choiceimage"></button>
<!-- For Ball Button -->
<button class="choicebutton" onclick =
" computerchoice = generatecomputerchoice();
result = getresult('Ball' , computerchoice);
show = print( 'Ball' , computerchoice , result)
"><img src="images/ball.png" alt="Ball image" class="choiceimage"></button>
<!-- For Stump Button -->
<button class="choicebutton" onclick = "
computerchoice = generatecomputerchoice();
result = getresult('Stump' , computerchoice);
show = print( 'Stump' , computerchoice , result)
"><img src="images/stump.png" alt="Stump image" class="choiceimage"></button>
<h3 id="usermove"></h3>
<h3 id="computermove"></h3>
<h3 id="result"></h3>
<h3 id="score"></h3>
<button class="resetbutton"
onclick="localStorage.clear()
resetscore();
"><img src="images/refresh.png" alt="refresh button" class="resetimage"></button>
</div>
<script>
let scorestr = localStorage.getItem('score');
resetscore(scorestr);
function resetscore(scorestr){
score= scorestr ? JSON.parse(scorestr) : {
win : 0,
Lose : 0,
Tie : 0,
};
score.displayscore = function(){
return `
won : ${score.win}
lose : ${score.Lose}
Tie : ${score.Tie}`}
print();
}
function generatecomputerchoice() {
let randomnumber = Math.random() * 3;
if (randomnumber > 0 && randomnumber <= 1) {
return 'Bat';
} else if (randomnumber > 1 && randomnumber <= 2) {
return 'Ball';
}
else {
return 'Stump';
}
return computerchoice;
}
function getresult(usermove,computermove){
if (usermove === computermove ){
score.Tie++;
return `It's Tie`;
}
else if (usermove === 'Bat' && computermove === 'Ball')
{
score.win++ ;
return `You won` ;
}
else if (usermove === 'Bat' && computermove === 'Stump')
{
score.Lose++ ;
return 'You lose';
}
else if (usermove === 'Ball' && computermove === 'Bat')
{
score.Lose++ ;
return 'You lose' ;
}
else if (usermove === 'Ball' && computermove === 'Stump')
{
score.win++ ;
return 'You win' ;
}
else if (usermove === 'Stump' && computermove === 'Bat')
{
score.win++ ;
return 'You win'
}
else if (usermove === 'Stump' && computermove === 'Ball')
{
score.Lose++ ;
return 'You lose'
}
}
function print(usermove , computermove , result){
console.log(score);
localStorage.setItem('Score',JSON.stringify(score));
document.querySelector('#usermove').innerText =
usermove ?`You Have chosen ${usermove}` : '';
document.querySelector('#computermove').innerText =
computermove ?`Computer choice ${computermove}` : '';
document.querySelector('#result').innerText = result || '';
document.querySelector('#score').innerText = score.displayscore();
}
</script>
</body>
</html>