{"id":301125,"student_id":10,"content":"var clock = 0;\nvar snake = game.snake; // 蛇\nvar arrows = []; // 紀錄格子數字的二維陣列\nvar numbers = []; // 紀錄箭頭方向的二維陣列\n\n// 初始化格子箭頭與格子數字的二維陣列\nfor (var x = 0; x \u003c 20; x++) {\n arrows[x] = []\n numbers[x] = []\n for (var y = 0; y \u003c 15; y++) {\n arrows[x][y] = undefined\n numbers[x][y] = undefined\n }\n}\n\ndrawArrows(arrows); //繪製箭頭\ndrawNumbers(numbers); //繪製數字\nforever(gameloop); //不斷執行遊戲迴圈\n// hamilton(); //創造漢米頓圈\narrows[1][1] = 90\narrows[2][1] = 180\narrows[2][2] = 270\narrows[1][2] = 0\nrandomHamilton(arrows, 144)\nconsole.log(arrows)\ncreateNumbers(); //初始化格子數字\n\nfunction gameloop () {\n if (key.up) snake.direction = 0 //轉向上方\n if (key.down) snake.direction = 180 //轉向下方\n if (key.right) snake.direction = 90 //轉向右方\n if (key.left) snake.direction = 270 //轉向左方\n\n clock += 1\n if (clock % 10 == 0) {\n AI()\n snake.move() //移動\n }\n}\n\n// 產生漢米頓圈\nfunction hamilton () {\n for (var y = 0; y \u003c 15; y++) {\n for (var x = 0; x \u003c 20; x++) {\n\n if (x % 2 == 0) {\n arrows[x][y] = 180;\n } else {\n arrows[x][y] = 0;\n }\n\n if (y == 0) arrows[x][y] = 270;\n\n if (y == 1 \u0026\u0026 x % 2 == 1) arrows[x][y] = 90;\n if (y == 14 \u0026\u0026 x % 2 == 0) arrows[x][y] = 90;\n\n if (x == 0 \u0026\u0026 y == 0) arrows[x][y] = 180;\n if (x == 19 \u0026\u0026 y == 1) arrows[x][y] = 0;\n\n }\n }\n}\n\n// 依據箭頭方向將數字填入格子\nfunction createNumbers () {\n var x = 0;\n var y = 0;\n\n for (var i = 0; i \u003c 300; i++) {\n numbers[x][y] = i;\n\n var d = arrows[x][y];\n if (d == 0) y -= 1;\n if (d == 180) y += 1;\n if (d == 90) x += 1;\n if (d == 270) x -= 1;\n }\n}\n\n// 判斷該格子是否可以作為捷徑\nfunction isShortCut (headNum, tailNum, foodNum, nextNum) {\n\n if (headNum \u003e foodNum) {\n if (nextNum \u003c headNum \u0026\u0026 nextNum \u003e foodNum) return false\n } else {\n if (nextNum \u003c headNum || nextNum \u003e foodNum) return false\n }\n\n if (headNum \u003e tailNum) {\n if (nextNum \u003c headNum \u0026\u0026 nextNum \u003e tailNum - 1) return false\n } else {\n if (nextNum \u003c headNum || nextNum \u003e tailNum - 1) return false\n }\n\n\n return true\n}\n\nfunction AI () {\n var head = snake.getHead() //取得蛇頭\n var tail = snake.getTail() //取得蛇尾\n var food = game.getFood() //取得食物\n\n if (arrows[head.x][head.y] != undefined) {\n snake.direction = arrows[head.x][head.y] //讓蛇隨著箭頭方向移動\n }\n\n var headNum = numbers[head.x][head.y] //蛇頭所在格子編號\n var tailNum = numbers[tail.x][tail.y] //蛇尾所在格子編號\n var foodNum = numbers[food.x][food.y] //食物所在格子編號\n\n\n if (head.x \u003c 19) {\n var nextNum = numbers[head.x + 1][head.y]\n if (isShortCut(headNum, tailNum, foodNum, nextNum)) {\n snake.direction = 90\n }\n } else if (head.y \u003c 14 ) {\n var nextNum = numbers[head.x][head.y + 1]\n if (isShortCut(headNum, tailNum, foodNum, nextNum)) {\n snake.direction = 180\n }\n } else if (head.x \u003e 0 ) {\n var nextNum = numbers[head.x - 1][head.y]\n if (isShortCut(headNum, tailNum, foodNum, nextNum)) {\n snake.direction = 270\n }\n } else if (head.y \u003e 0 ) {\n var nextNum = numbers[head.x][head.y - 1]\n if (isShortCut(headNum, tailNum, foodNum, nextNum)) {\n snake.direction = 0\n }\n }\n}","created_at":"2021-10-01T11:14:55.764+08:00","updated_at":"2021-10-03T23:04:56.808+08:00","name":"貪吃蛇 AI(預設版)","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/301125/688677b35a3a90af39c9ce99bd7c564f.jpg"},"parent_id":3,"plugin":"Game.set({ width: 1200, height: 900 })\n\nsetBackdrop('black')\n\nconst SIZE = 60\nconst W = 1200 / SIZE\nconst H = 900 / SIZE\n\nconst food = {\n x: Math.floor(Math.random() * W),\n y: Math.floor(Math.random() * H),\n}\n\n\nfunction createSnakeGame (bodies) {\n \n let snake = {\n direction: 90,\n bodies,\n }\n \n snake.move = function () {\n let head = bodies.pop()\n head.x = bodies[0].x\n head.y = bodies[0].y\n if (snake.direction === 0) head.y -= 1\n if (snake.direction === 90) head.x += 1\n if (snake.direction === 180) head.y += 1\n if (snake.direction === 270) head.x -= 1\n if (bodies.some(pos =\u003e pos.x === head.x \u0026\u0026 pos.y === head.y)) gameover()\n if (head.x \u003c 0 || head.y \u003c 0 || head.x \u003e= W || head.y \u003e= H) gameover()\n if (head.x === food.x \u0026\u0026 head.y === food.y) {\n bodies.push({ x: head.x, y: head.y })\n randomFoodPos(food, bodies)\n }\n bodies.unshift(head)\n }\n \n snake.getHead = function () {\n return bodies[0]\n }\n \n snake.getTail = function () {\n return bodies[bodies.length - 1]\n }\n \n function getFood () {\n return food\n }\n \n forever(function(){\n for (let i = 0; i \u003c bodies.length - 1; i++) {\n pen.color = 'lightgreen'\n pen.fillColor = 'lightgreen'\n let a = bodies[i]\n let b = bodies[i + 1]\n if (a.x \u003e b.x) pen.drawRect(a.x * SIZE - 10, a.y * SIZE + 10, SIZE, SIZE - 20)\n if (a.x \u003c b.x) pen.drawRect(a.x * SIZE + 10, a.y * SIZE + 10, SIZE, SIZE - 20)\n if (a.y \u003e b.y) pen.drawRect(a.x * SIZE + 10, a.y * SIZE - 10, SIZE - 20, SIZE)\n if (a.y \u003c b.y) pen.drawRect(a.x * SIZE + 10, a.y * SIZE + 10, SIZE - 20, SIZE)\n pen.drawRect(b.x * SIZE + 10, b.y * SIZE + 10, SIZE - 20, SIZE - 20)\n }\n pen.fillColor = 'red'\n pen.drawRect(food.x * SIZE + 10, food.y * SIZE + 10, SIZE - 20, SIZE - 20)\n })\n \n return {\n snake,\n getFood,\n }\n \n}\n\nfunction randomFoodPos (food, bodies) {\n let pos = []\n for (var x = 0; x \u003c 20; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (bodies.some(pos =\u003e pos.x === x \u0026\u0026 pos.y === y) == false) {\n pos.push({ x, y }) \n }\n }\n }\n if (pos.length === 0) return gameover()\n pos = pos[Math.floor(Math.random() * pos.length)]\n food.x = pos.x\n food.y = pos.y\n}\n\n\nfunction gameover () {\n stop()\n drawText('gameover', 10, 10, 'red', 60)\n}\n\n\nlet arrowSprites = []\nfor (var x = 0; x \u003c 20; x++) {\n arrowSprites[x] = []\n for (var y = 0; y \u003c 15; y++) {\n let arrow = createSprite('arrow.png')\n arrow.x = (x + 0.5) * SIZE\n arrow.y = (y + 0.5) * SIZE\n arrow.opacity = 0.5\n arrow.hidden = true\n arrowSprites[x][y] = arrow\n }\n}\n\nfunction drawNumbers (numbers) {\n forever(() =\u003e {\n for (var x = 0; x \u003c 20; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if (numbers[x][y] \u003e= 0) {\n drawText(numbers[x][y], x * SIZE, y * SIZE, 'white', 24)\n }\n }\n } \n })\n}\n\nfunction drawArrows (arrows) {\n forever(() =\u003e {\n for (var x = 0; x \u003c 20; x++) {\n for (var y = 0; y \u003c 15; y++) {\n if ([0, 90, 180, 270].includes(arrows[x][y])) {\n arrowSprites[x][y].hidden = false\n arrowSprites[x][y].direction = arrows[x][y]\n } else {\n arrowSprites[x][y].hidden = true\n }\n }\n }\n })\n}\n\nvar game = createSnakeGame([\n {x: 8, y: 5},\n {x: 7, y: 5},\n {x: 6, y: 5},\n {x: 5, y: 5},\n])\n\n\n\nfunction randomHamilton (arrows, depth) {\n \n if (depth \u003c= 0) return true\n \n var a = { x: 1, y: 1 }\n var b = { x: 1, y: 1 }\n var d = arrows[a.x][a.y]\n if (d === 0) b.y -= 1\n if (d === 90) b.x += 1\n if (d === 180) b.y += 1\n if (d === 270) b.x -= 1\n \n var arr = []\n \n for (let i = 0; i \u003c 300; i++) {\n \n if (a.y === b.y) {\n if (a.y \u003e 0 \u0026\u0026 arrows[a.x][a.y - 1] === undefined \u0026\u0026 arrows[b.x][b.y - 1] === undefined) {\n arr.push({ fromX: a.x, fromY: a.y, toX: b.x, toY: b.y, expand: 'top' })\n } \n if (a.y \u003c 14 \u0026\u0026 arrows[a.x][a.y + 1] === undefined \u0026\u0026 arrows[b.x][b.y + 1] === undefined) {\n arr.push({ fromX: a.x, fromY: a.y, toX: b.x, toY: b.y, expand: 'bottom' })\n } \n } else {\n if (a.x \u003e 0 \u0026\u0026 arrows[a.x - 1][a.y] === undefined \u0026\u0026 arrows[b.x - 1][b.y] === undefined) {\n arr.push({ fromX: a.x, fromY: a.y, toX: b.x, toY: b.y, expand: 'left' })\n } \n if (a.x \u003c 19 \u0026\u0026 arrows[a.x + 1][a.y] === undefined \u0026\u0026 arrows[b.x + 1][b.y] === undefined) {\n arr.push({ fromX: a.x, fromY: a.y, toX: b.x, toY: b.y, expand: 'right' })\n }\n }\n a.x = b.x\n a.y = b.y\n var d = arrows[b.x][b.y]\n if (d === 0) b.y -= 1\n if (d === 90) b.x += 1\n if (d === 180) b.y += 1\n if (d === 270) b.x -= 1\n if (a.x === 1 \u0026\u0026 a.y === 1) break\n }\n\n for (i = 0; i \u003c arr.length; i++) {\n let randA = Math.floor(Math.random() * arr.length)\n let randB = Math.floor(Math.random() * arr.length)\n let a = arr[randA]\n let b = arr[randB]\n arr[randA] = b\n arr[randB] = a\n }\n \n for (let i = 0; i \u003c arr.length; i++) {\n \n let { fromX, fromY, toX, toY, expand } = arr[i]\n \n if (expand === 'top') {\n if (toX \u003e fromX) {\n arrows[fromX][fromY] = 0\n arrows[fromX][fromY - 1] = 90\n arrows[toX][toY - 1] = 180\n } else {\n arrows[fromX][fromY] = 0\n arrows[fromX][fromY - 1] = 270\n arrows[toX][toY - 1] = 180\n }\n }\n \n if (expand === 'bottom') {\n if (toX \u003e fromX) {\n arrows[fromX][fromY] = 180\n arrows[fromX][fromY + 1] = 90\n arrows[toX][toY + 1] = 0\n } else {\n arrows[fromX][fromY] = 180\n arrows[fromX][fromY + 1] = 270\n arrows[toX][toY + 1] = 0\n }\n }\n \n if (expand === 'left') {\n if (toY \u003e fromY) {\n arrows[fromX][fromY] = 270\n arrows[fromX - 1][fromY] = 180\n arrows[toX - 1][toY] = 90\n } else {\n arrows[fromX][fromY] = 270\n arrows[fromX - 1][fromY] = 0\n arrows[toX - 1][toY] = 90\n }\n }\n \n if (expand === 'right') {\n if (toY \u003e fromY) {\n arrows[fromX][fromY] = 90\n arrows[fromX + 1][fromY] = 180\n arrows[toX + 1][toY] = 270\n } else {\n arrows[fromX][fromY] = 90\n arrows[fromX + 1][fromY] = 0\n arrows[toX + 1][toY] = 270\n }\n }\n \n let re = randomHamilton(arrows, depth - 1)\n \n if (re) {\n return true\n }\n else {\n if (expand === 'top') {\n if (toX \u003e fromX) {\n arrows[toX][toY] = 90\n arrows[toX][toY - 1] = undefined\n arrows[fromX][fromY - 1] =undefined\n } else {\n arrows[toX][toY] = 270\n arrows[toX][toY - 1] = undefined\n arrows[fromX][fromY - 1] = undefined\n }\n }\n \n if (expand === 'bottom') {\n if (toX \u003e fromX) {\n arrows[toX][toY] = 90\n arrows[toX][toY + 1] = undefined\n arrows[fromX][fromY + 1] = undefined\n } else {\n arrows[toX][toY] = 270\n arrows[toX][toY + 1] = undefined\n arrows[fromX][fromY + 1] = undefined\n }\n }\n \n if (expand === 'left') {\n if (toY \u003e fromY) {\n arrows[toX][toY] = 180\n arrows[toX - 1][toY] = undefined\n arrows[fromX - 1][fromY] = undefined\n } else {\n arrows[toX][toY] = 0\n arrows[toX - 1][toY] = undefined\n arrows[fromX - 1][fromY] = undefined\n }\n }\n \n if (expand === 'right') {\n if (toY \u003e fromY) {\n arrows[toX][toY] = 180\n arrows[toX + 1][toY] = undefined\n arrows[fromX + 1][fromY] = undefined\n } else {\n arrows[toX][toY] = 0\n arrows[toX + 1][toY] = undefined\n arrows[fromX + 1][fromY] = undefined\n }\n }\n }\n }\n \n return false\n}\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":197,"hashid":"wdks4egr3","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":6092662,"file_name":"arrow.png","project_id":301125,"asset_id":421340,"created_at":"2021-10-01T11:15:53.103+08:00","updated_at":"2021-10-01T11:15:53.103+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦