欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實(shí)現(xiàn)中國象棋游戲

 更新時(shí)間:2022年05月10日 14:15:52   作者:停止的人間花火  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)中國象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)中國象棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

使用table元素作表格,div元素作象棋。

效果如下:

代碼如下:

<html>
<head>
<title>中國象棋</title>
<meta charset="UTF-8">
<style>
table{
?? ?margin:10px;
?? ?border-collapse:collapse;
}
table.board{
?? ?background-color:white;
}
table,td.board{
?? ?border:1px solid black;
}

td{
?? ?height:65px;
?? ?width:65px;
?? ?
}

div.pieces{
?? ?margin:5px;
?? ?height:50px;
?? ?width:50px;
?? ?border:1px solid black;
?? ?border-radius:25px;
?? ?text-align:center;
?? ?font-family:Cursive;
?? ?font-size:1.5em;
?? ?background-color:#FAF0E6;
}
div.red{
?? ?color:#F08080
}
div.green{
?? ?color:#32CD32
}
</style>
</head>
<body>
?? ?
</body>
<script>
?? ?
?? ?//--------------------->棋盤的樣子
?? ?(function(){
?? ??? ?table = document.createElement("table");
?? ??? ?table.classList.add("board");
?? ??? ?tBody = document.createElement("tBody");
?? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ?row = tBody.insertRow(i);
?? ??? ??? ?for(var j=0;j<8;j++){
?? ??? ??? ??? ?cell = row.insertCell(j);
?? ??? ??? ??? ?if(i!=4){cell.classList.add("board")}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?table.style.position="absolute";
?? ??? ?table.style.top="80px";
?? ??? ?table.style.left="280px";
?? ??? ?table.appendChild(tBody);
?? ??? ?document.body.appendChild(table);
?? ??? ?
?? ?})();
?? ?
?? ?//--------------------->生成實(shí)際在使用的表格
?? ?(function(){
?? ??? ?table = document.createElement("table");
?? ??? ?tBody = document.createElement("tBody");
?? ??? ?for(var i=0;i<10;i++){
?? ??? ??? ?row = tBody.insertRow(i);
?? ??? ??? ?for(var j=0;j<9;j++){
?? ??? ??? ??? ?cell = row.insertCell(j);
?? ??? ??? ??? ?cell.setAttribute("data-x",j);
?? ??? ??? ??? ?cell.setAttribute("data-y",i);
?? ??? ??? ??? ?cell.addEventListener("click",clickBoard,false);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?table.appendChild(tBody);
?? ??? ?table.style.position="absolute";
?? ??? ?table.style.top="50px";
?? ??? ?table.style.left="250px";
?? ??? ?document.body.appendChild(table);
?? ?})();
?? ?
?? ?
?? ?//開始游戲字樣
?? ?(function(){
?? ??? ?beginText = document.createElement("h1");
?? ??? ?beginText.style.display="inline";
?? ??? ?beginText.innerHTML="游戲開始";
?? ??? ?beginText.addEventListener("click",function(event){
?? ??? ??? ?chessboard.init();
?? ??? ??? ?if(!chessboard.status){
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?},false);
?? ??? ?beginText.style.position="absolute";
?? ??? ?beginText.style.top="200px";
?? ??? ?beginText.style.left="1000px";
?? ??? ?document.body.appendChild(beginText);
?? ??? ?
?? ?})();
?? ?
?? ?
?? ?//走棋方展示
?? ?(function(){
?? ??? ?turnText = document.createElement("h1");
?? ??? ?turnText.innerHTML="紅方";
?? ??? ?turnText.style.position="absolute";
?? ??? ?turnText.style.top="250px";
?? ??? ?turnText.style.left="1000px";
?? ??? ?document.body.appendChild(turnText);
?? ?})();
?? ?
?? ?//-------------------->綁定三個(gè)事件
?? ?
?? ?//點(diǎn)擊棋盤
?? ?function clickBoard(event){
?? ??? ?if(chessboard.status){
?? ??? ??? ?if(chessboard.curPiece){
?? ??? ??? ??? ?var x = parseInt(this.getAttribute("data-x"));
?? ??? ??? ??? ?var y = parseInt(this.getAttribute("data-y"));
?? ??? ??? ??? ?
?? ??? ??? ??? ?//嘗試移動(dòng)棋子
?? ??? ??? ??? ?chessboard.curPiece.move(x,y);
?? ??? ??? ?}
?? ??? ??? ?event.stopPropagation();
?? ??? ?}else{
?? ??? ??? ?//游戲結(jié)束,什么也不做
?? ??? ??? ?event.stopPropagation();//阻止冒泡事件,因?yàn)槊芭菀矝]有意義
?? ??? ?}
?? ??? ?
?? ?}
?? ?
?? ?//選中棋子
?? ?function choosePiece(event){
?? ??? ?if(chessboard.status){
?? ??? ??? ?if(chessboard.turn == !!this.getAttribute("data-team") && !chessboard.curPiece){
?? ??? ??? ?
?? ??? ??? ??? ?var x = parseInt(this.parentNode.getAttribute("data-x"));
?? ??? ??? ??? ?var y = parseInt(this.parentNode.getAttribute("data-y"));
?? ??? ??? ??? ?//選中棋子
?? ??? ??? ??? ?chessboard.curPiece = chessboard.pieces[x][y];
?? ??? ??? ??? ?
?? ??? ??? ??? ?chessboard.curPiece.piece.style.backgroundColor="#B0E0E6";
?? ??? ??? ??? ?
?? ??? ??? ??? ?//阻止冒泡事件
?? ??? ??? ??? ?//因?yàn)辄c(diǎn)擊鍵盤事件是移動(dòng)棋子位置事件,當(dāng)前沒有選中棋子,自然就不需要冒泡了
?? ??? ??? ??? ?event.stopPropagation();
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?//游戲結(jié)束,什么也不做
?? ??? ??? ?event.stopPropagation();//阻止冒泡事件,因?yàn)槊芭菀矝]有意義
?? ??? ?}
?? ??? ?
?? ?}
?? ?
?? ?//取消選中棋子
?? ?window.addEventListener("click",function(event){
?? ??? ?if(chessboard.status){
?? ??? ??? ?if(chessboard.curPiece){
?? ??? ??? ??? ?chessboard.curPiece.piece.style.backgroundColor="#FAF0E6";
?? ??? ??? ??? ?chessboard.curPiece=null;
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?//游戲結(jié)束,什么也不做
?? ??? ?}
?? ??? ?
?? ?},false);
?? ?
?? ?//棋盤
?? ?chessboard = {
?? ??? ?init:function(){
?? ??? ??? ?for(i=0;i<9;i++){this.pieces[i]=[]; }?
?? ??? ??? ?if(this.copyPieces){
?? ??? ??? ?
?? ??? ??? ??? ?//直接拷貝this.copyPieces即可
?? ??? ??? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ??? ??? ?if(this.copyPieces[i]){
?? ??? ??? ??? ??? ??? ?for(var j=0;j<10;j++){
?? ??? ??? ??? ??? ??? ??? ?this.pieces[i][j]=this.copyPieces[i][j];
?? ??? ??? ??? ??? ??? ??? ?this.pieces1[i][j]=this.copyPieces[i][j];//用于保存一步以后的棋子布局
?? ??? ??? ??? ??? ??? ??? ?if(table.rows[j].cells[i].childNodes.length){
?? ??? ??? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ??? ??? ?for(var l=0;l<table.rows[j].cells[i].childNodes.length;l++){
?? ??? ??? ??? ??? ??? ??? ??? ??? ?table.rows[j].cells[i].removeChild(table.rows[j].cells[i].childNodes[l]);
?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?if(this.pieces[i][j]){
?? ??? ??? ??? ??? ??? ??? ??? ?table.rows[j].cells[i].appendChild(this.pieces[i][j].piece);//棋盤放上棋子
?? ??? ??? ??? ??? ??? ??? ??? ?this.pieces[i][j].positionX=i;//設(shè)置x軸
?? ??? ??? ??? ??? ??? ??? ??? ?this.pieces[i][j].positionY=j;//設(shè)置y軸
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}else{
?? ??? ??? ??? ?//第一次運(yùn)行,自己創(chuàng)建所有棋子
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?//紅棋
?? ??? ??? ??? ??? ?this.pieces[0][9] = new Car("CarR1",0,9,true);
?? ??? ??? ??? ??? ?this.pieces[1][9] = new Horse("HorseR1",1,9,true);
?? ??? ??? ??? ??? ?this.pieces[1][7] = new Cannon("CannonR1",1,7,true);
?? ??? ??? ??? ??? ?this.pieces[2][9] = new Elephant("ElephantR1",2,9,true,"相");
?? ??? ??? ??? ??? ?this.pieces[3][9] = new Bodyguards("BodyguardsR1",3,9,true);
?? ??? ??? ??? ??? ?this.pieces[4][9] = new Boss("bossR",4,9,true,"帥");
?? ??? ??? ??? ??? ?this.redBoss=this.pieces[4][9];
?? ??? ??? ??? ??? ?this.pieces[5][9] = new Bodyguards("BodyguardsR2",5,9,true);
?? ??? ??? ??? ??? ?this.pieces[6][9] = new Elephant("ElephantR2",6,9,true,"相");
?? ??? ??? ??? ??? ?this.pieces[7][7] = new Cannon("CannonR2",7,7,true);
?? ??? ??? ??? ??? ?this.pieces[7][9] = new Horse("HorseR2",7,9,true);
?? ??? ??? ??? ??? ?this.pieces[8][9] = new Car("CarR2",8,9,true);
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?this.pieces[0][6] = new Soldier("SoldierR1",0,6,true,"兵");
?? ??? ??? ??? ??? ?this.pieces[2][6] = new Soldier("SoldierR2",2,6,true,"兵");
?? ??? ??? ??? ??? ?this.pieces[4][6] = new Soldier("SoldierR3",4,6,true,"兵");
?? ??? ??? ??? ??? ?this.pieces[6][6] = new Soldier("SoldierR4",6,6,true,"兵");
?? ??? ??? ??? ??? ?this.pieces[8][6] = new Soldier("SoldierR5",8,6,true,"兵");
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//黑棋
?? ??? ??? ??? ??? ?this.pieces[0][0] = new Car("CarG1",0,0,false);
?? ??? ??? ??? ??? ?this.pieces[1][0] = new Horse("HorseG1",1,0,false);
?? ??? ??? ??? ??? ?this.pieces[1][2] = new Cannon("CannonG1",1,2,false);
?? ??? ??? ??? ??? ?this.pieces[2][0] = new Elephant("ElephantG1",2,0,false,"象");
?? ??? ??? ??? ??? ?this.pieces[3][0] = new Bodyguards("BodyguardsG1",3,0,false);
?? ??? ??? ??? ??? ?this.pieces[4][0] = new Boss("bossG",4,0,false,"將");
?? ??? ??? ??? ??? ?this.greenBoss=this.pieces[4][0];
?? ??? ??? ??? ??? ?this.pieces[5][0] = new Bodyguards("BodyguardsG2",5,0,false);
?? ??? ??? ??? ??? ?this.pieces[6][0] = new Elephant("ElephantG2",6,0,false,"象");
?? ??? ??? ??? ??? ?this.pieces[7][2] = new Cannon("CannonG2",7,2,false);
?? ??? ??? ??? ??? ?this.pieces[7][0] = new Horse("HorseG2",7,0,false);
?? ??? ??? ??? ??? ?this.pieces[8][0] = new Car("CarG2",8,0,false);
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?this.pieces[0][3] = new Soldier("SoldierR1",0,3,false,"卒");
?? ??? ??? ??? ??? ?this.pieces[2][3] = new Soldier("SoldierR2",2,3,false,"卒");
?? ??? ??? ??? ??? ?this.pieces[4][3] = new Soldier("SoldierR3",4,3,false,"卒");
?? ??? ??? ??? ??? ?this.pieces[6][3] = new Soldier("SoldierR4",6,3,false,"卒");
?? ??? ??? ??? ??? ?this.pieces[8][3] = new Soldier("SoldierR5",8,3,false,"卒");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?this.copyPieces=[];
?? ??? ??? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ??? ??? ?if(this.pieces[i]){
?? ??? ??? ??? ??? ??? ?this.copyPieces[i]=[];?
?? ??? ??? ??? ??? ??? ?for(var j=0;j<10;j++){
?? ??? ??? ??? ??? ??? ??? ?this.copyPieces[i][j]=this.pieces[i][j];
?? ??? ??? ??? ??? ??? ??? ?this.pieces1[i][j]=this.pieces[i][j];
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?//綁定事件單擊
?? ??? ??? ??? ?var divs = document.getElementsByClassName("pieces");
?? ??? ??? ??? ?for(var i=0;i<divs.length;i++){divs[i].addEventListener("click",choosePiece,false);}
?? ??? ??? ?}
?? ??? ??? ?this.status = true;
?? ??? ??? ?this.turn = true;
?? ??? ??? ?turnText.innerHTML="紅方";
?? ??? ?},
?? ??? ?gameOver:function (winner){
?? ??? ??? ?if(winner){alert("紅方勝利")}else{alert("黑方勝利")}
?? ??? ??? ?this.status=false;
?? ??? ?},
?? ??? ?changeTurn:function(){
?? ??? ??? ?this.turn = !this.turn;
?? ??? ??? ?chessboard.curPiece.piece.style.backgroundColor="#FAF0E6";
?? ??? ??? ?//當(dāng)前選中棋子置換為null
?? ??? ??? ?chessboard.curPiece=null;
?? ??? ??? ?if(this.turn){turnText.innerHTML="紅方"}else{turnText.innerHTML="黑方"}
?? ??? ??? ?
?? ??? ??? ?if(beKillAnyWay(this.turn)){
?? ??? ??? ??? ?alert("絕殺!");
?? ??? ??? ??? ?chessboard.gameOver(!this.turn);
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?},
?? ??? ?status:true,//游戲的狀態(tài),true:正在玩,false:結(jié)束了
?? ??? ?redBoss:null,
?? ??? ?greenBoss:null,
?? ??? ?curPiece:null,
?? ??? ?pieces:[],//存放棋子當(dāng)前布局狀態(tài)
?? ??? ?pieces1:[[],[],[],[],[],[],[],[],[]],//存放棋子走過一步后的布局狀態(tài)
?? ??? ?copyPieces:null,//存放棋子布局的初始狀態(tài)
?? ??? ?turn:true,//紅先黑后,true:紅
?? ?};
?? ??
?? ?//檢查跳的位置上是否有對手棋子
?? ?//有:返回true
?? ?//沒有:返回false
?? ?function checkEnemy(x,y){
?? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team != this.team){
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?
?? ?//擊殺對手棋子
?? ?function killEnemy(x,y){
?? ??? ?chessboard.pieces[x][y].beKill();
?? ?}
?? ?
?? ?//是否將軍
?? ?function canKilBoss(x,y){
?? ??? ?
?? ??? ?if(this.team){
?? ??? ??? ?for(var i=0;i<chessboard.pieces.length;i++){
?? ??? ??? ??? ?for(var j=0;j<chessboard.pieces[i].length;j++){
?? ??? ??? ??? ??? ?if(chessboard.pieces[i][j] && chessboard.pieces[i][j].team == this.team){
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?if(chessboard.pieces[i][j].checkPath(chessboard.greenBoss.positionX,chessboard.greenBoss.positionY)){return true;}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return false;
?? ??? ?}else{
?? ??? ??? ?for(var i=0;i<chessboard.pieces.length;i++){
?? ??? ??? ??? ?for(var j=0;j<chessboard.pieces[i].length;j++){
?? ??? ??? ??? ??? ?if(chessboard.pieces[i][j] && chessboard.pieces[i][j].team == this.team){
?? ??? ??? ??? ??? ??? ?if(chessboard.pieces[i][j].checkPath(chessboard.redBoss.positionX,chessboard.redBoss.positionY)){return true};
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return false;
?? ??? ?}
?? ?}

?? ?//被擊殺
?? ?function beKill(){
?? ??? ?chessboard.pieces[this.positionX][this.positionY]=null;
?? ??? ?chessboard.pieces1[this.positionX][this.positionY]=null;
?? ??? ?
?? ??? ?this.piece.parentNode.removeChild(this.piece);
?? ??? ?if(this.name=="將" || this.name=="帥"){
?? ??? ??? ?chessboard.gameOver(!this.team);
?? ??? ?}
?? ?}
?? ?
?? ?function check(x,y){
?? ??? ?if(this.checkPath(x,y) && this.checkBoss(x,y)){
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?
?? ?//檢查是否被絕殺
?? ?//會(huì):返回true,不會(huì):返回false
?? ?function beKillAnyWay(turn){
?? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ?for(var j=0;j<10;j++){
?? ??? ??? ??? ?if(chessboard.pieces[i][j] && chessboard.pieces[i][j].team == turn){
?? ??? ??? ??? ??? ?var paths = chessboard.pieces[i][j].pathCanGo();
?? ??? ??? ??? ??? ?for(var l=0;l<paths.length;l++){
?? ??? ??? ??? ??? ??? ?if(!chessboard.pieces[i][j].canBeKilBoss(paths[l]["x"],paths[l]["y"])){return false;}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return true;
?? ??? ?
?? ?}
?? ?
?? ?//走出這步后會(huì)不會(huì)送將
?? ?//會(huì):返回true,不會(huì):返回false
?? ?function canBeKilBoss(x,y){
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?//pieces1置換為1步以后的狀態(tài)
?? ??? ?var tempPiece = chessboard.pieces1[x][y];
?? ??? ?chessboard.pieces1[this.positionX][this.positionY]=null;
?? ??? ?chessboard.pieces1[x][y]=this;
?? ??? ?
?? ??? ?
?? ??? ?//this也置換為一步以后的狀態(tài)
?? ??? ?var tempX = this.positionX;
?? ??? ?var tempY = this.positionY;
?? ??? ?
?? ??? ?this.positionX=x;
?? ??? ?this.positionY=y;
?? ??? ?
?? ??? ?if(this.team){
?? ??? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ??? ?for(var j=0;j<10;j++){
?? ??? ??? ??? ??? ?if(chessboard.pieces1[i][j] && !chessboard.pieces1[i][j].team){
?? ??? ??? ??? ??? ??? ?if(chessboard.pieces1[i][j].checkPath(chessboard.redBoss.positionX,chessboard.redBoss.positionY,chessboard.pieces1)){
?? ??? ??? ??? ??? ??? ??? ?//恢復(fù)之前的狀態(tài)
?? ??? ??? ??? ??? ??? ??? ?this.positionX=tempX;
?? ??? ??? ??? ??? ??? ??? ?this.positionY=tempY;
?? ??? ??? ??? ??? ??? ??? ?chessboard.pieces1[this.positionX][this.positionY]=this;
?? ??? ??? ??? ??? ??? ??? ?chessboard.pieces1[x][y]=tempPiece;
?? ??? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?
?? ??? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ??? ?for(var j=0;j<10;j++){
?? ??? ??? ??? ??? ?if(chessboard.pieces1[i][j] && chessboard.pieces1[i][j].team){
?? ??? ??? ??? ??? ??? ?if(chessboard.pieces1[i][j].checkPath(chessboard.greenBoss.positionX,chessboard.greenBoss.positionY,chessboard.pieces1)){
?? ??? ??? ??? ??? ??? ??? ?//恢復(fù)之前的狀態(tài)
?? ??? ??? ??? ??? ??? ??? ?this.positionX=tempX;
?? ??? ??? ??? ??? ??? ??? ?this.positionY=tempY;
?? ??? ??? ??? ??? ??? ??? ?chessboard.pieces1[this.positionX][this.positionY]=this;
?? ??? ??? ??? ??? ??? ??? ?chessboard.pieces1[x][y]=tempPiece;
?? ??? ??? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?//恢復(fù)之前的狀態(tài)
?? ??? ?this.positionX=tempX;
?? ??? ?this.positionY=tempY;
?? ??? ?
?? ??? ?chessboard.pieces1[this.positionX][this.positionY]=this;
?? ??? ?chessboard.pieces1[x][y]=null;
?? ??? ?
?? ??? ?return false;
?? ?}
?? ?
?? ?//移動(dòng)棋子
?? ?function move(x,y){
?? ??? ?if(this.check(x,y)){
?? ??? ??? ?//有對手的棋子,則吃掉對手棋子
?? ??? ??? ?if(this.checkEnemy(x,y)){killEnemy(x,y);}
?? ??? ??? ?this.doMove(x,y);
?? ??? ??? ?//換手
?? ??? ??? ?chessboard.changeTurn();
?? ??? ??? ?
?? ??? ??? ?if(chessboard.status && this.canKilBoss(x,y)){alert("將軍!")}
?? ??? ?}
?? ?}
?? ?
?? ?//實(shí)際調(diào)整棋子的位置
?? ?function doMove(x,y){
?? ??? ?//棋子移動(dòng)至這個(gè)位置
?? ??? ?this.piece.parentNode.removeChild(this.piece);
?? ??? ?table.rows[y].cells[x].appendChild(this.piece);
?? ??? ?
?? ??? ?//二維數(shù)組處放置當(dāng)前棋子
?? ??? ?chessboard.pieces[x][y]=this;
?? ??? ?chessboard.pieces[this.positionX][this.positionY]=null;
?? ??? ?
?? ??? ?//一步后的二維數(shù)組同步更新
?? ??? ?chessboard.pieces1[x][y]=this;
?? ??? ?chessboard.pieces1[this.positionX][this.positionY]=null;
?? ??? ?
?? ??? ?this.positionX=x;
?? ??? ?this.positionY=y;
?? ?}
?? ?
?? ?//棋子的父類,定義了一些方法,供繼承使用(但是發(fā)現(xiàn)這個(gè)繼承用處不大,甚至有些負(fù)作用)
?? ?function Pieces(){
?? ??? ?this.checkEnemy=checkEnemy;
?? ??? ?this.killEnemy=killEnemy;
?? ??? ?this.beKill=beKill;
?? ??? ?this.canKilBoss=canKilBoss;
?? ??? ?this.canBeKilBoss=canBeKilBoss;
?? ??? ?this.move=move;
?? ??? ?this.doMove=doMove;
?? ??? ?this.checkBoss=checkBoss;
?? ??? ?this.check=check;
?? ?}
?? ?
?? ?//判斷車的位置是否合法
?? ?function checkCarPath(x,y,chessArr){
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?if((this.positionX==x && this.positionY!=y) || (this.positionX!=x && this.positionY==y)){
?? ??? ??? ?//前進(jìn)路線中不能有任何障礙
?? ??? ??? ?if(this.positionX==x){
?? ??? ??? ??? ?for(var i= Math.min(this.positionY,y) + 1; i< Math.max(this.positionY,y);i++){
?? ??? ??? ??? ??? ?if(chessArr){
?? ??? ??? ??? ??? ??? ?if(chessArr[x][i]){return false;}
?? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ?if(chessboard.pieces[x][i]){return false;}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}else{
?? ??? ??? ??? ?for(var i= Math.min(this.positionX,x) + 1; i< Math.max(this.positionX,x);i++){
?? ??? ??? ??? ??? ?if(chessArr){
?? ??? ??? ??? ??? ??? ?if(chessArr[i][y]){return false;}
?? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?if(chessboard.pieces[i][y]){return false;}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?
?? ?//車可以移動(dòng)的位置
?? ?function pathCanGo_car(chessArr){
?? ??? ?var paths = [];
?? ??? ?
?? ??? ?//x不變,y軸上可以移動(dòng)的位置
?? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ?if(i != this.positionY && this.check(this.positionX,i)){paths.push({"x":this.positionX,"y":i});}
?? ??? ??? ?}
?? ??? ?//y不變,x軸上可以移動(dòng)的位置
?? ??? ?for(var i=0;i<10;i++){
?? ??? ??? ?if(i != this.positionX && this.check(i,this.positionY)){paths.push({"x":i,"y":this.positionY});}
?? ??? ?}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//車
?? ?function Car(nID,nX,nY,nTeam){
?? ??? ?this.name="車";
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?this.checkPath=checkCarPath;
?? ??? ?this.pathCanGo=pathCanGo_car;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//判斷馬的位置是否合法
?? ?function checkHorsePath(x,y,chessArr){
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?//馬走日字
?? ??? ?if(Math.abs(x-this.positionX)==2 && Math.abs(y-this.positionY)==1){
?? ??? ??? ?//蹩馬腿
?? ??? ??? ?if(chessboard.pieces[(x+this.positionX)/2][this.positionY]){return false;}
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?if(Math.abs(x-this.positionX)==1 && Math.abs(y-this.positionY)==2){
?? ??? ??? ?//蹩馬腿
?? ??? ??? ?if(chessboard.pieces[this.positionX][(y+this.positionY)/2]){return false;}
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?
?? ?//馬可以移動(dòng)的位置
?? ?function pathCanGo_Horse(){
?? ??? ?var paths = [];
?? ??? ?//右二下一
?? ??? ?if(this.check(this.positionX+2,this.positionY+1)){paths.push({"x":this.positionX+2,"y":this.positionY+1});}
?? ??? ?//右二上一
?? ??? ?if(this.check(this.positionX+2,this.positionY-1)){paths.push({"x":this.positionX+2,"y":this.positionY-1});}
?? ??? ?//左二下一
?? ??? ?if(this.check(this.positionX-2,this.positionY+1)){paths.push({"x":this.positionX-2,"y":this.positionY+1});}
?? ??? ?//左二上一
?? ??? ?if(this.check(this.positionX-2,this.positionY-1)){paths.push({"x":this.positionX-2,"y":this.positionY-1});}
?? ??? ?//右一下二
?? ??? ?if(this.check(this.positionX+1,this.positionY+2)){paths.push({"x":this.positionX+1,"y":this.positionY+2});}
?? ??? ?//右一上二
?? ??? ?if(this.check(this.positionX+1,this.positionY-2)){paths.push({"x":this.positionX+1,"y":this.positionY-2});}
?? ??? ?//左一下二
?? ??? ?if(this.check(this.positionX-1,this.positionY+2)){paths.push({"x":this.positionX-1,"y":this.positionY+2});}
?? ??? ?//左一上二
?? ??? ?if(this.check(this.positionX-1,this.positionY-2)){paths.push({"x":this.positionX-1,"y":this.positionY-2});}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//馬
?? ?function Horse(nID,nX,nY,nTeam){
?? ??? ?this.name="馬";
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?this.checkPath=checkHorsePath;
?? ??? ?this.pathCanGo=pathCanGo_Horse;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//判斷象的位置是否合法
?? ?function checkElephantPath(x,y,chessArr){
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?//不能過河
?? ??? ?if(this.team){if(y<5){return false;}}
?? ??? ?else{if(y>4){return false;}}
?? ??? ?
?? ??? ?//象走田字
?? ??? ?if(Math.abs(x-this.positionX)==2 && Math.abs(y-this.positionY)==2){
?? ??? ??? ?//蹩象腿
?? ??? ??? ?if(!chessboard.pieces[(x+this.positionX)/2][(y+this.positionY)/2]){
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return false;?? ?
?? ?}
?? ?
?? ?//象可以移動(dòng)的位置
?? ?function pathCanGo_Elephant(){
?? ??? ?var paths = [];
?? ??? ?//右二下二
?? ??? ?if(this.check(this.positionX+2,this.positionY+2)){paths.push({"x":this.positionX+2,"y":this.positionY+2});}
?? ??? ?//右二上二
?? ??? ?if(this.check(this.positionX+2,this.positionY-2)){paths.push({"x":this.positionX+2,"y":this.positionY-2});}
?? ??? ?//左二下二
?? ??? ?if(this.check(this.positionX-2,this.positionY+2)){paths.push({"x":this.positionX-2,"y":this.positionY+2});}
?? ??? ?//左二上二
?? ??? ?if(this.check(this.positionX-2,this.positionY-2)){paths.push({"x":this.positionX-2,"y":this.positionY-2});}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//象
?? ?function Elephant(nID,nX,nY,nTeam,name){
?? ??? ?this.name=name;
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?this.checkPath=checkElephantPath;
?? ??? ?this.pathCanGo=pathCanGo_Elephant;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//判斷士的位置是否合法
?? ?function checkBodyguardsPath(x,y,chessArr){
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?//x軸不能出九宮格
?? ??? ?if(x!=3 && x!=4 && x!=5){return false;}
?? ??? ?
?? ??? ?//y軸不能出九宮格
?? ??? ?if(this.team){if(y<7){return false;}}
?? ??? ?else{if(y>2){return false;}}
?? ??? ?
?? ??? ?//士斜著走一步
?? ??? ?if(Math.abs(x-this.positionX)==1 && Math.abs(y-this.positionY)==1){
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?
?? ?//士可以移動(dòng)的位置
?? ?function pathCanGo_Bodyguards(){
?? ??? ?var paths = [];
?? ??? ?if(this.check(this.positionX+1,this.positionY+1)){
?? ??? ??? ?paths.push({"x":this.positionX+1,"y":this.positionY+1});
?? ??? ?}
?? ??? ?if(this.check(this.positionX+1,this.positionY-1)){
?? ??? ??? ?paths.push({"x":this.positionX+1,"y":this.positionY-1});
?? ??? ?}
?? ??? ?if(this.check(this.positionX-1,this.positionY+1)){
?? ??? ??? ?paths.push({"x":this.positionX-1,"y":this.positionY+1});
?? ??? ?}
?? ??? ?if(this.check(this.positionX-1,this.positionY-1)){
?? ??? ??? ?paths.push({"x":this.positionX-1,"y":this.positionY-1});
?? ??? ?}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//士,找不到合適的單詞,構(gòu)造器意為保鏢
?? ?function Bodyguards(nID,nX,nY,nTeam){
?? ??? ?this.name="士";
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?this.checkPath=checkBodyguardsPath;
?? ??? ?this.pathCanGo=pathCanGo_Bodyguards;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//將/帥的位置是否合法
?? ?function checkBossPath(x,y,chessArr){
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?if(x!=3 && x!=4 && x!=5){return false;}
?? ??? ?if(this.team){if(y<7){return false;}}
?? ??? ?else{if(y > 2){return false;}}
?? ??? ?if(Math.abs(x-this.positionX)==1 && Math.abs(y-this.positionY)==0){
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?if(Math.abs(x-this.positionX)==0 && Math.abs(y-this.positionY)==1){
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?? ?
?? ?//將可以移動(dòng)的位置
?? ?function pathCanGo_Boss(){
?? ??? ?var paths=[];
?? ??? ?if(this.check(this.positionX+1,this.positionY)){paths.push({"x":this.positionX+1,"y":this.positionY})}
?? ??? ?if(this.check(this.positionX-1,this.positionY)){paths.push({"x":this.positionX-1,"y":this.positionY})}
?? ??? ?if(this.check(this.positionX,this.positionY+1)){paths.push({"x":this.positionX,"y":this.positionY+1})}
?? ??? ?if(this.check(this.positionX,this.positionY-1)){paths.push({"x":this.positionX,"y":this.positionY-1})}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//將,帥
?? ?function Boss(nID,nX,nY,nTeam,name){
?? ??? ?this.name=name;
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?
?? ??? ?this.checkPath=checkBossPath;
?? ??? ?this.pathCanGo=pathCanGo_Boss;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//兵/卒的位置是否合法
?? ?function checkSoldierPath(x,y,chessArr){
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?if(this.team){
?? ??? ??? ?//不能后退
?? ??? ??? ?if(y>this.positionY){return false;}
?? ??? ??? ?//前進(jìn),合法
?? ??? ??? ?if(Math.abs(x-this.positionX)==0 && Math.abs(y-this.positionY)==1){
?? ??? ??? ?
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ??? ?//過河后可以橫向移動(dòng),然而也只能走一步
?? ??? ??? ?if(this.positionY<5){
?? ??? ??? ??? ?if(Math.abs(x-this.positionX)==1 && Math.abs(y-this.positionY)==0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?//不能后退
?? ??? ??? ?if(y<this.positionY){return false;}
?? ??? ??? ?//前進(jìn),合法
?? ??? ??? ?if(Math.abs(x-this.positionX)==0 && Math.abs(y-this.positionY)==1){
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ??? ?//過河后可以橫向移動(dòng),然而也只能走一步
?? ??? ??? ?if(this.positionY>4){
?? ??? ??? ??? ?if(Math.abs(x-this.positionX)==1 && Math.abs(y-this.positionY)==0){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?return false;
?? ?}
?? ?
?? ?//兵/卒可以移動(dòng)的位置
?? ?function pathCanGo_Soldier(){
?? ??? ?var paths = [];
?? ??? ?if(this.team){
?? ??? ??? ?if(this.check(this.positionX,this.positionY-1)){paths.push({"x":this.positionX,"y":this.positionY-1});}
?? ??? ??? ?if(this.positionY<5){
?? ??? ??? ??? ?if(this.check(this.positionX+1,this.positionY)){paths.push({"x":this.positionX+1,"y":this.positionY});}
?? ??? ??? ??? ?if(this.check(this.positionX-1,this.positionY)){paths.push({"x":this.positionX-1,"y":this.positionY});}
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?if(this.check(this.positionX,this.positionY+1)){paths.push({"x":this.positionX,"y":this.positionY+1});}
?? ??? ??? ?if(this.positionY>4){
?? ??? ??? ??? ?if(this.check(this.positionX+1,this.positionY)){paths.push({"x":this.positionX+1,"y":this.positionY});}
?? ??? ??? ??? ?if(this.check(this.positionX-1,this.positionY)){paths.push({"x":this.positionX-1,"y":this.positionY});}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//兵
?? ?function Soldier(nID,nX,nY,nTeam,name){
?? ??? ?this.name=name;
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?this.checkPath=checkSoldierPath;
?? ??? ?this.pathCanGo=pathCanGo_Soldier;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//炮的位置是否合法
?? ?function checkCannonPath(x,y,chessArr){
?? ?
?? ??? ?if(x<0 || x>8 || y<0 || y>9){return false;}
?? ??? ?if(chessArr){
?? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team == this.team){return false;}
?? ??? ?}else{
?? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team == this.team){return false;}
?? ??? ?}
?? ??? ?//炮走直線
?? ??? ?if(this.positionX==x || this.positionY==y){
?? ??? ??? ?var count = 0;//計(jì)數(shù)路線上的障礙
?? ??? ??? ?if(this.positionX==x){count = count2Piece("y",this.positionY,y,x,chessArr);}
?? ??? ??? ?else{count = count2Piece("x",this.positionX,x,y,chessArr);}
?? ??? ??? ?//沒有障礙,切目標(biāo)位置沒有棋子,合法
?? ??? ??? ?if(chessArr){
?? ??? ??? ??? ?
?? ??? ??? ??? ?if(count==0 && !chessArr[x][y]){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//有一個(gè)障礙
?? ??? ??? ??? ?if(count==1){
?? ??? ??? ??? ??? ?//且目標(biāo)位置上有對手棋子,合法
?? ??? ??? ??? ??? ?if(chessArr[x][y] && chessArr[x][y].team != this.team){
?? ??? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}else{
?? ??? ??? ??? ?if(count==0 && !chessboard.pieces[x][y]){
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//有一個(gè)障礙
?? ??? ??? ??? ?if(count==1){
?? ??? ??? ??? ??? ?//且目標(biāo)位置上有對手棋子,合法
?? ??? ??? ??? ??? ?if(chessboard.pieces[x][y] && chessboard.pieces[x][y].team != this.team){
?? ??? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?}
?? ??? ?//其余情況均不合法
?? ??? ?return false;
?? ?}
?? ?
?? ?//泡可以移動(dòng)的位置
?? ?function pathCanGo_Cannon(){
?? ??? ?var paths = [];
?? ??? ?for(var i=0;i<10;i++){
?? ??? ??? ?if(i!=this.positionY && this.check(this.positionX,i)){paths.push({"x":this.positionX,"y":i})}
?? ??? ?}
?? ??? ?for(var i=0;i<9;i++){
?? ??? ??? ?if(i!=this.positionX && this.check(i,this.positionY)){paths.push({"x":i,"y":this.positionY})}
?? ??? ?}
?? ??? ?return paths;
?? ?}
?? ?
?? ?//炮
?? ?function Cannon(nID,nX,nY,nTeam){
?? ??? ?this.name="炮";
?? ??? ?this.ID=nID;
?? ??? ?this.positionX=nX;
?? ??? ?this.positionY=nY;
?? ??? ?this.team=!!nTeam;
?? ??? ?this.checkPath=checkCannonPath;
?? ??? ?this.pathCanGo=pathCanGo_Cannon;
?? ??? ?this.piece = createPieces(this.name,this.positionX,this.positionY,this.name,this.team);
?? ?}
?? ?
?? ?//檢查老將問題
?? ?function checkBoss(x,y,chessArr){
?? ??? ?
?? ??? ?if(this.canBeKilBoss(x,y,chessArr)){
?? ??? ??? ?return false;
?? ??? ?}
?? ??? ?if(this.name == "將" || this.name == "帥"){//將要移動(dòng)的是將
?? ??? ??? ?if(this.team){//帥
?? ??? ??? ??? ?if(x!=chessboard.greenBoss.positionX){return true;}
?? ??? ??? ??? ?var count = count2Piece("y",y,chessboard.greenBoss.positionY,x,chessArr);
?? ??? ??? ??? ?return count > 0;
?? ??? ??? ?}else{//將
?? ??? ??? ??? ?if(x!=chessboard.redBoss.positionX){return true;}
?? ??? ??? ??? ?var count = count2Piece("y",y,chessboard.redBoss.positionY,x,chessArr);
?? ??? ??? ??? ?return count>0;
?? ??? ??? ?}
?? ??? ?}else{//將要移動(dòng)的不是將
?? ??? ?
?? ??? ??? ?//兩方的將不在同一條線,通過
?? ??? ??? ?if(chessboard.redBoss.positionX != chessboard.greenBoss.positionX){
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?var count=count2Piece("y",chessboard.redBoss.positionY,chessboard.greenBoss.positionY,chessboard.redBoss.positionX,chessArr);
?? ??? ??? ?
?? ??? ??? ?//新的位置在兩個(gè)老將的x軸上,且y軸在兩個(gè)老將之間
?? ??? ??? ?if(x == chessboard.redBoss.positionX && y < chessboard.redBoss.positionY && y > chessboard.greenBoss.positionY){count++;}
?? ??? ??? ?//舊的位置在兩個(gè)老將的x軸上,且y軸在兩個(gè)老將之間
?? ??? ??? ?if(this.positionX == chessboard.redBoss.positionX && y < chessboard.redBoss.positionY && y > chessboard.greenBoss.positionY){count--;}
?? ??? ??? ?return count>0;
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ?}
?? ?/**
?? ??? ?計(jì)算在一條線上的兩個(gè)棋子“之間”的棋子數(shù)量
?? ??? ?@param XOrY 計(jì)算x軸還是y軸上的棋子數(shù)量
?? ??? ?@param a1 要計(jì)算的軸上的兩點(diǎn)中的一點(diǎn)
?? ??? ?@param a2 要計(jì)算的軸上的兩點(diǎn)中的一點(diǎn)
?? ??? ?@param a3 不需要計(jì)算的軸上的值
?? ??? ?@param chessArr 要在哪個(gè)棋子布局上計(jì)算
?? ?*/
?? ?function count2Piece(XOrY,a1,a2,a3,chessArr){
?? ?
?? ??? ?var count=0;
?? ??? ?if(XOrY=='x' || XOrY=='X'){
?? ??? ??? ?for(var i= Math.min(a1,a2) + 1;i< Math.max(a1,a2);i++){
?? ??? ??? ??? ?if(chessArr){
?? ??? ??? ??? ??? ?if(chessArr[i][a3]){++count;}
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?if(chessboard.pieces[i][a3]){++count;}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?for(var i= Math.min(a1,a2) + 1;i< Math.max(a1,a2);i++){
?? ??? ??? ??? ?if(chessArr){
?? ??? ??? ??? ??? ?if(chessArr[a3][i]){++count;}
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?if(chessboard.pieces[a3][i]){++count;}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return count;
?? ?}
?? ?
?? ?//在dom中創(chuàng)建棋子
?? ?function createPieces(ID,x,y,name,team){
?? ??? ?var div = document.createElement("div");
?? ??? ?div.setAttribute("data-team",team?"red":"");
?? ??? ?div.classList.add("pieces");
?? ??? ?div.classList.add(team?"red":"green");
?? ??? ?div.appendChild(document.createTextNode(name));
?? ??? ?tBody.rows[y].cells[x].appendChild(div);
?? ??? ?return div;
?? ?}
?? ?
?? ?var p = new Pieces();
?? ?Car.prototype=p;
?? ?Horse.prototype=p;
?? ?Elephant.prototype=p;
?? ?Bodyguards.prototype=p;
?? ?Boss.prototype=p;
?? ?Soldier.prototype=p;
?? ?Cannon.prototype=p;
?? ?
?? ?chessboard.init();
?? ?
</script>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論