{"id":156633,"student_id":1998,"content":"// grid 儲存數獨的資料:\r\nfunction resolve (grid) {\r\n for (var x = 0; x \u003c 9; x++) {\r\n for (var y = 0; y \u003c 9; y++) {\r\n if (grid[x][y] === 0) {\r\n var arr = []; //針對第(x,y)格,儲存相對應所有行、列、宮的數字\r\n\r\n for (var i = 0; i \u003c 9; i++) {\r\n arr.push(grid[x][i]); //行\r\n arr.push(grid[i][y]); //列\r\n }\r\n\r\n var startX = x - (x % 3); //一個宮最左上角的x\r\n var startY = y - (y % 3); //一個宮最左上角的y\r\n\r\n for (var i = 0; i \u003c 3; i++) {\r\n for (var j = 0; j \u003c 3; j++) {\r\n arr.push(grid[startX+i][startY+j]); //此宮的所有人\r\n }\r\n }\r\n // var shuffleArr = shuffle();\r\n for (var i = 1; i \u003c= 9; i++) {\r\n if(arr.indexOf(i) === -1){\r\n grid[x][y] = i;\r\n if(resolve(grid)){\r\n return true;\r\n }\r\n grid[x][y] = 0;\r\n }\r\n }\r\n return false;\r\n }\r\n }\r\n }\r\n return true;\r\n}\r\n\r\n// 重置數獨的資料:\r\nfunction reset (grid) {\r\n for (var x=0; x\u003c9; x++) {\r\n for (var y=0; y\u003c9; y++) {\r\n grid[x][y] = 0;\r\n } \r\n }\r\n}\r\n\r\n// grid 儲存數獨的資料:\r\nfunction generate (grid) {\r\n for (var x = 0; x \u003c 9; x++) {\r\n for (var y = 0; y \u003c 9; y++) {\r\n if (grid[x][y] === 0) {\r\n var arr = []; //針對第(x,y)格,儲存相對應所有行、列、宮的數字\r\n\r\n for (var i = 0; i \u003c 9; i++) {\r\n arr.push(grid[x][i]); //行\r\n arr.push(grid[i][y]); //列\r\n }\r\n\r\n var startX = x - (x % 3); //一個宮最左上角的x\r\n var startY = y - (y % 3); //一個宮最左上角的y\r\n\r\n for (var i = 0; i \u003c 3; i++) {\r\n for (var j = 0; j \u003c 3; j++) {\r\n arr.push(grid[startX+i][startY+j]); //此宮的所有人\r\n }\r\n }\r\n var shuffleArr = shuffle();\r\n for (var i = 0; i \u003c 9; i++) {\r\n if(arr.indexOf(shuffleArr[i]) === -1){\r\n grid[x][y] = shuffleArr[i];\r\n if(generate(grid)){\r\n return true;\r\n }\r\n grid[x][y] = 0;\r\n }\r\n }\r\n return false;\r\n \r\n }\r\n \r\n }\r\n }\r\n }\r\n return true;\r\n \r\n}\r\n\r\n// 洗牌演算法\r\nshuffle();\r\nfunction shuffle () {\r\n var shuffleArr = [];\r\n //do something \r\n //var rand = (Math.random 產生 1-9 的隨機整數 )\r\n //把 rand 不重複的加進去 shuffleArr 陣列裡面 shuffleArr.push(x);\r\n // arr.indexOf(x) == -1 x不在陣列裡面\r\n //確保陣列裡面有 9 個隨機整數(1-9)\r\n var cnt = 0; //計算 shuffleArr 裡面有幾個值\r\n while(cnt \u003c 9){\r\n var rand = Math.floor(Math.random() * 9 + 1);\r\n if(shuffleArr.indexOf(rand) === -1){\r\n shuffleArr.push(rand);\r\n cnt += 1;\r\n }\r\n }\r\n // console.log(shuffleArr);\r\n return shuffleArr; //ex. [2,1,7,4,8,9,3,5,6]\r\n}","created_at":"2020-06-06T16:10:55.756+08:00","updated_at":"2020-06-13T16:27:58.542+08:00","name":"9x9 數獨產生器 副本","language":"javascript","screenshot":{"url":"https://cdn7.koding.school/uploads/project/screenshot/156633/89edcce60b8890776f43b51e69f48ea4.jpg"},"parent_id":49669,"plugin":"setBackdrop('bg.jpg');\n\nlet width = 40;\nlet margin = 10;\nlet offsetX = (640 - 40*9)/2;\nlet offsetY = 20;\nlet solveBtn = createSprite(\"solve_btn.jpg\");\nlet generateBtn = createSprite(\"generate_btn.jpg\");\n\nsolveBtn.y = 440;\ngenerateBtn.y = 440;\n\nsolveBtn.x += 100;\ngenerateBtn.x -= 100;\n\nforever(function() {\n solveBtn.scale = solveBtn.touched(cursor) ? 1.1: 1;\n generateBtn.scale = generateBtn.touched(cursor) ? 1.1: 1;\n});\n\nsolveBtn.on('click', ()=\u003e resolve(grid));\ngenerateBtn.on('click', ()=\u003e {\n reset(grid);\n generate(grid);\n});\n\n\nlet grid = [\n [8, 0, 0, 0, 0, 0, 0, 0, 0],\n [0, 0, 7, 5, 0, 0, 0, 0, 9],\n [0, 3, 0, 0, 0, 0, 1, 8, 0],\n [0, 6, 0, 0, 0, 1, 0, 5, 0],\n [0, 0, 9, 0, 3, 0, 0, 0, 0],\n [0, 0, 0, 7, 5, 0, 0, 0, 0],\n [0, 0, 2, 0, 7, 0, 0, 0, 4],\n [0, 0, 0, 0, 0, 3, 6, 1, 0],\n [0, 0, 0, 0, 0, 0, 8, 0, 0],\n];\n\n\nforever(function () {\n\n let colors = [\n ['', '', '', '', '', '', '', '', ''],\n ['', '', '', '', '', '', '', '', ''],\n ['', '', '', '', '', '', '', '', ''],\n ['', '', '', '', '', '', '', '', ''],\n ['', '', '', '', '', '', '', '', ''],\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 9 \u0026\u0026 focusOnY \u003c 9) {\n colors[focusOnX][focusOnY] = 'yellow';\n }\n\n for (var i = 0; i \u003c 9; i++) {\n if (v_checker(i)) {\n for (var z = 0; z \u003c 9; z++) colors[i][z] = 'red';\n }\n if (h_checker(i)) {\n for (var z = 0; z \u003c 9; z++) colors[z][i] = 'red';\n }\n if (b_checker(i)) {\n let offsetX = i%3;\n let offsetY = (i - offsetX)/3;\n for (let x = 0; x \u003c 3; x++) {\n for (let y = 0; y \u003c 3; y++) {\n colors[offsetX*3 + x][offsetY*3 + y] = 'red';\n }\n }\n }\n }\n\n for (let x = 0; x \u003c 9; x++) {\n for (let y = 0; y \u003c 9; y++) {\n pen.size = 0;\n if (colors[x][y] !== '') {\n pen.fillColor = colors[x][y];\n pen.drawRect(offsetX + x*width + 2.5, offsetY + y*width + 2.5, width - 5, width - 5);\n };\n if (grid[x][y] !== 0) {\n print(grid[x][y], offsetX + x*width + 2.5 + 8, offsetY + y*width + 2.5, 'black', 30);\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 9 \u0026\u0026 y \u003c 9) {\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 if (key['5']) grid[x][y] = 5;\n else if (key['6']) grid[x][y] = 6;\n else if (key['7']) grid[x][y] = 7;\n else if (key['8']) grid[x][y] = 8;\n else if (key['9']) grid[x][y] = 9;\n else grid[x][y] = 0;\n }\n});\n\nfunction v_checker (i) {\n var arr = [];\n for (var c = 0; c \u003c 9; 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 9; c++) {\n arr.push(grid[c][i]);\n }\n return !isValid(arr);\n}\n\nfunction b_checker (i) {\n let offsetX = i%3;\n let offsetY = (i - offsetX)/3;\n var arr = [];\n for (let x = 0; x \u003c 3; x++) {\n for (let y = 0; y \u003c 3; y++) {\n arr.push(grid[offsetX*3 + x][offsetY*3 + 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":36,"hashid":"kdms2q8n","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":2731272,"file_name":"solve_btn.jpg","project_id":156633,"asset_id":97472,"created_at":"2020-06-06T16:10:55.761+08:00","updated_at":"2020-06-06T16:10:55.761+08:00"},{"id":2731273,"file_name":"new_game_btn.jpg","project_id":156633,"asset_id":97471,"created_at":"2020-06-06T16:10:55.762+08:00","updated_at":"2020-06-06T16:10:55.762+08:00"},{"id":2731274,"file_name":"bg.jpg","project_id":156633,"asset_id":97470,"created_at":"2020-06-06T16:10:55.763+08:00","updated_at":"2020-06-06T16:10:55.763+08:00"},{"id":2731275,"file_name":"generate_btn.jpg","project_id":156633,"asset_id":97469,"created_at":"2020-06-06T16:10:55.764+08:00","updated_at":"2020-06-06T16:10:55.764+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦