{"id":28948,"student_id":2591,"content":"let map = [\n [0, 0, 0, 0, 0, 0],\n [0, 0, 0, 0, 1, 0],\n [0, 0, 0, 0, 0, 0],\n [0, 0, 0, 0, 0, 0],\n [1, 0, 1, 0, 0, 0],\n [0, 0, 0, 0, 1, 2]\n];\nlet wallsC = [\n [1, 0, 1, 0, 0, 0, 1],\n [1, 0, 1, 0, 0, 0, 1],\n [1, 1, 0, 1, 0, 1, 1],\n [1, 1, 0, 0, 0, 0, 1],\n [1, 0, 0, 1, 0, 0, 1],\n [1, 0, 0, 0, 0, 0, 1]\n];\nlet wallsR = [\n [1, 1, 1, 1, 1, 1],\n [1, 0, 0, 1, 1, 0],\n [0, 0, 0, 0, 0, 0],\n [0, 1, 1, 1, 0, 1],\n [1, 0, 0, 1, 1, 1],\n [0, 0, 1, 0, 0, 0],\n [1, 1, 1, 1, 1, 1]\n];\nlet w = 60; // 繪圖的寬度\nlet player = {\n x: 0, y: 0\n};\nlet sw = 0;\nlet Q = {};\nlet S = '0:0';\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 if (wallsR[player.y][player.x] == 1) {\n Q[_S][3] = -Infinity;\n }\n if (wallsC[player.y][player.x+1] == 1) {\n Q[_S][2] = -Infinity;\n }\n if (wallsC[player.y][player.x] == 1) {\n Q[_S][0] = -Infinity;\n }\n if (wallsR[player.y+1][player.x] == 1) {\n Q[_S][1] = -Infinity;\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 = 0;\n player.y = 0;\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 if (x \u003c= 6 \u0026\u0026 y \u003c= 5) {\n if (wallsC[y][x] == 1) {\n\n pen.drawRect(150+x*w-5, 60+y*w-5, 5, w+5);\n }\n }\n\n if (y \u003c= 6 \u0026\u0026 x \u003c= 5) {\n if (wallsR[y][x] == 1) {\n pen.drawRect(150+x*w-5, 60+y*w-5, w+5, 5);\n }\n }\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-18T10:55:21.638+08:00","updated_at":"2019-09-28T22:23:36.945+08:00","name":"Q-learning 迷宮","language":"javascript","screenshot":{"url":"https://cdn8.koding.school/uploads/project/screenshot/28948/fe0ce5105f9852b80f326d8e7e97fcef.jpg"},"parent_id":24502,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":135,"hashid":"kdms8jed","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":535594,"file_name":"0.png","project_id":28948,"asset_id":6,"created_at":"2018-05-18T10:55:21.645+08:00","updated_at":"2018-05-18T10:55:21.645+08:00"},{"id":535595,"file_name":"1.svg","project_id":28948,"asset_id":7,"created_at":"2018-05-18T10:55:21.647+08:00","updated_at":"2018-05-18T10:55:21.647+08:00"},{"id":535596,"file_name":"2.png","project_id":28948,"asset_id":8,"created_at":"2018-05-18T10:55:21.649+08:00","updated_at":"2018-05-18T10:55:21.649+08:00"},{"id":535597,"file_name":"0.wav","project_id":28948,"asset_id":9,"created_at":"2018-05-18T10:55:21.651+08:00","updated_at":"2018-05-18T10:55:21.651+08:00"},{"id":535598,"file_name":"1.wav","project_id":28948,"asset_id":10,"created_at":"2018-05-18T10:55:21.653+08:00","updated_at":"2018-05-18T10:55:21.653+08:00"}]
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
繁中
简中
English
日本語
1:1:1
1:1
全寬
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦