-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path088.乌龟对对碰.html
309 lines (266 loc) · 8.96 KB
/
088.乌龟对对碰.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/global.css">
<style>
.turtle-blind-box {
position: relative;
display: flex;
width: 400px;
height: 400px;
}
.turtle-grid {
position: absolute;
width: 100%;
height: 100%;
display: grid;
border-left: 2px solid #666;
border-top: 2px solid #666;
grid-template-columns: repeat(3, calc(100% / 3));
grid-template-rows: repeat(3, calc(100% / 3));
}
.turtle-grid-item {
border-right: 2px solid #666;
border-bottom: 2px solid #666;
counter-increment: item-counter;
position: relative;
}
.turtle-grid-item img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.turtle-grid-item::after {
position: absolute;
top: 0;
left: 0;
z-index: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
color: #000;
font-size: 40px;
content: counter(item-counter);
}
</style>
</head>
<body>
<!--
规则
共有10种颜色的乌龟,出现概率均等(各占总数的1/10),每次完全随机抽取,“买家”可以“许愿”一种幸运颜色,初始指定乌龟总数,依次放入1-9号格,放满后开始结算。
触发幸运色:每拆出1只幸运色乌龟,立刻加赠1只;
对对碰:2个同色格,一起拿走,加赠1只; ✔
三连位:同一直线上(横、竖、对角线)3个同色格,一起拿走,加赠5只; ✔
全不同:9个格子颜色全不同,一起拿走,加赠10只。
清台: +5
-->
<div class="turtle-blind-box">
<div class="turtle-grid">
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
<div class="turtle-grid-item"></div>
</div>
<!-- 开始面板 -->
<div class="start-panel">
<!-- 选择颜色 -->
<!-- 许愿色 -->
<!-- 选择数量 -->
<!-- 10包 30包 50包 -->
<!-- 开始 -->
</div>
</div>
<script type="module">
import { Randoms } from "https://gcore.jsdelivr.net/npm/@3r/tool/lib/randoms.js";
/**
* 初始化平面数组
* @author linyisonger
* @date 2025-01-13
*
* @template T
* @param {number} rows
* @param {number} cols
* @param {T|(x,y)=>T} init
*
* @return {T[][]}
*/
function createFlatArray(rows, cols, init = 0) {
let level1 = []
for (let y = 0; y < cols; y++) {
let level2 = [];
for (let x = 0; x < rows; x++) {
level2[x] = typeof init == "function" ? init(x, y) : init;
}
level1[y] = level2
}
return level1
}
// 红色、黄色、紫色、橙色、绿色、青色、粉色、蓝色、咖色和玫红色
const TURTLE_COLOR = {
NONE: '',
RED: 'red',
YELLOW: 'yellow',
PURPLE: 'purple',
ORANGE: 'orange',
GREEN: 'green',
CYAN: 'cyan',
PINK: 'pink',
BLUE: 'blue',
BROWN: 'brown',
ROSE: "rose"
}
class Turtle {
x = 0;
y = 0;
color = TURTLE_COLOR.NONE;
constructor(x, y) {
this.x = x;
this.y = y;
}
}
const assetConfig = {
'red': './assets/turtle/red.png',
'yellow': './assets/turtle/yellow.png',
'purple': './assets/turtle/purple.png',
'orange': './assets/turtle/orange.png',
'green': './assets/turtle/green.png',
'cyan': './assets/turtle/cyan.png',
'pink': './assets/turtle/pink.png',
'blue': './assets/turtle/blue.png',
'brown': './assets/turtle/brown.png',
"rose": './assets/turtle/rose.png',
}
let map = createFlatArray(3, 3, (x, y) => new Turtle(x, y)) // 地图
let colors = Object.keys(assetConfig) // 颜色
let turtles = map.flat(2) // 所有乌龟
let turtlesDom = document.querySelectorAll('.turtle-grid-item') // 九宫格
let luckyColor = colors[1]; // 幸运色
let buyCount = 20; // 购买数量
let unpackCount = 0; // 拆开数量
/**
* 相同的
* @author linyisonger
* @date 2025-01-16
*/
function same(self, target) {
return map[self.y][self.x].color == map[target.y][target.x].color
}
/**
* 二连判断
* @author linyisonger
* @date 2025-01-16
*/
function isTwin() {
let sameList = []
// 横向
for (let x = 0; x < 2; x++) {
for (let y = 0; y < 3; y++) {
let sameArray = isSameMatch({ x, y }, { x: 1, y: 0 }, 1)
sameArray.length && sameList.push(sameArray)
}
}
// 纵向
for (let y = 0; y < 2; y++) {
for (let x = 0; x < 3; x++) {
let sameArray = isSameMatch({ x, y }, { x: 0, y: 1 }, 1)
sameArray.length && sameList.push(sameArray)
}
}
return sameList;
}
/**
* 三连判断
* @author linyisonger
* @date 2025-01-16
*/
function isTriplet() {
let sameList = []
// 横向
for (let i = 0; i < 3; i++) {
let sameArray = isSameMatch({ x: 0, y: i }, { x: 1, y: 0 })
sameArray.length && sameList.push(sameArray)
}
// 纵向
for (let i = 0; i < 3; i++) {
let sameArray = isSameMatch({ x: i, y: 0 }, { x: 0, y: 1 })
sameArray.length && sameList.push(sameArray)
}
// 对角线 - 左上 右下
{
let sameArray = isSameMatch({ x: 0, y: 0 }, { x: 1, y: 1 })
sameArray.length && sameList.push(sameArray)
}
// 对角线 - 右上 左下
{
let sameArray = isSameMatch({ x: 2, y: 0 }, { x: -1, y: 1 })
sameArray.length && sameList.push(sameArray)
}
return sameList;
}
/**
* 是否相同位置匹配
* @author linyisonger
* @date 2025-01-16
*/
function isSameMatch(start, direction, count = 2) {
let sameArray = [start]
for (let i = 0; i < count; i++) {
let last = sameArray[sameArray.length - 1]
let target = { x: last.x + direction.x, y: last.y + direction.y }
if (!same(last, target)) return []
sameArray.push(target)
}
return sameArray;
}
/**
* 拆盲盒
* @author linyisonger
* @date 2025-01-16
*/
function unpack() {
// 颜色
let color = colors[Randoms.int(0, colors.length)]
// 幸运色
// 第一
let turtleIndex = turtles.findIndex(t => t.color == TURTLE_COLOR.NONE);
// 结束
if (buyCount == unpackCount) {
}
// 摆放
else if (turtleIndex > -1) {
turtles[turtleIndex].color = color;
// 新建IMG
let imgDom = document.createElement('img')
imgDom.src = assetConfig[color]
turtlesDom.item(turtleIndex).appendChild(imgDom)
unpackCount++;
unpack();
}
// 结算
else {
// 九不同
// 三连
isTriplet();
// 二连
isTwin();
// 清台
}
}
unpack()
console.log(isTwin());
</script>
</body>
</html>