<template>
<view class="wrap">
划消:{{ sdNum }} * {{ sdNum }}
<view class="btn" style="padding: 32rpx; background: pink" @click="clickBtn">点击划消按钮</view>
<view class="btn" style="padding: 32rpx; background: pink" @click="startGame"
>点击划消开始游戏按钮</view
>
<view class="btn" style="padding: 32rpx; background: pink" @click="endGame"
>点击划消结束后,计算时间游戏按钮</view
>
<view class="time">{{ startTime }}</view>
<view>99999--随机需要划消的值:{{ randomNum }}</view>
<view>88888--随机需要划消的值一共出现的次数:{{ repeatNum }}</view>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
// import { taskDetail, memberInfo } from '@/api/hmm'
// import store from '@/store'
// import { transformTimeTwo } from '@/utils/hmm'
const sdNum = ref(5)
// const countDownRef = ref(null)
const rightList = ref([])
// 随机值
const randomNum = ref(0)
// 重复的随机值 次数
const repeatNum = ref(0)
// 倒计时时间
const goToState = ref(false)
const timmer = ref(null)
const startTime = ref('03:00')
const stopTime = ref(0)
const newDateTime = ref(0)
onShow(() => {
generateShuDu()
})
// 初始化数独
const generateArr = () => {
const arr = []
for (let i = 0; i < sdNum.value; i++) {
arr[i] = []
for (let j = 0; j < sdNum.value; j++) {
arr[i][j] = Math.floor(Math.random() * 9) + 1
}
}
return arr
}
const generateShuDu = () => {
rightList.value = []
randomNum.value = 0
repeatNum.value = 0
const arr = generateArr()
console.log(arr)
// 随机数去重
const duplicateRemoval = []
for (let i = 0; i < arr.length; i++) {
// console.log(arr[i])
for (let j = 0; j < arr[i].length; j++) {
if (duplicateRemoval.indexOf(arr[i][j]) == -1) {
duplicateRemoval.push(arr[i][j])
}
// console.log(arr[i][j])
}
}
const randomValue = Math.floor(Math.random() * duplicateRemoval.length)
console.log(duplicateRemoval)
console.log(randomValue)
randomNum.value = duplicateRemoval[randomValue]
rightList.value = duplicateRemoval
console.log(222)
console.log(randomNum.value)
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] == randomNum.value) {
repeatNum.value = repeatNum.value + 1
}
}
}
console.log(repeatNum.value)
}
//
// 点击重新开始了数独游戏
const clickBtn = () => {
generateShuDu()
}
// 点击开始计时
const startGame = () => {
if (goToState.value == false) {
goToState.value = true
clearInterval(timmer.value)
timmer.value = null
startTime.value = '03:00'
stopTime.value = new Date().getTime() + 3 * 60 * 1000
// stopTime.value = new Date().getTime() + 5 * 1000
timmer.value = setInterval(() => {
newDateTime.value = stopTime.value - new Date().getTime()
console.log(newDateTime.value)
console.log(22)
if (newDateTime.value <= 0) {
startTime.value = '00:00'
clearInterval(timmer.value)
console.log('游戏超时自动结束')
} else {
startTime.value = transformTime(newDateTime.value)
}
}, 1000)
}
}
// 点击结束 计算分数
const endGame = () => {
if (goToState.value == true) {
goToState.value = false
clearInterval(timmer.value)
timmer.value = null
const residueTime = transformTime(newDateTime.value, 'stop')
console.log(residueTime)
}
}
// 时间转换
const transformTime = (date, state) => {
var datetime = ''
//计算出小时数
var leave1 = date % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
var minutes =
Math.floor(leave2 / (60 * 1000)) < 10
? '0' + Math.floor(leave2 / (60 * 1000))
: Math.floor(leave2 / (60 * 1000))
//计算相差秒数
var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
var seconds =
Math.round(leave3 / 1000) < 10 ? '0' + Math.round(leave3 / 1000) : Math.round(leave3 / 1000)
if (state == 'stop') {
const secondsNum = Number(seconds) + Number(minutes) * 60
return secondsNum
} else {
datetime = minutes + ':' + seconds
return datetime
}
}
</script>
<style lang="scss"></style>