{"id":28928,"student_id":10,"content":"// section 0\nvar Q = Brain(); // 鳥的大腦,記錄在不同的狀態下要做什麼反應(跳/不跳)\n\nvar S; // 上一次的狀態\nvar A; // 上一次的行為\nvar _S; // 這次的狀態\nvar alpha = 0.3; // 學習速率\nvar beta = 0.7; // 獎勵延遲\n\n// section 1\nwhen('click', game.jump);\n\nforever(function () {\n\n // section 2\n if (game.gameStatus == 'stop') {\n game.restart();\n\n // section 8\n reward(S, A, -1000, _S);\n }\n\n // section 3\n var food = game.getFood();\n\n // section 4\n _S = getStatus(game.bird, food);\n print(_S, 100, 10, 'white', 30);\n\n if(game.bird.touched(food)) {\n food.destroy();\n reward(S, A, 50, _S);\n console.log(\"過了!\");\n }else {\n reward(S, A, 1, _S);\n }\n \n // section 6\n if (Q[_S][0] \u003c Q[_S][1]) {\n A = 1;\n game.jump();\n } else {\n A = 0;\n }\n S = _S;\n\n});\n\n// section 4\n// 參考\n// 先印出「距離目標 X 距離為: 123.1234567... 」、「距離目標 Y 距離為: -15.1234567... 」\n// 接著印「距離目標 X 距離為: 8」、「距離目標 X 距離為: -1」\n// 最後印出「?:?」\nfunction getStatus (bird, food) {\n var x = Math.floor((food.x - bird.x)/15);\n var y = Math.floor((food.y - bird.y)/15);\n return x + ':' + y\n}\n\n// section 5\nfunction reward (S, A, R, _S) {\n var mr = Math.max(Q[_S][0], Q[_S][1]);\n Q[S][A] = Q[S][A]*(1 - alpha) + alpha*(R + beta*mr);\n}","created_at":"2018-05-17T13:08:51.326+08:00","updated_at":"2024-01-15T23:19:33.522+08:00","name":"flappy bird Q-learning(T)","language":"javascript","screenshot":{"url":"https://cdn4.koding.school/uploads/project/screenshot/28928/d35d30b8bc305a6d494c2fe261fde658.jpg"},"parent_id":28293,"plugin":"setBackdrop('bg.png');\n\nfunction Brain () {\n var w = 16;\n var h = 32;\n var obj = {};\n obj['undefined'] = [0, 0];\n for (var x = 0; x \u003c w; x++) {\n for (var y=-h; y \u003c h; y++) {\n obj[x + ':' + y] = [0, 0];\n }\n }\n return obj;\n}\n\nvar game = (() =\u003e {\n\n let points = 0;\n let speed = 3;\n\n let gravity = 0.25;\n let gameStatus = 'playing';\n\n let pairA = {\n x: 700*1/3 + 200, y: 240\n };\n let pairB = {\n x: 700*2/3 + 200, y: 240\n };\n let pairC = {\n x: 700*3/3 + 200, y: 240\n };\n\n let pipes = [pairA, pairB, pairC];\n\n let pipe1 = createSprite('pipe.png');\n pipe1.direction = 90;\n let pipe2 = createSprite('pipe.png');\n pipe2.direction = -90;\n let pipe3 = createSprite('pipe.png');\n pipe3.direction = 90;\n let pipe4 = createSprite('pipe.png');\n pipe4.direction = -90;\n let pipe5 = createSprite('pipe.png');\n pipe5.direction = 90;\n let pipe6 = createSprite('pipe.png');\n pipe6.direction = -90;\n\n let food = createSprite('food.png');\n\n let ground1 = createSprite('ground.png');\n ground1.y = 450;\n let ground2 = createSprite('ground.png');\n ground2.y = 450;\n ground2.x += 640;\n\n let bird = createSprite('bird.png');\n bird.x = 200;\n bird.vy = 0;\n bird.scale = 0.8;\n\n bird.on('touch', [pipe1, pipe2, pipe3, pipe4, pipe5, pipe6, ground1, ground2], () =\u003e gameStatus = 'stop');\n\n forever(()=\u003e {\n\n game.gameStatus = gameStatus; // suck\n\n pipe1.moveTo(pairA);\n pipe1.y -= 270;\n pipe1.x -= 20;\n pipe2.moveTo(pairA);\n pipe2.y += 270;\n pipe2.x -= 20;\n\n pipe3.moveTo(pairB);\n pipe3.y -= 270;\n pipe3.x -= 20;\n pipe4.moveTo(pairB);\n pipe4.y += 270;\n pipe4.x -= 20;\n\n pipe5.moveTo(pairC);\n pipe5.y -= 270;\n pipe5.x -= 20;\n pipe6.moveTo(pairC);\n pipe6.y += 270;\n pipe6.x -= 20;\n\n if (gameStatus === 'playing') {\n\n bird.vy += gravity;\n bird.y += bird.vy;\n\n pairA.x -= speed;\n pairB.x -= speed;\n pairC.x -= speed;\n\n ground1.x -= speed;\n ground2.x -= speed;\n if (ground1.x \u003c -320) ground1.x += 1280;\n if (ground2.x \u003c -320) ground2.x += 1280;\n\n if (pairA.x \u003c 0) {\n pairA.x += 700;\n pairA.y = Math.random() * 150 + 110\n }\n if (pairB.x \u003c 0) {\n pairB.x += 700;\n pairB.y = Math.random() * 150 + 110\n }\n if (pairC.x \u003c 0) {\n pairC.x += 700;\n pairC.y = Math.random() * 150 + 110\n }\n\n if ((pairA.x \u003e= bird.x - speed \u0026\u0026 pairA.x \u003c bird.x) ||\n (pairB.x \u003e= bird.x - speed \u0026\u0026 pairB.x \u003c bird.x) ||\n (pairC.x \u003e= bird.x - speed \u0026\u0026 pairC.x \u003c bird.x)) {\n game.getPoint = true;\n points++;\n } else {\n game.getPoint = false;\n }\n if (food == undefined) {\n food = createSprite('food.png');\n }\n }\n\n print(points, 10, 10, 'white', 45);\n });\n\n function jump() {\n bird.vy = -4.5;\n }\n\n on('click', jump);\n\n function restart() {\n bird.y = 240;\n bird.vy = 0;\n pairA.x = 700*1/3 + 200;\n pairA.y = Math.random() * 150 + 110\n pairB.x = 700*2/3 + 200;\n pairB.y = Math.random() * 150 + 110\n pairC.x = 700*3/3 + 200;\n pairC.y = Math.random() * 150 + 110\n points = 0;\n ground1.x = 320;\n ground2.x = 320 + 640;\n gameStatus = 'playing';\n }\n\n function getFood () {\n var f = pipes.filter((p) =\u003e {\n return p.x \u003e game.bird.x;\n }).sort((p1, p2) =\u003e {\n return p1.x - p2.x;\n })[0];\n food.x = f.x-10;\n food.y = f.y;\n return f;\n }\n\n return {\n restart: restart,\n jump: jump,\n bird: bird,\n getFood: getFood\n }\n})();","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":317,"hashid":"jzms9k3p","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":535152,"file_name":"food.png","project_id":28928,"asset_id":63914,"created_at":"2018-05-17T13:08:51.334+08:00","updated_at":"2018-05-17T13:08:51.334+08:00"},{"id":535153,"file_name":"bg.png","project_id":28928,"asset_id":57149,"created_at":"2018-05-17T13:08:51.336+08:00","updated_at":"2018-05-17T13:08:51.336+08:00"},{"id":535154,"file_name":"bird.png","project_id":28928,"asset_id":57150,"created_at":"2018-05-17T13:08:51.338+08:00","updated_at":"2018-05-17T13:08:51.338+08:00"},{"id":535155,"file_name":"ground.png","project_id":28928,"asset_id":57151,"created_at":"2018-05-17T13:08:51.340+08:00","updated_at":"2018-05-17T13:08:51.340+08:00"},{"id":535156,"file_name":"pipe.png","project_id":28928,"asset_id":57152,"created_at":"2018-05-17T13:08:51.341+08:00","updated_at":"2018-05-17T13:08:51.341+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦