{"id":230554,"student_id":3469,"content":"//循環排程吧檯 or '最短優先 + 先進先出吧檯'\nvar b1 = new Bar();\n//先進先出吧檯\nvar b2 = new Bar();\n//最短優先吧檯\nvar b3 = new Bar();\n\n\n\n//Round robin 循環排程演算法\n// setInterval(function () {\n// b1.serve(b1[0]);\n// //將服務玩得客人丟到陣列的最後\n// var task = b1.shift();\n// b1.push(task);\n// }, 1000);\n\n\n\n\n//FIFO(First-In, First-Out) 先進先出排程演算法\nsetInterval(function() {\n b2.sort(function(a, b) {\n return b.time - a.time;\n });\n b2.serve(b2[0]);\n}, 1000);\n\n\n\n\n\n//SJF(Short Job First) 最短優先排程演算法\nsetInterval(function() {\n b3.sort(function(a, b){\n return a.value - b.value;\n });\n b3.serve(b3[0]);\n}, 1000);\n\n\n\n\n\n//最短優先 + 先進先出演算法\nsetInterval(function(){\n b1.sort(function(a, b){\n return (b.time - b.value) - (a.time - a.value);\n });\n b1.serve(b1[0]);\n}, 1000);\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","created_at":"2021-02-21T14:13:51.876+08:00","updated_at":"2021-02-21T15:12:42.150+08:00","name":"酒吧排程演算法(預設版) 副本","language":"javascript","screenshot":{"url":"https://cdn7.koding.school/uploads/project/screenshot/230554/b5b0a1a99866e8a5255d52c83be0083e.jpg"},"parent_id":148160,"plugin":"Game.set({ width: 1200, height: 900 });\nsetBackdrop(\"bg.png\");\n\nconst COSTUMES = [['b0.png', 'b1.png'], ['r0.png', 'r1.png'], ['g0.png', 'g1.png'], ['y0.png', 'y1.png'], ['o0.png', 'o1.png']];\nvar bars = [];\nvar peopleSprite = {};\n\nfunction Bar (x, y) {\n \n if (bars.length \u003e 3) return alert('只能創造三個!');\n\n var table = createSprite(\"bar.png\");\n table.y = (2 - bars.length)*300 + 150;\n table.layer = 1;\n\n this.table = table;\n this.tasks = [];\n this.sprites = {};\n this.idx = 0;\n this.totalTime = 0;\n this.avgTime = 0;\n\n bars.push(this);\n\n forever(this.loop.bind(this));\n this.tasks.serve = this.serve.bind(this);\n\n return this.tasks;\n}\n\nBar.prototype = {\n \n loop: function () {\n \n var self = this;\n \n for (var i=0; i\u003cthis.tasks.length; i++) {\n var t = this.tasks[i];\n \n // 移除被放入不符合格式的物件\n if (!this.tasks[i]) {\n this.tasks.splice(i, 1);\n i--;\n continue;\n }\n \n // 印出每個叩叮的資訊\n var s = self.sprites[t.spriteId];\n print(t.value, s.x, s.y + 70, 'orange', 40); //啤酒需求個數\n print(t.time, s.x, s.y + 110, 'white', 40); //等待時間\n \n // 移除結束的任務\n if (t \u0026\u0026 t.value \u003c= 0) {\n this.tasks.splice(i, 1);\n this.sprites[t.spriteId].destroy();\n delete this.sprites[t.spriteId];\n }\n }\n\n var s = '平均等待 ' +this.avgTime.toFixed(3) + ' 秒'\n print(s, 10, this.table.y - 130, 'white', 30);\n },\n \n addTask: function (sprite, data) {\n this.tasks.push(data);\n for (var i = 0; i \u003c 10; i++) {\n if (this.sprites[this.idx] == undefined) {\n this.sprites[this.idx] = sprite;\n sprite.x = this.idx*120 + 60;\n sprite.y = this.table.y;\n data.spriteId = this.idx;\n break;\n }\n this.idx++;\n if (this.idx \u003e= 10) this.idx = 0;\n }\n },\n\n serve: function (data) {\n if (data == undefined) return;\n\n var s = this.sprites[data.spriteId]\n s.costumeId = 1;\n setTimeout(function() { \n data.value--;\n s.costumeId = 0;\n }, 700);\n }\n}\n\n\n// 每秒鐘更新一次task的等待時間\nsetInterval(function () {\n bars.forEach(function (bar) {\n bar.tasks.forEach(function (t) {\n if (t) t.time++;\n })\n bar.totalTime += bar.tasks.length;\n bar.avgTime = bar.totalTime/order;\n })\n}, 1000);\n\n\nvar order = 0;\n\nsetInterval(function () {\n\n if (bars.length == 0) return;\n \n // 如果有任合一間酒吧已經滿客,所有ㄐㄧ就不再新增\n if (bars.some(function (b) {return b.tasks.length \u003e= 10})) return;\n \n\n order++;\n\n var value = Math.floor(Math.random()*8) + 1;\n var costume = COSTUMES[Math.floor(Math.random()*COSTUMES.length)];\n\n bars.forEach(function (bar) {\n var s = createSprite(costume);\n var data = {\n value: value, time: 0, order: order\n }\n bar.addTask(s, data);\n s.on('click', () =\u003e data.value -= 1);\n });\n}, 2000);","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":86,"hashid":"zpesjzwyy","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":4134401,"file_name":"y1.png","project_id":230554,"asset_id":212457,"created_at":"2021-02-21T14:13:51.887+08:00","updated_at":"2021-02-21T14:13:51.887+08:00"},{"id":4134402,"file_name":"y0.png","project_id":230554,"asset_id":212458,"created_at":"2021-02-21T14:13:51.888+08:00","updated_at":"2021-02-21T14:13:51.888+08:00"},{"id":4134403,"file_name":"r0.png","project_id":230554,"asset_id":212459,"created_at":"2021-02-21T14:13:51.889+08:00","updated_at":"2021-02-21T14:13:51.889+08:00"},{"id":4134404,"file_name":"r1.png","project_id":230554,"asset_id":212460,"created_at":"2021-02-21T14:13:51.890+08:00","updated_at":"2021-02-21T14:13:51.890+08:00"},{"id":4134405,"file_name":"o1.png","project_id":230554,"asset_id":212461,"created_at":"2021-02-21T14:13:51.891+08:00","updated_at":"2021-02-21T14:13:51.891+08:00"},{"id":4134406,"file_name":"o0.png","project_id":230554,"asset_id":212462,"created_at":"2021-02-21T14:13:51.892+08:00","updated_at":"2021-02-21T14:13:51.892+08:00"},{"id":4134407,"file_name":"g0.png","project_id":230554,"asset_id":212463,"created_at":"2021-02-21T14:13:51.892+08:00","updated_at":"2021-02-21T14:13:51.892+08:00"},{"id":4134408,"file_name":"g1.png","project_id":230554,"asset_id":212464,"created_at":"2021-02-21T14:13:51.893+08:00","updated_at":"2021-02-21T14:13:51.893+08:00"},{"id":4134409,"file_name":"b0.png","project_id":230554,"asset_id":212465,"created_at":"2021-02-21T14:13:51.894+08:00","updated_at":"2021-02-21T14:13:51.894+08:00"},{"id":4134410,"file_name":"b1.png","project_id":230554,"asset_id":212466,"created_at":"2021-02-21T14:13:51.895+08:00","updated_at":"2021-02-21T14:13:51.895+08:00"},{"id":4134411,"file_name":"bg.png","project_id":230554,"asset_id":212467,"created_at":"2021-02-21T14:13:51.896+08:00","updated_at":"2021-02-21T14:13:51.896+08:00"},{"id":4134412,"file_name":"bar.png","project_id":230554,"asset_id":212468,"created_at":"2021-02-21T14:13:51.897+08:00","updated_at":"2021-02-21T14:13:51.897+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦