{"id":50292,"student_id":10,"content":"function 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 place(turn, best);\n}\n\n// 印出每個格子的分數\nforever(function(){\n for (var i=0; i\u003c9; i++) {\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// 評估函式\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 == 'OOO') return 100;\n if (s == 'XXX') return 100;\n if (s == 'OO') return 10;\n if (s == 'XX') return 10;\n if (s == 'O') return 1;\n if (s == 'X') return 1;\n return 0; // s == ''\n}\n","created_at":"2018-11-25T09:33:27.266+08:00","updated_at":"2019-11-09T01:23:57.975+08:00","name":"井字","language":"javascript","screenshot":{"url":"https://cdn1.koding.school/uploads/project/screenshot/50292/40ef19479011458edfc41a321eaa94c6.jpg"},"parent_id":2,"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":369,"hashid":"qmds9rk6","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
到這台電腦