forked from Aniket965/Hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsgame.html
146 lines (129 loc) · 5.41 KB
/
jsgame.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
137
138
139
140
141
142
143
144
145
146
<!--
Question:
Build a game in Javascript with following rules -
User has 1000 INR when he starts the game
Every try(click on start) is charged 100 INR
Game is over if user has less than 100 INR
Every try generates three random numbers, each random number is in 0-9 range
If all the numbers are odd/even (eg. 2 4 6), user gets 300 INR
If the numbers are in sequence with difference of 1 in any order (eg. 2 3 4 or 3 2 4 or 4 6 5), user gets 800 INR
If all the numbers are same (eg. 4 4 4), user gets 1000 INR
Else user gets no money
You can use any javascript plugin you are familiar with.
-->
<!-- Author: Ankitkumar Boghra([email protected])-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>JS Game</title>
<style>
.removeElement {
display: none;
}
</style>
</head>
<body>
<div id="intitialRules">
<h1>The luck game</h1>
<h2>Rules</h2>
<ul>
<li>You will have 1000 INR when you start the game</li>
<li>Every try(click on start) is charged 100 INR</li>
<li>Game is over if you have less than 100 INR</li>
<li>Every try generates three random numbers, each random number is in 0-9 range</li>
<li>If all the numbers are odd/even (eg. 2 4 6), you get 300 INR</li>
<li>If the numbers are in sequence with difference of 1 in any order (eg. 2 3 4 or 3 2 4 or 4 6 5), you get 800
INR
</li>
<li>If all the numbers are same (eg. 4 4 4), you get 1000 INR</li>
<li>Else you get no money</li>
</ul>
<hr>
<h3></h3>
</div>
<form id="form">
<input type="button" value="Try!!" onclick="tryOnce()">
</form>
<h2 id="randNums"></h2>
<h2 id="scoreUpdate"></h2>
<h2 id="score"></h2>
<script>
let score = 1000;//Initial score 1000 INR
function incrementScore(num) { score += num; }
function decrementScore(num) { score -= num; }
function printScore() { document.getElementById("score").innerHTML = "Your score is " + score + " INR"; }
function printRand(rand1, rand2, rand3) { document.getElementById("randNums").innerHTML = "You got " + rand1 + ", " + rand2 + ", " + rand3; }
function scoreUpdate(message, num) {
let idScoreUpdate = document.getElementById("scoreUpdate");
if (score < 100) {
idScoreUpdate.innerHTML = "GAME OVER!";
document.getElementById("intitialRules").classList.add("removeElement");
document.getElementById("form").classList.add("removeElement");
document.getElementById("randNums").classList.add("removeElement");
} else {
if (num == 0) {
idScoreUpdate.innerHTML = "Sorry you were not lucky this time.";
}
else {
idScoreUpdate.innerHTML = "Lucky you!! You earned " + num + ". (" + message + ")";
}
}
}
function checkSameNumbers(n1, n2, n3) {
if (n1 == n2 && n2 == n3)
return true;
else
return false;
}
function checkEvenOdd(n1, n2, n3) {
if ((n1 % 2 == 0 && n2 % 2 == 0 && n3 % 2 == 0) || (n1 % 2 == 1 && n2 % 2 == 1 && n3 % 2 == 1))
return true;
else
return false;
}
function checkDifferenceByOne(n1, n2, n3) {
let ary = [n1, n2, n3];
ary = ary.sort();
if ((ary[0] == (ary[1] - 1)) && (ary[0] == (ary[2] - 2)))
return true;
else
return false;
}
function tryOnce() {
decrementScore(100);//Decrement 100 for one try
let rand1 = Math.floor(Math.random() * 10);
let rand2 = Math.floor(Math.random() * 10);
let rand3 = Math.floor(Math.random() * 10);
printRand(rand1, rand2, rand3);
// let increment = check(5, 7, 7);//check for manual testing values
let increment = check(rand1, rand2, rand3);//check for earned score for this try
incrementScore(increment);//increment the score
printScore();
}
function check(n1, n2, n3) {
let increment = 0;
//check if all the numbers are same
if (checkSameNumbers(n1, n2, n3)) {
//All 3 numbers same //Increment by 1000 INR
increment = 1000;
scoreUpdate("Same numbers", increment);
} else if (checkDifferenceByOne(n1, n2, n3)) {
//Difference of 1 between all 3 numbers in any order //Increment by 800 INR
increment = 800;
scoreUpdate("Difference of 1", increment);
} else if (checkEvenOdd(n1, n2, n3)) {
//Odd or even //Increment by 300 INR
increment = 300;
scoreUpdate("even odd", increment);
} else {
increment = 0;
scoreUpdate("", increment);
}
return increment;
}
</script>
</body>
</html>