{"id":28765,"student_id":10,"content":"// 挖洞機的 x, y 位置\nvar x = 0;\nvar y = 0;\n\n// 記錄移動的點\nvar steps = [];\n\n// 預設起始點被挖掘過\ngrid[0][0].color = 'lightblue';\n\nforever(function(){\n pen.fillColor = 'red';\n pen.drawRect(x*width, y*width, width, width);\n\n var next = getNeighbors(x, y);\n \n if (next.length \u003e 0) {\n \n // 留下走過的座標記號\n steps.push({x: x, y: y});\n \n // 隨機挑選一個方向前進\n var rand = Math.floor(Math.random()*next.length);\n \n if (next[rand] == 'top') {\n grid[x][y].top = false;\n y -= 1;\n grid[x][y].bottom = false;\n }\n if (next[rand] == 'right') {\n grid[x][y].right = false;\n x += 1;\n grid[x][y].left = false;\n }\n if (next[rand] == 'bottom') {\n grid[x][y].bottom = false;\n y += 1;\n grid[x][y].top = false;\n }\n if (next[rand] == 'left') {\n grid[x][y].left = false;\n x -= 1;\n grid[x][y].right = false;\n }\n grid[x][y].color = 'lightblue';\n } else {\n if (steps.length \u003e 0) {\n grid[x][y].color = 'yellow';\n var back = steps.pop();\n x = back.x;\n y = back.y;\n }\n }\n});\n\n// 取得四個方向中可以挖掘的方向\nfunction getNeighbors (x, y) {\n var arr = [];\n if (check(x + 1, y)) {\n arr.push('right');\n }\n if (check(x - 1, y)) {\n arr.push('left');\n }\n if (check(x, y + 1)) {\n arr.push('bottom');\n }\n if (check(x, y - 1)) {\n arr.push('top');\n }\n return arr;\n}\n\n// 檢查 (x, y) 是否在地圖的範圍內,且尚未被挖掘過\nfunction check (x, y) {\n return x \u003e= 0 \u0026\u0026 x \u003c row \u0026\u0026\n y \u003e= 0 \u0026\u0026 y \u003c col \u0026\u0026\n grid[x][y].color == 'white';\n}","created_at":"2018-05-12T18:20:39.272+08:00","updated_at":"2019-11-11T09:55:52.296+08:00","name":"maze generator","language":"javascript","screenshot":{"url":null},"parent_id":2,"plugin":"var grid = [];\nvar width = 20;\nvar row = 640/width;\nvar col = 480/width;\n\nfor (var x = 0; x \u003c row; x++) {\n grid[x] = [];\n for (var y = 0; y \u003c col; y++) {\n grid[x][y] = {\n color: 'white',\n top: true,\n right: true,\n bottom: true,\n left: true\n };\n }\n}\n\nforever(function() {\n for (var cx = 0; cx \u003c row; cx++) {\n for (var cy = 0; cy \u003c col; cy++) {\n \n var cell = grid[cx][cy];\n pen.size = 0.001;\n pen.fillColor = cell.color;\n pen.drawRect(cx*width, cy*width, width, width);\n \n pen.size = 1;\n var x = cx*width;\n var y = cy*width;\n if (cell.top) pen.drawLine(x, y, x + width, y);\n if (cell.right) pen.drawLine(x + width, y, x + width, y + width);\n if (cell.bottom) pen.drawLine(x, y + width, x + width, y + width);\n if (cell.left) pen.drawLine(x, y + width, x, y);\n }\n }\n});\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":340,"hashid":"vngsr5n2","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
到這台電腦