오자棋 .wrapper {
너비: 600px;
위치: 친척;
}
/* 棋盘 */
div.chessboard {
margin: 30px 0 0 50px;
너비: 542px;
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/chessboard.png) 반복 없음 14px 14px rgb(250, 250, 250);
오버플로: 숨김;
상자 그림자: 2px 2px 8px #888;
-webkit-box-shadow: 2px 2px 8px #888;
-moz-box-shadow: 2px 2px 8px #888;
}
div.chessboard div {
float: 왼쪽;
너비: 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://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/white.png) 반복 없음 4px 4px;
}
div.chessboard div.hover {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover.png) 반복 없음 1px 1px;
}
div.chessboard div.hover-up {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_up.png) 반복 없음 1px 1px ;
}
div.chessboard div.hover-down {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_down.png) 반복 없음 1px 1px ;
}
div.chessboard div.hover-up-left {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_up_left.png) 반복 없음 1픽셀 1픽셀;
}
div.chessboard div.hover-up-right {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_up_right.png) 반복 없음 1픽셀 1픽셀;
}
div.chessboard div.hover-left {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_left.png) 반복 없음 1px 1px ;
}
div.chessboard div.hover-right {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_right.png) 반복 없음 1px 1px ;
}
div.chessboard div.hover-down-left {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_down_left.png) 반복 없음 1픽셀 1픽셀;
}
div.chessboard div.hover-down-right {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/hover_down_right.png) 반복 없음 1픽셀 1픽셀;
}
div.chessboard div.white-last {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/white_last.png) 반복 없음 4px 4px ;
}
div.chessboard div.black-last {
배경: url(http://sandbox.runjs.cn/uploads/rs/102/r2dy3tyw/black_last.png) 반복 없음 4px 4px ;
}
/* 右侧 */
div.operating-panel {
위치: 절대;
왼쪽: 610px;
상단: 150px;
너비: 200px;
텍스트 정렬: 가운데;
}
.operating-panel a {
디스플레이: 인라인 블록;
패딩: 10px 15px;
여백 하단: 39px;
여백 오른쪽: 8px;
왼쪽 여백: 8px;
배경: rgb(100, 167, 233);
텍스트 장식: 없음;
색상: #333;
글꼴 두께: 굵게;
글꼴 크기: 16px;
글꼴군: "微软雅黑", "宋体";
}
.operating-panel a:hover {
배경: rgb(48, 148, 247);
텍스트 장식: 없음;
}
.operating-panel a.disable, .operating-panel a.disable:hover {
cursor: default;
배경: rgb(197, 203, 209);
색상: rgb(130, 139, 148);
}
.operating-panel a.selected {
테두리: 5px 솔리드 #F3C242;
패딩: 5px 10px;
}
#result_tips {
색상: #CE4242;
글꼴 크기: 26px;
글꼴군: "微软雅黑";
}
#result_info {
여백 하단: 26px;
}
$(document).ready(function() {
fiveChess.init();
});
var fiveChess = {
NO_CHESS: 0,
BLACK_CHESS: -1,
WHITE_CHESS: 1,
chessArr: [], //체스 조각 기록
chessBoardHtml: " " ,
humanPlayer: "black",//Player 체스 색상
AIPlayer: "white",//AI 체스 색상
isPlayerTurn: true, //플레이어가 체스를 둘 차례입니다
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; }//게임 중이며 작동하지 않음
스위치(id) {
case "black_btn":
_fiveChess.humanPlayer = "black"; _fiveChess.AIPlayer = "white";
case "white_btn":
_fiveChess.humanPlayer = "white";
break; >case "first_move_btn":
_fiveChess.isPlayerTurn = true;
break;
case "second_move_btn":
_fiveChess.isPlayerTurn = false;
_fiveChess.gameOver();
}
else { //시작하려면 클릭하세요
_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;
}
}
//플레이어 체스 이벤트
var _fiveChess = this; function () {
if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) {
return;
}
if (!_fiveChess.isGameStart) {
_fiveChess.gameStart(); 🎜>}
var index = $(this).index(),
i = 인덱스 / 15 | 0,
j = 인덱스 % 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 = 인덱스 / 15 | 0,
j = 인덱스 % 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 |
j = index % 15; (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( "오른쪽 아래로 가리키기");
}
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列,color颜color
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. humanPlayer === "검은색" ? this.BLACK_CHESS : this.WHITE_CHESS,
m, n;
//x方向
for (m = j - 1; m >= 0; m--) {
if (this.chessArr[i][m] === chessColor) {
숫자 ;
}
else {
break;
}
}
for (m = j 1; m if (this.chessArr[i][m] === chessColor) {
nums ;
}
else {
break;
}
}
if (nums >= 5) {
this.playerWin();
반환;
}
else {
숫자 = 1;
}
//y方向
for (m = i - 1; m >= 0; m--) {
if (this.chessArr[m][j] === chessColor) {
숫자 ;
}
else {
break;
}
}
for (m = i 1; m if (this.chessArr[m][j] === chessColor) {
nums ;
}
else {
break;
}
}
if (nums >= 5) {
this.playerWin();
반환;
}
else {
숫자 = 1;
}
//左斜方向
for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
if (this.chessArr[m][n] === chessColor) {
nums ;
}
else {
break;
}
}
for (m = i 1, n = j 1; m if (this.chessArr[m][ n] === chessColor) {
nums ;
}
else {
break;
}
}
if (nums >= 5) {
this.playerWin();
반환;
}
else {
숫자 = 1;
}
//右斜方向
for (m = i - 1, n = j 1; m >= 0 && n if (this.chessArr[m][n] === chessColor) {
nums ;
}
else {
break;
}
}
for (m = i 1, n = j - 1; m = 0; m , n--) {
if (this.chessArr [m][n] === chessColor) {
nums ;
}
else {
break;
}
}
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: 함수(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.playerLastChess [1];
}
else {
chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
i = this.AILastChess[0]; >j = this.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 lineChess[0][nums] = i;
lineChess[1][nums] = m;
숫자 ;
}
else {
break;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess [0][k] * 15 lineChess[1][k], isPlayerWin ? this.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][ 숫자] = m;
lineChess[1][nums] = j;
숫자 ;
}
else {
break;
}
}
for (m = i 1; m if (this.chessArr[m][j] === chessColor) {
lineChess [0][숫자] = m;
lineChess[1][nums] = j;
숫자 ;
}
else {
break;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess [0][k] * 15 lineChess[1][k], isPlayerWin ? this.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;
숫자 ;
}
else {
break;
}
}
for (m = i 1, n = j 1; m if (this.chessArr[m][ n] === chessColor) {
lineChess[0][nums] = m;
lineChess[1][nums] = n;
숫자 ;
}
else {
break;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess [0][k] * 15 lineChess[1][k], isPlayerWin ? this.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;
숫자 ;
}
else {
break;
}
}
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;
숫자 ;
}
else {
break;
}
}
if (nums >= 5) {
for (k = nums - 1; k >= 0; k--) {
this.markChess(lineChess [0][k] * 15 lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
}
}
},
markChess: function (pos, color) {
$("div.chessboard div:eq(" pos ")").removeClass(color). addClass(color "-last");
},
//下子到i,j X方向 结果: 多少连子 两边是否截断
putDirectX: function (i, j, chessColor) {
var m, n,
숫자 = 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 = 거짓,
side2 = 거짓;
for (m = i - 1; m >= 0; m--) {
if (this.chessArr[m][j] === chessColor) {
nums ;
}
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;
}
};