{"id":57955,"student_id":119,"content":"var turn = \"O\";\nvar bs = 0;\nvar bms = 0;\nfunction AI (grid) {\n for (var i = 0; i \u003c 9; i++) {\n if (grid[i] == \"\") {\n grid[i] = \"O\";\n var score = evaluation(grid);\n grid[i] = \"X\";\n score += evaluation(grid);\n showScore(score, i);\n grid[i] = \"\";\n if (score \u003e bs) {\n bs = score;\n bms = i;\n }\n }}\n grid[bms] = turn;\n}\n\n// 印出每個格子的分數\n\nforever(function() {});\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\") {\n return 100;\n } else if (s == \"OO\") {\n return 10;\n } else if (s == \"O\") {\n return 1;\n } else if (s == \"XXX\") {\n return 100;\n } else if (s == \"XX\") {\n return 10;\n } else if (s == \"X\") {\n return 1;\n } else {\n return 0;\n }\n}","created_at":"2019-03-09T10:03:37.144+08:00","updated_at":"2019-10-26T13:11:16.520+08:00","name":"井字 副本","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/57955/fad7aad97c1a283e34f0e6e119ecbcf1.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":53,"hashid":"meysqgnm","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
到這台電腦