{"id":56877,"student_id":10,"content":"ibot.x = 4;\nibot.y = 1;\nibot.direction = 270;\n\n/**\n * R: 「順」時針旋轉\n * L: 「逆」時針旋轉\n * M: 向前「走」一格\n * B: 向後「退」一格\n * J: 跳到第 i 個指令繼續執行 ex: J5\n * ?J:「如果」艾伯特所在的格子有 x 物品「就」跳到第 y 個指令繼續執行 ex: red?J10\n * +: 從背包取出 1 個物品放到當前格子\n * -: 從當前格子取出 1 個物品放到背包\n */\nrunScript([\n 'M', 'M', '-', 'M', '-', 'M', '-',\n \n 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',\n \n '+', 'M', '+', 'M', '+', 'M', 'M'\n]);","created_at":"2019-02-14T13:33:00.569+08:00","updated_at":"2019-11-12T06:38:45.131+08:00","name":"背包 stack 先進後出:把紅色、藍色、綠色寶石放到指定的寶箱","language":"javascript","screenshot":{"url":"/uploads/project/screenshot/56877/cae621a452afd5b7f280f46d89261646.jpg"},"parent_id":56876,"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 ['', 'green_1', ''],\n ['', 'blue_1', ''],\n ['', 'red_1', ''],\n ['', '', ''],\n ['', '', ''],\n ['', '', ''],\n ['', 'red_1', ''],\n ['', 'blue_1', ''],\n ['', 'green_1', ''],\n];\nloadData(map);","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":536,"hashid":"5j3sr3g6","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
Orange Apple Learning Platform
My Projects
View Project Page
Export
Remix
Import
Delete
Download Android APP (APK)
Screenshot
繁中
简中
English
日本語
1:1:1
1:1
Full Width
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer