{"id":309474,"student_id":3469,"content":"forever(update);\n\n// grid [7][7] = 'b';\n\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] = 'w';\n var atk = evaluate(x, y);\n grid[x][y] = 'b';\n var def = evaluate(x, y);\n var score = atk + def;\n grid[x][y] = '';\n drawText(score, 115+x*30, 35+y*30, 'black', 10);\n }\n }\n }\n}\n\n// 當玩家下完會觸發 AI 函式換電腦下棋\nfunction AI() {\n var bestScore = -1;\n var bestX = -1;\n var bestY = -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] = 'w';\n var atk = evaluate(x, y) * 1.01;\n grid[x][y] = 'b';\n var def = evaluate(x, y);\n var score = atk + def;\n grid[x][y] = '';\n if (score \u003e bestScore) {\n bestScore = score;\n bestX = x;\n bestY = y;\n }\n }\n }\n }\n grid[bestX][bestY] = 'w';\n}\n\n// 評估函式,組合八個方向的分數\n// top(上)、bottom(下)、right(右)、left(左)\n// topRight(上右)、topLeft(上左)、bottomRight(下右)、bottomLeft(下左)\nfunction evaluate (x, y) {\n var g = grid;\n var t = getScore(g[x][y], g[x][y+1], g[x][y+2], g[x][y+3], g[x][y+3]);\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 r = getScore(g[x][y], g[x+1][y], g[x+2][y], g[x+3][y], g[x+4][y]);\n var l = getScore(g[x][y], g[x-1][y], g[x-2][y], g[x-3][y], g[x-4][y]);\n var tr = 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 tl = 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 br = 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 bl = 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 t*b+r*l+tr*bl+tl*br;\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":"2021-10-30T13:35:29.142+08:00","updated_at":"2021-10-30T14:43:57.379+08:00","name":"五子棋 AI 大師(預設版) 副本","language":"javascript","screenshot":{"url":"https://cdn5.koding.school/uploads/project/screenshot/309474/1d86e9bf4674dfaff27b6a3efc36038b.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":75,"hashid":"9ygspev6k","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":6327766,"file_name":"bg.png","project_id":309474,"asset_id":246845,"created_at":"2021-10-30T13:35:29.150+08:00","updated_at":"2021-10-30T13:35:29.150+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦