未分类

canvas 实现中国象棋_javascript技巧_脚本之家

17 3月 , 2020  

canvas 实现中国象棋_javascript技巧_脚本之家。以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

效果如下:

    #canvasWrap{ width: 600px; height: 550px; margin: 50px auto; } #currActive{ font-size: 20px; font-weight: bold; /*text-align: center;*/ margin-left:230px ; } #canvas{ float: left; background: #EAC591; } #panel{ width: 80px; padding: 10px; float: right; } #ul{ list-style: none; margin: 0; padding: 0; } #ul li{ padding: 2px; }             var obj = {}; // 初始化 obj.init = function{ var canvas = document.getElementById; this.ctx = canvas.getContext;// this.chunk =args&&args.chunk?args.chunk:50; // this.radius =args&&args.radius?args.radius:23; // this.CandidateCircleR =args&&args.ccr?args.ccr:5; this.radius = 23; this.chunk =50; this.CandidateCircleR = 5; $.text; this.steps = []; // 记录步骤 this.currActive = "red"; // 先下 this.init_back; $; this.addEvent(); } // 棋盘初始化 obj.init_back = function; this.drawColLine(); this.ctx.clearRect(this.chunk+1, this.chunk*5+1, this.chunk*8-2, this.chunk-2); this.drawsharpS; this.drawText(); } // 棋子初始化 obj.init_chess = function(){ var Car_b1 = {x:1,y:1,text:"车"} var Horse_b1 = {x:2,y:1,text:"马"} var Elephant_b1 = {x:3,y:1,text:"象"} var Scholar_b1 = {x:4,y:1,text:"士"} var Boss_b = {x:5,y:1,text:"将"} var Scholar_b2 = {x:6,y:1,text:"士"} var Elephant_b2 = {x:7,y:1,text:"象"} var Horse_b2 = {x:8,y:1,text:"马"} var Car_b2 = {x:9,y:1,text:"车"} var Cannon_b1 = {x:2,y:3,text:"炮"} var Cannon_b2 = {x:8,y:3,text:"炮"} var Soldier_b1 = {x:1,y:4,text:"卒"} var Soldier_b2 = {x:3,y:4,text:"卒"} var Soldier_b3 = {x:5,y:4,text:"卒"} var Soldier_b4 = {x:7,y:4,text:"卒"} var Soldier_b5 = {x:9,y:4,text:"卒"} var Car_r1 = {x:1,y:10,text:"车"} var Horse_r1 = {x:2,y:10,text:"马"} var Elephant_r1 = {x:3,y:10,text:"相"} var Scholar_r1 = {x:4,y:10,text:"仕"} var Boss_r = {x:5,y:10,text:"帅"} var Scholar_r2 = {x:6,y:10,text:"仕"} var Elephant_r2 = {x:7,y:10,text:"相"} var Horse_r2 = {x:8,y:10,text:"马"} var Car_r2 = {x:9,y:10,text:"车"} var Cannon_r1 = {x:2,y:8,text:"炮"} var Cannon_r2 = {x:8,y:8,text:"炮"} var Soldier_r1 = {x:1,y:7,text:"兵"} var Soldier_r2 = {x:3,y:7,text:"兵"} var Soldier_r3 = {x:5,y:7,text:"兵"} var Soldier_r4 = {x:7,y:7,text:"兵"} var Soldier_r5 = {x:9,y:7,text:"兵"} this.cheer_arr_B = [Car_b1,Horse_b1,Elephant_b1,Scholar_b1,Boss_b,Scholar_b2,Elephant_b2,Horse_b2,Car_b2,Cannon_b1,Cannon_b2,Soldier_b1,Soldier_b2,Soldier_b3,Soldier_b4,Soldier_b5]; this.cheer_arr_R = [Car_r1,Horse_r1,Elephant_r1,Scholar_r1,Boss_r,Scholar_r2,Elephant_r2,Horse_r2,Car_r2,Cannon_r1,Cannon_r2,Soldier_r1,Soldier_r2,Soldier_r3,Soldier_r4,Soldier_r5]; var that = this; $.each(this.cheer_arr_B,function{ e.color = "#000"; e.bgcolor = "#fff"; e.bgColor_b = "#000"; e.type = "black"; that.drawPiece; that.drawChessText; $.each(this.cheer_arr_R,function{ e.color = "#f00"; e.bgcolor = "#fff"; e.bgColor_b = "#f00"; e.type = "red"; that.drawPiece; that.drawChessText; this.cheer_arr_ALL = this.cheer_arr_B.concat; } // 更新棋局 obj.updateChess = function(){ this.ctx.clearRect(0,0,canvas.width,canvas.height); this.init_back(); var that = this; $.each(this.cheer_arr_ALL,function{ that.drawPiece; that.drawChessText.empty(); $.each(this.steps,function.append; }); } // 画横线 obj.drawRowLine = function(){ for{ this.drawLine; } } // 画竖线 obj.drawColLine = function(){ for{ this.drawLine; } } // 画直线 obj.drawLine = function{ var x0 = x0*this.chunk; var y0 = y0*this.chunk; var x1 = x1*this.chunk; var y1 = y1*this.chunk; this.ctx.beginPath(); this.ctx.strokeStyle = "#000"; this.ctx.lineWidth =lw?lw:1; this.ctx.moveTo; this.ctx.lineTo; this.ctx.stroke(); this.ctx.closePath(); } // 画# obj.drawsharpS = function; this.round; this.round; this.round; this.round; this.round; this.round; this.round; } // 画单个# obj.round = function{ var x0 = x0*this.chunk; var y0 = y0*this.chunk; this.ctx.beginPath(); this.ctx.strokeStyle = "#000"; this.ctx.lineWidth =1; if{ // 左上 this.ctx.moveTo; this.ctx.lineTo; this.ctx.lineTo; // 左下 this.ctx.moveTo; this.ctx.lineTo; this.ctx.lineTo; } if{ // 右上 this.ctx.moveTo; this.ctx.lineTo; this.ctx.lineTo; // 右下 this.ctx.moveTo; this.ctx.lineTo; this.ctx.lineTo; } this.ctx.stroke(); this.ctx.closePath(); } // 画X obj.drawX = function(){ this.drawLine; this.drawLine; this.drawLine; this.drawLine; } // 画楚河/汉界 obj.drawText = function(){ this.ctx.font = "bold 30px Courier New"; this.ctx.fillStyle = "#000"; this.ctx.fillText("楚 河", this.chunk*2, this.chunk*5+this.chunk/2+10); this.ctx.fillText("汉 界", this.chunk*6, this.chunk*5+this.chunk/2+10); this.ctx.font = "12px Courier New"; this.text_arr =["九","八","七","六","五","四","三","二","一"]; for{ this.ctx.fillText, this.chunk*; this.ctx.fillText(this.text_arr[i], this.chunk*-5, this.chunk*10+30+10); } } // 画棋子形状 obj.drawPiece = function{ this.ctx.beginPath(); this.ctx.fillStyle =e.bgcolor; this.ctx.strokeStyle = e.bgColor_b; this.ctx.lineWidth =2; this.ctx.arc(e.x*this.chunk, e.y*this.chunk, this.radius, 0, Math.PI * 2, true); this.ctx.closePath; this.ctx.stroke(); } // 画棋子文字 obj.drawChessText = function{ this.ctx.font = "bold 30px Courier New"; this.ctx.fillStyle = e.color; var offset = this.ctx.measureText.width/2; this.ctx.fillText(e.text, e.x*this.chunk-offset, e.y*this.chunk+10); } // 增加点击事件 obj.addEvent = function(){ var that = this; this.checked = false; $.on("mousedown",function{ for{ for{ var temp_i = i*that.chunk; var temp_j = j*that.chunk; var distanct = Math.sqrt(Math.pow+Math.pow; if(distanct<=that.radius){ var overChess = false; $.each(that.cheer_arr_ALL,function{ if{ overChess = true; var p ={x:ee.x,y:ee.y};// console.log; if(that.currActive != ee.type&&!that.checked){ return false; } if{// console.log; that.drawChecked; that.preChess = ee; that.drawCandidate(); that.checked = true; }else if(that.preChess.x == ee.x&&that.preChess.y == ee.y){// console.log; that.updateChess(); that.checked = false; }else if(that.preChess.type == ee.type){// console.log; that.updateChess(); that.drawChecked; that.preChess = ee; that.drawCandidate(); }else{ // 是否能吃子 if{ that.eat; }else if(that.preChess.text == "帅"){ // 对将 if{ var canEat =true; $.each(that.cheer_arr_ALL,function{ if(eee.x ==that.preChess.x&&eee.y==j){ if{ for(var t=that.preChess.y-1;t>j;t--){ if(that.inArray{ canEat = false; break; } } }else{ canEat = false; } return false; } }); if{ that.eat; } } }else if(that.preChess.text == "将"){ if{ var canEat =true; $.each(that.cheer_arr_ALL,function{ if(eee.x ==that.preChess.x&&eee.y==j){ if{ for(var t=that.preChess.y+1;t<j;t++){ if(that.inArray{ canEat = false; break; } } }else{ canEat = false; } return false; } }); if{ that.eat; } } } } return false; } }); if{// alert; }else{ // 是否能移动 if(that.checked&&that.Move_rule{// console.log; that.move; } // 记谱 obj.note = function{ var distance = Math.abs; var step; if{ $.text; var oldP = this.text_arr[ee.x-1]; var newP = this.text_arr[i-1]; var num = this.text_arr[9-distance]; if{ if{ console.log; step = ee.text+oldP+"进"+num; }else{ console.log(ee.text+oldP+"进"+newP); step = ee.text+oldP+"进"+newP; } }else if{ if{ console.log; step = ee.text+oldP+"退"+num; }else{ console.log(ee.text+oldP+"退"+newP); step = ee.text+oldP+"退"+newP; } }else{ console.log(ee.text+oldP+"平"+newP); step = ee.text+oldP+"平"+newP; } }else{ $.text{ if{ console.log(ee.text+ee.x+"进"+distance); step = ee.text+ee.x+"进"+distance }else{ console.log; step = ee.text+ee.x+"进"+i; } }else if{ if{ console.log(ee.text+ee.x+"退"+distance); step = ee.text+ee.x+"退"+distance; }else{ console.log; step = ee.text+ee.x+"退"+i; } }else{ console.log; step = ee.text+ee.x+"平"+i; } } this.steps.push; } // 是否结束 obj.isOver = function{ if{ alert.empty(); return true; }else if{ alert.empty(); return true; }else{ return false; } } // 吃子 obj.eat = function{ this.cheer_arr_ALL.splice; if{ this.ctx.clearRect(0,0,canvas.width,canvas.height); this.init(); return false; }; } // 移动 obj.move = function{ var that = this; $.each(that.cheer_arr_ALL,function{ if(eee.x ==that.preChess.x&&eee.y==that.preChess.y){ that.note; eee.x= i; eee.y = j; that.currActive = eee.type=="red"?"black":"red"; return false; } }); that.updateChess(); that.checked = false; } // 画选中棋子状态 obj.drawChecked = function{ var temp_x = p.x*this.chunk; var temp_y = p.y*this.chunk; this.ctx.beginPath(); this.ctx.strokeStyle = "#00f"; this.ctx.lineWidth =1; this.ctx.moveTo(temp_x-this.radius,temp_y-this.radius+10); this.ctx.lineTo(temp_x-this.radius,temp_y-this.radius); this.ctx.lineTo(temp_x-this.radius+10,temp_y-this.radius); this.ctx.moveTo(temp_x-this.radius,temp_y+this.radius-10); this.ctx.lineTo(temp_x-this.radius,temp_y+this.radius); this.ctx.lineTo(temp_x-this.radius+10,temp_y+this.radius); this.ctx.moveTo(temp_x+this.radius,temp_y-this.radius+10); this.ctx.lineTo(temp_x+this.radius,temp_y-this.radius); this.ctx.lineTo(temp_x+this.radius-10,temp_y-this.radius); this.ctx.moveTo(temp_x+this.radius,temp_y+this.radius-10); this.ctx.lineTo(temp_x+this.radius,temp_y+this.radius); this.ctx.lineTo(temp_x+this.radius-10,temp_y+this.radius); this.ctx.stroke(); this.ctx.closePath(); } // 画候选位置 obj.drawCandidate = function(){ switch{ case "车": var temp_y = this.preChess.y; while(!this.inArray(this.preChess.x,++temp_y)&&temp_y<=10){ this.drawCandidateCircle(this.preChess.x,temp_y); } var temp_y = this.preChess.y; while(!this.inArray(this.preChess.x,--temp_y)&&temp_y>0){ this.drawCandidateCircle(this.preChess.x,temp_y); } var temp_x = this.preChess.x; while(!this.inArray(++temp_x,this.preChess.y)&&temp_x<10){ this.drawCandidateCircle(temp_x,this.preChess.y); } var temp_x = this.preChess.x; while(!this.inArray(--temp_x,this.preChess.y)&&temp_x>0){ this.drawCandidateCircle(temp_x,this.preChess.y); } break; case "马": if(!this.inArray(this.preChess.x-2,this.preChess.y-1) &&this.preChess.x-2>=1&&this.preChess.y-1>=1 &&!this.inArray(this.preChess.x-1,this.preChess.y)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-1); } if(!this.inArray(this.preChess.x-1,this.preChess.y-2) &&this.preChess.x-1>=1&&this.preChess.y-2>=1 &&!this.inArray(this.preChess.x,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y-2); } if(!this.inArray(this.preChess.x+1,this.preChess.y-2) &&this.preChess.x+1<=9&&this.preChess.y-2>=1 &&!this.inArray(this.preChess.x,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y-1) &&this.preChess.x+2<=9&&this.preChess.y-1>=1 &&!this.inArray(this.preChess.x+1,this.preChess.y)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-1); } if(!this.inArray(this.preChess.x+2,this.preChess.y+1) &&this.preChess.x+2<=9&&this.preChess.y+1<=10 &&!this.inArray(this.preChess.x+1,this.preChess.y)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+1); } if(!this.inArray(this.preChess.x+1,this.preChess.y+2) &&this.preChess.x+1<=9&&this.preChess.y+2<=10 &&!this.inArray(this.preChess.x,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y+2); } if(!this.inArray(this.preChess.x-1,this.preChess.y+2) &&this.preChess.x-1>=1&&this.preChess.y+2<=10 &&!this.inArray(this.preChess.x,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y+2); } if(!this.inArray(this.preChess.x-2,this.preChess.y+1) &&this.preChess.x-2>=1&&this.preChess.y+1<=10 &&!this.inArray(this.preChess.x-1,this.preChess.y)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+1); } break; case "相": if{ if(!this.inArray(this.preChess.x-2,this.preChess.y-2) &&!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y-2) &&!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-2); } }else if{ if(!this.inArray(this.preChess.x-2,this.preChess.y+2) &&!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+2); } if(!this.inArray(this.preChess.x+2,this.preChess.y+2) &&!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+2); } }else if{ if(!this.inArray(this.preChess.x+2,this.preChess.y-2) &&!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y+2) &&!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+2); } }else if{ if(!this.inArray(this.preChess.x-2,this.preChess.y-2) &&!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-2); } if(!this.inArray(this.preChess.x-2,this.preChess.y+2) &&!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+2); } }else{ if(!this.inArray(this.preChess.x+2,this.preChess.y-2) &&!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y+2) &&!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+2); } if(!this.inArray(this.preChess.x-2,this.preChess.y-2) &&!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-2); } if(!this.inArray(this.preChess.x-2,this.preChess.y+2) &&!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+2); } } break; case "象": if{ if(!this.inArray(this.preChess.x-2,this.preChess.y+2) &&!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+2); } if(!this.inArray(this.preChess.x+2,this.preChess.y+2) &&!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+2); } }else if{ if(!this.inArray(this.preChess.x-2,this.preChess.y-2) &&!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y-2) &&!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-2); } }else if{ if(!this.inArray(this.preChess.x+2,this.preChess.y-2) &&!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y+2) &&!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+2); } }else if{ if(!this.inArray(this.preChess.x-2,this.preChess.y-2) &&!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-2); } if(!this.inArray(this.preChess.x-2,this.preChess.y+2) &&!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+2); } }else{ if(!this.inArray(this.preChess.x+2,this.preChess.y-2) &&!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y-2); } if(!this.inArray(this.preChess.x+2,this.preChess.y+2) &&!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+2,this.preChess.y+2); } if(!this.inArray(this.preChess.x-2,this.preChess.y-2) &&!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y-2); } if(!this.inArray(this.preChess.x-2,this.preChess.y+2) &&!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-2,this.preChess.y+2); } } break; case "仕": if(this.preChess.x==5&&this.preChess.y==9){ if(!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y-1); } if(!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y+1); } if(!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y-1); } if(!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y+1); } }else{ this.drawCandidateCircle; } break; case "士": if(this.preChess.x==5&&this.preChess.y==2){ if(!this.inArray(this.preChess.x-1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y-1); } if(!this.inArray(this.preChess.x-1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y+1); } if(!this.inArray(this.preChess.x+1,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y-1); } if(!this.inArray(this.preChess.x+1,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y+1); } }else{ this.drawCandidateCircle; } break; case "帅": if(!this.inArray(this.preChess.x,this.preChess.y-1)&&this.preChess.y>8){ this.drawCandidateCircle(this.preChess.x,this.preChess.y-1); } if(!this.inArray(this.preChess.x,this.preChess.y+1)&&this.preChess.y<10){ this.drawCandidateCircle(this.preChess.x,this.preChess.y+1); } if(!this.inArray(this.preChess.x-1,this.preChess.y)&&this.preChess.x>4){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y); } if(!this.inArray(this.preChess.x+1,this.preChess.y)&&this.preChess.x<6){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y); } break; case "将": if(!this.inArray(this.preChess.x,this.preChess.y-1)&&this.preChess.y>1){ this.drawCandidateCircle(this.preChess.x,this.preChess.y-1); } if(!this.inArray(this.preChess.x,this.preChess.y+1)&&this.preChess.y<3){ this.drawCandidateCircle(this.preChess.x,this.preChess.y+1); } if(!this.inArray(this.preChess.x-1,this.preChess.y)&&this.preChess.x>4){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y); } if(!this.inArray(this.preChess.x+1,this.preChess.y)&&this.preChess.x<6){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y); } break; case "兵": if(this.preChess.y>5&&!this.inArray(this.preChess.x,this.preChess.y-1)){ this.drawCandidateCircle(this.preChess.x,this.preChess.y-1); }else if{ if(!this.inArray(this.preChess.x,this.preChess.y-1)&&this.preChess.y>1){ this.drawCandidateCircle(this.preChess.x,this.preChess.y-1); } if(!this.inArray(this.preChess.x-1,this.preChess.y)&&this.preChess.x>1){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y); } if(!this.inArray(this.preChess.x+1,this.preChess.y)&&this.preChess.x<9){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y); } } break; case "卒": if(this.preChess.y<=5&&!this.inArray(this.preChess.x,this.preChess.y+1)){ this.drawCandidateCircle(this.preChess.x,this.preChess.y+1); }else if{ if(!this.inArray(this.preChess.x,this.preChess.y+1)&&this.preChess.y<10){ this.drawCandidateCircle(this.preChess.x,this.preChess.y+1); } if(!this.inArray(this.preChess.x-1,this.preChess.y)&&this.preChess.x>1){ this.drawCandidateCircle(this.preChess.x-1,this.preChess.y); } if(!this.inArray(this.preChess.x+1,this.preChess.y)&&this.preChess.x<9){ this.drawCandidateCircle(this.preChess.x+1,this.preChess.y); } } break; case "炮": var temp_y = this.preChess.y; while(!this.inArray(this.preChess.x,++temp_y)&&temp_y<=10){ this.drawCandidateCircle(this.preChess.x,temp_y); } var temp_y = this.preChess.y; while(!this.inArray(this.preChess.x,--temp_y)&&temp_y>0){ this.drawCandidateCircle(this.preChess.x,temp_y); } var temp_x = this.preChess.x; while(!this.inArray(++temp_x,this.preChess.y)&&temp_x<10){ this.drawCandidateCircle(temp_x,this.preChess.y); } var temp_x = this.preChess.x; while(!this.inArray(--temp_x,this.preChess.y)&&temp_x>0){ this.drawCandidateCircle(temp_x,this.preChess.y); } break; } } // 画候选形状 obj.drawCandidateCircle = function{ this.ctx.beginPath(); this.ctx.fillStyle ="#eee"; this.ctx.strokeStyle = "#000"; this.ctx.lineWidth =2; this.ctx.arc(x*this.chunk, y*this.chunk, this.CandidateCircleR, 0, Math.PI * 2, true);// this.ctx.translate(x*this.chunk,y*this.chunk);// this.ctx.rotate;// this.ctx.translate(1*this.chunk,1*this.chunk);//设置画布上的位置,也就是旋转的中心点// this.ctx.rotate;// this.ctx.fillRect(x*this.chunk-5,y*this.chunk-5,10,10); this.ctx.closePath; this.ctx.stroke(); } // 棋子移动规则 obj.Move_rule = function{ switch{ case "车": return this.rule_Car; case "马": return this.rule_Horse; case "相": return this.rule_Elephant_r; case "象": return this.rule_Elephant_b; case "仕": return this.rule_Scholar_r; case "士": return this.rule_Scholar_b; case "帅": return this.rule_Boss_r; case "将": return this.rule_Boss_b; case "兵": return this.rule_Soldier_r; case "卒": return this.rule_Soldier_b; case "炮": if==0){ return true; } return false; } } // 棋子吃子规则 obj.Eat_rule = function{ switch{ case "车": return this.rule_Car; case "马": return this.rule_Horse; case "相": return this.rule_Elephant_r; case "象": return this.rule_Elephant_b; case "仕": return this.rule_Scholar_r; case "士": return this.rule_Scholar_b; case "帅": return this.rule_Boss_r; case "将": return this.rule_Boss_b; case "兵": return this.rule_Soldier_r; case "卒": return this.rule_Soldier_b; case "炮": if==1){ return true; } return false; } } // 车的规则 obj.rule_Car = function{ if(this.preChess.x ==i||this.preChess.y==j){ if{ if{// console.log; var hasObstacle = false; for(var p = this.preChess.y+1;p<j;p++){ if{ hasObstacle = true; break; } } if{ return false; } } if{// console.log; var hasObstacle = false; for(var p = this.preChess.y-1;p>j;p--){ if{ hasObstacle = true; break; } } if{ return false; } } } if{ if{// console.log; var hasObstacle = false; for(var p = this.preChess.x+1;p<i;p++){ if{ hasObstacle = true; break; } } if{ return false; } } if{// console.log; var hasObstacle = false; for(var p = this.preChess.x-1;p>i;p--){ if{ hasObstacle = true; break; } } if{ return false; } } } return true; } return false; } // 马的规则 obj.rule_Horse = function{ var hasObstacle = false; var that = this; if((Math.abs==1&&Math.abs==2) ||(Math.abs==2&&Math.abs==1)){ if{ // 左 $.each(that.cheer_arr_ALL,function{ if(ee.x ==that.preChess.x-1&&ee.y==that.preChess.y){ hasObstacle =true; return false; } }); if{ return false; } }else if{ // 右 $.each(that.cheer_arr_ALL,function{ if(ee.x ==that.preChess.x+1&&ee.y==that.preChess.y){ hasObstacle =true; return false; } }); if{ return false; } }else if{ // 上 $.each(that.cheer_arr_ALL,function{ if(ee.x ==that.preChess.x&&ee.y==that.preChess.y-1){ hasObstacle =true; return false; } }); if{ return false; } }else if{ // 下 $.each(that.cheer_arr_ALL,function{ if(ee.x ==that.preChess.x&&ee.y==that.preChess.y+1){ hasObstacle =true; return false; } }); if{ return false; } } return true; } return false; } // 红相的规则 obj.rule_Elephant_r = function{ var hasObstacle = false; var that = this; if((Math.abs==2&&Math.abs==2)&&j>=6){ var vgaX = /2; var vgaY = /2; console.log; $.each(that.cheer_arr_ALL,function{ if(ee.x ==vgaX&&ee.y==vgaY){ hasObstacle =true; return false; } }); if{ return false; } return true; } return false; } // 黑象的规则 obj.rule_Elephant_b = function{ var hasObstacle = false; var that = this; if((Math.abs==2&&Math.abs==2)&&j<6){ var vgaX = /2; var vgaY = /2;// console.log; $.each(that.cheer_arr_ALL,function{ if(ee.x ==vgaX&&ee.y==vgaY){ hasObstacle =true; return false; } }); if{ return false; } return true; } return false; } // 红仕的规则 obj.rule_Scholar_r = function{ if(this.preChess.x==5&&this.preChess.y==9){ if(Math.abs==1&&Math.abs==1){ return true; } }else if{ return true; } return false; } // 黑仕的规则 obj.rule_Scholar_b = function{ if(this.preChess.x==5&&this.preChess.y==2){ if(Math.abs==1&&Math.abs==1){ return true; } }else if{ return true; } return false; } // 帅的规则 obj.rule_Boss_r = function{ if((Math.abs==1&&this.preChess.y==j) ||(this.preChess.x==i&&Math.abs==1)){ if(i>=4&&i<=6&&j>=8&&j<=10){ return true; }else{ return false; } } return false; } // 将的规则 obj.rule_Boss_b = function{ if((Math.abs==1&&this.preChess.y==j) ||(this.preChess.x==i&&Math.abs==1)){ if(i>=4&&i<=6&&j>=1&&j<=3){ return true; }else{ return false; } } return false; } // 兵的规则 obj.rule_Soldier_r = function{ if{ if((this.preChess.x ==i&&this.preChess.y-1==j)||(this.preChess.x-1 ==i&&this.preChess.y==j)||(this.preChess.x+1 ==i&&this.preChess.y==j)){ return true; } }else{ if(this.preChess.x ==i&&this.preChess.y-1==j){ return true; } } return false; } // 卒的规则 obj.rule_Soldier_b = function{ if{ if((this.preChess.x ==i&&this.preChess.y+1==j)||(this.preChess.x-1 ==i&&this.preChess.y==j)||(this.preChess.x+1 ==i&&this.preChess.y==j)){ return true; } }else{ if(this.preChess.x ==i&&this.preChess.y+1==j){ return true; } } return false; } // 炮的规则 obj.rule_Cannon = function{ var that = this; if(this.preChess.x ==i||this.preChess.y==j){ var t =0; if{ var temp = this.preChess.y; if{ while{ $.each(this.cheer_arr_ALL,function{ if(ee.x ==that.preChess.x&&ee.y==temp){ t++; return false; } }); } return t; }else{ while{ $.each(this.cheer_arr_ALL,function{ if(ee.x ==that.preChess.x&&ee.y==temp){ t++; return false; } }); } return t; } }else{ var temp = this.preChess.x; if{ while{ $.each(this.cheer_arr_ALL,function{ if(ee.x ==temp&&ee.y==that.preChess.y){ t++; return false; } }); } return t; }else{ while{ $.each(this.cheer_arr_ALL,function{ if(ee.x ==temp&&ee.y==that.preChess.y){ t++; return false; } }); } return t; } } } return 2; } obj.inArray = function{ var hasObstacle = false; $.each(this.cheer_arr_ALL,function{ if{ hasObstacle = true; return false; } }); return hasObstacle; } obj.init();// obj.init({// chunk:50, // 格子大小// radius:23, // 棋子半径// ccr:3 // 候选小圆半径// });  

代码如下:


相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图