代码下载
HTML代码
">

首頁  >  文章  >  web前端  >  純JS實作五子棋遊戲相容各瀏覽器(附源碼)_javascript技巧

純JS實作五子棋遊戲相容各瀏覽器(附源碼)_javascript技巧

WBOY
WBOY原創
2016-05-16 17:35:221551瀏覽

純JS五子棋(各瀏覽器兼容)
效果圖:
純JS實作五子棋遊戲相容各瀏覽器(附源碼)_javascript技巧 
代碼下載
HTML代碼

複製程式碼 程式碼如下:





五子棋




.wrapper {
寬度:600px;
位置:相對;
}
/* 棋盤*/
div.chessboard {
margin: 30px 0 0 50px;
寬度:542px;
背景:url(http ://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/chessboard.png) no-repeat 14px 14px rgb(250, 250, 250);
溢出:隱藏;
盒子陰影:2p 2px 8px #888;
-webkit-box-shadow:2px 2px 8px #888;
-moz-box-shadow:2px 2px 8px #888;
}
div.chessboard divo>div. >float: left;
寬度:36px;
高度:36px;
邊框頂部:0px 實心#ccc;
左邊框:0px 實心#ccc;
右邊界:0; 🎜>邊框底部:0;
遊標:指針;
}
/* 棋子*/
div.chessboard div.black {
背景:url(http://sandbox.runjs .cn/uploads/rs/102/r2dy3tyw/black.png ) 無重複4px 4px;
}
div.chessboard div.white {
背景:url(http://cn /uploads/rs/102/r2dy3tyw/white.png) no-repeat 4px 4px;
}
div.chessboard div.hover {
背景:url(http://sandbox.runjs.cn/cn uploads/rs/102/r2dy3tyw/hover.png) no-repeat 1px 1px;
}
div.chessboard div.hover-up {
背景:url(http://cn /uploads/rs/102/r2dy3tyw/hover_up.png) no-repeat 1px 1px ;
}
div.chessboard div.hover-down {
背景:url(http://sandbox.chesrunjs. cn/uploads/rs/102/r2dy3tyw/hover_down.png) no-repeat 1px 1px ;
}
div.chessboard div.hover-up-left {
背景:url(http://chessandbox .runjs.cn/uploads/rs/102/r2dy3tyw/hover_up_left.png) no-repeat 1 像素1 像素;
}
div.chessboard div.hover-up-right {
背景:url( http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_up_right.png) no-repeat 1 像素1 像素;
}
div.chessboard div.hover-left {
背景:url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_left.png) no-repeat 1px 1px ;
}
div.chessboard div.hover-right {
}
div.chessboard div.hover-right {
背景:url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_right.png) no-repeat 1px 1px ;
}
div.chessboard div.hover-down-left {
背景:url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_down_left.png) no-repeat 1 像素1 像素;
}
div.chessboard div.hhover -down-right {
背景:url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_down_right.png) no-repeat 1 像素1 像素;
}
div .chessboard div.white-last {
背景:url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/white_last.png) no-repeat 4px 4px ;
}
}
div.chessboard div.black-last {
背景:url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/black_last.png) no-repeat 4px 4px ;
}
/* 右側*/
div.operating-panel {
位置:絕對;
左:610px;
頂部:150px;
寬度:200px;
文字對齊:居中;
}
.operating-panel a {
顯示:內聯塊;
內邊距:10px 15px;
邊距底部:39px;
右邊距:8px ;
左邊距:8px;
背景:rgb(100, 167, 233);
文字裝飾:無;
顏色:#333;
字體粗體:粗體;
顏色:#333;
字體粗體:粗體;
顏色:#333;
字體粗體:粗體;
顏色:#333;
字體粗體:粗體;
顏色:#333;
字體粗體>字體大小:16px;
font-family: "微軟雅黑", "宋體";
}
.operating-panel a:hover {
背景: rgb(48, 148, 247 );
文字裝飾:無;
}
.operating-panel a.disable, .operating-panel a.disable:hover {
遊標:預設;
背景:rgb(1977 , 203, 209);
顏色:rgb(130, 139, 148);
}
.operating-panel a.selected {
邊框:5pxsolid #F3C242;
:5px 10px;
}
#result_tips {
顏色:#CE4242;
字體大小:26px;
font-family: "微軟雅黑";
} } #result_info { margin-bottom: 26px; } 風格> $(document).ready(function() { FiveChess.init( ); });
var fiveChess = {
NO_CHESS: 0,
BLACK_CHESS: -1,
WHITE_CHESS: 1,
chessArr: [], //棋子記錄
chessBoardHt: "ches" ,
humanPlayer: "black",//玩家棋子顏色
AIPlayer: "white",//AI棋子顏色
isPlayerTurn: true, //輪到player下棋
totalGames: cookieHandle. getCookie("totalGames") || 0,//總局數
winGames: cookieHandle.getCookie("winGames") || 0,//玩家贏局數
isGameStart: false,//遊戲已經開始
isGameOver: false, //遊戲結束
playerLastChess: [], //玩家最後下子位置
AILastChess: [], //AI最後下子位置
init: function () {
this.chessBoardHtml = $("div.chessboard").html();
var _fiveChess = this;
//右側操作按鈕
$(".operating-panel a").click( function (event) {
event.preventDefault();
var id = $(this).attr("id");
if (_fiveChess.isGameStart && id !== "replay_btn" ) { return; }//正在遊戲不操作
switch (id) {
case "black_btn":
_fiveChess.humanPlayer = "black";
_fiveChess.AIPlayer = "white";
break;
case "white_btn":
_fiveChess.humanPlayer = "white";
_fiveChess.AIPlayer = "black";
break;
case "first_move_bivefirst_move isPlayerTurn = true;
break;
case "second_move_btn":
_fiveChess.isPlayerTurn = false;
break;
case "replay_btn":
break;
case "replay_btn":
if (_fiveChess.isGameStart) {//點重玩
_fiveChess.gameOver();
}
else { //點開始
_fiveChess.gameStart();
}
_fiveChess.gameStart();
}
)
break;
}
if (id !== "replay_btn") {
$(this).addClass("selected").siblings().removeClass("selected");
}
});
this.resetChessBoard();
$("#result_info").html("勝率:" (this.winGames * 100 / this.totalGames | 0) "%") ;
},
//重置棋盤
resetChessBoard: function () {
$("div.chessboard").html(this.chessBoardHtml);
$("#result_tips ").html("");
this.isGameOver = false;
this.isPlayerTurn = $("#first_move_btn").hasClass("selected");
//初始化棋子狀態
var i, j;
for (i = 0; i this.chessArr[i] = [];
for (j = 0; j this.chessArr[i][j] = this.NO_CHESS;
}
}
//player下棋事件
var _fiveChess = this;
$("div .chessboard div").click(function () {
if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) {
return;
}
if (!_fiveChess.isGameStart) {🎜> >_fiveChess.gameStart();
}
var index = $(this).index(),
i = index / 15 | 0,
j = index % 15;
if (_fiveChess.chessArr[i][j] === _fiveChess.NO_CHESS) {
_fiveChess.playChess(i, j, _fiveChess.humanPlayer);
if (i === 0 && j === 0 ) {
$(this).removeClass("hover-up-left");
}
else if (i === 0 && j === 14) {
$(this ).removeClass("hover-up-right");
}
else if (i === 14 && j === 0) {
$(this).removeClass("hover-down -left");
}
else if (i === 14 && j === 14) {
$(this).removeClass("hover-down-right");
}
else if (i === 0) {
$(this).removeClass("hover-up");
}
else if (i === 14) {
$(this).removeClass("hover-down");
}
else if (j === 0) {
$(this).removeClass("hover-left");
}
else if (j === 14) {
$(this).removeClass("hover-right");
}
else {
$(this). removeClass("hover");
}
_fiveChess.playerLastChess = [i, j];
_fiveChess.playerWinOrNot(i, j);
}
})
/ /滑鼠在棋盤上移動效果
$("div.chessboard div").hover(
function () {
if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) { return; } var index = $(this).index(),
i = index / 15 | 0,
j = index % 15;
if (_fiveChess.chessArr[i][j] === _fiveChess.NO_CHESS) {
if (i === 0 && j === 0) {
$(this).addClass("hover-up-left");
}
else if (i === 0 && j === 14) {
$(this).addClass("hover-up-right");
}
else if (i === 14 && j === 0) {
$(this).addClass("hover-down-left");
}
else if (i === 14 && j === 14) {
$(this).addClass("hover-down-right");
}
else if (i === 0) {
$(this).addClass("hover-up" );
}
else if (i === 14) {
$(this).addClass("hover-down");
}
else if (j === 0) {
$(this).addClass("hover-left");
}
else if (j === 14) {
$(this).addClass("hover- right");
}
else {
$(this).addClass("hover");
}
}
},
function () {
if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) { return; }
var index = $(this).index(),
i = index / 15 | 0,
j = index % 15;
if (i === 0 && j === 0) {
$(this).removeClass("hover-up-left");
}
else if (i === 0 && j === 14) {
$(this).removeClass("hover-up-right");
}
else if (i === 14 && j == = 0) {
$(this).removeClass("hover-down-left");
}
else if (i === 14 && j === 14) {
$ (this).removeClass("hover-down-right");
}
else if (i === 0) {
$(this).removeClass("hover-up");
}
else if (i === 14) { $(this).removeClass("hover-down"); }
else if (j === 0) {
$(this).removeClass("hover-left");
}
else if (j === 14) {
$(this).removeClass("hover-right");
}
else {
$(this).removeClass("hover");
}
}
);
},
gameStart: function () {
this.totalGames ;
cookieHandle.setCookie({ name: "totalGames", value: this.totalGames, expiresHours: 365 * 24 });
//AI先手
if (!this.isPlayerTurn) {
this.AImoveChess();
}
this.isGameStart = true;
$(".operating-panel p a").addClass("disable");
$("#replay_btn").html("重玩");
},
gameOver: function () {
this.isGameStart = false;
$(".operating-panel a").removeClass("disable");
$("#replay_btn").html("開始");
$("#result_info").html("勝率:" (this.winGames * 100 / this.totalGames | 0) "%");
},
//下棋 i行,j列,顏色顏色
playChess: function (i, j, color) {
this.chessArr[i][j] = color == this.BLACK_CHESS : this.WHITE_CHESS; =「黑色」?
if (color === this.AIPlayer) {
$("div.chessboard div." color "-last").addClass(color).removeClass(color "-last");
$("div.chessboard div:eq(" (i * 15 j) ")").addClass(color "-last");
}
else {
$("div.chessboard div:eq(" (i * 15 j) ")").addClass(color);
}
},
//玩家是否取勝
playerWinOrNot: function (i, j) {
var nums = 1, //連續個數
chessColor = this. this.BLACK_CHESS : this.WHITE_CHESS,
m, n;
//x方向
for (m = j - 1; m >= 0; m--) {
if (this.chessArr[i][m] === chessColor) {
數字;
}
其他{
規則;
}
}
for (m = j 1; m if (this.chessArr [i][m] === chessColor) {
nums ;
}
其他 {
規則;
}
}
if (nums >= 5) {
this.playerWin();
回傳;
}
其他 {
nums = 1;
}
//y方向
for (m = i - 1; m >= 0; m--) {
if (this.chessArr[m][j] === chessColor ) {
數字;
}
其他 {
規則;
}
}
for (m = i 1; m if (this.chessArr[m] [j] === chessColor) {
nums ;
}
其他 {
規則;
}
}
if (nums >= 5) {
this.playerWin();
回傳;
}
其他 {
nums = 1;
}
//左斜方向
for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
if (this.chessArr[m][n] === chessColor) {
nums ;
}
其他 {
規則;
}
}
for (m = i 1, n = j 1; m if (this.chessArr[m][ n] === chessColor) {
nums ;
}
其他 {
規則;
}
}
if (nums >= 5) {
this.playerWin();
回傳;
}
其他 {
nums = 1;
}
//右斜方向
for (m = i - 1, n = j 1; m >= 0 && n if (this .chessArr[m][n] === chessColor) {
nums ;
}
其他 {
規則;
}
}
for (m = i 1, n = j - 1; m = 0; m , n --) {
if (this.chessArr [m][n] === chessColor) {
nums ;
}
其他 {
規則;
}
}
if (nums >= 5) {
this.playerWin();
返回;
}
this.AImoveChess();
},
playerWin: function () {
this.winGames ;
cookieHandle.setCookie({ name: "winGames", value: this.winGames, expiresHours: 365 * 24 });
this.showResult(true);
this.gameOver();
},
//AI下棋
AImoveChess: function () {
this.isPlayerTurn = false;
var maxX = 0,
maxY = 0,
maxWeight = 0,
i, j, tem;
for (i = 14; i >= 0; i--) {
for (j = 14; j >= 0; j--) {
if (this.chessArr[i] ] [j] !== this.NO_CHESS) {
繼續;
}
tem = this.computeWeight(i, j);
if (tem > maxWeight) {
maxWeight = tem;
maxX = i;
maxY = j;
}
}
}
this.playChess(maxX, maxY, this.AIPlayer);
this.AILastChess = [maxX, maxY];
if ((maxWeight >= 100000 && maxWeight = 500000)) {
this.showResult(false);
this.gameOver();
}
else {
this.isPlayerTurn = true;
}
},
showResult: function(isPlayerWin) {
if (isPlayerWin) {
$("#result_tips").html("恭喜你!行為");
}
else {
$("#result_tips").html("哈哈,你輸咯!");
}
this.isGameOver = true;
this.showWinChesses(isPlayerWin);
},
/ 標記顯示模擬棋子
showWinChesses: function (🎜>/ 標記顯示模擬棋子
showWinChesses: function (isPlayerWin) {
var nums = 1, //連續棋子個數
lineChess = [],//連續棋子位置
i,
j,
chessColor,
m, n; if ( isPlayerWin) {
chessColor = this. humanPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
i = this.playerLastChess[0];
j = this.player ]LastChess [1] ;
}
else {
chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
i = this.AILastChess[0]; .AILastChess[1] ;
}
$("div.chessboard div." this.AIPlayer "-last").addClass(this.AIPlayer).removeClass(this.AIPlayer "-last ")
//x方向
lineChess[0] = [i];
lineChess[1] = [j];
for (m = j - 1; m >= 0 ; m--) {
if ( this.chessArr[i][m] === chessColor) {
lineChess[0][nums] = i
lineChess[1][nums] = m
nums; ;
}
else {
break;}
for (m = j 1; m if (this.chessArr[i][m] === chessColor) {
lineChess[0][nums] = i;
lineChess[1][nums] = m;
數字;
}
其他 {
規則;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess) [0][k] * 15 lineChess[1][k], isPlayerWin ?這。 humanPlayer : this.AIPlayer);
}
回傳;
}
//y方向
nums = 1;
lineChess[0] = [i];
lineChess[1] = [j];
for (m = i - 1; m >= 0; m--) {
if (this.chessArr[m][j] === chessColor) {
lineChess[0][數] = 公尺;
lineChess[1][nums] = j;
數字;
}
其他 {
規則;
}
}
for (m = i 1; m if (this .chessArr[m][j] === chessColor) {
lineChess [0][數字] = m;
lineChess[1][nums] = j;
數字;
}
其他 {
規則;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess) [0][k] * 15 lineChess[1][k], isPlayerWin ?這。 humanPlayer : this.AIPlayer);
}
回傳;
}
//左斜方向
nums = 1;
lineChess[0] = [i];
lineChess[1] = [j];
for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
if (this.chessArr[m][ n] === chessColor) {
lineChess[0][nums] = m;
lineChess[1][nums] = n;
數字;
}
其他 {
規則;
}
}
for (m = i 1, n = j 1; m if (this.chessArr[m][ n] === chessColor) {
lineChess[0][nums] = m;
lineChess[1][nums] = n;
數字;
}
其他 {
規則;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess) [0][k] * 15 lineChess[1][k], isPlayerWin ?這。 humanPlayer : this.AIPlayer);
}
回傳;
}
//右斜方向
nums = 1;
lineChess[0] = [i];
lineChess[1] = [j];
for (m = i - 1, n = j 1; m >= 0 && n if (this.chessArr[m][n] == = chessColor ) {
lineChess[0][nums] = m;
lineChess[1][nums] = n;
數字;
}
其他 {
規則;
}
}
for (m = i 1, n = j - 1; m = 0; m , n--) {
if (this.chessArr [m][n] === chessColor) {
lineChess[0][nums] = m;
lineChess[1][nums] = n;
數字;
}
其他 {
規則;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess) [0][k] * 15 lineChess[1][k], isPlayerWin ?這。 humanPlayer : this.AIPlayer);
}
}
},
markChess: 函數 (pos, color) {
$("div.chessboard div:eq(" pos ")").removeClass(color). addClass(顏色“-最後”);
},
//下子到i,j X方向結果: 多少連子兩邊是否截斷
putDirectX: function (i, j, chessColor) {
var m, n,
nums = 1,
side1 = false,
side2 = false;
for (m = j - 1; m >= 0; m--) {
if (this.chessArr[i] [m] === chessColor) {
nums ;
}
else {
if (this.chessArr[i][m] === this.NO_CHESS) {
side1 = true;
}
休息;
}
}
for (m = j 1; m if (this.chessArr[i][m] == = chessColor) {
nums ;
}
else {
if (this.chessArr[i][m] === this.NO_CHESS) {
side2 = true;
}
休息;
}
}
return {"nums": nums, "side1": side1, "side2": side2};
},
//下子到i,j Y方向結果
putDirectY: function (i, j, chessColor) {
var m, n,
nums = 1,
side1 = false,
side2 = false;
for (m = i - 1; m >= 0; m--) {
if (this.chessArr[m][j] === chessColor ) {
數字;
}
else {
if (this.chessArr[m][j] === this.NO_CHESS) {
side1 = true;
}
休息;
}
}
for (m = i 1; m if (this.chessArr[m][j] == = chessColor) {
nums ;
}
else {
if (this.chessArr[m][j] === this.NO_CHESS) {
side2 = true;
}
休息;
}
}
return {"nums": nums, "side1": side1, "side2": side2};
},
//下子到i,j XY方向 结果
putDirectXY: function (i, j, chessColor) {
var m, n,
nums = 1,
side1 = false,
side2 = false;
for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
if (this.chessArr[m][n] === chessColor) {
nums++;
}
else {
if (this.chessArr[m][n] === this.NO_CHESS) {
side1 = true;
}
break;
}
}
for (m = i + 1, n = j + 1; m if (this.chessArr[m][n] === chessColor) {
nums++;
}
else {
if (this.chessArr[m][n] === this.NO_CHESS) {
side2 = true;
}
break;
}
}
return {"nums": nums, "side1": side1, "side2": side2};
},
putDirectYX: function (i, j, chessColor) {
var m, n,
nums = 1,
side1 = false,
side2 = false;
for (m = i - 1, n = j + 1; m >= 0 && n if (this.chessArr[m][n] === chessColor) {
nums++;
}
else {
if (this.chessArr[m][n] === this.NO_CHESS) {
side1 = true;
}
break;
}
}
for (m = i + 1, n = j - 1; m = 0; m++, n--) {
if (this.chessArr[m][n] === chessColor) {
nums++;
}
else {
if (this.chessArr[m][n] === this.NO_CHESS) {
side2 = true;
}
break;
}
}
return {"nums": nums, "side1": side1, "side2": side2};
},
//计算下子至i,j的权重
computeWeight: function (i, j) {
var weight = 14 - (Math.abs(i - 7) + Math.abs(j - 7)), //基于棋盘位置权重
pointInfo = {},//某点下子后连子信息
chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
//x方向
pointInfo = this.putDirectX(i, j, chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
pointInfo = this.putDirectX(i, j, -chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
//y方向
pointInfo = this.putDirectY(i, j, chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
pointInfo = this.putDirectY(i, j, -chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
//左斜方向
pointInfo = this.putDirectXY(i, j, chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
pointInfo = this.putDirectXY(i, j, -chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
//右斜方向
pointInfo = this.putDirectYX(i, j, chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
pointInfo = this.putDirectYX(i, j, -chessColor);
weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
return weight;
},
//权重方案 独:两边为空可下子,单:一边为空
weightStatus: function (nums, side1, side2, isAI) {
var weight = 0;
switch (nums) {
case 1:
if (side1 && side2) {
weight = isAI ? 15 : 10;//独一
}
break;
case 2:
if (side1 && side2) {
weight = isAI ? 100 : 50;//独二
}
else if (side1 || side2) {
weight = isAI ? 10 : 5;//单二
}
break;
case 3:
if (side1 && side2) {
weight = isAI ? 500 : 200;//独三
}
else if (side1 || side2) {
weight = isAI ? 30 : 20;//单三
}
break;
case 4:
if (side1 && side2) {
weight = isAI ? 5000 : 2000;//独四
}
else if (side1 || side2) {
weight = isAI ? 400 : 100;//单四
}
break;
case 5:
weight = isAI ? 100000 : 10000;//五
break;
default:
weight = isAI ? 500000 : 250000;
break;
}
return weight;
}
};
























































































































































































































































黑子
白子



先手
後手


開始

胜率:100%






純JS實作五子棋遊戲相容各瀏覽器(附源碼)_javascript技巧
純JS實作五子棋遊戲相容各瀏覽器(附源碼)_javascript技巧
preload
preload
preload
preload
preload
preload
preload
preload
preload
preload
preload



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn