一个简单的数独Demo

举报
拿我格子衫来 发表于 2022/03/18 00:28:03 2022/03/18
【摘要】 闲来无事,想学习点东西,而最近又在玩数独,于是乎准备使用各种技术折腾一个数独的小游戏。 下面是最简单的,数独原型。还在不断完善中。             <!DOCTYPE html><html> <head> ...

闲来无事,想学习点东西,而最近又在玩数独,于是乎准备使用各种技术折腾一个数独的小游戏。

下面是最简单的,数独原型。还在不断完善中。

 

 

 

 

 

 


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>数独</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  8. <style type="text/css">
  9. html,body{height: 100%;user-select: none;}
  10. .container{height: 100%;}
  11. .flex-center{
  12. margin-top: 100px;
  13. height:auto;
  14. display:flex;
  15. justify-content: center;
  16. align-items: center;
  17. }
  18. .sudu-container{
  19. width: 450px;
  20. height: 450px;
  21. border: 1px solid #ddd;
  22. box-sizing: content-box;
  23. border-bottom: none;
  24. }
  25. .sudu-container :nth-child(9n){border-right: 0px;}
  26. .sudu-item{
  27. width: 50px;
  28. height: 50px;
  29. box-sizing: border-box;
  30. border-right: 1px solid #ddd;
  31. border-bottom: 1px solid #ddd;
  32. float: left;
  33. cursor: pointer;
  34. line-height: 50px;
  35. text-align: center;
  36. font-weight: 700;
  37. font-size: 20px;
  38. }
  39. .sudu-item.active {
  40. background-color: #E6A23C;
  41. color: #fff;
  42. }
  43. .wait-number {display: block;width: 100%;margin: 0 auto;}
  44. .wait-number .number-ul{
  45. list-style: none;
  46. text-align: center;
  47. width: auto;
  48. display: flex;
  49. justify-content: center;
  50. padding: 0;
  51. margin-top: 20px;
  52. }
  53. .wait-number .number-ul li{
  54. float: left; width: 50px;
  55. height: 50px;
  56. box-sizing: border-box;
  57. border-top: 1px solid #ddd;
  58. border-right: 1px solid #ddd;
  59. border-bottom: 1px solid #ddd;
  60. float: left;
  61. cursor: pointer;
  62. line-height: 50px;
  63. text-align: center;
  64. font-weight: 700;
  65. font-size: 20px;
  66. }
  67. .wait-number .number-ul :first-child{
  68. border-left: 1px solid #ddd;
  69. }
  70. .wait-number .number-ul li:hover{
  71. background-color: #909399;
  72. color: #fff;
  73. }
  74. .wait-number .number-ul li.active{
  75. background-color: #E6A23C;
  76. color: #fff;
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <div class="container">
  82. <div class="flex-center">
  83. <div class="sudu-container">
  84. <div class="sudu-items-container" id="J_sudu_items">
  85. <!-- <div class="sudu-item" data-index="0"></div> -->
  86. </div>
  87. </div>
  88. </div>
  89. <div class="wait-number">
  90. <ul class="number-ul" id="J_number_ul">
  91. <li>1</li>
  92. <li>2</li>
  93. <li>3</li>
  94. <li>4</li>
  95. <li>5</li>
  96. <li>6</li>
  97. <li>7</li>
  98. <li>8</li>
  99. <li>9</li>
  100. </ul>
  101. </div>
  102. </div>
  103. <script src="./index.js"></script>
  104. <script>
  105. // 产出一个正确的数独数组
  106. function createShuDuArr () {
  107. let arr = [], finArr = [];
  108. for(let i = 1; i < 10; i ++) {
  109. arr.push(i)
  110. }
  111. finArr = new Array(9).fill(arr)
  112. return finArr.flat()
  113. }
  114. let currentIndex = null
  115. let correctArr = createShuDuArr()
  116. console.log(correctArr)
  117. let suduDataArr = new Array(81).fill(null)
  118. let suduContainerEl = document.querySelector("#J_sudu_items")
  119. let suduItem = [] // new Array(81).fill('<div class="sudu-item"></div>')
  120. for(let i = 0; i < 81; i++) {
  121. suduItem.push(`<div class="sudu-item" data-index="${i}"></div>`)
  122. }
  123. suduContainerEl.innerHTML = suduItem.join('\n')
  124. suduDataArr[1] = 1
  125. suduDataArr[20] = 5
  126. suduDataArr[30] = 3
  127. suduDataArr.forEach((outerItem, outerIndex) => {
  128. if (outerItem) {
  129. suduContainerEl.querySelector(`.sudu-item:nth-child(${outerIndex+1})`).innerHTML = outerItem
  130. }
  131. })
  132. // 初始化数据
  133. function initData () {
  134. }
  135. // 渲染数据到view
  136. function renderDataOnView () {
  137. }
  138. // 点击底部数字
  139. function clickNumberItem (even) {
  140. let targetEl = even.target
  141. suduContainerEl.querySelector(`.sudu-item:nth-child(${ currentIndex + 1})`).innerHTML = targetEl.innerHTML
  142. if(targetEl.innerHTML !== correctArr[currentIndex]) {
  143. console.log('错误')
  144. }
  145. // 验证 当前currentIndex 是否是正确的数字
  146. // 需要验证suduDataArr是否是数独
  147. }
  148. // 点击数独区域
  149. function clickSuduNumberItem (even) {
  150. let targetEl = even.target
  151. if(!targetEl.classList.contains('active')) {
  152. let activeItem = document.querySelector("#J_sudu_items .active")
  153. if(activeItem && activeItem.classList.contains('active')) {
  154. activeItem.classList.remove('active')
  155. }
  156. targetEl.classList.add('active')
  157. }
  158. currentIndex = parseInt(targetEl.dataset.index, 10)
  159. }
  160. document.querySelector("#J_number_ul").addEventListener('click', clickNumberItem, false)
  161. document.querySelector("#J_sudu_items").addEventListener('click', clickSuduNumberItem, false)
  162. </script>
  163. </body>
  164. </html>

 

复习了以下知识,

classList的remove, add,contains

css中 flex, box-sizing , :nth-clild, :nth-clild(9n)

new Array(10).fill('<div>123</div>')

addEventListener(evenName, func, Bloean)

 

文章来源: fizzz.blog.csdn.net,作者:拿我格子衫来,版权归原作者所有,如需转载,请联系作者。

原文链接:fizzz.blog.csdn.net/article/details/104230102

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。