-
Notifications
You must be signed in to change notification settings - Fork 1
/
black-white.html
174 lines (139 loc) · 5.54 KB
/
black-white.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
<html>
<head>
<title>Black and white</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<style>
.content {
background-color: #b0ecd8;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}
.card {
margin: 0 auto;
width: 200px;
height: 300px;
background-color: gray;
border-radius: 10px;
transition: background-color 0.5s ease;
box-shadow: 4px 4px 8px 0 rgba(0,0,0,0.2);
}
.pick-card {
display: flex;
justify-content: center;
}
.pick-card .btn {
margin: 20px;
box-shadow: 4px 4px 8px 0 rgb(0 0 0 / 20%);
}
.pick-card .btn:active {
box-shadow: 1px 1px 5px 0 rgb(0 0 0 / 20%);
}
.card.white {
background-color: white;
}
.card.black {
background-color: black;
}
</style>
</head>
<body>
<div class="content">
<div class="card"></div>
<div class="pick-card">
<button type="button" class="btn btn-light">White</button>
<button type="button" class="btn btn-dark">Black</button>
</div>
</div>
<script>
class CardSession {
constructor({onSessionEnd}) {
this.onSessionEnd = onSessionEnd;
this.totalCardsCount = 0;
this.currentCardsCount = 0;
this.sucessCardsCount = 0;
this.pickedCard = null;
}
start(cardsCount) {
this.totalCardsCount = cardsCount;
this.currentCardsCount = 0;
this.sucessCardsCount = 0;
}
getNextCard() {
this.checkResult();
if (this.pickCard === null) return;
const color = Math.random() > 0.5 ? 'black' : 'white';
if (color === this.pickedCard) {
++this.sucessCardsCount;
this.pickedCard = null;
}
++this.currentCardsCount;
return color;
}
pickCard(color) {
if (this.pickCard === null) return;
this.pickedCard = color;
this.checkResult();
}
checkResult() {
if (this.currentCardsCount === this.totalCardsCount) {
const res = this.sucessCardsCount / this.totalCardsCount * 100;
this.onSessionEnd(res);
}
}
}
const main = () => {
startSession();
}
// const addKeydownEventListener = (session) => {
// const cardEl = document.querySelector('.card');
// let colorClass = 'default';
// const arrowRightCode = 39;
// const arrowUpCode = 38;
// const arrowDownCode = 40;
// document.onkeydown = (event) => {
// event.preventDefault();
// if (event.keyCode === arrowRightCode) {
// cardEl.classList.remove(colorClass);
// colorClass = session.getNextCard();
// cardEl.classList.add(colorClass);
// }
// // if (event.keyCode === arrowUpCode) {
// // session.setSuccess();
// // }
// if (event.keyCode === arrowDownCode) {
// session.checkResult();
// }
// }
// };
const addPickCardListener = (session) => {
const buttons = document.querySelectorAll('.pick-card .btn');
const card = document.querySelector('.card');
let resultColor = 'default';
buttons.forEach(button =>
button.addEventListener('click', (event) => {
const pickedColor = button.classList.contains('btn-light') ? 'white' : 'black';
session.pickCard(pickedColor);
setTimeout(() => {
card.classList.remove(resultColor);
resultColor = session.getNextCard();
card.classList.add(resultColor);
}, 300);
})
);
};
const startSession = () => {
const onSessionEnd = (res) => {
alert(`Session is over. Your result: ${res}%.`);
location.reload();
};
const session = new CardSession({onSessionEnd});
session.start(10);
// addKeydownEventListener(session);
addPickCardListener(session);
};
main();
</script>
</body>
</html>