<script lang="ts">
type CardItem = { icon: string; isDel: boolean; isSelect: boolean, index: number }
const icons = ['a', 'b', 'c', "d"]
const total = 20
let list: CardItem[] = []
let isLock = false
function handlerTap(card: CardItem) {
if (isLock) return
list[card.index].isSelect = true
const selectors = list.filter(item => item.isSelect && !item.isDel)
if (selectors.length <= 1) return
isLock = true
const [item1, item2] = selectors
setTimeout(() => {
if (item1.icon === item2.icon) {
list[item1.index].isDel = true
list[item2.index].isDel = true
}
list = list.map(item => ({...item, isSelect: false}))
isLock = false
if (list.every(item => item.isDel)) console.log( "your win!")
}, 800)
}
function createW(icons: string[], total: number) {
const times = Math.floor(total / (icons.length * 2))
const lastCount = total % (icons.length * 2)
const W = []
for (let index = 0; index < times; index++)
W.push(...icons, ...icons)
const lastList = createRandomList(icons, lastCount / 2)
W.push(...lastList, ...lastList)
return W
}
function createRandomList(W: string[], total: number, fn: (<T>(icon: string, index?: number) => T) = icon => icon) {
const list: any[] = []
function it(time: number): any {
if (time === 0) return list
const randomNum = Math.floor(Math.random() * (W.length))
list.push(fn(W[randomNum], total-time))
W.splice(randomNum, 1)
return it(--time)
}
return it(total)
}
list = createRandomList(createW(icons, total),
total,
(icon: string, index) => ({ icon, isDel: false, isSelect: false, index }))
</script>
<div class="game-box">
{#each list as item}
<div class="grid">
{#if !item.isDel}
<div class="card {item.isSelect && 'select'}" on:click="{() => handlerTap(item)}">
<div class="top">{item.icon}</div>
</div>
{/if}
</div>
{/each}
</div>
<style lang="less">
.game-box{
margin: 10px auto 0;
width: 90vw;
height: 80vh;
display: grid;
grid-template-columns: repeat(4, calc(100% / 4 - 3px));
grid-template-rows: repeat(5, calc(100% / 5 - 3px));
grid-row-gap:3px;
grid-column-gap: 3px;
.card{
height: 100%;
width: 100%;
box-sizing: border-box;
position: relative;
transform-style: preserve-3d;
transform: rotateY(180deg);
transition: all 600ms;
background: pink;
&.select {
transform: rotateY(0);
}
.top{
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
background: white;
border: 2px solid #b6a6dc;
transform: translateZ(1px);
}
}
}
</style>
|