{"id":56872,"student_id":10,"content":"ibot.x = 2;\nibot.y = 2;\nibot.direction = 90;\n\n/**\n * R: 「順」時針旋轉\n * L: 「逆」時針旋轉\n * M: 向前「走」一格\n * B: 向後「退」一格\n * J: 跳到第 i 個指令繼續執行 ex: J5\n * ?J:「如果」艾伯特所在的格子有 x 物品「就」跳到第 y 個指令繼續執行 ex: red?J5\n * +: 從背包取出 1 個物品放到當前格子\n * -: 從當前格子取出 1 個物品放到背包\n */\nrunScript([\n 'M', 'M',\n 'black?J14',\n '-', 'B', 'B', '+', 'M', 'M', 'red?J2',\n 'B', 'B',\n 'R', 'J0',\n 'B', 'B'\n]);","created_at":"2019-02-14T11:32:24.136+08:00","updated_at":"2019-11-11T06:34:39.549+08:00","name":"5蘋果 x 4籃子 = ?","language":"javascript","screenshot":{"url":"https://cdn9.koding.school/uploads/project/screenshot/56872/195a42efa73fad57497ae17a783376d9.jpg"},"parent_id":56871,"plugin":"setBackdrop(\"black\");\n\nvar grid = [];\n\nvar ibot = {\n x: 0,\n y: 0,\n direction: 0,\n stepForward: function () {\n this.direction = this.direction%360;\n if (this.direction \u003c 0) this.direction+=360;\n if (this.direction === 0) this.y--;\n if (this.direction === 90) this.x++;\n if (this.direction === 180) this.y++;\n if (this.direction === 270) this.x--;\n },\n stepBack: function () {\n this.direction = this.direction%360;\n if (this.direction \u003c 0) this.direction+=360;\n if (this.direction === 0) this.y++;\n if (this.direction === 90) this.x--;\n if (this.direction === 180) this.y--;\n if (this.direction === 270) this.x++;\n }\n}\n\nvar backpack = [];\n\nfunction loadData (map) {\n for (var x = 0; x \u003c map.length; x++) {\n grid[x] = [];\n for (var y = 0; y \u003c map[x].length; y++) {\n let color = map[x][y].split('_')[0] || 'black';\n let count = map[x][y].split('_')[1] || 0;\n count = Number(count);\n \n grid[x][y] = { color, count }\n }\n }\n}\n\nfunction runScript (script) {\n \n let idx = 0;\n \n let id = setInterval(function () {\n let cmd = script[idx];\n console.log(cmd);\n if (cmd === undefined) {\n console.log('done!')\n return clearInterval(id);\n }\n\n if (cmd === 'R') ibot.direction+=90;\n if (cmd === 'L') ibot.direction-=90;\n if (cmd === 'M') ibot.stepForward();\n if (cmd === 'B') ibot.stepBack();\n if (cmd === '-') {\n if (grid[ibot.x][ibot.y].count \u003e 0) {\n grid[ibot.x][ibot.y].count--;\n backpack.push(grid[ibot.x][ibot.y].color);\n if (grid[ibot.x][ibot.y].count === 0) {\n grid[ibot.x][ibot.y].color = 'black';\n }\n }\n }\n if (cmd === '+') {\n let d = backpack.pop();\n if (d) {\n if (grid[ibot.x][ibot.y].color == 'black') {\n grid[ibot.x][ibot.y].color = d;\n grid[ibot.x][ibot.y].count = 1;\n } else {\n grid[ibot.x][ibot.y].count++;\n }\n }\n }\n if (cmd[0] === 'J') {\n let target = Number(cmd.slice(1));\n idx = target - 1;\n }\n if (cmd.indexOf('?J') !== -1) {\n let color = cmd.split('?J')[0];\n let target = cmd.split('?J')[1];\n if (grid[ibot.x][ibot.y].color === color) {\n idx = target - 1;\n }\n }\n \n idx++;\n\n }, 250);\n\n}\n\n\nforever(function(){\n \n let offsetX = 50;\n let offsetY = 50;\n\n for (var x=0; x\u003cgrid.length; x++) {\n for (var y=0; y\u003cgrid[x].length; y++) {\n pen.size = '3';\n pen.color = '#fff';\n pen.fillColor = grid[x][y].color;\n pen.drawRect(x*50 + offsetX, y*50 + offsetY, 50, 50);\n \n if (grid[x][y].count \u003e 0)\n print(grid[x][y].count, x*50 + 7 + offsetX, y*50 + 7 + offsetY, '#fff');\n } \n }\n \n // draw ibot\n pen.size = 5;\n pen.fillColor = 'rgba(0, 0, 0, 0)';\n pen.color = 'gold';\n pen.drawRect(ibot.x*50+3 + offsetX, ibot.y*50+2 + offsetY, 44, 44);\n \n let head = [ibot.x*50 + 25 + offsetX, ibot.y*50 + 25 + offsetY];\n if (ibot.direction === 0) head[1] -= 20;\n if (ibot.direction === 90) head[0] += 20;\n if (ibot.direction === 180) head[1] += 20;\n if (ibot.direction === 270) head[0] -= 20;\n pen.fillColor = 'gold';\n pen.drawCircle(head[0], head[1], 4);\n \n // backpack\n print(`背包(${backpack.length}) [${backpack}]`, 5, 5, '#fff');\n \n})\n\n//----------------- 題目設定 -----------------//\nlet map = [\n ['', '', 'red_5', '', ''],\n ['', '', '', '', ''],\n ['red_5', '', '', '', 'red_5'],\n ['', '', '', '', ''],\n ['', '', 'red_5', '', ''],\n];\nloadData(map);","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":366,"hashid":"zpesqrm8","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
到這台電腦