日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

基于javascript canvas實現五子棋游戲

瀏覽:198日期:2023-06-19 13:10:55

本文實例為大家分享了基于canvas的五子棋的具體代碼,供大家參考,具體內容如下

第一部分:核心類Gobang

屬性:

this.box = box; // 存放五子棋的容器this.canvas = null; // 畫布this.ctx = null;this.size = 600; // 棋盤大小this.cellNum = 20; // 單行棋格數量this.padding = this.size/this.cellNum; // padding值this.cellSize = (this.size-this.padding*2)/this.cellNum; // 棋格大小this.pieceSize = this.cellSize*3/4; // 棋子大小this.color = ['black', '#aaa']; // 棋子顏色this.myPieceType = null; // 玩家棋子類型this.aiPieceType = null; // 電腦棋子類型this.myPieces = []; // 玩家累計棋子this.aiPieces = []; // 電腦累計棋子this.isMyTurn = true; // 先手this.curPos = [this.cellNum/2-1, this.cellNum/2-1]; // 當前點擊位置this.timeId = null; // 定時器id

方法:

init// 初始化方法,獲取canvas設置寬高,獲取ctxcreateChessboard// 創建背景棋盤drawPiece// 畫一個棋子clearPiece// 清除棋子registClick// 注冊鼠標點擊事件,主要的邏輯函數isIn// 判斷否在所下的棋子里面isInAll// 判斷是否在所有下的棋子里面isFull// 是否下滿aiPutPiece// 電腦落子,只是簡單的實現了,獲取玩家落子位子周圍一格的隨機位置putPiece// 實現下棋的函數isWin// 勝利判斷,個人人為比較男一點點的算法run// 運行,類的入口函數,里面調用了,·init·/createChessBoard/registClick方法

第二部分:源代碼

Gobang.js

/** 五子棋 **/function Gobang(box){ this.box = box; // 存放五子棋的容器 this.canvas = null; // 畫布 this.ctx = null; this.size = 600; // 棋盤大小 this.cellNum = 20; // 單行棋格數量 this.padding = this.size/this.cellNum; // padding值 this.cellSize = (this.size-this.padding*2)/this.cellNum; // 棋格大小 this.pieceSize = this.cellSize*3/4; // 棋子大小 this.color = ['black', '#aaa']; // 棋子顏色 this.myPieceType = null; // 玩家棋子類型 this.aiPieceType = null; // 電腦棋子類型 this.myPieces = []; // 玩家累計棋子 this.aiPieces = []; // 電腦累計棋子 this.isMyTurn = true; // 先手 this.curPos = [this.cellNum/2-1, this.cellNum/2-1]; // 當前點擊位置 this.timeId = null; // 定時器id // 初始化方法 this.init = function(){ // 創建canvas this.canvas = document.createElement('canvas'); // 設置寬高 this.canvas.width = this.canvas.height = this.size; // 加入到容器中 this.box.appendChild(this.canvas); // 獲取ctx this.ctx = this.canvas.getContext('2d'); }; // 創建背景棋盤 this.createChessboard = function(){ // ----------- 邊框 ----------- this.ctx.lineWidth = 10; this.ctx.lineJoin = 'round'; this.ctx.strokeRect(0, 0, this.size, this.size); // ----------- 創建棋盤 ----------- this.ctx.lineWidth = 1; for (var i = 0; i <= this.cellNum; i++) { // 畫橫線 this.ctx.beginPath(); this.ctx.moveTo(this.padding, this.padding+i*this.cellSize); this.ctx.lineTo(this.size-this.padding, this.padding+i*this.cellSize); this.ctx.stroke(); // 畫豎線 this.ctx.beginPath(); this.ctx.moveTo(this.padding+i*this.cellSize, this.padding); this.ctx.lineTo(this.padding+i*this.cellSize, this.size-this.padding); this.ctx.stroke(); } }; // 畫一個棋子 this.drawPiece = (x, y, type=0) => { // 根據坐標計算出圖中位置 var posX, posY; posX = this.padding + x * this.cellSize; posY = this.padding + y * this.cellSize; // 創建漸變色 var grd = this.ctx.createRadialGradient(posX, posY, this.pieceSize/18, posX, posY, this.pieceSize); // type: 0, 黑棋 1, 白棋 grd.addColorStop(0, this.color[1-type]); grd.addColorStop(0, this.color[type]); this.ctx.fillStyle = grd; // 畫圓 this.ctx.beginPath(); this.ctx.arc(posX, posY, this.pieceSize/2, 0, 2*Math.PI); this.ctx.fill(); }; // 清除棋子 this.clearPiece = (x, y) => { // 清除棋子所在位置的像素 var posX, posY; posX = this.padding + x * this.cellSize - this.pieceSize/2; posY = this.padding + y * this.cellSize - this.pieceSize/2; this.ctx.clearRect(posX, posY, this.pieceSize, this.pieceSize); // 補上十字架 this.ctx.lineWidth = 1; // 豎線 this.ctx.beginPath(); this.ctx.moveTo(posX+this.pieceSize/2, posY); this.ctx.lineTo(posX+this.pieceSize/2, posY+this.pieceSize); this.ctx.stroke(); // 橫線 this.ctx.beginPath(); this.ctx.moveTo(posX, posY+this.pieceSize/2); this.ctx.lineTo(posX+this.pieceSize, posY+this.pieceSize/2); this.ctx.stroke(); }; // 注冊鼠標點擊事件 this.registClick = function(){ this.canvas.addEventListener('click', (ev) => { // 將位置坐標,轉換為點 var x = Math.round((ev.clientX - this.padding)/this.cellSize); x = x <= 0 ? 0 : x; x = x > this.cellNum ? this.cellNum : x; var y = Math.round((ev.clientY - this.padding)/this.cellSize); y = y <= 0 ? 0 : y; y = y > this.cellNum ? this.cellNum : y; // 設置當前位置 this.curPos = [x, y]; // 玩家落子 if(this.isMyTurn && !this.isInAll(this.curPos)){ // 判斷是否輪到玩家,并且下的位置是否重復this.putPiece(this.myPieces, this.curPos); } else return; // 輪到玩家的時候才能落子 // 判斷輸贏 if(this.isWin(this.myPieces)) {setTimeout(function(){alert('you win!');}, 100); return;} // 電腦落子 this.aiPutPiece(); // 判斷輸贏 if(this.isWin(this.aiPieces)) {setTimeout(function(){alert('robot win!');}, 100); return;} this.isMyTurn = true; }); }; // 判斷否在所下的棋子里面 this.isIn = (pos, arr) => { var len = arr.length; for(var i=0; i < len; i++){ if(pos[0] == arr[i][0] && pos[1] == arr[i][1]) return true; } return false; }; // 判斷是否在所有下的棋子里面 this.isInAll = (pos) => { return this.isIn(pos, this.myPieces.concat(this.aiPieces)); } // 是否下滿 this.isFull = () => { return (this.myPieces.length + this.aiPieces.length) == (this.cellNum+1) * (this.cellNum+1); }; // 電腦落子 this.aiPutPiece = ()=>{ var x, y; // 目前,制作了一點功能,就是在玩家剛剛落子的周圍一格落子 // 1. 獲得隨機的周圍的坐標 while(1){ x = this.curPos[0] + Math.pow(-1, parseInt(Math.random()*2)); y = this.curPos[1] + Math.pow(-1, parseInt(Math.random()*2)); if(x >=0 && x <=20 && y >= 0 && y <=20 && !this.isInAll([x, y])) break; } // 2. 落子 this.putPiece(this.aiPieces, [x, y], 1); } // 實現下棋的函數 this.putPiece = (pieces, pos, type=0) => { this.drawPiece(pos[0], pos[1], type); pieces.push(pos); } // 勝利判斷 this.isWin = (pieces) => { /* * 這里不用遍歷棋盤來判斷四個方向,只需要判斷當前落子位置的四個方向。 */ var x, y, count = 0; // 處在水平線上 判斷 x = this.curPos[0]-1; y = this.curPos[1]; while(1) if(this.isIn([x, y], pieces)) {count++; x--;} else break; // 左邊 x = this.curPos[0]+1; y = this.curPos[1]; while(1) if(this.isIn([x, y], pieces)) {count++; x++;} else break; // 右邊 if(count >= 4) return true; else /** 左右匹配失敗 **/ count = 0;// 處在垂直線上 判斷 比較四次 x = this.curPos[0]; y = this.curPos[1]-1; while(1) if(this.isIn([x, y], pieces)) {count++; y--;} else break; // 上邊 x = this.curPos[0]; y = this.curPos[1]+1; while(1) if(this.isIn([x, y], pieces)) {count++; y++;} else break; // 下邊 if(count >= 4) return true; else /** 上下匹配失敗 **/ count = 0; // 處在左對角線上的判斷 x = this.curPos[0]-1; y = this.curPos[1]-1; while(1) if(this.isIn([x, y], pieces)) {count++; x--; y--;} else break; // 左上 x = this.curPos[0]+1; y = this.curPos[1]+1; while(1) if(this.isIn([x, y], pieces)) {count++; x++; y++;} else break; // 右下 if(count >= 4) return true; else /** 左對角線匹配失敗 **/ count = 0; // 處在右對角線上的判斷 x = this.curPos[0]+1; y = this.curPos[1]-1; while(1) if(this.isIn([x, y], pieces)) {count++; x++; y--;} else break; // 右上 x = this.curPos[0]-1; y = this.curPos[1]+1; while(1) if(this.isIn([x, y], pieces)) {count++; x--; y++;} else break; // 左下 if(count >= 4) return true; else /** 右對角線匹配失敗 **/ return false; }; // 運行 this.run = function(){ // 初始化方法 this.init(); // 創建棋盤 this.createChessboard(); // 注冊點擊事件 this.registClick(); }}

五子棋.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>06-五子棋</title> <script src='http://m.b3g6.com/gobang.js'></script> <style> *{ margin: 0; padding: 0; } </style></head><body> <div id='box'></div> <script> var box = document.getElementById('box'); var gobang = new Gobang(box); gobang.run(); </script></body></html>

更多有趣的經典小游戲實現專題,分享給大家:

C++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲精品福利电影| 成人高清一区| 亚洲作爱视频| 99国产成+人+综合+亚洲欧美| 香蕉久久99| 在线国产一区二区| 99国产精品99久久久久久粉嫩| 国产精品99免费看| 欧美国产91| 国产亚洲精品自拍| 亚洲人成网77777色在线播放| 蜜臀av性久久久久蜜臀aⅴ流畅| 免费视频一区二区| 日韩三区四区| 美女视频黄免费的久久| 国产精品麻豆成人av电影艾秋 | 在线一区免费| 亚洲精品日本| 久久久国产精品入口麻豆| 国产日韩视频在线| 日本在线高清| 四虎成人精品一区二区免费网站| 国产欧美日韩综合一区在线播放| 欧产日产国产精品视频| 蜜臀va亚洲va欧美va天堂| 欧美国产中文高清| 欧美另类专区| 欧美精品三级在线| 精品1区2区3区4区| 国产高清亚洲| 蘑菇福利视频一区播放| 国产一区二区三区视频在线| 国产亚洲一区在线| 亚洲美女久久精品| 综合干狼人综合首页| 国产精品一在线观看| 国产精品久久久久久久久久10秀| 麻豆精品视频在线观看视频| 国产成人精品亚洲日本在线观看| 一区二区国产精品| 亚洲精品一区三区三区在线观看| 日韩av中文字幕一区二区| 日本精品影院| 国产一区二区三区日韩精品| 亚洲精品一区二区在线播放∴| 视频福利一区| zzzwww在线看片免费| 久久的色偷偷| 欧美一区二区三区久久精品| 欧美精品高清| 激情久久99| 捆绑调教美女网站视频一区| 日本午夜精品视频在线观看| 亚洲主播在线| 好看不卡的中文字幕| 色偷偷色偷偷色偷偷在线视频| 欧美激情aⅴ一区二区三区 | 亚洲二区三区不卡| 亚洲高清久久| 九色porny丨国产首页在线| 精品中文字幕一区二区三区四区| 日本少妇一区二区| 日本午夜精品久久久| 蜜臀av性久久久久蜜臀aⅴ四虎 | 麻豆成人91精品二区三区| 国产精品2023| 精品国产亚洲一区二区在线观看| 国产精品综合色区在线观看| 国产精品亚洲综合在线观看| 久久香蕉网站| 精品视频在线观看网站| 日韩免费福利视频| 欧美精品一区二区久久| 欧美午夜不卡影院在线观看完整版免费| re久久精品视频| 中文字幕中文字幕精品| 国产乱子精品一区二区在线观看| 综合亚洲视频| 日韩在线观看中文字幕| 欧美天堂一区| 深夜福利视频一区二区| 中文在线资源| 午夜日韩av| 国产精品多人| 久久亚洲专区| 四虎精品一区二区免费| 麻豆成人91精品二区三区| 蜜臀av免费一区二区三区| 婷婷亚洲成人| 天堂√8在线中文| 在线免费观看亚洲| 日韩黄色大片| 亚洲一区二区三区高清| 国产私拍福利精品视频二区| 午夜av不卡| 久久精品99国产精品| 国语精品一区| 四虎精品一区二区免费| 日韩欧美字幕| 久久精品超碰| 亚洲涩涩av| 亚洲精品福利电影| 欧美日韩一区二区国产| 亚洲欧美视频| 999精品色在线播放| 欧美日韩夜夜| 免费在线观看一区二区三区| 久久伊人久久| 国产精品亚洲四区在线观看| 国产精品美女| 久久久一二三| 国产高潮在线| 精品国产a一区二区三区v免费| 日本亚洲最大的色成网站www | 在线亚洲免费| 爽好久久久欧美精品| 国产成人精品一区二区三区在线| 欧美日韩在线精品一区二区三区激情综合 | 亚洲欧美日韩国产| 久久狠狠婷婷| 蜜桃久久久久| 欧美精品福利| 国产精品jk白丝蜜臀av小说| 日韩有吗在线观看| 亚洲毛片在线免费| 蜜桃免费网站一区二区三区| 欧美/亚洲一区| 国产在线成人| 在线手机中文字幕| 国产在线|日韩| 欧美手机在线| 久久福利精品| 日本中文字幕不卡| 欧美午夜网站| 国产一区二区三区国产精品| 国内自拍视频一区二区三区| 亚洲最新无码中文字幕久久| 日韩在线看片| 亚洲精品91| 欧美一区=区三区| 福利一区二区| 不卡一区2区| 亚洲免费福利一区| 婷婷综合福利| 美女久久久久久| 久久国产毛片| 一区二区精彩视频| 久久精品一本| 亚洲四虎影院| 综合一区二区三区| 精品三级国产| 尤物网精品视频| 国产日韩欧美三级| 日韩欧美一区二区三区在线观看| 在线日韩电影| 日韩不卡一区二区| 国产成人精品亚洲日本在线观看| 国产亚洲精品v| 国产日韩在线观看视频| 日本免费一区二区三区四区| 亚洲综合精品| 91欧美在线| 日本午夜精品| 成人av动漫在线观看| 国产福利资源一区| 中文欧美日韩| 成人午夜毛片| 欧美一区=区三区| 日韩一级网站| 成人国产精选| 日韩av二区在线播放| 91精品电影| 四虎4545www国产精品| 蜜桃av一区二区三区电影| 国产在线一区不卡| 国产亚洲一区| 亚洲精品少妇| 国产视频久久| 日本高清不卡一区二区三区视频| 久久国产精品色av免费看| 男人操女人的视频在线观看欧美 | 国产模特精品视频久久久久| 国产精品久久| 亚洲精品在线国产| 日韩午夜黄色| 久久国产毛片| 久久久夜夜夜| 久久99视频| 国产精品中文字幕制服诱惑| 日本不卡视频一二三区| 天使萌一区二区三区免费观看| 美女少妇全过程你懂的久久| 亚洲欧美伊人| 亚洲综合丁香| 亚洲精品美女| 欧美日韩一区二区三区不卡视频 | 国产情侣久久| 精品国产一区二区三区av片| 精品国产亚洲一区二区三区大结局| 欧美成人一二区|