-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
121 lines (92 loc) · 2.37 KB
/
game.html
File metadata and controls
121 lines (92 loc) · 2.37 KB
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
<html>
<head>
</head>
<body>
<p id="money">Current Money: Ƶ10</p>
<p id="winnings">Winnings last bet: Ƶ0</p><br>
<p id="demo">0</p>
<p id="flipsDone">0</p>
<p id="heads">0</p>
<p id="tails">0</p><br>
Flip Selection: <select id="flipChoice">
<option value="1">Heads</option>
<option value="2">Tails</option>
</select><br>
Bet: <input type="text" id="betAmount"><br>
<button onclick="getNew()" id="flipCoinBtn">Flip!</button><br>
<br>
<br>
<button onclick="newGame()">New Game</button>
<script>
var currentMoney = 10;
var bet = 0;
var coinFlip = 0;
var numberFlips = 0;
var numberHeads = 0;
var numberTails = 0;
var selection = 0;
function getNew() {
bet = document.getElementById("betAmount").value;
if(bet <= currentMoney)
{
currentMoney = currentMoney - bet;
numberFlips++;
coinFlip = Math.floor((Math.random() * 2) + 1);
selection = document.getElementById("flipChoice");
selection = selection.value;
checkFlip();
currentMoney = currentMoney + bet;
updateBoard();
if(currentMoney <= 0)
{
document.getElementById("flipCoinBtn").disabled = true;
}
}
else
{
window.alert("More money selected than money available");
}
}
function checkFlip() {
if(coinFlip == 1)
{
document.getElementById("demo").innerHTML = "Result: Heads";
numberHeads++;
}
else if(coinFlip == 2)
{
document.getElementById("demo").innerHTML = "Result: Tails";
numberTails++;
}
if(selection == coinFlip)
{
bet = bet * 2;
document.getElementById("winnings").innerHTML = "Winnings last bet: Ƶ" + bet;
}
else
{
bet = 0;
document.getElementById("winnings").innerHTML = "Winnings last bet: Ƶ" + bet;
}
}
function newGame() {
document.getElementById("flipCoinBtn").disabled = false;
currentMoney = 10;
bet = 0;
coinFlip = 0;
numberFlips = 0;
numberHeads = 0;
numberTails = 0;
selection = 0;
updateBoard();
}
function updateBoard()
{
document.getElementById("heads").innerHTML = "No. Heads: " + numberHeads;
document.getElementById("tails").innerHTML = "No. Tails: " + numberTails;
document.getElementById("money").innerHTML = "Current Money: Ƶ" + currentMoney;
document.getElementById("flipsDone").innerHTML = "Number of Flips: " + numberFlips;
}
</script>
</body>
</html>