-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (58 loc) · 2.01 KB
/
app.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
let dice_1_img = document.createElement('img');
let dice_2_img = document.createElement('img');
let message = document.getElementById('message');
let points = document.getElementById('points')
let dices = [1, 2, 3, 4, 5, 6];
let rollCount = 0;
let showDice = () => {
let _1st_dice = getRandomDice();
let _2nd_dice = getRandomDice();
dice_1_img.src = `./images/${_1st_dice}.png`;
dice_2_img.src = `./images/${_2nd_dice}.png`;
let dice_img_container = document.getElementById('dice-img-container');
dice_img_container.appendChild(dice_1_img);
dice_img_container.appendChild(dice_2_img);
if( rollCount == 0) {
firstRollLogic( _1st_dice, _2nd_dice )
} else {
secondRollLogic( _1st_dice, _2nd_dice )
}
}
let getRandomDice = () => {
let randIndex = Math.floor(Math.random() * 6);
return dices[randIndex];
}
let firstRollLogic = ( value1, value2) => {
let sum = value1 + value2;
if(sum == 7 || sum == 11){
message.textContent = "YOU WIN!!"
message.style.color = "green";
rollCount = 0;
} else if( sum == 2 || sum == 3 || sum == 12){
message.textContent = "YOU Lost";
message.style.color = "red";
} else {
message.textContent = `Roll again till you get same points,
or If 7, you will lost`;
message.style.color = "blue";
points.textContent = sum;
rollCount++
}
}
let secondRollLogic = (value1, value2) => {
let sum = value1 + value2;
if( sum == points.textContent){
message.textContent = "Points Matched, YOU WIN!!"
message.style.color = "green";
} else if(sum == 7){
message.textContent = "It is 7, YOU LOST";
message.style.color = "red";
} else{
message.textContent = `Roll again till you get same points,
or If 7 you will lost`;
message.style.color = "blue";
}
}
document.getElementById('play-btn').onclick = function () {
showDice();
}