首页 > 其他分享 >uniapp js 数独小游戏 9*9 数独 2.0

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

时间:2024-08-28 14:15:49浏览次数:4  
标签:uniapp arr return ++ 小游戏 let 数独 Math

效果图:

 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>

标签:uniapp,arr,return,++,小游戏,let,数独,Math
From: https://www.cnblogs.com/dreammiao/p/18384547

相关文章

  • uniapp js 数独小游戏 n*n 看控制台的打印 数独 1.0
    uniappjs 数独小游戏n*n 看控制台的打印game.vue<template> <view>4567</view></template><scriptsetuplang="ts">import{ref}from'vue'import{onShow}from'@dcloudio/uni-app'constsdNum=ref(......
  • C语言实现三子棋小游戏
    前言与概述本文章讲述如何通过C语言开发一款三子棋的小游戏。笔者才识浅薄,如有错误,欢迎各位编程大佬在评论区批评指正,笔者不胜感激。游戏介绍三子棋是一款益智的趣味小游戏。多名玩家在3*3的棋盘下棋,棋盘共九个方格,每个方格最多只能放置一枚棋子。只要有一名玩家下的三个棋......
  • 移动端+PC端源码,智慧城管执法系统,后端框架:springboot,移动端:uniapp
    市管理综合执法管理平台实现执法办案、业务全流程在线办理,依托移动端+PC端的“两端”应用模式,保障能够通过信息化手段进行日常的执法办案工作,强化执法监督功能。提供了案件在线办理、当事人信用管理、文书电子送达、沿街店铺分析等功能,全面赋能执法队员,提高执法队员办案效率。......
  • uniapp 在线预览pdf 或者 文件
    页面代码:<template><view><web-view:src="fileUrl"></web-view></view></template><script>exportdefault{data(){return{fileUrl:"&quo......
  • uniapp微信小程序获取小程序新版本
    functioncheckUpdate(){//使用该接口,可以获知是否有新版本小程序、新版本是否下载好以及应用新版本的能力。constupdateManager=uni.getUpdateManager()updateManager.onCheckForUpdate(function(res){//请求完新版本信息的回调console.log(res.hasUp......
  • vue3uniapps使用富文本mp-html插件
    1.实现效果具体需求:顶部是搜索栏,包括搜索结果个数,目前跳到第几个,包含上一个、下一个按钮。富文本区域关键词高亮黄色,当前关键词为高亮橙色。如图2.版本号用到vue3和uniapp,mp-html插件版本是v2.5.0,插件地址:https://ext.dcloud.net.cn/plugin?id=805用npm方式打包放到......
  • 使用 UniApp 实现摄像头视频流的接入并在页面上显示视频流
    UniApp是一个使用Vue.js开发所有前端应用的框架,它支持一次开发,多端部署(包括H5、小程序和APP)。下面我将展示如何使用UniApp实现摄像头视频流的接入,并在页面上显示视频流。我还会提供一些使用场景以及代码优化建议。使用场景直播应用:用户可以实时分享自己的画面。在线......
  • 手把手带你用C语言实现控制台小游戏扫雷(附源码)
    文章目录一、扫雷游戏整体设计思路1.扫雷游戏功能说明2.游戏的分析和设计3.文件结构设计:二、主函数大致模型三、创建棋盘四、初始化棋盘五、打印棋盘六、布置雷七、排查雷八、源码九、如何把游戏分享给小伙伴十、扫雷进阶的一些思路一、扫雷游戏整体设计思路1.扫雷......
  • Java拼图小游戏
    登录界面注册界面游戏页面上代码App.javaimportcom.ui.GameJFrame;importcom.ui.LoginJFrame;importcom.ui.RegisterJFrame;publicclassApp{publicstaticvoidmain(String[]args){newLoginJFrame();//newGameJFrame();//......
  • C++实现的数独游戏
    数独游戏是什么数独(Sudoku)是一种基于数字的逻辑推理游戏,起源于18世纪的瑞士数学家莱昂哈德·欧拉(LeonhardEuler)的拉丁方阵,但现代数独的规则由美国架桥杂志在20世纪后半叶所推广,随后在日本得到了广泛流行,并被命名为“数独”(意为“数字独立”)。如今,数独已经成为一种在世界各......