{"id":28963,"student_id":2591,"content":"let map = [\n [1, 1, 1, 1, 1, 1],\n [1, 0, 0, 0, 0, 1],\n [1, 0, 0, 1, 0, 1],\n [1, 0, 1, 2, 0, 1],\n [1, 0, 0, 0, 0, 1],\n [1, 1, 1, 1, 1, 1]\n];\n\nlet w = 60; // 繪圖的寬度\nlet player = {\n x: 1, y: 1\n};\nlet sw = 0;\nlet Q = {};\nlet S = '1:1';\nlet A;\nlet _S;\n\nlet alpha = 0.7;\nlet beta = 0.8;\n\nlet i = 0;\nforever(function () {\n // i++;\n // if (i%2 == 0) update();\n});\n\nfunction update () {\n\n _S = player.x + ':' + player.y\n\n if (Q[S] === undefined) Q[S] = [0, 0, 0, 0];\n if (Q[_S] === undefined) Q[_S] = [0, 0, 0, 0];\n \n let c = map[player.y][player.x];\n\n if (c === 0) Q = reward(Q, S, A, -100, _S); // 白色\n if (c == 1) Q = reward(Q, S, A, -1000, _S); // 黑洞\n if (c == 2) Q = reward(Q, S, A, +3000, _S); // 寶藏\n\n // 撞到黑洞或寶藏就重新開始\n if (c == 1 || c == 2) {\n player.x = 1;\n player.y = 1;\n } else {\n S = _S;\n A = Q[S].indexOf(Math.max(...Q[S]));\n console.log(A+\":\"+player.x+\":\"+player.y);\n if (A == 3) player.y -= 1; // 上\n if (A == 2) player.x += 1; // 右\n if (A == 1) player.y += 1; // 下\n if (A === 0) player.x -= 1; // 左\n }\n}\n\nfunction reward (Q, S, A, R, _S) {\n let mr = Math.max(...Q[_S]);\n Q[S][A] = Math.round(Q[S][A]*(1 - alpha) + alpha*(R + beta*mr));\n return Q;\n}\n\n// 繪圖\nforever(() =\u003e {\n for (let x = 0; x \u003c 7; x++) {\n for (let y = 0; y \u003c 7; y++) {\n pen.fillColor = 'black';\n \n if (x \u003c= 5 \u0026\u0026 y \u003c= 5) {\n if (map[y][x] == 1) pen.fillColor = 'black';\n else if (map[y][x] == 2) pen.fillColor = 'red';\n else pen.fillColor = '#eee';\n pen.drawRect(150+x*w, 60+y*w, w-5, w-5);\n if (Q[x+':'+y]) {\n if (Q[x+':'+y][0] !== undefined) {\n if (Q[x+':'+y][0] != -Infinity) {\n print(Q[x+':'+y][0], 150+x*w, 60+y*w+22, \"black\", 12);\n }\n }\n if (Q[x+':'+y][1] !== undefined) {\n if (Q[x+':'+y][1] != -Infinity) {\n print(Q[x+':'+y][1], 150+x*w+17, 60+y*w+45, \"black\", 12);\n }\n }\n if (Q[x+':'+y][2] !== undefined) {\n if (Q[x+':'+y][2] != -Infinity) {\n print(Q[x+':'+y][2], 150+x*w+30, 60+y*w+22, \"black\", 12);\n }\n }\n if (Q[x+':'+y][3] !== undefined) {\n if (Q[x+':'+y][3] != -Infinity) {\n print(Q[x+':'+y][3], 150+x*w+17, 60+y*w, \"black\", 12);\n }\n }\n }\n }\n }\n }\n pen.fillColor = 'orange';\n pen.drawRect(150+player.x*w, 60+player.y*w, w-5, w-5);\n});\n\nsetInterval(function() {\n if (sw)\n update();\n}, 50);\n\nwhen('keydown', \"space\", function() {\n sw = !sw\n});\nwhen(\"mousedown\", function() {\n if (!sw) {\n update();\n }\n});\nfunction GetRound(num, len) {\n return Math.round(num * Math.pow(10, len)) / Math.pow(10, len);\n}","created_at":"2018-05-18T17:48:07.916+08:00","updated_at":"2019-10-30T16:19:33.845+08:00","name":"Q-learning 迷宮","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/28963/23805f03ccff22654b03cb17c0dd5f4a.jpg"},"parent_id":28948,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":133,"hashid":"5j3sj3w8","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":535804,"file_name":"1.wav","project_id":28963,"asset_id":10,"created_at":"2018-05-18T17:48:07.924+08:00","updated_at":"2018-05-18T17:48:07.924+08:00"},{"id":535805,"file_name":"0.wav","project_id":28963,"asset_id":9,"created_at":"2018-05-18T17:48:07.925+08:00","updated_at":"2018-05-18T17:48:07.925+08:00"},{"id":535806,"file_name":"2.png","project_id":28963,"asset_id":8,"created_at":"2018-05-18T17:48:07.927+08:00","updated_at":"2018-05-18T17:48:07.927+08:00"},{"id":535807,"file_name":"1.svg","project_id":28963,"asset_id":7,"created_at":"2018-05-18T17:48:07.928+08:00","updated_at":"2018-05-18T17:48:07.928+08:00"},{"id":535808,"file_name":"0.png","project_id":28963,"asset_id":6,"created_at":"2018-05-18T17:48:07.929+08:00","updated_at":"2018-05-18T17:48:07.929+08:00"}]
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
繁中
简中
English
日本語
1:1:1
1:1
全寬
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦