{"id":171240,"student_id":1909,"content":"// 電腦玩家的程式\nfunction AI (grid) {\n var max_score = -1\n var max_idx = -1\n for (var i = 0; i \u003c grid.length; i++) {\n if (grid[i] == \"\") {\n grid[i] = \"X\"\n var attacking_score = evaluation(grid)\n if (attacking_score \u003e max_score) {\n max_score = attacking_score\n max_idx = i\n }\n grid[i] = \"\"\n grid[i] = \"O\"\n var defending_score = evaluation(grid)\n if (defending_score \u003e max_score) {\n max_score = defending_score\n max_idx = i\n }\n grid[i] = \"\"\n var total_score = attacking_score+defending_score\n // showScore(total_score, i)\n }\n }\n \n grid[max_idx] = \"X\"\n}\n\n// 印出每個格子的分數\n// forever(function() {\n// for (var i = 0; i \u003c grid.length; i++) {\n// if (grid[i] == \"\") {\n// grid[i] = \"X\"\n// var attacking_score = evaluation(grid)\n// grid[i] = \"\"\n// grid[i] = \"O\"\n// var defending_score = evaluation(grid)\n// grid[i] = \"\"\n// var total_score = attacking_score+defending_score\n// showScore(total_score, i)\n\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// 計算某一條線的分數(總共有八條線)\n//例如s:grid[1]+grid[4]+grid[7]=\u003e\"0\"+\"0\"+\"\"=\"00\" = s\nfunction getScore (s) {\n if (s == \"X\") {\n return 1\n } else if (s == \"XX\") {\n return 10\n } else if (s == \"XXX\") {\n return 100\n } else if (s == \"O\") {\n return 1\n } else if (s == \"OO\") {\n return 10\n } else if (s == \"OOO\") {\n return 100\n } else {\n return 0\n }\n}","created_at":"2020-07-25T16:16:29.188+08:00","updated_at":"2020-07-25T18:02:48.388+08:00","name":"井字 副本","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/171240/a5be13480e1af109a06897edd76849a1.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":46,"hashid":"2pdskq33","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
到這台電腦