{"id":31609,"student_id":10,"content":"function AI () {\n for (var x=0; x\u003cW; x++) {\n for(var y=0; y\u003cH; y++) {\n if (grid[x][y].num \u003e 0) {\n var flag = nearFlag(x, y);\n var open = nearOpen(x, y);\n var unopen = 8 - open;\n \n if (unopen == grid[x][y].num) {\n nearSetFlag(x, y);\n }\n if (flag == grid[x][y].num) {\n nearClick(x, y);\n }\n }\n }\n }\n}\n\nfunction nearFlag (x, y) {\n var count = 0;\n getNear(x, y).forEach((b) =\u003e {\n if (b.flag) count++; \n });\n return count;\n}\n\nfunction nearOpen(x, y) {\n var count = 0;\n getNear(x, y).forEach((b) =\u003e {\n if (b.open) count++; \n });\n return count;\n}\n\nfunction nearSetFlag (x, y) {\n getNear(x, y).forEach((b) =\u003e {\n if (!b.open) b.flag = true;\n });\n}\n\nfunction nearClick (x, y) {\n getNear(x, y).forEach((b) =\u003e {\n if (!b.open) b.click();\n });\n}\n\nfunction getNear (posX, posY) {\n var blocks = [];\n for (var x=-1; x\u003c2; x++) {\n for (var y=-1; y\u003c2; y++) {\n if (x == 0 \u0026\u0026 y == 0) continue;\n if (grid[posX + x] \u0026\u0026 grid[posX + x][posY + y]) {\n blocks.push(grid[posX + x][posY + y]); \n }\n \n } \n }\n return blocks;\n}","created_at":"2018-06-13T16:32:16.185+08:00","updated_at":"2024-02-14T10:19:01.131+08:00","name":"AI 踩地雷","language":"javascript","screenshot":{"url":"https://cdn2.koding.school/uploads/project/screenshot/31609/210c9bf44c5600acd77e977d38a68d82.jpg"},"parent_id":2,"plugin":"var grid = [];\nvar W = 640/40;\nvar H = 480/40;\nvar status = 'playing';\n\nfor (var x=0; x\u003cW; x++) {\n grid[x] = [];\n for(var y=0; y\u003cH; y++) {\n grid[x][y] = {\n isBomb: false,\n open: false,\n flag: false,\n x: x,\n y: y,\n click: function () {click(this.x, this.y)}\n }\n }\n}\n\nfor (var i=0; i\u003c30; i++) {\n var x = Math.floor(Math.random()*W);\n var y = Math.floor(Math.random()*H);\n grid[x][y].isBomb = true;\n}\n\nforever(function(){\n for (var x=0; x\u003cW; x++) {\n for(var y=0; y\u003cH; y++) {\n var b = grid[x][y];\n \n if (b.open) {\n pen.fillColor = '#ccc';\n pen.drawRect(x*40, y*40, 40, 40);\n if (b.num \u003e 0) print(b.num, x*40, y*40, 'black', 20);\n } else {\n pen.fillColor = '#fff';\n pen.drawRect(x*40, y*40, 40, 40);\n }\n \n if (b.isBomb \u0026\u0026 status == 'gameOver') {\n pen.fillColor = '#f00';\n pen.drawRect(x*40, y*40, 40, 40);\n }\n \n if (b.flag) {\n pen.fillColor = '#00f';\n pen.drawCircle(x*40 + 20, y*40 + 20, 10);\n }\n }\n }\n \n if (status == 'gameOver') {\n print('Game Over', 200, 200, 'black', 60);\n stop();\n }\n \n if (empty.length \u003e 0) {\n var pos = empty.pop();\n [[-1, -1],[0, -1], [1, -1],[-1, 0],[1, 0],[-1, 1],[0, 1], [1, 1]].forEach((arr) =\u003e {\n if (grid[pos.x + arr[0]] \u0026\u0026 grid[pos.x + arr[0]][pos.y + arr[1]] \u0026\u0026 \n grid[pos.x + arr[0]][pos.y + arr[1]].open == false \u0026\u0026\n grid[pos.x + arr[0]][pos.y + arr[1]].flag == false) {\n click(pos.x + arr[0], pos.y + arr[1]);\n }\n });\n }\n \n check();\n});\n\n\nwhen('mousedown', function () {\n var x = Math.floor(cursor.x/40);\n var y = Math.floor(cursor.y/40);\n if (cursor.left) click(x, y);\n if (cursor.right) flag(x, y);\n});\n\nvar empty = [];\n\nfunction click (x, y) {\n if (grid[x][y].flag) return;\n if (grid[x][y].isBomb) return gameOver();\n grid[x][y].open = true;\n grid[x][y].num = 0;\n [[-1, -1],[0, -1], [1, -1],[-1, 0],[1, 0],[-1, 1],[0, 1], [1, 1]].forEach((arr) =\u003e {\n if (grid[x + arr[0]] \u0026\u0026 grid[x + arr[0]][y + arr[1]] \u0026\u0026 \n grid[x + arr[0]][y + arr[1]].isBomb) grid[x][y].num++; \n });\n \n if (grid[x][y].num == 0) empty.push({x: x, y: y});\n}\n\nfunction flag (x, y) {\n grid[x][y].flag = !grid[x][y].flag;\n}\n\nfunction check () {\n for (var x=0; x\u003cW; x++) {\n for(var y=0; y\u003cH; y++) {\n if (!grid[x][y].isBomb \u0026\u0026 !grid[x][y].open) return;\n }\n }\n print('win', 200, 200, 'black', 60);\n stop();\n}\n\nfunction gameOver () {\n status = 'gameOver';\n}","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":651,"hashid":"npms4kvw","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":591023,"file_name":"0.png","project_id":31609,"asset_id":6,"created_at":"2018-06-13T16:32:16.192+08:00","updated_at":"2018-06-13T16:32:16.192+08:00"},{"id":591024,"file_name":"1.svg","project_id":31609,"asset_id":7,"created_at":"2018-06-13T16:32:16.194+08:00","updated_at":"2018-06-13T16:32:16.194+08:00"},{"id":591025,"file_name":"2.png","project_id":31609,"asset_id":8,"created_at":"2018-06-13T16:32:16.195+08:00","updated_at":"2018-06-13T16:32:16.195+08:00"},{"id":591026,"file_name":"0.wav","project_id":31609,"asset_id":9,"created_at":"2018-06-13T16:32:16.196+08:00","updated_at":"2018-06-13T16:32:16.196+08:00"},{"id":591027,"file_name":"1.wav","project_id":31609,"asset_id":10,"created_at":"2018-06-13T16:32:16.198+08:00","updated_at":"2018-06-13T16:32:16.198+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦