{"id":24474,"student_id":10,"content":"let map = [];\nfor (var x=0; x\u003c16; x++) {\n map[x] = [];\n for (var y=0; y\u003c12; y++) {\n if (x == 0 || y == 0 || x == 15 || y == 11) map[x][y] = 1;\n else map[x][y] = Math.random()\u003c0.3 ? 1 : 0;\n }\n}\nmap[14][10] = 2;\n\nlet w = 40;\nlet player = {x: 1, y: 1};\n\nlet Q = [];\nlet S = '1:1';\nlet A;\nlet _S;\n\nlet alpha = 0.7;\nlet beta = .8;\n\nlet i=0;\nforever(function () {\n i++;\n if (i%2 == 0) update();\n});\n\nfunction update () {\n \n _S = getStatus(player);\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.x][player.y];\n if (c == 0) {\n Q = reward(Q, S, A, -5, _S);\n } \n if (c == 1) {\n Q = reward(Q, S, A, -500, _S);\n player.x = 1;\n player.y = 1;\n }\n if (c == 2) {\n Q = reward(Q, S, A, +100000, _S);\n player.x = 1;\n player.y = 1;\n }\n\n S = _S;\n A = Q[S].indexOf(Math.max(...Q[S]));\n // if (Math.random() \u003c 0.1) A = Math.floor(Math.random()*4);\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\nfunction reward (Q, S, A, R, _S) {\n let mr = Math.max(...Q[_S]);\n Q[S][A] = Q[S][A]*(1 - alpha) + alpha*(R + beta*mr); \n return Q;\n}\n\nfunction getStatus (player) {\n return player.x + ':' + player.y;\n}\n\nforever(() =\u003e {\n\n // 畫地圖\n for(let x=0; x\u003c16; x++) {\n for(let y=0; y\u003c12; y++) {\n if (map[x][y] == 1) pen.fillColor = 'black';\n else if (map[x][y] == 2) pen.fillColor = 'red';\n else pen.fillColor = '#eee';\n pen.drawRect(x*w, y*w, w-5, w-5);\n }\n }\n\n // 畫角色\n pen.fillColor = 'orange';\n pen.drawRect(player.x*w, player.y*w, w-5, w-5);\n\n});","created_at":"2018-03-15T14:03:50.772+08:00","updated_at":"2019-11-12T00:08:36.355+08:00","name":"Q-learning 迷宮","language":"javascript","screenshot":{"url":"/uploads/project/screenshot/24474/865f87fbd0983b0a51356ac7d8059359.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":551,"hashid":"qmds34er","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":463199,"file_name":"0.png","project_id":24474,"asset_id":6,"created_at":"2018-03-15T14:03:50.792+08:00","updated_at":"2018-03-15T14:03:50.792+08:00"},{"id":463200,"file_name":"1.svg","project_id":24474,"asset_id":7,"created_at":"2018-03-15T14:03:50.796+08:00","updated_at":"2018-03-15T14:03:50.796+08:00"},{"id":463201,"file_name":"2.png","project_id":24474,"asset_id":8,"created_at":"2018-03-15T14:03:50.801+08:00","updated_at":"2018-03-15T14:03:50.801+08:00"},{"id":463202,"file_name":"0.wav","project_id":24474,"asset_id":9,"created_at":"2018-03-15T14:03:50.806+08:00","updated_at":"2018-03-15T14:03:50.806+08:00"},{"id":463203,"file_name":"1.wav","project_id":24474,"asset_id":10,"created_at":"2018-03-15T14:03:50.810+08:00","updated_at":"2018-03-15T14:03:50.810+08:00"}]
Orange Apple Learning Platform
My Projects
View Project Page
Export
Remix
Import
Delete
Download Android APP (APK)
Screenshot
繁中
简中
English
日本語
1:1:1
1:1
Full Width
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer