{"id":187736,"student_id":2002,"content":"// 黑子(電腦)先下\ngrid[7][7] = 'b';\n\n// 印出評估函式計算的分數在每個格子上\nforever(function() {\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 score = evaluation(x, y);\n grid[x][y] = \"b\";\n score += evaluation(x, y);\n grid[x][y] = \"\";\n }\n }\n }\n});\n\n// 當玩家下完會觸發 AI 函式換電腦下棋\nfunction AI() {\n var bestPoint = 0;\n var bestX;\n var bestY;\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 points = evaluation(x, y, grid);\n grid[x][y] = \"b\";\n points += evaluation(x, y, grid) * 1.1;\n grid[x][y] = \"\";\n if (points \u003e bestPoint) {\n bestPoint = points;\n bestX = x;\n bestY = y;\n }\n }\n }\n }\n grid[bestX][bestY] = \"b\";\n}\n\n// 評估函式\nfunction evaluation (x, y) {\n var G = grid; //縮寫減少程式,大寫 G 是為了不要和小寫 g 變數命名衝突\n var a = check(G[x][y], G[x][y + 1], G[x][y + 2], G[x][y + 3], G[x][y + 4]);\n var b = check(G[x][y], G[x][y - 1], G[x][y - 2], G[x][y - 3], G[x][y - 4]);\n var c = check(G[x][y], G[x + 1][y], G[x + 2][y], G[x + 3][y], G[x + 4][y]);\n var d = check(G[x][y], G[x - 1][y], G[x - 2][y], G[x - 3][y], G[x - 4][y]);\n var e = check(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 = check(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 g = check(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 = check(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 + g + h;\n}\n\n// 傳入五個格子內容,計算連成幾個\nfunction check (a, b, c, d, e) {\n if (b != a) {\n return 1;\n }\n if (c != a) {\n return 10;\n }\n if (d != a) {\n return 100;\n }\n if (e != a) {\n return 1000;\n }\n return 10000;\n}\n// ---------------------------------------------------------------------------------\n// setBackdrop(\"bg.png\");\n\n// var zz = [ 0, 0, 0, 0, 0, 0, 0, 0, 0];\n// var v1 = [ 4, 3, 2, 1, 0, -1, -2, -3, -4];\n// var v2 = [-4, -3, -2, -1, 0, 1, 2, 3, 4];\n\n// var game = (function () {\n\n// let grid = {};\n \n// let status = 'playing';\n\n// let instance = {\n// place: place,\n// placeWhite: placeWhite,\n// placeBlack: placeBlack,\n// click: click,\n// current: 'b',\n// grid: grid,\n// check: check,\n// }\n\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][y] + grid[x + vx][y + vy] + grid[x + vx*2][y + vy*2] + grid[x + vx*3][y + vy*3] + grid[x + vx*4][y + vy*4];\n// if (re == 'bbbbb') {\n// print(\"黑方獲勝\");\n// status = 'stop';\n// }\n// if (re == 'wwwww') {\n// print(\"白方獲勝\");\n// status = 'stop';\n// }\n// }\n\n// when('click', function () {\n// var x = Math.floor((cursor.x - 97)/30);\n// var y = Math.floor((cursor.y - 17)/30);\n \n\n// if (x \u003c 0 || x \u003e= 15 || y \u003c 0 || y \u003e= 15) return;\n// place(x, y);\n \n// AI();\n \n \n// });\n\n// forever(function () {\n// for (var x=0; x\u003c15; x++) {\n// for (var y=0; y\u003c15; y++) {\n// pen.size = 0;\n// if (grid[x][y] == '') continue;\n// if (grid[x][y] == 'b') pen.fillColor = 'black';\n// if (grid[x][y] == 'w') pen.fillColor = 'white';\n// pen.drawCircle(x*30 + 110, y*30 + 30, 14);\n// }\n// }\n// for (var x=0; x\u003c15; x++) {\n// for (var y=0; y\u003c15; 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// if (status === \"stop\") {\n// stop();\n// }\n// });\n \n \n\n// function place (x, y) {\n// if (status === 'stop') return;\n// if (grid[x][y] !== '') return;\n// // grid[x][y] = instance.current;\n// // instance.current = instance.current == 'w' ? 'b' : 'w';\n// var count = 0;\n// for (var xx=0; xx\u003c15; xx++) {\n// for (var yy=0; yy\u003c15; yy++) {\n// if (grid[xx][yy] !== '') count++;\n// }\n// }\n// grid[x][y] = count%2 === 0 ? 'b' : 'w';\n// }\n \n// function placeWhite (x, y) {\n// if (status == 'stop') return;\n// // if (grid[x][y] != undefined) return;\n// grid[x][y] = 'w';\n// instance.current = 'b';\n// }\n \n// function placeBlack (x, y) {\n// if (status == 'stop') return;\n// // if (grid[x][y] != undefined) return;\n// grid[x][y] = 'b';\n// instance.current = 'w';\n// }\n\n// let funcs = [];\n// function click (func) {\n// funcs.push(func);\n// }\n\n// function check(color, x, y) {\n \n// if (color == 'white' || color == 'w') color = 'w';\n// if (color == 'black' || color == 'b') color = 'b';\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// function checkLine(grid, x, y, vx, vy, who) {\n// for (var i=0; i\u003cvx.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// grid[x][y] = temp;\n// return result;\n// }\n\n// return instance;\n// })();\n\n// game.click(game.placeWhite);\n\n// grid = game.grid;\n\n// function showScore (score, x, y) {\n// print(score, 115 + x*30, 35 + y*30, 'black', 10);\n// }","created_at":"2020-08-30T13:41:34.114+08:00","updated_at":"2023-07-20T21:35:46.739+08:00","name":"五子棋AI 副本","language":"javascript","screenshot":{"url":"https://cdn5.koding.school/uploads/project/screenshot/187736/8fb2220aa638d3b0d43f0c015488ea3e.jpg"},"parent_id":52798,"plugin":"setBackdrop(\"bg.png\");\n\nvar zz = [ 0, 0, 0, 0, 0, 0, 0, 0, 0];\nvar v1 = [ 4, 3, 2, 1, 0, -1, -2, -3, -4];\nvar v2 = [-4, -3, -2, -1, 0, 1, 2, 3, 4];\n\nvar game = (function () {\n\n let grid = {};\n \n let status = 'playing';\n\n let instance = {\n place: place,\n placeWhite: placeWhite,\n placeBlack: placeBlack,\n click: click,\n current: 'b',\n grid: grid,\n check: check,\n }\n\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][y] + grid[x + vx][y + vy] + grid[x + vx*2][y + vy*2] + grid[x + vx*3][y + vy*3] + grid[x + vx*4][y + vy*4];\n if (re == 'bbbbb') {\n print(\"黑方獲勝\");\n status = 'stop';\n }\n if (re == 'wwwww') {\n print(\"白方獲勝\");\n status = 'stop';\n }\n }\n\n when('click', function () {\n var x = Math.floor((cursor.x - 97)/30);\n var y = Math.floor((cursor.y - 17)/30);\n \n\n if (x \u003c 0 || x \u003e= 15 || y \u003c 0 || y \u003e= 15) return;\n place(x, y);\n \n AI();\n \n \n });\n\n forever(function () {\n for (var x=0; x\u003c15; x++) {\n for (var y=0; y\u003c15; y++) {\n pen.size = 0;\n if (grid[x][y] == '') continue;\n if (grid[x][y] == 'b') pen.fillColor = 'black';\n if (grid[x][y] == 'w') pen.fillColor = 'white';\n pen.drawCircle(x*30 + 110, y*30 + 30, 14);\n }\n }\n for (var x=0; x\u003c15; x++) {\n for (var y=0; y\u003c15; 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 if (status === \"stop\") {\n stop();\n }\n });\n \n \n\n function place (x, y) {\n if (status === 'stop') return;\n if (grid[x][y] !== '') return;\n // grid[x][y] = instance.current;\n // instance.current = instance.current == 'w' ? 'b' : 'w';\n var count = 0;\n for (var xx=0; xx\u003c15; xx++) {\n for (var yy=0; yy\u003c15; yy++) {\n if (grid[xx][yy] !== '') count++;\n }\n }\n grid[x][y] = count%2 === 0 ? 'b' : 'w';\n }\n \n function placeWhite (x, y) {\n if (status == 'stop') return;\n // if (grid[x][y] != undefined) return;\n grid[x][y] = 'w';\n instance.current = 'b';\n }\n \n function placeBlack (x, y) {\n if (status == 'stop') return;\n // if (grid[x][y] != undefined) return;\n grid[x][y] = 'b';\n instance.current = 'w';\n }\n\n let funcs = [];\n function click (func) {\n funcs.push(func);\n }\n\n function check(color, x, y) {\n \n if (color == 'white' || color == 'w') color = 'w';\n if (color == 'black' || color == 'b') color = 'b';\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 function checkLine(grid, x, y, vx, vy, who) {\n for (var i=0; i\u003cvx.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 grid[x][y] = temp;\n return result;\n }\n\n return instance;\n})();\n\ngame.click(game.placeWhite);\n\ngrid = game.grid;\n\nfunction showScore (score, x, y) {\n print(score, 115 + x*30, 35 + y*30, 'black', 10);\n}\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":45,"hashid":"36ys65n8","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3189759,"file_name":"bg.png","project_id":187736,"asset_id":105415,"created_at":"2020-08-30T13:41:34.170+08:00","updated_at":"2020-08-30T13:41:34.170+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦