uniapp js 数独小游戏 9*9 数独 2.0

news/2024/9/22 4:26:26/文章来源:https://www.cnblogs.com/dreammiao/p/18384547

效果图:

 game.vue

<template>
<view>
<view class="main">
<view class="foot">
<view v-if="!isTip" class="sudoku_area">
<view v-for="(row, index) of rowList" :key="index" class="sudoku_row"
:style="{borderRight:(index==2 || index==5)?'2px solid #ae7a36':index==8?'none':'1px solid #dab88a'}">
<view v-for="(value, cellIndex) of row" :key="cellIndex" class="sudoku_cell"
:style="{borderBottom:(cellIndex==2 || cellIndex==5)?'2px solid #ae7a36':cellIndex==8?'none':'1px solid #dab88a'}">
<input v-if="isOkNew(index, cellIndex)" :value="value" style="background: #fffae1;"
:disabled="isOkNew(index, cellIndex)" />
<input v-else maxlength="1" type="number" v-model="inputValue"
style="background: #f8e7a4; font-weight: bold;"
@input="changeValue(index, cellIndex)" />
</view>
</view>
</view>
<view v-else class="sudoku_area">
<view v-for="(row, index) of rightList" :key="index" class="sudoku_row">
<view v-for="(value, cellIndex) of row" :key="cellIndex" class="sudoku_cell">
<input style="height:5vh;color: black;" :value="value"
:disabled="isOkNew(index, cellIndex)" />
</view>
</view>
</view>
</view>

<view style="margin-top: 64rpx; display: flex;">
<button class="button-class" @click="goRestart()">
重开
</button>
<button class="button-class" @click="showTip()">
答案<span v-if="isTip">{{ count }}s</span>
</button>
<button class="button-class" @click="checkShudu()">
提交
</button>
</view>
</view>
</view>
</template>

<script>
export default {
data() {
return {
rowList: [], // 数独二维数组
rowListNew: [], // 题目数组
rightList: [], // 正确的数独数组
inputValue: '', // input数值
isTip: false, // 是否展示提示
count: 5, // 倒计时
myTimer: ''
}
},
onLoad() {
this.generateShuDu()
},
methods: {
// 初始化数独
generateArr() {
const arr = []
for (let i = 0; i < 9; i++) {
arr[i] = []
for (let j = 0; j < 9; j++) {
arr[i][j] = 0
}
}
console.log(arr);
return arr
},
// 生成1-9的随机整数
generateRandom() {
return Math.floor(Math.random() * 9 + 1)
},

//判断重开
goRestart() {
uni.showModal({
title: '提示',
content: '重开后将清空已填的数,并且不会保存',
showCancel: true,
cancelText: '继续',
confirmText: '重开',
success: res => {
if (res.confirm) {
this.generateShuDu()
} else if (res.cancel) {
return
}
},
fail: () => {},
complete: () => {}
});
},

// 生成数独
generateShuDu() {
const arr = this.generateArr()
//生成数独
for (let i = 0; i < 9; i++) {
let time = 0
for (let j = 0; j < 9; j++) {
arr[i][j] = time === 9 ? 0 : this.generateRandom()
if (arr[i][j] === 0) { // 不是第一列,则倒退一列
if (j > 0) {
j -= 2
continue
} else { // 是第一列,则倒退到上一行的最后一列
i--
j = 8
continue
}
}
if (this.isCorret(arr, i, j)) {
time = 0 // 初始化time,为下一次填充做准备
} else {
time++ // 次数增加1
j-- // 继续填充当前格
}
}
}
this.rightList = JSON.parse(JSON.stringify(arr))

// 随机删除部分数独内容
this.delSomeList(arr)
},
// 是否满足行、列和3X3区域不重复的要求
isCorret(arr, row, col) {
return (this.checkRow(arr, row) && this.checkLine(arr, col) && this.checkNine(arr, row, col))
},
// 检测行是否符合标准
checkRow(arr, row) {
for (let j = 0; j < 8; j++) {
if (arr[row][j] === 0) {
continue
}
for (let k = j + 1; k < 9; k++) {
if (arr[row][j] === arr[row][k]) {
return false
}
}
}
return true
},
// 检测列是否符合标准
checkLine(arr, col) {
for (let j = 0; j < 8; j++) {
if (arr[j][col] === 0) {
continue
}
for (let k = j + 1; k < 9; k++) {
if (arr[j][col] === arr[k][col]) {
return false
}
}
}
return true
},
// 检测3X3是否符合标准
checkNine(arr, row, col) {
// 获得左上角的坐标
const j = Math.floor(row / 3) * 3
const k = Math.floor(col / 3) * 3
// 循环比较
for (let i = 0; i < 8; i++) {
if (arr[j + Math.floor(i / 3)][k + i % 3] === 0) {
continue
}
for (let m = i + 1; m < 9; m++) {
if (arr[j + Math.floor(i / 3)][k + Math.round(i % 3)] === arr[j + Math.floor(m / 3)][k + Math
.round(m % 3)
]) {
return false
}
}
}
return true
},

// 随机删除部分数独内容
delSomeList(arr) {
const randomNum = Math.floor(Math.random() * 30) + 50
for (let a = 0; a < randomNum; a++) {
const i = Math.floor(Math.random() * 9)
const j = Math.floor(Math.random() * 9)
arr[i][j] = ''
}
this.rowList = JSON.parse(JSON.stringify(arr))
this.rowListNew = JSON.parse(JSON.stringify(arr))
},

// 是否为题目数字
isOkNew(index, cellIndex) {
if (this.rowListNew[index][cellIndex] === '') {
return false
}
return true
},
// 填写数独
changeValue(index, cellIndex) {
this.inputValue === '' ? this.rowList[index][cellIndex] = '' : this.rowList[index][cellIndex] =
parseInt(this.inputValue)
this.inputValue = ''
},

// 提交数独
checkShudu() {
const answer = this.rowList
for (let i = 0; i < answer.length; i++) {
for (let j = 0; j < answer[i].length; j++) {
if (answer[i][j] === '') {
uni.showToast({
title: '数独未填完',
icon: 'none'
});
return
}
}
}
if (answer.toString() === this.rightList.toString()) {
uni.showToast({
title: '答案正确',
icon: 'none'
});
} else {
uni.showToast({
title: '答案错误',
icon: 'none'
});
}
},
// 提示
showTip() {
this.isTip = true
this.countDown()
},
// 倒计时
countDown() {
// 有定时器 -> 清除定时器(避免重复设置)
if (this.myTimer) {
clearInterval(this.myTimer)
}
// 设置定时器
this.myTimer = setInterval(() => {
if (this.count > 0) {
this.count--
if (this.count === 0) {
// 倒计时到0时发送请求 并清除定时器
this.isTip = false
this.count = 5
clearInterval(this.myTimer)
}
}
}, 1000)
}

}
}
</script>

<style lang="scss">
.main {
display: flex;
flex-direction: column;
}

.sudoku_area {
border: 2px solid #ae7a36;
display: flex;
justify-content: center;
border-radius: 16rpx;
overflow: hidden;
}

.button-class {
width: 184rpx;
height: 88rpx;
background-color: #fffae1;
font-size: 32rpx;
line-height: 88rpx;
color: #6c4e27;
border-radius: 24rpx;
}

.sudoku_cell {
width: 76rpx;
box-sizing: border-box;
text-align: center;
line-height: 76rpx;
color: #6c4e27;
}

.sudoku_cell input {
width: 76rpx;
height: 76rpx;
}

.foot {
display: flex;
justify-content: center;
cursor: pointer;
margin-top: 32rpx;
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/788533.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024-08-28:用go语言,给定一个从1开始、长度为n的整数数组nums,定义一个函数greaterCount(arr, val)可以返回数组arr中大于val的元素数量。 按照以下规则进行n次

2024-08-28:用go语言,给定一个从1开始、长度为n的整数数组nums,定义一个函数greaterCount(arr, val)可以返回数组arr中大于val的元素数量。 按照以下规则进行n次操作,将nums中的元素分配到两个数组arr1和arr2中: 1.第一次操作将nums[1]加入arr1。 2.第二次操作将nums[2]加…

WPF 如何利用Blend给Button添加波纹效果

先看一下效果吧:如果不会写动画或者懒得写动画,就直接交给Blend来做吧; 其实Blend操作起来很简单,有点类似于在操作PS,我们只需要设置关键帧,鼠标点来点去就可以了,Blend会自动帮我们生成我们想要的动画效果. 第一步:要创建一个空的WPF项目 第二步:右键我们的项目,在最…

绘制ply模型顶点的法线(通过两点)

import trimesh import matplotlib.pyplot as plt# 读取点云文件 mesh = trimesh.load_mesh(test.ply)# 计算法线 mesh.vertex_normals# 创建一个新的图形窗口 fig = plt.figure() ax = fig.add_subplot(111, projection=3d)# 绘制顶点 ax.scatter(mesh.vertices[:, 0], mesh.v…

HandyControl 初识 第一次正确引入

在 App.xaml配置HandyControl,一个是皮肤资源,一个是主题资源 完整代码: ` <Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="pack://application:,,,/HandyControl;component/Th…

信息熵计算程序[Python+CSV格式数据集]

0 前言为了便于学习决策树信息熵相关知识,笔者编写了一个专门用于计算变量信息熵、条件熵、信息增益、信息增益比的程序,方便提升学习效率。 程序中包含了计算过程的数据和详细信息以及最终计算结果。 编程语言为Python,搭配CSV数据格式使用。1 数据集 1.1 游玩数据集 根据天…

【整理】 【Windows系列】Windows安全日志分析实战:关键事件+详解

参考🔗: https://mp.weixin.qq.com/s?__biz=MzI5MjY4MTMyMQ==&mid=2247485189&idx=1&sn=f97aca178ab188d35e3182bf89ddf4dc&chksm=ec7ce403db0b6d151ee60369468e79229d8d3a264edb2967d7bd2ecdcd69e93af2abfb68dd1c&cur_album_id=3541179802739621890&a…

【ROS教程】ROS文件系统和基础架构

@目录1.工作空间目录1.1 package.xml2.启动节点的方式2.1 一次启动一个2.2 一次启动多个3.ROS常用命令3.1 增3.2 查3.3 执行3.3.1 加载环境变量3.3.2 运行节点3.4 查看计算图4.创建功能包4.1 选择工作目录4.2 创建功能包目录4.3 建立功能包1.工作空间目录WorkSpace --- 自定义的…

沉浸式体验吸尘器产品的3D可视化盛宴

在这个科技日新月异的时代,每一个细微之处都蕴含着创新的火花,而家居清洁作为我们日常生活中不可或缺的一环,也正在经历一场前所未有的变革。大家可以想象一下,无需亲临实体店,只需轻点鼠标或滑动屏幕,一款款精心设计的吸尘器便以3D形态跃然眼前,仿佛触手可及。这不仅仅…

Valid注解

文章链接地址:https://blog.csdn.net/m0_58680865/article/details/127817779文章目录前言 一、@Valid注解1、源码解析 2、所属的包 3、参数校验使用注解(1)空校验 (2)Boolean校验 (3)长度校验 (4)日期校验 (5)数值校验 (6)其他校验4、具体使用使用 @Valid 进行参…

【ACMMM2024】Multi-Scale and Detail-Enhanced Segment Anything Model for Salient Object Detection

论文:https://arxiv.org/pdf/2408.04326 代码:https://github.com/BellyBeauty/MDSAM论文的研究动机就是使用SAM来解决显著性检测(SOD)问题,主要有两个改进:提出了Lightweight Multi-Scale Adapter, LMSA来微调SAM 提出了Multi-Level Fusion Module, MLFM 和 Detail Enha…

Amazon Bedrock 实践:零基础创建贪吃蛇游戏

本文探讨了如何利用 Amazon Bedrock 和大型语言模型,快速创建经典的贪吃蛇游戏原型代码。重点展示了利用提示工程,将创新想法高效转化为可运行代码方面的过程。文章还介绍了评估和优化提示词质量的最佳实践。亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有…

题解:P11000 [蓝桥杯 2024 省 Python B] 数字串个数

P1100,纪念这个特别的数字,来水一篇。用 \(1 \sim 9\) 没有任何特殊情况的方法数:\(9^{10000}\)。 排除没有 \(3\) 和 \(7\) 的方法。 \(9^{10000} - 8^{10000} - 8^{10000}\) 加上 \(3\) 和 \(7\) 混一起的方法数。 \(9^{10000} - {(9 - 1)}^{10000} - {(9 - 1)}^{10000} +…