{"id":225403,"student_id":1998,"content":"//grid[4] = 'o';\n\nforever(loop); //不斷執行遊戲迴圈\n\n//遊戲迴圈中印出格子分數\nfunction loop () {\n for (var i = 0; i \u003c grid.length; i++) {\n if (grid[i] == \"\") {\n grid[i] = \"o\"\n var score = evaluate(grid)\n grid[i] = \"x\"\n score += evaluate(grid)\n grid[i] = \"\";\n showScore(score, i);\n }\n }\n}\n\nfunction AI (grid) {\n var bestScore = -1\n var bestIdx = 0\n for (var i = 0; i \u003c grid.length; i++) {\n if (grid[i] == \"\") {\n grid[i] = \"o\"\n var score = evaluate(grid)\n grid[i] = \"x\"\n score += evaluate(grid)\n if (score \u003e bestScore) {\n bestScore = score\n bestIdx = i\n }\n grid[i] = \"\";\n showScore(score, i);\n }\n }\n grid[bestIdx] = \"x\"\n}\n\n//評估函式\nfunction evaluate (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//計算某一條線的分數\nfunction getScore (a, b, c) {\n var line = a + b + c\n if (line == \"o\") {\n return 1\n } else if (line == \"oo\") {\n return 10\n } else if (line == \"ooo\") {\n return 100\n } else {\n return 0\n }\n}","created_at":"2021-01-30T16:07:59.715+08:00","updated_at":"2021-01-30T17:08:23.334+08:00","name":"井字遊戲AI(預設版) 副本","language":"javascript","screenshot":{"url":"https://cdn7.koding.school/uploads/project/screenshot/225403/4a22354d35896a703e16030503400681.jpg"},"parent_id":172912,"plugin":"Game.set({width: 1200, height: 900});\n\nsetBackdrop('#f8e8cc');\n\nvar scores = [0, 0, 0, 0, 0, 0, 0, 0, 0];\nvar grid = ['', '', '', '', '', '', '', '', ''];\nvar offsetX = 240;\nvar offsetY = 90;\nvar width = 240;\nvar turn = 'o';\n\nforever(loop);\nwhen('click', onclick);\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\nfunction onclick () {\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 var olen = grid.filter(item =\u003e item == 'o').length;\n var xlen = grid.filter(item =\u003e item == 'x').length;\n grid[i] = xlen \u003e= olen ? 'o' : 'x';\n AI(grid);\n}\n\nfunction loop () {\n var winner = check();\n if (winner) {\n text = winner + ' 獲勝!';\n stop();\n } else if (grid.indexOf('') === -1) {\n text = '平手';\n stop();\n } else {\n text = 'playing';\n }\n drawText(text , 500, 20, 'black', 45, 'Comic Sans MS');\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 var color = grid[i] == 'o' ? '#ff595a' : '#4a86e8';\n \n drawText(grid[i], xx + 40, yy, color, 240, 'Comic Sans MS');\n \n pen.size = 30;\n pen.color = '#222';\n pen.fillColor = '#fff';\n pen.drawRect(xx, yy, 240, 240);\n }\n}\n\nfunction showScore (score, i) {\n var x = i%3;\n var y = (i - x)/3;\n var xx = x*width + offsetX + 20;\n var yy = y*width + offsetY + 20;\n print(score, xx, yy, 'black', 60, 'Comic Sans MS');\n}\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":28,"hashid":"zpesjz5r4","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":4019624,"file_name":"Gymnopedie_No_1.mp3","project_id":225403,"asset_id":244103,"created_at":"2021-01-30T16:07:59.722+08:00","updated_at":"2021-01-30T16:07:59.722+08:00"},{"id":4019625,"file_name":"koding.png","project_id":225403,"asset_id":244104,"created_at":"2021-01-30T16:07:59.724+08:00","updated_at":"2021-01-30T16:07:59.724+08:00"},{"id":4019626,"file_name":"space_bg.jpg","project_id":225403,"asset_id":244105,"created_at":"2021-01-30T16:07:59.725+08:00","updated_at":"2021-01-30T16:07:59.725+08:00"},{"id":4019627,"file_name":"earth.png","project_id":225403,"asset_id":244106,"created_at":"2021-01-30T16:07:59.726+08:00","updated_at":"2021-01-30T16:07:59.726+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦