{"id":670308,"student_id":2589,"content":"// 黑子(電腦)先下\ngrid[7][7] = 'b';\n\nforever(update);\n\n// 印出評估函式計算的分數在每個格子上\nfunction update () {\n for (var y = 0; y \u003c 15; y++) {\n for (var x = 0; x \u003c 15; x++) {\n if (grid[y][x] == '') {\n grid[y][x] = 'b';\n var score = evaluation(x, y, grid);\n grid[y][x] = 'w';\n score += evaluation(x, y, grid);\n grid[y][x] = '';\n // drawText(score, 115 + x*30, 35 + y*30, 'black', 10);\n }\n }\n }\n}\n\n// 當玩家下完會觸發 AI 函式換電腦下棋\nfunction AI() {\n var bestScore = 0;\n var bestY;\n var bestX;\n var secondY;\n var secondX;\n for (var y = 0; y \u003c 15; y++) {\n for (var x = 0; x \u003c 15; x++) {\n if (grid[y][x] == '') {\n grid[y][x] = 'b';\n var score = evaluation(x, y, grid)*1.1;\n grid[y][x] = 'w';\n score += evaluation(x, y, grid);\n grid[y][x] = '';\n if (score \u003e bestScore) {\n bestScore = score;\n secondY = bestY; //必須寫在設定 bestX 之前\n secondX = bestX; //必須寫在設定 bestX 之前\n bestY = y;\n bestX = x;\n }\n }\n }\n }\n \n if (Math.random() \u003c 0.2 \u0026\u0026 secondX) { // 0.2 的機率會失誤\n grid[secondY][secondX] = 'b';\n } else {\n grid[bestY][bestX] = 'b';\n }\n}\n\n// 評估函式,組合八個方向的分數\n// north(北)、south(南)、west(西)、east(東)\n// northwest(西北)、southeast(東南)、northeast(東北)、southwest(西南)\nfunction evaluation (x, y) {\n var g = grid;\n var n = getScore(g[y][x], g[y-1][x], g[y-2][x], g[y-3][x], g[y-4][x]);\n var s = getScore(g[y][x], g[y+1][x], g[y+2][x], g[y+3][x], g[y+4][x]);\n var w = getScore(g[y][x], g[y][x-1], g[y][x-2], g[y][x-3], g[y][x-4]);\n var e = getScore(g[y][x], g[y][x+1], g[y][x+2], g[y][x+3], g[y][x+4]);\n var nw = getScore(g[y][x], g[y-1][x-1], g[y-2][x-2], g[y-3][x-3], g[y-4][x-4]);\n var se = getScore(g[y][x], g[y+1][x+1], g[y+2][x+2], g[y+3][x+3], g[y+4][x+4]);\n var ne = getScore(g[y][x], g[y-1][x+1], g[y-2][x+2], g[y-3][x+3], g[y-4][x+4]);\n var sw = getScore(g[y][x], g[y+1][x-1], g[y+2][x-2], g[y+3][x-3], g[y+4][x-4]);\n \n return n*s + w*e + nw*se + ne*sw;\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":"2023-12-29T21:03:38.939+08:00","updated_at":"2023-12-29T21:04:03.776+08:00","name":"五子棋 AI_正課_試玩版","language":"javascript","screenshot":{"url":null},"parent_id":2,"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 y = -14; y \u003c= 29; y++) {\n grid[y] = {};\n for (let x = -14; x \u003c= 29; x++) {\n grid[y][x] = '';\n }\n }\n\n function isWin (x, y, vx, vy) {\n var re = grid[y + vy*0][x + vx*0] +\n grid[y + vy*1][x + vx*1] +\n grid[y + vy*2][x + vx*2] +\n grid[y + vy*3][x + vx*3] +\n grid[y + vy*4][x + vx*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 y = 0; y \u003c 15; y++) {\n for (var x = 0; x \u003c 15; x++) {\n pen.size = 0;\n if (grid[y][x] == '') continue;\n if (grid[y][x] == BLACK) pen.fillColor = 'black';\n if (grid[y][x] == 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[y][x] !== '') return false;\n\n var count = 0;\n for (var yy = 0; yy \u003c 15; yy++) {\n for (var xx = 0; xx \u003c 15; xx++) {\n if (grid[yy][xx] !== '') count++;\n }\n }\n grid[y][x] = 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[y][x];\n grid[y][x] = 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[yy][xx] != 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":30,"hashid":"zpeszg5mg","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":15038155,"file_name":"bg.png","project_id":670308,"asset_id":710119,"created_at":"2023-12-29T21:03:52.012+08:00","updated_at":"2023-12-29T21:03:52.012+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦