-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflip-game.html
More file actions
172 lines (143 loc) · 4.3 KB
/
flip-game.html
File metadata and controls
172 lines (143 loc) · 4.3 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
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
<!doctype html>
<html>
<head>
<title>Flip Game</title>
<style>
body {
background-color: white;
color: black;
font-family: "Times New Roman", Times, serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 50px;
}
h1 {
margin-bottom: 20px;
}
.scoreboard {
margin: 20px 0;
font-size: 20px;
text-align: center;
}
.result-box {
margin: 20px auto;
font-size: 24px;
font-weight: bold;
padding: 20px;
border: 2px solid black;
width: 250px;
border-radius: 10px;
text-align: center;
}
input[type="text"] {
padding: 8px;
font-size: 18px;
width: 100px;
text-align: center;
border: 2px solid black;
border-radius: 5px;
margin-right: 10px;
}
input[type="text"]::placeholder {
font-size: 14px;
}
button {
padding: 10px 20px;
font-size: 18px;
border: 2px solid black;
border-radius: 10px;
cursor: pointer;
background-color: white;
color: black;
}
.coin {
width: 100px;
height: 100px;
border-radius: 50%;
margin: 20px auto;
line-height: 100px;
font-size: 14px; /* fits text inside coin */
font-weight: bold;
border: 3px solid gold;
display: flex;
justify-content: center;
align-items: center;
transition: transform 1s ease-in-out;
}
.input-container {
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Coin Flip Game</h1>
<div class="scoreboard">
Wins: <span id="wins">0</span> | Losses: <span id="losses">0</span>
</div>
<div class="coin" id="coin">?</div>
<div class="result-box" id="resultBox">Make your guess!</div>
<div class="input-container">
<input type="text" id="guessInput" placeholder="Heads or Tails">
<button id="flipBtn">Flip Coin</button>
</div>
<script>
let wins = 0;
let losses = 0;
let flipCount = 0;
const winsSpan = document.getElementById('wins');
const lossesSpan = document.getElementById('losses');
const coinDiv = document.getElementById('coin');
const resultBox = document.getElementById('resultBox');
const flipBtn = document.getElementById('flipBtn');
const guessInput = document.getElementById('guessInput');
function flipCoin() {
const guess = guessInput.value.toLowerCase();
if (guess !== "heads" && guess !== "tails") {
resultBox.textContent = "Please enter 'Heads' or 'Tails'";
console.log("Invalid input:", guess);
return;
}
flipCount++;
coinDiv.textContent = "?";
const spins = Math.floor(Math.random() * 6 + 3);
const rotationDegrees = spins * 360;
const delay = 1000 + flipCount * 200;
coinDiv.style.transition = `transform ${delay}ms ease-in-out`;
coinDiv.style.transform = `rotateY(${rotationDegrees}deg)`;
console.log(`Flip #${flipCount} | Player guess: ${guess}`);
setTimeout(() => {
// Player's result
const playerResult = Math.random() < 0.5 ? 'heads' : 'tails';
coinDiv.textContent = playerResult.charAt(0).toUpperCase() + playerResult.slice(1);
// Computer's result
const computerResult = Math.random() < 0.5 ? 'heads' : 'tails';
// Determine winner
let outcome;
if (guess === playerResult) {
wins++;
outcome = `Player wins! Computer got ${computerResult.charAt(0).toUpperCase() + computerResult.slice(1)}`;
} else {
losses++;
outcome = `Player loses! Computer got ${computerResult.charAt(0).toUpperCase() + computerResult.slice(1)}`;
}
resultBox.textContent = outcome;
winsSpan.textContent = wins;
lossesSpan.textContent = losses;
// Log all to console
console.log(`Player coin: ${playerResult} | Computer coin: ${computerResult}`);
console.log(outcome);
console.log(`Scoreboard => Wins: ${wins}, Losses: ${losses}`);
console.log("-------------------------");
// Reset input
guessInput.value = "";
guessInput.focus();
}, delay);
}
flipBtn.addEventListener('click', flipCoin);
</script>
</body>
</html>