{"id":165322,"student_id":2589,"content":"grid[4] = 'o';\n\nfunction AI (grid) {\n var best;\n var bestScore = 0;\n for (var i = 0; i \u003c 9; i++) {\n if (grid[i] === '') {\n grid[i] = 'x';\n score = evaluation(grid);\n grid[i] = 'o';\n score += evaluation(grid);\n if (score \u003e= bestScore) {\n best = i;\n bestScore = score;\n }\n grid[i] = '';\n }\n }\n grid[best] = 'o';\n}\n\n//顯示分數\nforever(function () {\n for (var i=0; i\u003c9; i++) {\n if (grid[i] == '') {\n grid[i] = 'x';\n var score = evaluation(grid);\n grid[i] = 'o';\n score += evaluation(grid);\n grid[i] = '';\n showScore(score, i);\n }\n }\n});\n\n//評估函式\nfunction evaluation (grid) {\n var a = getScore(grid[0], grid[1], grid[2]);\n var b = getScore(grid[3], grid[4], grid[5]);\n var c = getScore(grid[6], grid[7], grid[8]);\n var d = getScore(grid[0], grid[3], grid[6]);\n var e = getScore(grid[1], grid[4], grid[7]);\n var f = getScore(grid[2], grid[5], grid[8]);\n var g = getScore(grid[0], grid[4], grid[8]);\n var h = getScore(grid[2], grid[4], grid[6]);\n return a + b + c + d + e + f + g + h;\n}\n\n//計算某一條線的分數\nfunction getScore (a, b, c) {\n var line = a + b + c;\n if (line == 'ooo') return 100;\n if (line == 'xxx') return 100;\n if (line == 'oo') return 10;\n if (line == 'xx') return 10;\n if (line == 'o') return 1;\n if (line == 'x') return 1;\n return 0; // s == ''\n}","created_at":"2020-07-14T00:46:15.957+08:00","updated_at":"2020-07-14T00:53:24.791+08:00","name":"井字遊戲AI(完整版)2","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/165322/79f6f4ba884bfb7736250af8b74558da.jpg"},"parent_id":2,"plugin":"Game.set({width: 1200, height: 900});\n\nsetBackdrop('#f8e8cc');\n\nvar scores = [0, 0, 0, 0, 0, 0, 0, 0, 0];\nvar grid = ['', '', '', '', '', '', '', '', ''];\nvar offsetX = 240;\nvar offsetY = 90;\nvar width = 240;\nvar turn = 'o';\n\nforever(loop);\nwhen('click', onclick);\n\nfunction check () {\n var g = grid;\n if (g[0] == g[1] \u0026\u0026 g[1] == g[2]) return g[0];\n if (g[3] == g[4] \u0026\u0026 g[4] == g[5]) return g[3];\n if (g[6] == g[7] \u0026\u0026 g[7] == g[8]) return g[6];\n if (g[0] == g[3] \u0026\u0026 g[3] == g[6]) return g[0];\n if (g[1] == g[4] \u0026\u0026 g[4] == g[7]) return g[1];\n if (g[2] == g[5] \u0026\u0026 g[5] == g[8]) return g[2];\n if (g[0] == g[4] \u0026\u0026 g[4] == g[8]) return g[0];\n if (g[2] == g[4] \u0026\u0026 g[4] == g[6]) return g[2];\n return false;\n}\n\nfunction onclick () {\n var x = Math.floor((cursor.x - offsetX)/width);\n var y = Math.floor((cursor.y - offsetY)/width);\n var i = y*3 + x;\n if (x \u003e 2 || y \u003e 2 || x \u003c 0 || y \u003c 0) return;\n if (grid[i] !== '') return;\n var olen = grid.filter(item =\u003e item == 'o').length;\n var xlen = grid.filter(item =\u003e item == 'x').length;\n grid[i] = xlen \u003e= olen ? 'o' : 'x';\n AI(grid);\n}\n\nfunction loop () {\n var winner = check();\n if (winner) {\n text = winner + ' 獲勝!';\n stop();\n } else if (grid.indexOf('') === -1) {\n text = '平手';\n stop();\n } else {\n text = 'playing';\n }\n drawText(text , 500, 20, 'black', 45, 'Comic Sans MS');\n \n for (var i=0; i \u003c 9; i++) {\n var x = i%3;\n var y = (i - x)/3;\n var xx = x*width + offsetX;\n var yy = y*width + offsetY;\n var color = grid[i] == 'o' ? '#ff595a' : '#4a86e8';\n \n drawText(grid[i], xx + 40, yy, color, 240, 'Comic Sans MS');\n \n pen.size = 30;\n pen.color = '#222';\n pen.fillColor = '#fff';\n pen.drawRect(xx, yy, 240, 240);\n }\n}\n\nfunction showScore (score, i) {\n var x = i%3;\n var y = (i - x)/3;\n var xx = x*width + offsetX + 20;\n var yy = y*width + offsetY + 20;\n print(score, xx, yy, 'black', 60, 'Comic Sans MS');\n}\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":93,"hashid":"wdksrryg","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":2873888,"file_name":"Gymnopedie_No_1.mp3","project_id":165322,"asset_id":182919,"created_at":"2020-07-14T00:46:15.963+08:00","updated_at":"2020-07-14T00:46:15.963+08:00"},{"id":2873889,"file_name":"koding.png","project_id":165322,"asset_id":182918,"created_at":"2020-07-14T00:46:15.964+08:00","updated_at":"2020-07-14T00:46:15.964+08:00"},{"id":2873890,"file_name":"space_bg.jpg","project_id":165322,"asset_id":182917,"created_at":"2020-07-14T00:46:15.965+08:00","updated_at":"2020-07-14T00:46:15.965+08:00"},{"id":2873891,"file_name":"earth.png","project_id":165322,"asset_id":182916,"created_at":"2020-07-14T00:46:15.966+08:00","updated_at":"2020-07-14T00:46:15.966+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦