{"id":33663,"student_id":1486,"content":"// noprotect\n\nsetBackdrop(\"bg.jpg\");\n\nvar spikes = [];\n\nvar topSpike = createSprite(\"topSpike.png\");\n topSpike.y = 35;\n\nvar downSpike = createSprite(\"downSpike.png\");\n downSpike.y = 445;\n\nvar bird = createSprite(\"bird.png\");\n bird.rotationStyle = \"flipped\";\n var vy = 0;\n var score = 0;\n\nvar leftApple = createSprite(\"apple.png\");\n leftApple.hidden = true;\n leftApple.scale = 1;\nvar rightApple = createSprite(\"apple.png\");\n rightApple.hidden = true;\n rightApple.scale = 1;\n\nvar firstSpikeY = 65;\nvar leftSpikes = [];\nvar rightSpikes = [];\nvar confilictor = [];\n\nvar Q = new Brain(); //大腦\nvar S; //上一次的狀態\nvar A; //上一次的行為\nvar _S; //這一次的狀態\nvar alpha = 0.5; //學習速率\nvar beta = 0.9; //獎勵延遲\n\nfunction Brain (){\n var obj = {};\n var w = 640/8;\n var h = 480/8;\n obj['undefined'] = [0, 0];\n for (var x = -w-1; x \u003c w+1; x++) {\n for (var y= -h-1; y \u003c h+1; y++) {\n for(var a = 0; a \u003c 6; a++){\n obj[x + ':' + y + ':' + a] = [0, 0];\n }\n }\n }\n return obj;\n}\n\nfunction restart(){\n bird.x = 320;\n bird.y = 240;\n bird.direction = 90;\n vy = -6;\n score = 0;\n for(var i in leftSpikes){\n leftSpikes[i].hidden = true;\n }\n for(var j in rightSpikes){\n rightSpikes[j].hidden = true;\n }\n showRightSpikes();\n}\n\n\nfunction getStatus (){\n var apple;\n if(bird.direction == 90){\n apple = rightApple;\n }else{\n apple = leftApple;\n }\n return (Math.floor(apple.x/8) - Math.floor(bird.x/8)) + \":\" + (Math.floor(apple.y/6) - Math.floor(bird.y/6) + ':' + ((apple.y-65)/50-1));\n}\n\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}\n\n\n\nfunction jump(){\n vy = -6;\n}\n\nfunction showLeftSpikes(){\n for(var i in rightSpikes){\n rightSpikes[i].hidden = true;\n }\n rightApple.hidden = true;\n var space = Math.floor(Math.random()*5);\n for(var j in leftSpikes){\n if(j != space){ \n leftSpikes[j].hidden = false;\n }else{\n leftApple.moveTo(leftSpikes[j]);\n leftApple.x += 10;\n leftApple.hidden = false;\n }\n }\n}\n\nfunction showRightSpikes(){\n for(var i in leftSpikes){\n leftSpikes[i].hidden = true;\n }\n leftApple.hidden = true;\n var space1 = Math.floor(Math.random()*5);\n for(var j in rightSpikes){\n if(j != space1){ \n rightSpikes[j].hidden = false;\n }else{\n rightApple.moveTo(rightSpikes[j]);\n rightApple.x -= 10;\n rightApple.hidden = false;\n }\n }\n}\n\nwhen(\"click\", function(){\n jump();\n});\n\nfor(var i = 0; i \u003c 8; i++){\n var leftSpike = createSprite({\n x: 185,\n y: firstSpikeY + i*50,\n hidden: true,\n costumes: \"leftSpike.png\"\n });\n \n var rightSpike = createSprite({\n x: 455,\n y: firstSpikeY + i*50,\n hidden: true,\n costumes: \"rightSpike.png\"\n });\n if(i !== 0 \u0026\u0026 i != 7){\n leftSpikes.push(leftSpike);\n rightSpikes.push(rightSpike);\n }else{\n leftSpike.hidden = false;\n rightSpike.hidden = false;\n confilictor.push(leftSpike);\n confilictor.push(rightSpike);\n }\n}\n\nshowRightSpikes();\n\nforever(function(){\n _S = getStatus();\n print(_S, 100, 10, 'white', 30);\n \n if((bird.touched(leftApple) || bird.touched(rightApple) || bird.x \u003c 175 || bird.x \u003e 465) \u0026\u0026 !bird.touched(topSpike) \u0026\u0026 !bird.touched(downSpike) \u0026\u0026 !bird.touched(leftSpikes) \u0026\u0026 !bird.touched(rightSpikes) \u0026\u0026 !bird.touched(confilictor)){\n score++;\n reward(S, A, 50, _S);\n if(bird.direction == 90){\n showLeftSpikes();\n bird.direction += 180;\n vy = -0;\n }else{\n showRightSpikes();\n bird.direction += 180;\n vy = -0;\n }\n }else if(bird.touched(topSpike) || bird.touched(downSpike) || bird.touched(leftSpikes) || bird.touched(rightSpikes) || bird.touched(confilictor)){\n restart();\n reward(S, A, -1000, _S);\n }else{\n reward(S, A, 1, _S);\n }\n \n if(Q[_S][0] \u003c Q[_S][1]){\n A = 1;\n jump();\n }else{\n A=0;\n }\n S=_S;\n \n bird.stepForward(3);\n bird.y += vy;\n vy += 0.4;\n \n if(score \u003c 10){\n print(score, 300, 205, '#deebef', 60);\n }else{\n print(score, 285, 205, '#deebef', 60);\n }\n});\n","created_at":"2018-07-04T21:17:19.408+08:00","updated_at":"2019-11-08T14:22:18.194+08:00","name":"快樂鳥與邪惡刺 Q-Learning","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/33663/05473c6bf9e8a9de807959b10a1408c1.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":279,"hashid":"zpesz25d","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":621698,"file_name":"downSpike.png","project_id":33663,"asset_id":25087,"created_at":"2018-07-04T21:17:29.961+08:00","updated_at":"2018-07-04T21:17:29.961+08:00"},{"id":621699,"file_name":"leftSpike.png","project_id":33663,"asset_id":25077,"created_at":"2018-07-04T21:17:29.964+08:00","updated_at":"2018-07-04T21:17:29.964+08:00"},{"id":621700,"file_name":"rightSpike.png","project_id":33663,"asset_id":25076,"created_at":"2018-07-04T21:17:30.420+08:00","updated_at":"2018-07-04T21:17:30.420+08:00"},{"id":621696,"file_name":"bg.jpg","project_id":33663,"asset_id":25073,"created_at":"2018-07-04T21:17:29.490+08:00","updated_at":"2018-07-04T21:17:29.490+08:00"},{"id":621697,"file_name":"bird.png","project_id":33663,"asset_id":28839,"created_at":"2018-07-04T21:17:29.492+08:00","updated_at":"2018-07-04T21:17:29.492+08:00"},{"id":621701,"file_name":"topSpike.png","project_id":33663,"asset_id":25088,"created_at":"2018-07-04T21:17:30.423+08:00","updated_at":"2018-07-04T21:17:30.423+08:00"},{"id":621702,"file_name":"apple.png","project_id":33663,"asset_id":63917,"created_at":"2018-07-04T21:28:19.696+08:00","updated_at":"2018-07-04T21:28:19.696+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦