基于javascript canvas實(shí)現(xiàn)五子棋游戲
本文實(shí)例為大家分享了基于canvas的五子棋的具體代碼,供大家參考,具體內(nèi)容如下
第一部分:核心類Gobang
屬性:
this.box = box; // 存放五子棋的容器 this.canvas = null; // 畫(huà)布 this.ctx = null; this.size = 600; // 棋盤(pán)大小 this.cellNum = 20; // 單行棋格數(shù)量 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 = []; // 玩家累計(jì)棋子 this.aiPieces = []; // 電腦累計(jì)棋子 this.isMyTurn = true; // 先手 this.curPos = [this.cellNum/2-1, this.cellNum/2-1]; // 當(dāng)前點(diǎn)擊位置 this.timeId = null; // 定時(shí)器id
方法:
init// 初始化方法,獲取canvas設(shè)置寬高,獲取ctx createChessboard// 創(chuàng)建背景棋盤(pán) drawPiece// 畫(huà)一個(gè)棋子 clearPiece// 清除棋子 registClick// 注冊(cè)鼠標(biāo)點(diǎn)擊事件,主要的邏輯函數(shù) isIn// 判斷否在所下的棋子里面 isInAll// 判斷是否在所有下的棋子里面 isFull// 是否下滿 aiPutPiece// 電腦落子,只是簡(jiǎn)單的實(shí)現(xiàn)了,獲取玩家落子位子周?chē)桓竦碾S機(jī)位置 putPiece// 實(shí)現(xiàn)下棋的函數(shù) isWin// 勝利判斷,個(gè)人人為比較男一點(diǎn)點(diǎn)的算法 run// 運(yùn)行,類的入口函數(shù),里面調(diào)用了,·init·/createChessBoard/registClick方法
第二部分:源代碼
Gobang.js
/** 五子棋 **/ function Gobang(box){ this.box = box; // 存放五子棋的容器 this.canvas = null; // 畫(huà)布 this.ctx = null; this.size = 600; // 棋盤(pán)大小 this.cellNum = 20; // 單行棋格數(shù)量 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 = []; // 玩家累計(jì)棋子 this.aiPieces = []; // 電腦累計(jì)棋子 this.isMyTurn = true; // 先手 this.curPos = [this.cellNum/2-1, this.cellNum/2-1]; // 當(dāng)前點(diǎn)擊位置 this.timeId = null; // 定時(shí)器id // 初始化方法 this.init = function(){ // 創(chuàng)建canvas this.canvas = document.createElement("canvas"); // 設(shè)置寬高 this.canvas.width = this.canvas.height = this.size; // 加入到容器中 this.box.appendChild(this.canvas); // 獲取ctx this.ctx = this.canvas.getContext("2d"); }; // 創(chuàng)建背景棋盤(pán) this.createChessboard = function(){ // ----------- 邊框 ----------- this.ctx.lineWidth = 10; this.ctx.lineJoin = "round"; this.ctx.strokeRect(0, 0, this.size, this.size); // ----------- 創(chuàng)建棋盤(pán) ----------- this.ctx.lineWidth = 1; for (var i = 0; i <= this.cellNum; i++) { // 畫(huà)橫線 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(); // 畫(huà)豎線 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(); } }; // 畫(huà)一個(gè)棋子 this.drawPiece = (x, y, type=0) => { // 根據(jù)坐標(biāo)計(jì)算出圖中位置 var posX, posY; posX = this.padding + x * this.cellSize; posY = this.padding + y * this.cellSize; // 創(chuàng)建漸變色 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; // 畫(huà)圓 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); // 補(bǔ)上十字架 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(); }; // 注冊(cè)鼠標(biāo)點(diǎn)擊事件 this.registClick = function(){ this.canvas.addEventListener("click", (ev) => { // 將位置坐標(biāo),轉(zhuǎn)換為點(diǎn) 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; // 設(shè)置當(dāng)前位置 this.curPos = [x, y]; // 玩家落子 if(this.isMyTurn && !this.isInAll(this.curPos)){ // 判斷是否輪到玩家,并且下的位置是否重復(fù) this.putPiece(this.myPieces, this.curPos); } else return; // 輪到玩家的時(shí)候才能落子 // 判斷輸贏 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; // 目前,制作了一點(diǎn)功能,就是在玩家剛剛落子的周?chē)桓衤渥? // 1. 獲得隨機(jī)的周?chē)淖鴺?biāo) 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); } // 實(shí)現(xiàn)下棋的函數(shù) this.putPiece = (pieces, pos, type=0) => { this.drawPiece(pos[0], pos[1], type); pieces.push(pos); } // 勝利判斷 this.isWin = (pieces) => { /* * 這里不用遍歷棋盤(pán)來(lái)判斷四個(gè)方向,只需要判斷當(dāng)前落子位置的四個(gè)方向。 */ 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; // 處在左對(duì)角線上的判斷 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 /** 左對(duì)角線匹配失敗 **/ count = 0; // 處在右對(duì)角線上的判斷 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 /** 右對(duì)角線匹配失敗 **/ return false; }; // 運(yùn)行 this.run = function(){ // 初始化方法 this.init(); // 創(chuàng)建棋盤(pán) this.createChessboard(); // 注冊(cè)點(diǎn)擊事件 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="../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>
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用js canvas實(shí)現(xiàn)五子棋游戲
- JS+Canvas實(shí)現(xiàn)五子棋游戲
- js+canvas實(shí)現(xiàn)五子棋小游戲
- JS+canvas五子棋人機(jī)對(duì)戰(zhàn)實(shí)現(xiàn)步驟詳解
- JS canvas繪制五子棋的棋盤(pán)
- 原生JS+Canvas實(shí)現(xiàn)五子棋游戲
- JS+canvas實(shí)現(xiàn)的五子棋游戲【人機(jī)大戰(zhàn)版】
- 原生JS+Canvas實(shí)現(xiàn)五子棋游戲?qū)嵗?/a>
- Javascript和HTML5利用canvas構(gòu)建Web五子棋游戲?qū)崿F(xiàn)算法
- js canvas實(shí)現(xiàn)五子棋小游戲
相關(guān)文章
js實(shí)現(xiàn)權(quán)限樹(shù)的更新權(quán)限時(shí)的全選全消功能
上一篇發(fā)了添加權(quán)限時(shí)的權(quán)限樹(shù)JS源碼,下面把更新時(shí)的也發(fā)給大家借鑒一下,因?yàn)楦聲r(shí)候牽扯到判斷已有權(quán)限等,所以,還要麻煩一些。2009-02-02JavaScript中清空數(shù)組的方法總結(jié)
本文給大家總結(jié)了三種js清空數(shù)組的方法,每種方法都與眾不同,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-12-12在js文件中寫(xiě)el表達(dá)式取不到值的原因及解決方法
在js文件中寫(xiě)el表達(dá)式取不到值,百度一下,將經(jīng)驗(yàn)總結(jié)如下,有類似情況的朋友可以參考下2013-12-12Ant Design Pro 下實(shí)現(xiàn)文件下載的實(shí)現(xiàn)代碼
這篇文章主要介紹了Ant Design Pro 下實(shí)現(xiàn)文件下載的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12一個(gè)JavaScript用逗號(hào)分割字符串實(shí)例
分割字符串的方法有很多,這篇文章主要介紹了一個(gè)JavaScript用逗號(hào)分割字符串實(shí)例,需要的朋友可以參考下2014-09-09uniapp實(shí)現(xiàn)全局設(shè)置字體大小(小中大的字體切換)
隨著UniApp的流行,越來(lái)越多的開(kāi)發(fā)者選擇使用它來(lái)構(gòu)建跨平臺(tái)應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)全局設(shè)置字體大小(小中大的字體切換)的相關(guān)資料,需要的朋友可以參考下2023-06-06自己編寫(xiě)的支持Ajax驗(yàn)證的JS表單驗(yàn)證插件
創(chuàng)建一個(gè)JavaScript表單驗(yàn)證插件,可以說(shuō)是一個(gè)繁瑣的過(guò)程,涉及到初期設(shè)計(jì)、開(kāi)發(fā)與測(cè)試等等環(huán)節(jié)。實(shí)際上一個(gè)優(yōu)秀的程序員不僅是技術(shù)高手,也應(yīng)該是善假于外物的。本文介紹的這個(gè)不錯(cuò)的JavaScript表單驗(yàn)證插件,支持ajax驗(yàn)證,有需要的小伙伴可以參考下2015-05-05