{"id":27744,"student_id":10,"content":"game.placeBlack(7, 7);\n\ngame.click(game.placeWhite);\ngame.click(AI);\n\nfunction AI() {\n \n var bestScore = 0;\n var bestX;\n var bestY;\n\n for (var x = 0; x \u003c 15; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (game.grid[x][y] == 'empty') {\n var score = getAttactPoints(x, y) + getDefinedPoints(x, y);\n if (score \u003e bestScore) {\n bestScore = score;\n bestX = x;\n bestY = y;\n }\n }\n }\n }\n game.placeBlack(bestX, bestY);\n}\n\nfunction getAttactPoints(x, y) {\n var result = game.check('black', x, y);\n var score = result[1]*1 +\n result[2]*3 +\n result[3]*11 +\n result[4]*35 +\n result[5]*350;\n return score;\n}\n\nfunction getDefinedPoints(x, y) {\n var result = game.check('white', x, y);\n var score = result[1]*1 +\n result[2]*3 +\n result[3]*10 +\n result[4]*30 +\n result[5]*300;\n return score;\n}\n\nforever(function(){\n for (var x = 0; x \u003c 15; x++) {\n for (var y = 0; y \u003c 15; y++) {\n var point = getAttactPoints(x, y);\n print(point, 115 + x*30, 35 + y*30, 'black', 10);\n }\n }\n});\n","created_at":"2018-05-02T16:35:00.541+08:00","updated_at":"2019-10-20T22:24:22.037+08:00","name":"五子棋初階 AI","language":"javascript","screenshot":{"url":"https://cdn8.koding.school/uploads/project/screenshot/27744/a87164b08711c65f13532c44eb82be8b.jpg"},"parent_id":23795,"plugin":"setBackdrop(\"bg.png\");\n\nvar game = (function () {\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 grid = {};\n var current = 'b';\n var gameOver = false;\n \n for (var x=-4; x\u003c=19; x++) {\n grid[x] = {};\n for (var y=-4; y\u003c=19; y++) {\n grid[x][y] = 'empty';\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] == 'empty') 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 if (gameOver) stop();\n });\n \n function placeWhite (x, y) {\n if (grid[x][y] != 'empty') return;\n grid[x][y] = 'w';\n current = 'b';\n checkWin();\n }\n \n function placeBlack (x, y) {\n if (grid[x][y] != 'empty') return;\n grid[x][y] = 'b';\n current = 'w';\n checkWin();\n }\n \n function checkWin () {\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 }\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 gameOver = true;\n }\n if (re == 'wwwww') {\n print(\"白方獲勝\");\n gameOver = true;\n }\n }\n\n function click (func) {\n when('click', function () {\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 || gameOver) return;\n func(x, y);\n });\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 grid[x][y] = temp;\n return result;\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 return {\n placeWhite: placeWhite,\n placeBlack: placeBlack,\n click: click,\n grid: grid,\n check: check,\n }\n})();\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":296,"hashid":"4y3szj4k","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":511151,"file_name":"bg.png","project_id":27744,"asset_id":33400,"created_at":"2018-05-02T16:35:00.549+08:00","updated_at":"2018-05-02T16:35:00.549+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦