{"id":193343,"student_id":1909,"content":"var grid = []\n//產生牆壁或通道\nfor (var i = 0; i \u003c 12; i++) {\n line = \"\"\n if (i == 0 || i == 11){\n line = \"****************\"\n }else{\n for (var j = 0; j \u003c 16; j++) {\n if(j == 0 || j == 15){\n line += \"*\"\n }else if(i == 1 \u0026\u0026 j == 1){\n line += \" \"\n }else{\n line += Math.random()\u003e0.5 ? '*' : ' '\n }\n } \n }\n grid.push(line)\n}\n//隨機選一個位置放起司\nwhile(true){\n var x = Math.floor(Math.random() * 14 + 1)\n var y = Math.floor(Math.random() * 10 + 1)\n if (grid[y][x] == ' '){\n grid[y] = grid[y].substring(0,x)+'C'+grid[y].substring(x+1)\n break\n }\n}\ninit(grid);\n// var grid = []\n// //產生牆壁或通道\n// for (var i = 0; i \u003c 12; i++) {\n// line = \"\"\n// if (i == 0 || i == 11){\n// line = \"****************\"\n// }else{\n// for (var j = 0; j \u003c 16; j++) {\n// if(j == 0 || j == 15){\n// line += \"*\"\n// }else if(i == 1 \u0026\u0026 j == 1){\n// line += \" \"\n// }else{\n// line += Math.random()\u003e0.7 ? '*' : ' '\n// }\n// } \n// }\n// grid.push(line)\n// }\n// //隨機選一個位置放起司\n// while(true){\n// var x = Math.floor(Math.random() * 14 + 10)\n// var y = Math.floor(Math.random() * 10 + 6)\n// if (grid[y][x] == ' '){\n// grid[y] = grid[y].substring(0,x)+'C'+grid[y].substring(x+1)\n// break\n// }\n// }\n// init(grid);\n\n\n\n\n\n\n\n\nvar initX = 1\nvar initY = 1\nplayer.reset(initX,initY); // 初始化老鼠位置\n\nvar Q = {}; //老鼠的大腦\nvar S1; //上一次的狀態(位置),格式\"x,y\"\nvar S2; //當前的狀態(位置),格式\"x,y\"\nvar A; //上一次的動作(移動方向)\nvar alpha = 0.8; // 學習速率\nvar beta = 0.5; // 衰減值\n\nsetInterval(train,50)\nfunction train(){\n S1 = player.x+\",\"+player.y;\n if (Q[S1] == undefined) {\n Q[S1] = [0,0,0,0]\n }\n A = findMaxIndex(Q[S1])\n if (A==0) {\n player.x-=1\n }else if (A== 1) {\n player.x+=1\n } else if(A==2){\n player.y-=1\n }else if (A==3){\n player.y+=1\n }\n \n S2 = player.x+\",\"+player.y;\n if (Q[S2] == undefined) {\n Q[S2] = [0,0,0,0]\n }\n if (grid[player.y][player.x] == \"*\") {\n reward(Q,S1,A,S2,-100)\n player.reset(initX,initY);\n }\n if (grid[player.y][player.x] == \"C\") {\n reward(Q,S1,A,S2,10)\n player.reset(initX,initY);\n }\n if (grid[player.y][player.x] == \" \") {\n reward(Q,S1,A,S2,-1)\n \n }\n}\nfunction findMaxIndex(ls){\n var maxIndex = -1\n var maxNum = -Infinity\n for (var i = 0; i \u003c ls.length; i++) {\n if (ls[i]\u003emaxNum) {\n maxNum = ls[i]\n maxIndex = i\n }\n }\n return maxIndex\n\n \n}\n\n// 懲罰獎勵\nfunction reward (Q, S1, A, S2, score) {\n Q[S1][A] = alpha*Q[S1][A]+(1-alpha)*(score+beta*Math.max(...Q[S2]))\n}","created_at":"2020-09-19T16:36:48.543+08:00","updated_at":"2020-10-31T16:55:49.855+08:00","name":"老鼠找起司(2) 副本","language":"javascript","screenshot":{"url":"https://cdn5.koding.school/uploads/project/screenshot/193343/89edcce60b8890776f43b51e69f48ea4.jpg"},"parent_id":65017,"plugin":"var rat = createSprite('mouse.png');;\nvar cheese = createSprite('cheese.png');\nvar W;\nvar H;\nvar width = 40;\n\nfunction init (grid) {\n\n H = grid.length;\n W = grid[0].length;\n\n for (var y = 0; y \u003c H; y++) {\n for (var x = 0; x \u003c W; x++) {\n if (grid[y][x] == 'C') {\n cheese.moveTo(x*width + width/2, y*width + width/2);\n }\n }\n }\n\n forever(function() {\n for (var y = 0; y \u003c H; y++) {\n for (var x = 0; x \u003c W; x++) {\n pen.fillColor = (grid[y][x] == '*' ? 'black' : 'white')\n pen.drawRect(x*width, y*width, width, width);\n }\n }\n });\n}\n\nfunction randomGrid (col, row, rate) {\n var col = col \u003c 16 ? col : 16;\n var row = row \u003c 12 ? row : 12;\n var grid = [];\n for (var y = 0; y \u003c row - 2; y++) {\n grid[y] = '';\n for (var x = 0; x \u003c col - 2 ; x++) {\n if (x == col - 3 \u0026\u0026 y == row - 3) grid[y] += 'C';\n else grid[y] += Math.random() \u003c rate ? '*' : ' ';\n }\n grid[y] = '*' + grid[y] + '*';\n }\n var s = Array(col).fill('*').join('');\n grid.push(s);\n grid.unshift(s);\n return grid;\n}\n\nvar player = {\n x: 1,\n y: 1,\n reset: function (x, y) {\n this.x = x;\n this.y = y;\n }\n}\n\nforever(function () {\n rat.moveTo(player.x*width + width/2, player.y*width + width/2);\n})\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":207,"hashid":"yeyse629","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3305509,"file_name":"mouse.png","project_id":193343,"asset_id":117128,"created_at":"2020-09-19T16:36:48.548+08:00","updated_at":"2020-09-19T16:36:48.548+08:00"},{"id":3305510,"file_name":"cheese.png","project_id":193343,"asset_id":117129,"created_at":"2020-09-19T16:36:48.549+08:00","updated_at":"2020-09-19T16:36:48.549+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦