{"id":103212,"student_id":37,"content":"// grid 儲存數獨的資料:\nfunction solve (grid) {\n for (var x = 0; x \u003c 4; x++) {\n for (var y = 0; y \u003c 4; y++) {\n if (grid[x][y] == 0) {\n var arr = []\n for (i = 0; i \u003c 4; i++) {\n arr.push(grid[x][i]);\n arr.push(grid[i][y]);\n }\n var offsetX = Math.floor(x/2)\n var offsetY = Math.floor(y/2)\n for (var xx = 0; xx \u003c 2; xx++) {\n for (var yy = 0; yy \u003c 2; yy++) {\n arr.push(grid[offsetX*2+xx][offsetY*2+yy]);\n }\n }\n for (var i = 1; i \u003c= 4; i++) {\n if (arr.indexOf(i) == -1) {\n grid[x][y] = i;\n if(solve(grid)){\n return true;\n }\n grid[x][y] = 0;\n }\n }\n // console.log(arr)\n return false;\n }\n }\n }\n return true;\n}","created_at":"2019-10-27T10:11:20.803+08:00","updated_at":"2019-11-11T09:24:46.863+08:00","name":"4x4 數獨破解 副本","language":"javascript","screenshot":{"url":"https://cdn5.koding.school/uploads/project/screenshot/103212/ef41d079ededbca7cdcfb1a7c34e4854.jpg"},"parent_id":47893,"plugin":"setBackdrop('bg.jpg');\n\nlet width = 90;\nlet margin = 10;\nlet offsetX = (640 - 360)/2;\nlet offsetY = 30;\nlet solveBtn = createSprite(\"solve_it_btn.jpg\");\nlet newGameBtn = createSprite(\"new_game_btn.jpg\");\n\nnewGameBtn.x = offsetX + 100;\nnewGameBtn.y = 440;\nsolveBtn.x = offsetX + 260;\nsolveBtn.y = 440;\n\nforever(function() {\n newGameBtn.scale = newGameBtn.touched(cursor) ? 1.1: 1;\n solveBtn.scale = solveBtn.touched(cursor) ? 1.1: 1;\n});\n\nnewGameBtn.on('click', newGame);\nsolveBtn.on('click', ()=\u003e solve(grid));\n\n\nlet challenges = [\n [\n [0, 0, 0, 2],\n [0, 1, 0, 0],\n [4, 0, 0, 0],\n [0, 0, 2, 0],\n ],\n [\n [0, 2, 0, 0],\n [0, 0, 0, 1],\n [0, 0, 3, 0],\n [4, 0, 0, 0],\n ],\n [\n [0, 3, 0, 0],\n [0, 0, 0, 2],\n [1, 0, 0, 0],\n [0, 0, 4, 0],\n ],\n [\n [0, 2, 0, 0],\n [0, 0, 3, 0],\n [2, 0, 0, 0],\n [0, 0, 0, 4],\n ],\n [\n [0, 0, 1, 0],\n [0, 2, 0, 0],\n [0, 0, 0, 3],\n [4, 0, 0, 0],\n ],\n [\n [0, 4, 0, 0],\n [0, 0, 3, 0],\n [0, 0, 0, 2],\n [1, 0, 0, 0],\n ],\n [\n [0, 0, 0, 3],\n [2, 0, 0, 0],\n [0, 0, 4, 0],\n [0, 2, 0, 0],\n ],\n [\n [0, 0, 2, 0],\n [0, 4, 0, 0],\n [0, 0, 0, 3],\n [1, 0, 0, 0],\n ],\n [\n [0, 0, 0, 4],\n [0, 1, 0, 0],\n [2, 0, 0, 0],\n [0, 0, 3, 0],\n ],\n [\n [0, 3, 0, 0],\n [0, 0, 0, 4],\n [1, 0, 0, 0],\n [0, 0, 1, 0],\n ],\n [\n [2, 0, 0, 0],\n [0, 0, 4, 0],\n [0, 3, 0, 0],\n [0, 0, 0, 1],\n ],\n [\n [0, 0, 0, 3],\n [4, 0, 0, 0],\n [0, 1, 0, 0],\n [0, 0, 3, 0],\n ],\n [\n [0, 0, 0, 4],\n [0, 3, 0, 0],\n [2, 0, 0, 0],\n [0, 0, 2, 0],\n ],\n [\n [0, 0, 2, 0],\n [0, 4, 0, 0],\n [0, 0, 0, 1],\n [3, 0, 0, 0],\n ],\n [\n [0, 0, 0, 2],\n [3, 0, 0, 0],\n [0, 1, 0, 0],\n [0, 0, 4, 0],\n ]\n]\n\ngrid = [[], [], [], []];\n\n\nfunction newGame () {\n let rand = Math.floor(Math.random()*challenges.length);\n for (let x = 0; x \u003c 4; x++) {\n for (let y = 0; y \u003c 4; y++) {\n grid[x][y] = challenges[rand][x][y];\n }\n }\n}\nnewGame();\n\n\nforever(function () {\n\n let colors = [\n ['', '', '', ''],\n ['', '', '', ''],\n ['', '', '', ''],\n ['', '', '', ''],\n ];\n\n\n let focusOnX = Math.floor((cursor.x - offsetX)/width);\n let focusOnY = Math.floor((cursor.y - offsetY)/width);\n if (focusOnX \u003e= 0 \u0026\u0026 focusOnY \u003e= 0 \u0026\u0026 focusOnX \u003c 4 \u0026\u0026 focusOnY \u003c 4) {\n colors[focusOnX][focusOnY] = 'yellow';\n }\n\n for (var i = 0; i \u003c 4; i++) {\n if (v_checker(i)) {\n for (var z = 0; z \u003c 4; z++) colors[i][z] = 'red';\n }\n if (h_checker(i)) {\n for (var z = 0; z \u003c 4; z++) colors[z][i] = 'red';\n }\n if (b_checker(i)) {\n let offsetX = i%2;\n let offsetY = (i - offsetX)/2;\n for (let x = 0; x \u003c 2; x++) {\n for (let y = 0; y \u003c 2; y++) {\n colors[offsetX*2 + x][offsetY*2 + y] = 'red'\n }\n }\n }\n }\n\n for (let x = 0; x \u003c 4; x++) {\n for (let y = 0; y \u003c 4; y++) {\n pen.size = 0;\n if (colors[x][y] !== '') {\n pen.fillColor = colors[x][y];\n pen.drawRect(offsetX + x*90 + 2.5, offsetY + y*90 + 2.5, 85, 85);\n };\n if (grid[x][y] !== 0) {\n print(grid[x][y], offsetX + x*90 + 2.5 + 27, offsetY + y*90 + 2.5 + 10, 'black', 60);\n }\n }\n }\n});\n\nwhen('keydown', () =\u003e {\n let x = Math.floor((cursor.x - offsetX)/width);\n let y = Math.floor((cursor.y - offsetY)/width);\n if (x \u003e= 0 \u0026\u0026 y \u003e= 0 \u0026\u0026 x \u003c 4 \u0026\u0026 y \u003c 4) {\n if (key['1']) grid[x][y] = 1;\n else if (key['2']) grid[x][y] = 2;\n else if (key['3']) grid[x][y] = 3;\n else if (key['4']) grid[x][y] = 4;\n else grid[x][y] = 0;\n }\n});\n\n\nfunction v_checker (i) {\n var arr = [];\n for (var c = 0; c \u003c 4; c++) {\n arr.push(grid[i][c]);\n }\n return !isValid(arr);\n}\n\nfunction h_checker (i) {\n var arr = [];\n for (var c = 0; c \u003c 4; c++) {\n arr.push(grid[c][i]);\n }\n return !isValid(arr);\n}\n\nfunction b_checker (i) {\n let offsetX = i%2;\n let offsetY = (i - offsetX)/2;\n var arr = [];\n for (let x = 0; x \u003c 2; x++) {\n for (let y = 0; y \u003c 2; y++) {\n arr.push(grid[offsetX*2 + x][offsetY*2 + y]);\n }\n }\n return !isValid(arr);\n}\n\n\nfunction isValid (arr) {\n for (var a = 0; a \u003c arr.length - 1; a++) {\n for (var b = a+1; b \u003c arr.length; b++) {\n if (arr[a] == arr[b] \u0026\u0026 arr[a] != '') {\n return false;\n }\n }\n }\n return true;\n}","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":49,"hashid":"4y3s5kdn","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":1749216,"file_name":"bg.jpg","project_id":103212,"asset_id":94504,"created_at":"2019-10-27T10:11:20.810+08:00","updated_at":"2019-10-27T10:11:20.810+08:00"},{"id":1749217,"file_name":"new_game_btn.jpg","project_id":103212,"asset_id":94503,"created_at":"2019-10-27T10:11:20.812+08:00","updated_at":"2019-10-27T10:11:20.812+08:00"},{"id":1749218,"file_name":"solve_it_btn.jpg","project_id":103212,"asset_id":94502,"created_at":"2019-10-27T10:11:20.813+08:00","updated_at":"2019-10-27T10:11:20.813+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦