{"id":215546,"student_id":3463,"content":"forever(update);\ngrid[7][7] = 'b'\n// 印出評估函式計算的分數在每個格子上\nfunction update () {\n for (var x = 0; x \u003c 15; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (grid[x][y] == '') {\n grid[x][y] = 'b'\n var score = evaluate(x, y)\n grid[x][y] = ''\n\n drawText(score, 115+x*30, 35+y*30, 'black', 10)\n }\n }\n }\n}\n\n// // 當玩家下完會觸發 AI 函式換電腦下棋\nfunction AI() {\n var bestx = -1\n var besty = -1\n var bestscore = -1\n for (var x = 0; x \u003c 15; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (grid[x][y] == '') {\n grid[x][y] = 'b'\n var atkscore = evaluate(x, y)\n grid[x][y] = 'w'\n var defscore = evaluate(x, y)\n var score = atkscore*1.1 +defscore\n grid[x][y] = ''\n\n if (score \u003e bestscore) {\n bestscore = score\n bestx = x\n besty = y\n\n }\n\n\n grid[x][y] = ''\n\n drawText(score, 115+x*30, 35+y*30, 'black', 10)\n }\n }\n }\n grid[bestx][besty] = 'b'\n}\n\n// // 評估函式,組合八個方向的分數\n// // top(上)、bottom(下)、right(右)、left(左)\n// // topRight(上右)、topLeft(上左)、bottomRight(下右)、bottomLeft(下左)\nfunction evaluate (x, y) {\n var g = grid\n var a = getScore(g[x][y], g[x][y-1], g[x][y-2], g[x][y-3], g[x][y-4])\n var b = getScore(g[x][y], g[x][y+1], g[x][y+2], g[x][y+3], g[x][y+4])\n var c = getScore(g[x][y], g[x+1][y], g[x+2][y], g[x+3][y], g[x+4][y])\n var d = getScore(g[x][y], g[x-1][y], g[x-2][y], g[x-3][y], g[x-4][y])\n var e = getScore(g[x][y], g[x+1][y+1], g[x+2][y+2], g[x+3][y+3], g[x+4][y+4])\n var f = getScore(g[x][y], g[x+1][y-1], g[x+2][y-2], g[x+3][y-3], g[x+4][y-4])\n var h = getScore(g[x][y], g[x-1][y-1], g[x-2][y-2], g[x-3][y-3], g[x-4][y-4])\n var i = getScore(g[x][y], g[x-1][y+1], g[x-2][y+2], g[x-3][y+3], g[x-4][y+4])\n return a*b+c*d+e*f+h*i\n}\n\n// // 傳入五個格子內容,計算連成幾個\nfunction getScore (a, b, c, d, e) {\n if (b != a) return 1\n if (c != a) return 10\n if (d != a) return 100\n if (e != a) return 1000\n return 10000\n}","created_at":"2020-12-26T13:38:30.617+08:00","updated_at":"2021-01-12T14:38:22.671+08:00","name":"五子棋 AI 大師(預設版) 副本","language":"javascript","screenshot":{"url":"https://cdn8.koding.school/uploads/project/screenshot/215546/7242cf691f4caf2da61f6a7d1ece5acf.jpg"},"parent_id":177828,"plugin":"setBackdrop('bg.png');\n\nconst BLACK = 'b';\nconst WHITE = 'w';\nconst ZZ = [0, 0, 0, 0, 0, 0, 0, 0, 0];\nconst V1 = [4, 3, 2, 1, 0, -1, -2, -3, -4];\nconst V2 = [-4, -3, -2, -1, 0, 1, 2, 3, 4];\n\n\nvar game = (function () {\n\n let grid = {};\n for (let x = -14; x \u003c= 29; x++) {\n grid[x] = {};\n for (let y = -14; y \u003c= 29; y++) {\n grid[x][y] = '';\n }\n }\n\n function isWin (x, y, vx, vy) {\n var re = grid[x + vx*0][y + vy*0] +\n grid[x + vx*1][y + vy*1] +\n grid[x + vx*2][y + vy*2] +\n grid[x + vx*3][y + vy*3] +\n grid[x + vx*4][y + vy*4];\n\n if (re == 'bbbbb') {\n drawText('黑方獲勝');\n stop();\n }\n if (re == 'wwwww') {\n drawText('白方獲勝');\n stop();\n }\n }\n \n //當玩家點擊格子時\n function onclick () {\n var x = Math.floor((cursor.x - 97)/30);\n var y = Math.floor((cursor.y - 17)/30);\n\n if (x \u003c 0 || x \u003e= 15 || y \u003c 0 || y \u003e= 15) return;\n \n place(x, y) \u0026\u0026 AI();\n }\n\n //繪製棋子\n function render () {\n for (var x = 0; x \u003c 15; x++) {\n for (var y = 0; y \u003c 15; y++) {\n pen.size = 0;\n if (grid[x][y] == '') continue;\n if (grid[x][y] == BLACK) pen.fillColor = 'black';\n if (grid[x][y] == WHITE) pen.fillColor = 'white';\n pen.drawCircle(x*30 + 110, y*30 + 30, 14);\n }\n }\n for (var x = 0; x \u003c 15; x++) {\n for (var y = 0; y \u003c 15; y++) {\n isWin(x, y, 1, 0);\n isWin(x, y, -1, 0);\n isWin(x, y, 0, 1);\n isWin(x, y, 0, -1);\n isWin(x, y, 1, 1);\n isWin(x, y, -1, 1);\n isWin(x, y, 1, -1);\n isWin(x, y, -1, -1);\n }\n }\n }\n\n function place (x, y) {\n\n if (grid[x][y] !== '') return false;\n\n var count = 0;\n for (var xx = 0; xx \u003c 15; xx++) {\n for (var yy = 0; yy \u003c 15; yy++) {\n if (grid[xx][yy] !== '') count++;\n }\n }\n grid[x][y] = count%2 === 0 ? BLACK: WHITE;\n \n return true;\n }\n\n function check(color, x, y) {\n\n if (color == 'white' || color == BLACK) color = WHITE;\n if (color == 'black' || color == WHITE) color = BLACK;\n\n var temp = grid[x][y];\n grid[x][y] = color;\n var result = [0, 1, 0, 0, 0, 0];\n var a, b, c;\n var start, len;\n for (len = 5; len \u003e= 2; len--) {\n for (start = 5 - len; start \u003c= 4; start++) {\n z = ZZ.slice(start, start + len);\n d = V1.slice(start, start + len);\n i = V2.slice(start, start + len);\n if (checkLine(grid, x, y, d, z, color)) result[len]++;\n if (checkLine(grid, x, y, z, d, color)) result[len]++;\n if (checkLine(grid, x, y, d, d, color)) result[len]++;\n if (checkLine(grid, x, y, d, i, color)) result[len]++;\n }\n }\n\n grid[x][y] = temp;\n\n return result;\n }\n\n function checkLine(grid, x, y, vx, vy, who) {\n for (var i = 0; i \u003c vx.length; i++) {\n var xx = x + vx[i];\n var yy = y + vy[i];\n if (grid[xx][yy] != who) return false;\n }\n return true;\n }\n\n when('click', onclick);\n forever(render);\n\n return {\n place: place,\n current: BLACK,\n grid: grid,\n };\n})();\n\ngrid = game.grid;","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":87,"hashid":"d5msvyqey","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3790167,"file_name":"bg.png","project_id":215546,"asset_id":246845,"created_at":"2020-12-26T13:38:30.641+08:00","updated_at":"2020-12-26T13:38:30.641+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦