{"id":58661,"student_id":173,"content":"\nfunction AI (grid) {\n var b=0;\n var bi;\n var to=0;\n for(var i=0;i\u003c9;i++){\n \n to=0;\n if(grid[i]==\"\"){\n grid[i]=\"X\";\n to+=evaluation(grid);\n console.log(evaluation(grid))\n grid[i]=\"O\";\n to+=evaluation(grid);\n console.log(evaluation(grid))\n if(to\u003eb){\n b=to;\n bi=i;\n }\n \n \n grid[i]=\"\";\n }\n \n}\n console.log(b,bi)\n grid[bi]=turn\n}\n\n\n\n// 印出每個格子的分數\nforever(function(){\n \n});\n\n// 評估函式\nfunction evaluation (g) {\n var total=0;\n \n total+=getScore(g[0]+g[1]+g[2])\n total+=getScore(g[3]+g[4]+g[5])\n total+=getScore(g[6]+g[7]+g[8])\n total+=getScore(g[0]+g[3]+g[6])\n total+=getScore(g[1]+g[4]+g[7])\n total+=getScore(g[2]+g[5]+g[8])\n total+=getScore(g[0]+g[4]+g[8])\n total+=getScore(g[2]+g[4]+g[6])\n return total;\n}\n\n// 計算某一條線的分數\nfunction getScore (s) {\n \n \n if(s==\"O\"){\n return 1;\n }else if(s==\"OO\"){\n return 10;\n }else if(s==\"OOO\"){\n return 100;\n }else if(s==\"X\"){\n return 1;\n }else if(s==\"XX\"){\n return 10;\n }else if(s==\"XXX\"){\n return 100;\n }else{\n return 0;\n }\n \n \n}\n","created_at":"2019-03-09T18:57:47.752+08:00","updated_at":"2019-10-09T21:38:47.552+08:00","name":"井字 副本","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/58661/dca846151e5d54263c5e590914363a60.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":77,"hashid":"vngs9pwj","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
到這台電腦