-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiceGame.html
210 lines (170 loc) · 6.47 KB
/
diceGame.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Dice Game Logic</h1>
<input type="text" id="players" placeholder="players"/>
<input type="text" id="dices" placeholder="dices"/>
<button onclick="startGame()">Start Game</button>
<br>
<small id="errorMsg"></small>
<br>
<br>
<div id="stateText">Result will show here</div>
<div id="result"></div>
</body>
<script>
let players = []
let dices = []
let points = []
let gameRecord = []
let gamePlay = -1
let readyPlayer = 0
async function startGame() {
const playerForm = document.getElementById('players').value
const diceForm = document.getElementById('dices').value
const errorMsg = document.getElementById('errorMsg')
errorMsg.innerHTML = ''
document.getElementById('result').innerHTML = ''
players = []
dices = []
points = []
gameRecord = []
gamePlay = -1
readyPlayer = 0
if (!playerForm || !diceForm) {
errorMsg.innerHTML = 'Player & Dice is required'
return
}
//set ready player
readyPlayer = parseInt(playerForm)
await createPoint(playerForm)
await createPlayer(playerForm, diceForm)
await checkAvailablePlayer(players)
}
function createPoint(nPlayers) {
return new Promise(function(resolve) {
for (let p=0; p < nPlayers; p++) {
points[`player-${p+1}`] = 0
}
resolve()
})
}
function createPlayer(nPlayers, nDices) {
return new Promise(function(resolve) {
for (let p=0; p < nPlayers; p++) {
players[`player-${p+1}`] = []
for (let d=0; d < nDices; d++) {
players[`player-${p+1}`].push(Math.floor(Math.random() * 6) + 1);
}
}
resolve()
})
}
function playDice(player) {
return new Promise(function(resolve) {
let i = 1
let temptData = []
for (let p in players) {
// init zero array
temptData[0] = []
for (let d=0; d < players[`player-${i}`].length; d++) {
temptData[0].push(Math.floor(Math.random() * 6) + 1)
}
// push data from temptData
players[`player-${i}`] = temptData[0];
//set game record original
gameRecord[gamePlay].orinal[`player-${i}`] = players[`player-${i}`]
i++
}
resolve()
})
}
function evaluate(playerDatas) {
return new Promise(function(resolve) {
let i = 1
for (let p in playerDatas) {
// count point
points[`player-${i}`] += playerDatas[`player-${i}`].filter(function(x) {
return x === 6
}).length
// record point
gameRecord[gamePlay].point[`player-${i}`] = points[`player-${i}`]
// remove dice if 6
playerDatas[`player-${i}`] = playerDatas[`player-${i}`].filter(function(x) {
return x !== 6
})
// set game record evaluate
gameRecord[gamePlay].evaluate[`player-${i}`] = playerDatas[`player-${i}`]
i++
}
resolve()
})
}
async function checkAvailablePlayer(players) {
let i = 1
readyPlayer = 0
await new Promise(function(resolve) {
for (let p in players) {
if (players[`player-${i}`].length > 0) {
readyPlayer = readyPlayer + 1
}
i++
}
resolve()
})
if (readyPlayer > 1) {
gamePlay = gamePlay + 1
//create new record game set
gameRecord[gamePlay] = {
orinal: [],
evaluate: [],
point: []
}
await playDice(players)
await evaluate(players)
await checkAvailablePlayer(players)
} else {
const element = document.getElementById('result')
gameRecord.forEach((ele, idx) => {
let text = ''
const createEle = document.createElement('div')
text = `Game ${idx+1} <br>`
text += `Original <br>`
text += `==================================================== <br>`
for (let ori in ele.orinal) {
text += `${ori}[${idx > 0 ? gameRecord[idx-1].point[ori] : 0}]:[ ${ele.orinal[ori].toString()} ] <br>`
}
text += `---------------------------------------------------------------------------------------- <br>`
text += `Evaluate <br>`
text += `==================================================== <br>`
for (let evaluate in ele.evaluate) {
text += `${evaluate}[${ele.point[evaluate]}]:[ ${ele.evaluate[evaluate].toString()} ] <br>`
}
text += `---------------------------------------------------------------------------------------- <br> <br>`
createEle.innerHTML = text
element.appendChild(createEle)
})
// find winner
let curentBigest = {
winner: null,
point: 0
}
for (point in points) {
if (curentBigest.point < points[point]) {
curentBigest = { winner: point, point: points[point] }
}
}
const createEle = document.createElement('div')
let text
text = `Winner is <b>${curentBigest.winner}</b> with point <b>${curentBigest.point}</b>`
createEle.innerHTML = text
element.appendChild(createEle)
}
}
</script>
</html>