{"id":114593,"student_id":37,"content":"function AI (grid) {\n var best;\n var bestScore = 0;\n for (var i = 0; i \u003c 9; i++) {\n var score = 0;\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 bestScore = best\n best = i;\n }\n grid[i] = \"\";\n }\n\n }\n grid[best] = \"X\";\n}\n\n// 印出每個格子的分數\nforever(function() {\n for (var i = 0; i \u003c 9; i++) {\n var score = 0;\n if (grid[i] == \"\") {\n grid[i] = \"X\";\n score += evaluation(grid);\n grid[i] = \"O\";\n score += evaluation(grid);\n showScore(score, i)\n grid[i] = \"\";\n }\n\n }\n});\n\n// 評估函式\nfunction evaluation (g) {\n return getScore(g[0] + g[1] + g[2])+\n getScore(g[3] + g[4] + g[5])+\n getScore(g[6] + g[7] + g[8])+\n getScore(g[0] + g[3] + g[6])+\n getScore(g[1] + g[4] + g[7])+\n getScore(g[2] + g[5] + g[8])+\n getScore(g[0] + g[4] + g[8])+\n getScore(g[2] + g[4] + g[6]);\n}\n\n// 計算某一條線的分數\nfunction getScore (s) {\n if (s == \"XXX\") {\n return 100;\n }\n if (s == \"XX\") {\n return 10;\n }\n if (s == \"X\") {\n return 1;\n }\n if (s == \"OOO\") {\n return 100;\n }\n if (s == \"OO\") {\n return 10;\n }\n if (s == \"O\") {\n return 1;\n }\n\n return 0;\n}","created_at":"2019-12-15T09:55:20.512+08:00","updated_at":"2019-12-15T11:16:57.159+08:00","name":"井字 副本","language":"javascript","screenshot":{"url":"https://cdn8.koding.school/uploads/project/screenshot/114593/89edcce60b8890776f43b51e69f48ea4.jpg"},"parent_id":52799,"plugin":"setBackdrop(\"#333\");\n\nvar turn = 'O';\nvar offsetX = 110;\nvar offsetY = 40;\nvar width = 400/3;\nvar status = 'playing'; // playing, gameover\n\nvar grid = ['', '', '', '', '', '', '', '', ''];\n\nforever(function() {\n\n \n var winner = check();\n if (winner) {\n print(winner + '獲勝!', 10, 10, 'white');\n status = 'gameover';\n } else {\n print('playing', 10, 10, 'white');\n }\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 \n pen.color = '#fff';\n pen.size = 2;\n pen.fillColor = '#222';\n \n pen.drawRect(xx, yy, width, width);\n if (grid[i] === 'O') {\n print(grid[i], xx + 30, yy + 20, 'red', width/1.5);\n }\n if (grid[i] === 'X') {\n print(grid[i], xx + 30, yy + 20, 'blue', width/1.5);\n }\n }\n})\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\n\nwhen('click', function () {\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 if (status != 'playing') return;\n place(turn, i);\n // grid[i] = turn;\n // turn = turn == 'O' ? 'X': 'O';\n\n AI(grid);\n turn = turn == 'O' ? 'X' : 'O';\n\n});\n\nfunction place (who, idx) {\n grid[idx] = who;\n turn = turn == 'O' ? 'X' : 'O';\n}\n\n\nvar scores = [0,0,0,0,0,0,0,0,0];\nfunction showScore (score, i) {\n var x = i%3;\n var y = (i - x)/3;\n print(score, x*width + offsetX, y*width + offsetY, '#fff', 40);\n}\n\n// forever(function () {\n// for (var i = 0; i \u003c 9; i++) {\n// showScore(scores[i], i);\n// }\n// });\n\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":51,"hashid":"ej9sgw44","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦