-
Notifications
You must be signed in to change notification settings - Fork 7
/
JavaScript Roulette Game
157 lines (141 loc) · 4.04 KB
/
JavaScript Roulette Game
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
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Game</title>
<style>
*{
box-sizing:border-box;
}
.bet{
position:absolute;
max-width:25px;
padding:3px;
left:0;
right:0;
margin-left: auto;
margin-right:auto;
background-color:yellow;
overflow:hidden;
border-radius:50%;
color:black;
}
.output{
display:grid;
width:80vw;
height:80vh;
margin:auto;
padding:0;
}
.box{
position:relative;
text-align:center;
border:1px solid #ddd;
}
</style>
</head>
<body>
<div class="gamearea">
</div>
<script src="game1.js"></script>
</body>
</html>
const gamearea = document.querySelector('.gamearea');
const score = createEle(gamearea,'div','Score :','score');
const btn = createEle(gamearea,'button','Spin','btn');
const message = createEle(gamearea,'div','Press Spin','message');
const output = createEle(gamearea,'div','','output');
const game = {x:7,y:9,coins:50,sel:[],eles:[],winner:false,styler:['black','white']};
const total = game.x * game.y;
btn.disabled = true;
btn.addEventListener('click',spinner);
createBoard();
updateScore();
function spinner(){
btn.disabled = true;
const ranVal = Math.floor(Math.random()*total)+1;
console.log(ranVal);
game.winner = ranVal-1;
game.styler = [game.eles[ranVal-1].style.backgroundColor,game.eles[ranVal-1].style.color];
const win = game.sel.includes(ranVal);
console.log(win);
const eles = output.querySelectorAll('.bet');
eles.forEach((el)=>{
el.remove();
console.log(el);
})
if(win){
const winAmount = total;
game.coins += winAmount;
message.innerHTML = `Winner on ${ranVal} you won ${winAmount}`;
createEle(game.eles[ranVal-1],'div','$','bet');
game.eles[ranVal-1].style.backgroundColor ='green';
}else{
message.innerHTML = `Lost sorry you did not bet on ${ranVal}`;
game.eles[ranVal-1].style.backgroundColor ='purple';
}
game.sel = [];
updateScore();
game.eles.forEach((el)=>{
el.bet = false;
})
}
function createBoard(){
for(let i=0;i<total;i++){
const temp = createEle(output,'div',`${i+1}`,'box');
if(i%2){
temp.style.backgroundColor = 'red';
}else{
temp.style.backgroundColor = 'black';
temp.style.color = 'white';
}
game.eles.push(temp);
temp.bet = false;
temp.addEventListener('click',(e)=>{
btn.disabled = false;
if(game.winner){
const parTemp = game.eles[game.winner];
parTemp.style.backgroundColor = game.styler[0];
parTemp.style.color = game.styler[1];
game.winner = false;
const bets = parTemp.querySelector('.bet');
if(bets) {bets.remove();}
}
console.log(temp.textContent);
if(temp.bet){
console.log(game.winner);
const bets = temp.querySelector('.bet');
bets.remove();
//console.log(bets);
temp.bet = false;
game.coins++;
const index = game.sel.indexOf(i+1);
if(index > -1){
game.sel.splice(index,1);
}
}else{
game.sel.push(i+1);
game.coins--;
temp.bet = true;
createEle(temp,'div','$','bet');
}
updateScore();
},true);
}
output.style.setProperty(`grid-template-columns`,`repeat(${game.x},1fr)`)
}
function updateScore(){
score.innerHTML = `Coins : ${game.coins}`;
console.log(game.sel);
}
function createEle(parent,eleType,html,eleClass){
const ele = document.createElement(eleType);
ele.innerHTML = html;
ele.classList.add(eleClass);
return parent.appendChild(ele);
}
/*
const div = document.createElement('div');
div.innerHTML = 'Hello World';
div.classList.add('myClass');
gamearea.append(div);
*/