{"id":28969,"student_id":10,"content":"var btn1 = createSprite(\"btn.png\");\nbtn1.x = 50;\nbtn1.y = 100;\nbtn1.scale = 0.3;\nbtn1.disable = false;\n\nvar btn2 = createSprite(\"btn.png\");\nbtn2.x = 50;\nbtn2.y = 200;\nbtn2.scale = 0.3;\nbtn2.disable = true;\n\nvar btn3 = createSprite(\"btn.png\");\nbtn3.x = 50;\nbtn3.y = 300;\nbtn3.scale = 0.3;\nbtn3.disable = true;\n\nvar btn4 = createSprite(\"btn.png\");\nbtn4.x = 50;\nbtn4.y = 400;\nbtn4.scale = 0.3;\n\nbtn1.on(\"click\", move);\nbtn2.on(\"click\", calc);\nbtn3.on(\"click\", update);\nbtn4.on(\"click\", () =\u003e sw = !sw);\n\nvar 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\nvar Q = {};\nfor (var x = 0; x \u003c 10; x++) {\n for (var y = 0; y \u003c 10; y++) {\n Q[x+':'+y] = [0, 0, 0, 0];\n }\n}\n\nvar w = 70;\nvar player = { x: 1, y: 1 };\nvar sw = 0;\nvar S = '1:1';\nvar A;\nvar _S;\nvar alpha = 0.5;\nvar beta = 0.1;\nvar re = '';\nvar formula = '';\n\nvar clock = 0;\nsetInterval(function() {\n clock++;\n if (sw) {\n if (clock%3 == 0) move();\n if (clock%3 == 1) calc();\n if (clock%3 == 2) update();\n }\n}, 30);\n\n\nfunction move () {\n if (btn1.disable) return;\n \n var block = map[player.y][player.x];\n S = player.x + ':' + player.y;\n\n if (block == 1 || block == 2) {\n player.x = 1;\n player.y = 1;\n } else {\n A = Q[S].indexOf(Math.max(...Q[S]));\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 btn1.disable = true;\n btn2.disable = false;\n }\n}\n\nfunction calc () {\n if (btn2.disable) return;\n\n var block = map[player.y][player.x];\n _S = player.x + ':' + player.y;\n if (block == 0) re = reward(S, A, -10, _S); // 白色\n if (block == 1) re = reward(S, A, -100, _S); // 黑洞\n if (block == 2) re = reward(S, A, +500, _S); // 寶藏\n \n var r = block == 0 ? '-10' : block == 1 ? '-100' : '+500';\n formula = `Q[${S}] = (${Q[S][A]} + (${r} + Max([${Q[_S].toString()}])*0.1)/2`\n \n btn2.disable = true;\n btn3.disable = false;\n}\n\nfunction update () {\n if (btn3.disable) return;\n\n Q[S][A] = re;\n btn3.disable = true;\n btn1.disable = false;\n}\n\nfunction reward (S, A, R, _S) {\n var mr = Math.max(...Q[_S]);\n return Math.round(Q[S][A]*(1 - alpha) + alpha*(R + beta*mr));\n}\n\n// 繪圖\nforever(() =\u003e {\n \n btn1.opacity = btn1.disable ? 0.5 : 1;\n btn2.opacity = btn2.disable ? 0.5 : 1;\n btn3.opacity = btn3.disable ? 0.5 : 1;\n\n for (var x = 0; x \u003c 6; x++) {\n for (var y = 0; y \u003c 6; y++) {\n pen.fillColor = 'black';\n\n if (map[y][x] == 0) pen.fillColor = 'white';\n if (map[y][x] == 1) pen.fillColor = 'black';\n if (map[y][x] == 2) pen.fillColor = 'red';\n pen.drawRect(110+x*w, 60+y*w, w-5, w-5);\n \n var s = Q[x+':'+y];\n if (map[x][y] == 0) {\n print(s[0], 110+x*w, 60+y*w+22, \"black\", 12);\n print(s[1], 110+x*w+17, 60+y*w+45, \"black\", 12);\n print(s[2], 110+x*w+30, 60+y*w+22, \"black\", 12);\n print(s[3], 110+x*w+17, 60+y*w, \"black\", 12); \n }\n }\n }\n pen.fillColor = 'orange';\n pen.drawRect(110+player.x*w, 60+player.y*w, w-5, w-5);\n print(formula, 20, 20, 'black', 20);\n});","created_at":"2018-05-18T18:07:28.827+08:00","updated_at":"2019-10-19T21:55:10.288+08:00","name":"Q-learning","language":"javascript","screenshot":{"url":"/uploads/project/screenshot/28969/cd0f4788569754e6917dd57af8388304.jpg"},"parent_id":28963,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":503,"hashid":"kdms8jpd","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":536978,"file_name":"btn.png","project_id":28969,"asset_id":65588,"created_at":"2018-05-18T19:34:30.911+08:00","updated_at":"2018-05-18T19:35:10.593+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