{"id":304845,"student_id":3469,"content":"{\"html\":\"\u003c!DOCTYPE html\u003e\\n\u003chtml lang=\\\"en\\\"\u003e\\n\\n\u003chead\u003e\\n \u003cmeta charset=\\\"UTF-8\\\"\u003e\\n \u003cmeta name=\\\"viewport\\\" content=\\\"width=device-width, initial-scale=1.0\\\"\u003e\\n \u003cmeta http-equiv=\\\"X-UA-Compatible\\\" content=\\\"ie=edge\\\"\u003e\\n \u003ctitle\u003eSudoku Solver\u003c/title\u003e\\n \u003clink rel=\\\"stylesheet\\\" href=\\\"index.css\\\"\u003e\\n \u003cscript src=\\\"https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js\\\"\u003e\u003c/script\u003e\\n\u003c/head\u003e\\n\\n\u003cbody\u003e\\n \u003cdiv id=\\\"sudoku\\\"\u003e\\n \u003ch1\u003eSudoku\u003c/h1\u003e\\n \u003cdiv class=\\\"game\\\"\u003e\\n \u003cdiv class=\\\"grid\\\"\u003e\\n \u003cdiv v-for=\\\"row, y in grid\\\" class=\\\"row\\\"\u003e\\n \u003cdiv v-for=\\\"col, x in row\\\" class=\\\"col\\\"\u003e\\n \u003cinput\\n v-model.number=\\\"grid[y][x]\\\"\\n @focus=\\\"onfocus(y, x)\\\"\\n @focusout=\\\"onfocusout(y, x)\\\"\\n :disabled=\\\"originGrid[y][x] != 0\\\"\\n :style=\\\"textColor(y, x)\\\"\\n \u003e\\n \u003c/div\u003e\\n \u003c/div\u003e\\n \u003c/div\u003e\\n \u003cdiv class=\\\"vertical\\\"\u003e\\n \u003cdiv v-for=\\\"i in 9\\\"\u003e\\n \u003cdiv v-show=\\\"v_check(i - 1)\\\" class=\\\"active\\\"\u003e\u003c/div\u003e\\n \u003c/div\u003e\\n \u003c/div\u003e\\n \u003cdiv class=\\\"horizontal\\\"\u003e\\n \u003cdiv v-for=\\\"i in 9\\\"\u003e\\n \u003cdiv v-show=\\\"h_check(i - 1)\\\" class=\\\"active\\\"\u003e\u003c/div\u003e\\n \u003c/div\u003e\\n \u003c/div\u003e\\n \u003cdiv class=\\\"blocks\\\"\u003e\\n \u003cdiv v-for=\\\"i in 9\\\"\u003e\\n \u003cdiv v-show=\\\"b_check(i - 1)\\\" class=\\\"active\\\"\u003e\u003c/div\u003e\\n \u003c/div\u003e\\n \u003c/div\u003e\\n \u003c/div\u003e\\n \u003cp v-if=\\\"isWin\\\" class=\\\"status\\\"\u003e恭喜你,獲得勝利了!\u003c/p\u003e\\n \u003cbutton @click=\\\"newGame\\\"\u003e新遊戲\u003c/button\u003e\\n \u003cbutton @click=\\\"resolve\\\"\u003e破解\u003c/button\u003e\\n \u003c/div\u003e\\n\u003c/body\u003e\\n\u003c/html\u003e\",\"css\":\"body {\\n display: flex;\\n justify-content: center;\\n align-items: center;\\n background-color: lightyellow;\\n}\\n\\n#sudoku {\\n text-align: center;\\n}\\n\\nh1 {\\n font-size: 1.5rem;\\n}\\n\\nbutton {\\n margin: 10px 5px;\\n}\\n\\n.game {\\n width: 450px;\\n height: 450px;\\n border: 1px solid black;\\n position: relative;\\n}\\n\\n.row {\\n width: 100%;\\n height: calc(100%/9);\\n}\\n\\n.col {\\n position: relative;\\n display: inline-block;\\n width: calc(100%/9);\\n height: 100%;\\n margin: 0;\\n border: 1px solid #00000050;\\n box-sizing: border-box;\\n}\\n\\ninput {\\n position: absolute;\\n top: 0;\\n left: 0;\\n width: calc(100% - 2px);\\n height: calc(100% - 2px);\\n margin: 0;\\n text-align: center;\\n border-width: 0px;\\n box-sizing: border-box;\\n font-size: 1.2rem;\\n outline: none;\\n color: black;\\n background-color: rgba(0, 0, 0, 0);\\n}\\n\\ninput:disabled {\\n font-weight: 900;\\n font-size: 1.1rem;\\n background-color: rgba(0, 0, 0, 0);\\n cursor: not-allowed;\\n}\\n\\ninput:focus {\\n color: black !important;\\n}\\n\\n.grid {\\n width: 100%;\\n height: 100%;\\n position: absolute;\\n top: 0;\\n left: 0;\\n}\\n\\n.vertical,\\n.horizontal,\\n.blocks {\\n position: absolute;\\n top: 0;\\n left: 0;\\n right: 0;\\n bottom: 0;\\n display: flex;\\n flex-wrap: wrap;\\n pointer-events: none;\\n}\\n\\n.vertical \u003e div {\\n width: calc(100%/9);\\n height: 100%;\\n position: relative;\\n}\\n\\n.horizontal \u003e div {\\n width: 100%;\\n height: calc(100%/9);\\n position: relative;\\n}\\n\\n.blocks \u003e div {\\n width: 150px;\\n height: 150px;\\n border: 1px solid black;\\n box-sizing: border-box;\\n position: relative;\\n}\\n\\n.vertical .active {\\n z-index: -1;\\n position: absolute;\\n top: -12px;\\n left: 12px;\\n right: 12px;\\n bottom: -12px;\\n background-color: #ff595960;\\n}\\n\\n.horizontal .active {\\n z-index: -1;\\n position: absolute;\\n top: 12px;\\n left: -12px;\\n right: -12px;\\n bottom: 12px;\\n background-color: #ffa40060;\\n}\\n\\n.blocks .active {\\n z-index: -1;\\n position: absolute;\\n top: 6px;\\n left: 6px;\\n right: 6px;\\n bottom: 6px;\\n background-color: #00c4b360;\\n}\\n\",\"js\":\"// 史上最難的數獨題目\\nvar maze = [\\n [8, 0, 0, 0, 0, 0, 0, 0, 0],\\n [0, 0, 3, 6, 0, 0, 0, 0, 0],\\n [0, 7, 0, 0, 9, 0, 2, 0, 0],\\n [0, 5, 0, 0, 0, 7, 0, 0, 0],\\n [0, 0, 0, 0, 4, 5, 7, 0, 0],\\n [0, 0, 0, 1, 0, 0, 0, 3, 0],\\n [0, 0, 1, 0, 0, 0, 0, 6, 8],\\n [0, 0, 8, 5, 0, 0, 0, 1, 0],\\n [0, 9, 0, 0, 0, 0, 4, 0, 0],\\n];\\n\\nvar grid = new Sudoku(maze); //初始化\\n\\n\\n//-------------------- 破解題目的演算法 --------------------//\\n\\n//「破解」按鈕按下時執行此函式\\nfunction autoplay () {\\n count = 0;\\n resolve();\\n console.log('遞迴嘗試了 ' + count + ' 次!');\\n}\\n\\nfunction resolve () {\\n\\n //限制遞迴次數避免網頁癱瘓\\n if (count \u003e= 300000) return;\\n count++;\\n\\n for (var y = 0; y \u003c 9; y++) {\\n for (var x = 0; x \u003c 9; x++) {\\n if (grid[y][x] === 0) {\\n var arr = [];\\n\\n for (var a = 0; a \u003c 9; a++) {\\n arr.push(grid[y][a]);\\n }\\n\\n for (var b = 0; b \u003c 9; b++) {\\n arr.push(grid[b][x]);\\n }\\n\\n var offsetX = x - x % 3;\\n var offsetY = y - y % 3;\\n\\n for (var l = 0; l \u003c 3; l++) {\\n for (var h = 0; h \u003c 3; h++) {\\n arr.push(grid[offsetY + l][offsetX + h]);\\n }\\n }\\n for (var k = 0; k \u003c= 9; k++) {\\n if (arr.indexOf(k) == -1) {\\n grid[y][x] = k;\\n var result = resolve();\\n if (result) {\\n return true;\\n } else {\\n grid[y][x] = 0;\\n }\\n }\\n }\\n return false;\\n }\\n }\\n }\\n return true;\\n}\\n\\n\\n//-------------------- 產生題目的演算法 --------------------//\\n\\n//「新遊戲」按鈕按下時執行此函式\\nfunction newGame () {\\n count = 0;\\n clearAll();\\n generator();\\n randomHide();\\n console.log('遞迴嘗試了 ' + count + ' 次!');\\n}\\n\\nfunction generator () {\\n \\n //限制遞迴次數避免網頁癱瘓\\n if (count \u003e= 300000) return;\\n count++;\\n\\n for (var y = 0; y \u003c 9; y++) {\\n for (var x = 0; x \u003c 9; x++) {\\n if (grid[y][x] === 0) {\\n var arr = [];\\n\\n for (var a = 0; a \u003c 9; a++) {\\n arr.push(grid[y][a]);\\n }\\n\\n for (var b = 0; b \u003c 9; b++) {\\n arr.push(grid[b][x]);\\n }\\n\\n var offsetX = x - x % 3;\\n var offsetY = y - y % 3;\\n\\n for (var l = 0; l \u003c 3; l++) {\\n for (var h = 0; h \u003c 3; h++) {\\n arr.push(grid[offsetY + l][offsetX + h]);\\n }\\n }\\n \\n var random_arr = shuffle();\\n \\n for (var k = 0; k \u003c 9; k++) {\\n var candidate = random_arr[k];\\n if (arr.indexOf(candidate) == -1) {\\n grid[y][x] = candidate;\\n var result = generator();\\n if (result) {\\n return true;\\n } else {\\n grid[y][x] = 0;\\n }\\n }\\n }\\n return false;\\n }\\n }\\n }\\n return true;\\n}\\n\\n// 清空數獨\\nfunction clearAll () {\\n for (var i = 0; i \u003c 9; i++) {\\n for (var k = 0; k \u003c 9; k++) {\\n grid[i][k] = 0;\\n }\\n }\\n}\\n\\n// 洗牌演算法\\nfunction shuffle() {\\n var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];\\n for (var i = 0; i \u003c 100; i++) {\\n var v1 = Math.floor(Math.random() * 9);\\n var v2 = Math.floor(Math.random() * 9);\\n var tamp = arr[v1];\\n arr[v1] = arr[v2];\\n arr[v2] = tamp;\\n }\\n return arr;\\n}\\n\\n// 隨機挖空完整的數獨\\nfunction randomHide() {\\n for (var i = 0; i \u003c 56; i++) {\\n var randX = Math.floor(Math.random()* 9);\\n var randY = Math.floor(Math.random()* 9);\\n if (grid[randX][randY] === 0) {\\n i--;\\n } else {\\n grid[randX][randY] = 0;\\n }\\n }\\n //方法2\\n // var cnt = 0;\\n // while(cnt \u003c 56){\\n // var randX = Math.floor(Math.random()* 9);\\n // var randY = Math.floor(Math.random()* 9);\\n // if (grid[randX][randY] != 0) {\\n // grid[randX][randY] = 0;\\n // cnt++;\\n // }\\n // }\\n}\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\"}","created_at":"2021-10-16T13:38:11.347+08:00","updated_at":"2021-10-16T14:36:28.852+08:00","name":"9x9數獨(預設版) 副本","language":"web","screenshot":{"url":null},"parent_id":165292,"plugin":"// 將二維陣列轉為二維物件給 vue 去更好做綁定\nfunction array3dToGrid(arr3d) {\n let grid = {};\n for (let x = 0; x \u003c 9; x++) {\n grid[x] = {};\n for (let y = 0; y \u003c 9; y++) {\n grid[x][y] = arr3d[x][y] !== 0 ? arr3d[x][y]: 0;\n }\n }\n return grid;\n}\n\n\n\nfunction Sudoku (arr3d) {\n \n var grid = array3dToGrid(arr3d);\n\n new Vue({\n \n el: '#sudoku',\n \n data: {\n grid: grid,\n originGrid: JSON.parse(JSON.stringify(grid)),\n },\n \n methods: {\n //水平檢查\n h_check(y) {\n let arr = [];\n for (let i = 0; i \u003c 9; i++) {\n arr.push(grid[y][i]);\n }\n return !this.isValid(arr);\n },\n \n //垂直檢查\n v_check(x) {\n let arr = [];\n for (let i = 0; i \u003c 9; i++) {\n arr.push(grid[i][x]);\n }\n return !this.isValid(arr);\n },\n\n //大格子檢查\n b_check(i) {\n let offsetX = i % 3;\n let offsetY = (i - offsetX) / 3;\n let arr = [];\n for (let x = 0; x \u003c 3; x++) {\n for (let y = 0; y \u003c 3; y++) {\n arr.push(grid[offsetY * 3 + y][offsetX * 3 + x]);\n }\n }\n return !this.isValid(arr);\n },\n\n isValid(arr) {\n for (let a = 0; a \u003c arr.length - 1; a++) {\n for (let b = a + 1; b \u003c arr.length; b++) {\n if (arr[a] === arr[b] \u0026\u0026 arr[b] !== 0) return false;\n }\n }\n return true;\n },\n\n textColor(x, y) {\n var num = grid[x][y];\n return {\n color: `rgba(0, 0, 0, ${ num == 0 ? 0.05 : 1})`,\n fontWeight: this.originGrid[x][y] != 0 ? 'bold' : 'normal',\n }\n },\n \n onfocus(x, y) {\n if (this.grid[x][y] == 0) this.grid[x][y] = '';\n },\n \n onfocusout(x, y) {\n for (var i=1; i\u003c=9; i++) {\n if (this.grid[x][y] == i) return;\n }\n this.grid[x][y] = 0;\n },\n\n resolve() {\n window.autoplay \u0026\u0026 window.autoplay(); //學生實作\n },\n\n newGame() {\n window.newGame \u0026\u0026 window.newGame(); //學生實作\n \n this.originGrid = JSON.parse(JSON.stringify(this.grid));\n },\n \n },\n \n computed: {\n isWin() {\n for (let x=0; x\u003c9; x++) {\n for (let y=0; y\u003c9; y++) {\n if (this.grid[x][y] == 0) return false;\n }\n }\n \n for (var i=0; i\u003c9; i++) {\n if (this.v_check(i) || this.h_check(i) || this.b_check(i)) {\n return false;\n }\n }\n \n return true;\n },\n }\n });\n \n return grid;\n\n}\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":93,"hashid":"p93skvpeq","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
前往網站頁面
1:1:1
1:1
full
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦