-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAABB.html
More file actions
261 lines (228 loc) · 10.4 KB
/
AABB.html
File metadata and controls
261 lines (228 loc) · 10.4 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<title>Guess Number</title>
<style>
body {
min-height: 450px;
background-image: url('https://1.bp.blogspot.com/-zjtpfaCfVNw/X-FcyfXrxOI/AAAAAAABdFE/I7TA9lviiugSB-VHTcJtA1I4UMxv-kumwCNcBGAsYHQ/s1500/onepiece19_kurohige_teach.png');
background-size: 600px;
background-repeat: no-repeat;
background-position: bottom;
}
.container-md {
max-width: 400px !important;
}
#guess-area {
position: absolute;
bottom: 100px;
left: 0;
right: 0;
margin: auto;
}
body {
position: relative;
}
</style>
</head>
<body>
<!-- 按鈕區 -->
<div class="container-md">
<div class="col-auto my-3">
<div class="input-group d-flex justify-content-center">
<button type="button" id="start"
style="width: 100px; height: 100px; background-color: transparent; border:none; background-size: 100px; position: relative;">
<img src="https://4.bp.blogspot.com/-exP4mmxcGrI/UU--vTE0_SI/AAAAAAAAO9A/Ro7Evu7eEhg/s1600/kaizokusen.png"
style="width:100px; height:100px;">
<span class="text-light fw-bold fs-5" style="position:absolute; top: 50px; left:35px;">開始</span>
</button>
<button type="button" id="restart"
style="width: 100px; height: 100px; background-color: transparent; border:none; background-size: 100px; position: relative;">
<img src="https://4.bp.blogspot.com/-exP4mmxcGrI/UU--vTE0_SI/AAAAAAAAO9A/Ro7Evu7eEhg/s1600/kaizokusen.png"
style="width:100px; height:100px;">
<span class="text-light fw-bold fs-5" style="position:absolute; top: 50px; left:40px;">重來</span>
</button>
<button type="button" id="peep"
style="width: 100px; height: 100px; background-color: transparent; border:none; background-size: 100px; position: relative;">
<img src="https://4.bp.blogspot.com/-exP4mmxcGrI/UU--vTE0_SI/AAAAAAAAO9A/Ro7Evu7eEhg/s1600/kaizokusen.png"
style="width:100px; height:100px;">
<span class="text-light fw-bold fs-5" style="position:absolute; top: 50px; left:35px;">看答案</span>
</button>
</div>
</div>
</div>
<!-- 答案區 -->
<div class="container-md" id="show">
<table class="table"></table>
</div>
<!-- 猜測區 -->
<div class="container-md" id="guess-area">
<div class="col-auto my-3">
<div class="input-group">
<div class="input-group-text">猜數字</div>
<input type="text" class="form-control" id="guess_input" placeholder="Please keyin 4 different digits"
onkeyup="value=value.replace(/[^\d]/g,'')">
<button class="btn btn-warning" type="button" id="guess">猜!</button>
</div>
</div>
</div>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js"
integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT"
crossorigin="anonymous"></script>
<script>
// 開始 button
let start = document.getElementById("start");
let answer = [];
start.addEventListener('click', function () {
let num_array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
answer = shuffler(num_array).splice(0, 4); //選位置0-3的數值
let tr = document.createElement("tr");
let td = document.createElement("td");
// td.innerHTML = `<span class="badge bg-warning" style="margin-left:5px;">開始</span>`
td.innerHTML = "開始"
td.setAttribute("style", "height:50px; text-align:center; color:#fff;");
tr.appendChild(td);
table.appendChild(tr);
start.disabled = true;
});
//Fisher-Yates Shuffler
function shuffler(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let i_origin = array[i];
let j_origin = array[j];
array[i] = j_origin;
array[j] = i_origin;
}
return array;
}
//guess button
let guess = document.getElementById("guess");
let table = document.getElementsByTagName("table")[0];
guess.addEventListener('click', function () {
judge();
setBgimg();
});
document.addEventListener("keydown", function (event) {
if (event.keyCode == 13) {
judge();
setBgimg();
}
});
//judge function
function judge() {
let guess_input = document.getElementById("guess_input");
//string to array
guess_array = guess_input.value.split("");
let a = 0;
let b = 0;
//判斷輸入長度是否為4
if (answer.length == 0) {
window.alert("請先按【開始】");
}
else if (guess_array.length != 4) {
window.alert("請輸入4個不同的數字!!!\n請輸入4個不同的數字!!!\n請輸入4個不同的數字!!!\n很重要所以説三次!")
guess_input.value = "";
}
else {
//計算A
for (let i = 0; i < 4; i++) {
if (guess_array[i] == answer[i]) {
a++;
}
}
//計算B + A
guess_array.forEach(function (item) {
if (parseInt(item) == NaN) {
window.alert("猜數字!是數字!其他的字我不要!!!謝謝!");
}
else if (answer.includes(parseInt(item))) {
b++;
}
})
//計算B
b = b - a;
//step1:判斷成功或失敗; step2:呈現結果在table中
if (a != 4) {
showResult("bg-danger")
}
else {
showResult("bg-success");
}
//設定 text form
guess_input.value = "";
//加入【再來一次】button
let tr = document.createElement("tr");
let td = document.createElement("td");
td.innerHTML = `<button class="btn btn-danger" id="again" style="margin-left:100px;">恭喜恭喜!再來一次!</button>`
td.setAttribute("style", "height:50px;");
if (a == 4) {
tr.appendChild(td);
table.appendChild(tr);
//再來一次
let again = document.getElementById("again");
again.addEventListener('click', function () {
start.disabled = false;
table.innerHTML = "";
answer = [];
// window.alert("請再按一次【開始】");
resetBgimg()
});
}
//showResult function
function showResult(bg_type) {
let tr = document.createElement("tr");
let td = document.createElement("td");
tr.setAttribute("style", "text-align:center;")
td.innerHTML = `<span class="badge ${bg_type}" style="margin-left:5px; color:#fff;">${a}A${b}B</span> ${guess_input.value}`
td.setAttribute("style", "height:50px;color:#fff;");
tr.appendChild(td);
table.appendChild(tr);
}
}
}
//隨著table調整背景圖高度
function setBgimg() {
let table = document.getElementsByTagName("table")[0];
table_h = table.offsetHeight;
body_h = table_h + 450;
let body = document.getElementsByTagName("body")[0];
body.setAttribute("style", `min-height: ${body_h}px;background-image: url('https://1.bp.blogspot.com/-zjtpfaCfVNw/X-FcyfXrxOI/AAAAAAABdFE/I7TA9lviiugSB-VHTcJtA1I4UMxv-kumwCNcBGAsYHQ/s1500/onepiece19_kurohige_teach.png');background-size: 600px;background-repeat: no-repeat;background-position: bottom;`);
}
//重新設定背景高度
function resetBgimg() {
let body = document.getElementsByTagName("body")[0];
body.setAttribute("style", `min-height: 450px;background-image: url('https://1.bp.blogspot.com/-zjtpfaCfVNw/X-FcyfXrxOI/AAAAAAABdFE/I7TA9lviiugSB-VHTcJtA1I4UMxv-kumwCNcBGAsYHQ/s1500/onepiece19_kurohige_teach.png');background-size: 600px;background-repeat: no-repeat;background-position: bottom;`);
}
//放棄重來
let restart = document.getElementById("restart");
restart.addEventListener("click", function () {
start.disabled = false;
table.innerHTML = "";
answer = [];
// window.alert("請再按一次【開始】");
resetBgimg()
});
//看答案
let peep = document.getElementById("peep");
peep.addEventListener('click', function () {
if (answer.length == 0) {
window.alert("遊戲還沒開始,請按【開始】鍵")
}
else {
window.alert(`答案:${answer.join("")}`);
}
});
</script>
</body>
</html>