{"id":168530,"student_id":10,"content":"grid[4] = 'o';\n\nforever(loop); //不斷執行遊戲迴圈\n\n//遊戲迴圈中印出格子分數\nfunction loop () {\n for (var i=0; i\u003c9; i++) {\n if (grid[i] == '') {\n grid[i] = 'x';\n var score = evaluate(grid);\n grid[i] = 'o';\n score += evaluate(grid);\n grid[i] = '';\n showScore(score, i);\n }\n }\n}\n\nfunction AI (grid) {\n var bestIndex;\n var bestScore = 0;\n for (var i = 0; i \u003c 9; i++) {\n if (grid[i] == '') {\n grid[i] = 'x';\n score = evaluate(grid);\n grid[i] = 'o';\n score += evaluate(grid);\n if (score \u003e= bestScore) {\n bestIndex = i;\n bestScore = score;\n }\n grid[i] = '';\n }\n }\n grid[bestIndex] = 'o';\n}\n\n//評估函式\nfunction evaluate (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-20T11:47:03.149+08:00","updated_at":"2024-01-16T13:08:19.387+08:00","name":"井字遊戲AI(完整版)","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/168530/d7f72eb85f77b49e3fb4f79f33aa33bb.jpg"},"parent_id":3,"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":234,"hashid":"kdmsky8w","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":2919828,"file_name":"Gymnopedie_No_1.mp3","project_id":168530,"asset_id":239754,"created_at":"2020-07-20T11:47:15.536+08:00","updated_at":"2020-07-20T11:47:15.536+08:00"},{"id":2919829,"file_name":"koding.png","project_id":168530,"asset_id":239755,"created_at":"2020-07-20T11:47:15.544+08:00","updated_at":"2020-07-20T11:47:15.544+08:00"},{"id":2919830,"file_name":"space_bg.jpg","project_id":168530,"asset_id":239756,"created_at":"2020-07-20T11:47:15.548+08:00","updated_at":"2020-07-20T11:47:15.548+08:00"},{"id":2919831,"file_name":"earth.png","project_id":168530,"asset_id":239757,"created_at":"2020-07-20T11:47:15.555+08:00","updated_at":"2020-07-20T11:47:15.555+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦