h5使用canvas模拟制作的中国象棋实现残局走子

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="Access-Control-Allow-Origin" content="*">
	<title>中国象棋</title>
	<style>
		html,body{
			width: 100%;
			height: 100%;
			padding: 0;
			margin: 0;
		}
		#content{
			width: 100%;
			height: 100%;
			padding: 20px 0;
			box-sizing: border-box;
		}
		.title{
			width: 100%;
			height: 80px;
			text-align: center;
			line-height:80px;
			font-size: 30px;
			font-weight: bold;
			color: rgb(139, 12, 12);
		}
	</style>
</head>

<body>
	<div id="title" class="title" ></div>
	<div id="content">
	</div>
	<audio id="bgMusic" src="#" controls autoplay></audio>
	<script id="data-tpl" type="text/html">
		<canvas id="canvas1" style="background-image: url(./img/qipan.png); background-size: 100% 100%;" width="1005" height="1105">不支持Canvas</canvas>
		{{each data item index}}
		<img src="{{item.pieceImageUrl}}" id="{{item.ids}}" hidden="hidden"/>
		{{/each}}
		
	</script>
	<script id="title-tpl" type="text/html">
		<h4>红棋先:{{data}}步杀</h4>

	</script>
	<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
	<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
	<script type="text/javascript" src="./template.js"></script>
	<script type="text/javascript" src="./template-web.js"></script>
	<script type="text/javascript">
		document.body.style.overflow='hidden';
		var threeId
 		var oneid
 		var twoid
 		var openid
		var surfaceName
		var audio = document.getElementById("bgMusic");
 		var searchHref = window.location.search.replace('?', '');
 		var params = searchHref.split('&');	
 			params.forEach(function(param) {
 				var paramSplit = param.split('=');
 				if(paramSplit[0]=='id'){
 					threeId=paramSplit[1]
 				}
 				if(paramSplit[0]=='oneid'){
 					oneid=paramSplit[1]
 				}
 				if(paramSplit[0]=='twoid'){
 					twoid=paramSplit[1]
 				}
 				if(paramSplit[0]=='openid'){
 					openid=paramSplit[1]
 				}
				if(paramSplit[0]=='name'){
    				surfaceName=paramSplit[1]
    			}
 			}); 
		var Chess
		var Answer 
		var useTime
		// 黑方走子路线
		var Blackgo
		var numberKill
		var nowtime = new Date().getTime
		window.onload = function() {
			$.ajax({
				url: "https://app.tongxinyizhi.com/txyz/ItemBank/getSimpleItemBankById",
				data: {
					id: threeId,
				  surfaceName: surfaceName
				},
				dataType: 'json',
				xhrFields:{
					'Access-Control-Allow-Origin': '*'},
				success: function( result ) {
					console.log('waihua', result)
					Chess = result.simpleItemBanks
					Answer = result.simpleItemBankAnswers
					Blackgo = result.Blackgo  
					let num = Blackgo.length+1
					var html = template('data-tpl', {data:Chess});
					var html1 = template('title-tpl',{data:num})
					document.getElementById('content').innerHTML = html;
					document.getElementById('title').innerHTML = html1;
					object.init();
					setTimeout(function() {  
						drewChess(Chess)
					}, 1000);  		
				}
			})
			// 	Chess = [{"id":28,"pieceName":"黑士","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hshi.png","positionX":"450","positionY":"150","simpleId":"4","type":"1","n*":0,"name":"士","redBlack":0,"piece":4,"ids":"shi"},
			// 	{"id":35,"pieceName":"黑士","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hshi.png","positionX":"550","positionY":"50","simpleId":"4","type":"1","n*":0,"name":"士","redBlack":0,"piece":4,"ids":"shi"},
			// 	{"id":29,"pieceName":"黑将","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hjiang.png","positionX":"450","positionY":"50","simpleId":"4","type":"1","n*":0,"name":"将","redBlack":0,"piece":5,"ids":"jiang"},
			// 	{"id":36,"pieceName":"黑车","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hche.png","positionX":"350","positionY":"850","simpleId":"4","type":"1","n*":0,"name":"将","redBlack":0,"piece":4,"ids":"che"},
			// 	{"id":30,"pieceName":"黑卒","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hzu.png","positionX":"550","positionY":"850","simpleId":"4","type":"1","n*":0,"name":"卒","redBlack":0,"piece":7,"ids":"zu"},
			// 	{"id":30,"pieceName":"黑卒","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hzu.png","positionX":"350","positionY":"650","simpleId":"4","type":"1","n*":0,"name":"卒","redBlack":0,"piece":7,"ids":"zu"},
			// 	{"id":30,"pieceName":"黑卒","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hzu.png","positionX":"350","positionY":"450","simpleId":"4","type":"1","n*":0,"name":"卒","redBlack":0,"piece":7,"ids":"zu"},
			// 	{"id":32,"pieceName":"红马","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rma.png","positionX":"650","positionY":"450","simpleId":"4","type":"1","n*":1,"name":"马","redBlack":1,"piece":2,"ids":"r_ma"},
			// 	{"id":34,"pieceName":"红兵","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rbing.png","positionX":"250","positionY":"250","simpleId":"4","type":"1","n*":1,"name":"车","redBlack":1,"piece":7,"ids":"r_bing"},
			// 	{"id":31,"pieceName":"红兵","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rbing.png","positionX":"650","positionY":"350","simpleId":"4","type":"1","n*":1,"name":"车","redBlack":1,"piece":7,"ids":"r_bing"},
			// 	{"id":34,"pieceName":"红炮","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rpao.png","positionX":"350","positionY":"950","simpleId":"4","type":"1","n*":1,"name":"车","redBlack":1,"piece":6,"ids":"r_pao"},
			// 	{"id":33,"pieceName":"红帅","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rshuai.png","positionX":"450","positionY":"950","simpleId":"4","type":"1","n*":1,"name":"帅","redBlack":1,"piece":5,"ids":"r_jiang"}]
			// 	Blackgo = [

			// 	{"id":29,"pieceName":"黑将","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hjiang.png","positionX":"350","positionY":"50","simpleId":"4","type":"1","n*":0,"name":"将","redBlack":0,"piece":5,"ids":"jiang"},
			// 	{"id":36,"pieceName":"黑车","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/hche.png","positionX":"350","positionY":"850","simpleId":"4","type":"1","n*":0,"name":"将","redBlack":0,"piece":4,"ids":"che"},
			// ]	
			// 	Answer = [

			// 	{"id":32,"pieceName":"红马","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rma.png","positionX":"550","positionY":"250","simpleId":"4","type":"1","n*":1,"name":"马","redBlack":1,"piece":2,"ids":"r_ma"},
			// 	{"id":32,"pieceName":"红兵","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rbing.png","positionX":"350","positionY":"250","simpleId":"4","type":"1","n*":1,"name":"车","redBlack":1,"piece":7,"ids":"r_bing"},
			// 	{"id":32,"pieceName":"红兵","pieceImageUrl":"https://app.tongxinyizhi.com/image/chess/rbing.png","positionX":"350","positionY":"150","simpleId":"4","type":"1","n*":1,"name":"车","redBlack":1,"piece":7,"ids":"r_bing"},
			// ]		
			// // 几步杀 
			// 	numberKill = Blackgo.length
			// 	var html = template('data-tpl', {data:Chess});
			// 				document.getElementById('content').innerHTML = html; 
			// 				object.init();
			// 			setTimeout(function() { 
			// 				drewChess(Chess)
			// 			}, 600);  
			}
			//0记录红子  走子次数
			var redGonum = 0
			// 记录黑子走子次数
			var blackGonum = 0
			//建一个数组来存放我们点击的那个棋子的所有参数
			//参数:x坐标,y坐标,哪边下子,判断是红棋还是黑棋,判断棋名字 
    	var desc_click = [];
    	//判断当前是哪边出手1表示"红方"0表示"黑方"
			var whose = 1
			var object = {
				//初始化
				init: function() {
					//棋盘外框
					var canvas1 = document.getElementById("canvas1");
					var context1 = canvas1.getContext("2d");
					this.ctx = canvas1.getContext("2d");
					this.ctx.lineWidth = 4;
					this.ctx.strokeStyle = "#5B4209";
					// this.ctx.strokeRect(100, 100, 800, 900);
					canvas1.onclick = function(e){
						//三个参数 第一个是当前的context,第二个是点击的棋子到画板左边的距离,第三个是当前点击的棋子到画板上边的距离
						get_chess(context1,e.clientX - canvas1.offsetLeft,e.clientY - canvas1.offsetTop);
						// 更换当前的出手的人
						// update_h2();
					};			
					this.row();
					this.cols();
					// this.drawFont();
					//注意:现在的原点中心为(100,100),所以下面的所有坐标在原来的基础上加(x+100,y+100);
						//中心点一(1000,200)
				this.center(200, 300);
				//中心点二(700,200)
				this.center(800, 300);
				//中心点三(5,300)
				this.center1(105, 400);
				//中心点四(200,300)
				this.center(300, 400);
				//中心点五(400,300)
				this.center(500, 400);
				//中心点六(600,300)
				this.center(700, 400);
				//中心点七(800,300)
				this.center2(900, 400);
				//中心点八(800,600)
				this.center2(900, 700);
				//中心点九(600,600)
				this.center(700, 700);
				//中心点十(400,600)
				this.center(500, 700);
				//中心点十一(200,600)
				this.center(300, 700);
				//中心点十二(5,600)
				this.center1(105, 700);
				//中心点十三(700,700)
				this.center(800, 800);
				//中心点十四(100,700)
				this.center(200, 800);
				//必须当页面中的图片资源加载成功,再画棋子
				},

				//此方法用来画棋盘线
				LineDrawing: function(mx, my, lx, ly) {
					this.ctx.beginPath();
					this.ctx.moveTo(mx, my);
					this.ctx.lineTo(lx, ly);
					this.ctx.stroke();
				},
				//棋盘行
				row: function() {
					for(var i = 200; i <= 900; i += 100) {
						this.ctx.beginPath();
						this.ctx.moveTo(105, i);
						this.ctx.lineTo(900, i);
						this.ctx.stroke();
					}
				},
				//棋盘列
				cols: function() {
					for(var i = 200; i <= 800; i += 100) {
						this.ctx.beginPath();
						this.ctx.moveTo(i, 105);
						this.ctx.lineTo(i, 1000);
						this.ctx.stroke();
					}
					//清除指定的矩形区域
					//this.ctx.clearRect(5, 402,795, 95);
					this.ctx.clearRect(102.5, 502, 795, 95);
					//斜线
					this.LineDrawing(400, 105, 600, 300);
					this.LineDrawing(400, 805, 600, 1000);
					//反斜线
					this.LineDrawing(600, 105, 400, 300);
					this.LineDrawing(600, 805, 400, 1000);
				},
				//坐标的中心点
				center: function(x, y) {
					this.ctx.lineWidth = 5;
					//中心点一(100,200)
					//左上
					this.LineDrawing(x - 10, y - 30, x - 10, y - 10);
					this.LineDrawing(x - 10, y - 10, x - 30, y - 10);
					//右上
					this.LineDrawing(x + 10, y - 30, x + 10, y - 10);
					this.LineDrawing(x + 10, y - 10, x + 30, y - 10);
					//左下
					this.LineDrawing(x - 10, y + 30, x - 10, y + 10);
					this.LineDrawing(x - 10, y + 10, x - 30, y + 10);
					//右下
					this.LineDrawing(x + 10, y + 30, x + 10, y + 10);
					this.LineDrawing(x + 10, y + 10, x + 30, y + 10);
				},
				//坐标的中心点
			center1: function(x, y) {
				this.ctx.lineWidth = 5;
					//右上
					this.LineDrawing(x + 10, y - 30, x + 10, y - 10);
					this.LineDrawing(x + 10, y - 10, x + 30, y - 10);
					this.LineDrawing(x + 10, y + 30, x + 10, y + 10);
				this.LineDrawing(x + 10, y + 10, x + 30, y + 10);
			},
			//坐标的中心点
			center2: function(x, y) {
				this.ctx.lineWidth = 5;
				//右上
				this.LineDrawing(x - 10, y - 30, x - 10, y - 10);
				this.LineDrawing(x - 10, y - 10, x - 30, y - 10);
				this.LineDrawing(x - 10, y + 30, x - 10, y + 10);
				this.LineDrawing(x - 10, y + 10, x - 30, y + 10);
			},

			};
			// 绘制棋子
			function drewChess(Chess){
				for(var i in Chess){
					var id = Chess[i].ids
					var x = Chess[i].positionX
					var y = Chess[i].positionY	
					var ids = document.getElementById(id)
					//ids.onload = function (params) {
					object.ctx.drawImage(ids, x, y, 100, 100);
					//}	
				}
			}
			// 结果弹出框
			function showToast(msg,duration){  
				duration=isNaN(duration)?3000:duration;  
				var m = document.createElement('div');  
				m.innerHTML = msg;  
				m.style.cssText="width:50%; min-width:180px; background:#000; opacity:0.6; height:auto;min-height: 100px; color:#fff; line-height:100px; text-align:center; font-size:40px; border-radius: 20px; position:fixed; top: 50%; left: 50%;transform: translate(-50%, -50%); z-index:999999;";  
				document.body.appendChild(m);  
				setTimeout(function() {  
					var d = 0.5;  
					m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';  
					m.style.opacity = '0';  
					setTimeout(function() { document.body.removeChild(m) }, d * 1000);  
				}, duration);  
			}  
			// 走子
			function drewsChess(context,Chess,xwqe,yqwe,idname){
				this.repaint(context)
				for(var i in Chess){
					var id = Chess[i].ids
					var x = Chess[i].positionX
					var y = Chess[i].positionY	
					var ids = document.getElementById(id)
					context.drawImage(ids, x, y, 100, 100);
				}
				whose==1?whose= 0 : whose= 1
				redGonum++
				var index = redGonum-1
				setTimeout(() => {
					console.log('index', index)
					if(numberKill>0){
						console.log('不是一步杀')
						for(var i in Answer){
							console.log(Answer[index])
							if(Answer[index].ids==idname&&xwqe==Answer[index].positionX&&yqwe==Answer[index].positionY){
								var datatime = new Date().getTime
								useTime = datatime-nowtime
								if(Blackgo.length+1==redGonum){
									console.log('走成功,闯关成功')
									$.ajax({
										url: "https://app.tongxinyizhi.com/txyz/ItemBank/addClearance",
										data: {
											oneId: oneid,
											twoId:twoid,
											threeId :threeId,
											openId:openid,
											itemId:threeId,
											useTime:useTime
										},
										dataType: 'json',
										xhrFields:{
											'Access-Control-Allow-Origin': '*'},
										success: function( result ) {
											console.log('8888', result)
											if(result.success=='1001'){
												audio.src="https://app.tongxinyizhi.com/music/win.mp3"
												audio.play()
												showToast('闯关成功',1000)
												wx.miniProgram.getEnv(function(res) {
													if(res.miniprogram) {
														setTimeout(function() {
															wx.miniProgram.navigateBack({
																delta:0
															});
														}, 1000);
													}
												});
											}
										}
									})
									break;
								}else{
									if(whose==0){
										for(var i in Chess){
											var j=Chess[i].pieceName
											for(var k in Blackgo){
												if(j==Blackgo[blackGonum].pieceName){
													if(Chess[i].pieceName==j && Blackgo[blackGonum].pieceName==j){
														console.log('Chess',Chess[i])
														// clearChess(context,Chess[i].positionX,Chess[i].positionY)
														Chess[i].positionX = Blackgo[blackGonum].positionX
														Chess[i].positionY = Blackgo[blackGonum].positionY
														setTimeout(() => {
															this.repaint(context)
															// Chess.push(Blackgo[blackGonum])
															console.log(Chess)
															drewChess(Chess)
														}, 1500);
													}
												}
											}
										}
										// this.object.row()
										// this.object.cols()
										whose==1?whose=0:whose=1
										blackGonum++
										// setTimeout(() => {
										// 	drewChess(Chess)
										// 	whose==1?whose=0:whose=1
										// 	blackGonum++
										// }, 1500);
									}
								}
							}else{
								audio.src="https://app.tongxinyizhi.com/music/fail.mp3"
								audio.play()
								wx.miniProgram.getEnv(function(res) {
									if(res.miniprogram) {
										setTimeout(function() {
											wx.miniProgram.navigateBack({
												delta:0
											});
										}, 1000);
									}
								});
								showToast('闯关失败',1000)
							}
							break
						}
					}else{
						console.log('是一步杀')
						for(var i=0;i<Answer.length;i++){
							//alert(i)
							if(Answer[i].ids==idname&&xwqe==Answer[i].positionX&&yqwe==Answer[i].positionY){
								console.log('zou88')
								var datatime = new Date().getTime
								useTime = datatime-nowtime
								$.ajax({
									url: "https://app.tongxinyizhi.com/txyz/ItemBank/addClearance",
									data: {
										oneId: oneid,
										twoId:twoid,
										threeId :threeId,
										openId:openid,
										itemId:threeId,
										useTime:useTime

									},
									dataType: 'json',
									xhrFields:{
										'Access-Control-Allow-Origin': '*'},
									success: function( result ) {

										if(result.success=='1001'){
											audio.src="https://app.tongxinyizhi.com/music/win.mp3"
											//alert(audio.src)
											audio.play()
											//ii=0
											// alert('闯关成功')
											showToast('闯关成功',1000)
											wx.miniProgram.getEnv(function(res) {
												if(res.miniprogram) {
													setTimeout(function() {
														wx.miniProgram.navigateBack({
															delta:0
														});
													}, 1000);
												}
											});
										}
									}
								})
								break;
							}else{
								if(i==Answer.length-1){

									audio.src="https://app.tongxinyizhi.com/music/fail.mp3"
									//alert(audio.src)
									audio.play()
									showToast('闯关失败',1000)
									wx.miniProgram.getEnv(function(res) {
										if(res.miniprogram) {
											setTimeout(function() {
												wx.miniProgram.navigateBack({
													delta:0
												});
											}, 1000);
										}
									});
								}
							}
						}
					}
				}, 100);				
			}
			function draw_Chess_All(context){
				for (var i = 0;i < Chess.length; i++) {				
					draw_Chess_One(Chess[i].x,Chess[i].y,Chess[i].ids,Chess[i].whose);				
				}
			}
			function draw_Chess_One(x,y,ids,whose){
				var ids = document.getElementById(ids)
				object.ctx.drawImage(ids, x+50, y+50, 100, 100);
			}
			// 点击到棋子后的显示框
			function draw_check(context,x,y){
				context.lineWidth = 6
      	context.strokeStyle = '#973c51'
				//重新画整个棋盘
				//  repaint(context);
				//开始画左上角
				context.beginPath();
				context.moveTo(x-46,y-33);
				context.lineTo(x-46,y-46);
				context.lineTo(x-33,y-46);
				//开始画右上角
				context.moveTo(x+33,y-46);
				context.lineTo(x+46,y-46);
				context.lineTo(x+46,y-33);
				//开始画左下角
				context.moveTo(x-46,y+33);
				context.lineTo(x-46,y+46);
				context.lineTo(x-33,y+46);
				//开始画右下角
				context.moveTo(x+46,y+33);
				context.lineTo(x+46,y+46);
				context.lineTo(x+33,y+46);
				context.stroke();
			}
			
			// 清除要走的棋子
			function clearChess(context,cx,cy){
				object.ctx.clearRect(0,0,1000,1100)
			}
			// 重绘
			function repaint(context){
				context.clearRect(0,0,1005, 1105);
				context.lineWidth = 4;
				context.strokeStyle = "#5B4209";
				object.LineDrawing()
				object.row()
				object.cols()
				//中心点一(1000,200)
				object.center(200, 300);
					//中心点二(700,200)
					object.center(800, 300);
					//中心点三(5,300)
					object.center1(105, 400);
					//中心点四(200,300)
					object.center(300, 400);
					//中心点五(400,300)
					object.center(500, 400);
					//中心点六(600,300)
					object.center(700, 400);
					//中心点七(800,300)
					object.center2(900, 400);
					//中心点八(800,600)
					object.center2(900, 700);
					//中心点九(600,600)
					object.center(700, 700);
					//中心点十(400,600)
					object.center(500, 700);
					//中心点十一(200,600)
					object.center(300, 700);
					//中心点十二(5,600)
					object.center1(105, 700);
					//中心点十三(700,700)
					object.center(800, 800);
					//中心点十四(100,700)
					object.center(200, 800);
				drewChess(Chess);
			}
			
			// 点击次数
			var number=0
			// 点击canvas之后的操作
			function get_chess(context,x,y){
				console.log('点击次数1:', number)
				number ++
				let eat=true
				//alert(number)
				//定义俩个变量
				console.log('点击次数2:', number)
				var sub_x = 0,sub_y = 0;
				if (x<50||y<50||x>950||y>1010) {return false};
				//这里就是一个四舍五入和向下取整,以便棋子能在棋盘上按格走
				if (x%100>80||x%100<20) {sub_x = 100*Math.round(x/100)};
				if (x%100>30&&x%100<70) {sub_x = x>100?(Math.floor(x/100)*100 ):50};
				if (y%100>80||y%100<20) {sub_y = 100*Math.round(y/100)};
				if (y%100>30&&y%100<70) {sub_y = y>100?(Math.floor(y/100)*100 ):50};
				//alert('x'+x+"---"+y)
				//alert('sub'+sub_x+"---"+sub_y)
				if(sub_x > 0 && sub_y > 0){
					console.log('点击次数3:', number)
					if(number == 1){
						for (var i = 0;i < Chess.length; i++) {
							if(Chess[i].positionX==sub_x-50 && Chess[i].positionY==sub_y-50){
								if(Chess[i].redBlack == 1){
									draw_check(context,sub_x,sub_y);//画棋子点中的框
									desc_click = [sub_x-50,sub_y-50,i,Chess[i].redBlack,Chess[i].piece,Chess[i].ids];
									console.log('desc_click',desc_click)
									break
								} else {
									number = 0
									console.log(number)
									break
								}
							}
						}
					}
					if (number == 2){	
						console.log(number)
						if (is_chess(sub_x-50,sub_y-50)){
							console.log('有旗子','sub_x-50',sub_x-50,'sub_y-50',sub_y-50)
							for (var i = 0;i < Chess.length; i++) {
								console.log('Chess[i].positionX',Chess[i].positionX,'Chess[i].positionY',Chess[i].positionY)
								if(Chess[i].positionX==sub_x-50 && Chess[i].positionY==sub_y-50){
									console.log('有旗子2',Chess[i])
									if(Chess[i].redBlack == 1){
										repaint(context)
										drewChess(Chess)
										draw_check(context,sub_x,sub_y)
										desc_click = [sub_x-50,sub_y-50,i,Chess[i].redBlack,Chess[i].piece,Chess[i].ids]
										console.log('desc_click3',desc_click)
										number = 1
									} else {
										console.log('desc_click5',desc_click)
										let idx = desc_click[2]
										let rb = desc_click[3]
										console.log('Chess[idx]',Chess[idx])
										let oldx = Chess[idx].positionX
										const oldy = Chess[idx].positionY	
										var idname = Chess[idx].ids
										if(go(sub_x-50,sub_y-50,oldx,oldy,desc_click[4],Chess[idx].redBlack,eat)){
											console.log('ahaha',)
											clearChess()
											Chess[idx].positionX=sub_x-50
											Chess[idx].positionY=sub_y-50
											drewsChess(context,Chess,sub_x-50,sub_y-50,idname)
											number=0
											for(let i = 0;i < Chess.length; i++){
												if(Chess[i].positionX == sub_x-50 && Chess[i].positionY == sub_y-50){
													Chess.splice(i,1)
												}
											}
											// repaint()
											// console.log(Chess[idx])
											
											//alert(idname)
											//数组清除数据
											
										} else{
											number = 1
										}
									}
									break
								}
							}
						}else{	
						// 棋子不存在可以走
							console.log('棋子不存在可以走',desc_click)
							let idx = desc_click[2]
							if(go(sub_x-50,sub_y-50,desc_click[0],desc_click[1],desc_click[4],Chess[idx].redBlack,eat)){
								clearChess()
								var idname = Chess[idx].ids
								//alert(idname)
								//数组清除数据
								Chess[idx].positionX=sub_x-50
								Chess[idx].positionY=sub_y-50
								number=0
								drewsChess(context,Chess,sub_x-50,sub_y-50,idname)
							} else{
								console.log('走子规则不对')
								number = 1
							}
						}
					} else{
						number = 1
					}
				}
			}		
			//吃子
			function go(xwqe,yqwe,oldx,oldy,piece,whose,eat){
			//alert(eat)
				var can_go = true;
				var num = 0;
				var x1 = parseInt(oldx)
				var y1 = parseInt(oldy) 
				var x2 = xwqe
				var y2 = yqwe
				var txt = parseInt(piece)
				//定义最大值和最小值
				var min_x = x1 > x2?x2:x1;
				var max_x = x1 > x2?x1:x2;
				var min_y = y1 > y2?y2:y1;
				var max_y = y1 > y2?y1:y2;
				//定义是否可以吃子
				//判断是不是俩个将帅面对面
				if (is_face_to_face(x2)) {
					showToast('不能送将',1000)
					return false;
				}
				//下棋
				//alert(txt)
				switch (txt){
					case 1:
						// 如果俩车不在同一条直线上
						if(x1!=x2&&y1!=y2){
							//不能吃子
							can_go = false;
							break;
						}
						//开始遍历
						for(var i = 0;i < Chess.length; i++){
							//
							if(Chess[i].positionX == x2 && Chess[i].positionY > min_y && Chess[i].positionY < max_y ){
								can_go = false; 
							}
							//
							if(Chess[i].positionY == y1 && Chess[i].positionX > min_x && Chess[i].positionX < max_x){
								can_go = false;
							}
						}
						break;
					case 2://如果是马怎么走
						can_go = false;
						if (x1 + 100 === x2 && y1 + 200 === y2 && !is_banMa(x1,x2,y1,y2)) {
							can_go = true
          	}
          	if (x1 + 200 === x2 && y1 + 100 === y2 &&!is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 - 200 === x2 && y1 - 100 === y2 && !is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 + 200 === x2 && y1 - 100 === y2 && !is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 - 200 === x2 && y1 + 100 === y2 && !is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 - 100 === x2 && y1 - 200 === y2 && !is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 - 100 === x2 && y1 + 200 === y2 && !is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 + 100 === x2 && y1 - 200 === y2 && !is_banMa(x1,x2,y1,y2)) {
          	  can_go = true
          	}
						break;
					case 3://象怎么走
						can_go = false;
						if(whose == 0 && y2 < 250) {
							break};
						if(whose == 1 && y2 > 750) {
							break};
							if (x1 + 200 === x2 && y1 + 200 === y2 && !is_chessCloseege(x1,x2,y1,y2)) {
							can_go = true
          	}
          	if (x1 + 200 === x2 && y1 - 200 === y2 && !is_chessCloseege(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 - 200 === x2 && y1 + 200 === y2 && !is_chessCloseege(x1,x2,y1,y2)) {
          	  can_go = true
          	}
          	if (x1 - 200 === x2 && y1 -200 === y2 && !is_chessCloseege(x1,x2,y1,y2)) {
          	  can_go = true
          	}
						break;
					case 4://士怎么走
						can_go= false;
						if(x2>550||x2<350){break};
						if(whose == 1&&y2<750){break};
						if(x1+100==x2&&y1+100==y2){can_go = true};
						if(x1+100==x2&&y1-100==y2){can_go = true};
						if(x1-100==x2&&y1+100==y2){can_go = true};
						if(x1-100==x2&&y1-100==y2){can_go = true};
						break;
					case 5://帅怎么走
						can_go = false;
						if(x2<350||x2>550){break};
						//红棋的帅
						if(whose==1&&y2<750){  
						break};
						//黑棋的将
						if(whose==0&&y2>250){  
						break};
						//判断怎么能走
						if(x1+100==x2&&y1==y2){can_go=true };
						if(x1-100==x2&&y1==y2){can_go=true };
						if(x1==x2&&y1+100==y2){can_go=true };
						if(x1==x2&&y1-100==y2){can_go=true };
						break;
					case 6://炮怎么走
						//不能原地走
						//alert("wolaile")
						if(x1!=x2&&y1!=y2){
						//alert("1")
							can_go = false;
							break;
						}
						//如果吃子
						if(is_chess(x2,y2)){
							 if(x1 == x2 || y1 == y2) {
								for(var i = 0;i < Chess.length; i++){
						         if(Chess[i].positionX==x1&&Chess[i].positionY>min_y&&Chess[i].positionY<max_y){num = num + 1};
						         if(Chess[i].positionY==y1&&Chess[i].positionX>min_x&&Chess[i].positionX<max_x){num = num + 1};
								 }
								if (num !== 1) {
									showToast('按规则走',1000)
									can_go = false
								}
								break
							 }
							for(var i = 0;i < Chess.length; i++){
								if(Chess[i].positionX==x1&&Chess[i].positionY>min_y&&Chess[i].positionY<max_y){showToast('按规则走',1000)
								 can_go = false};
								if(Chess[i].positionY==y1&&Chess[i].positionX>min_x&&Chess[i].positionX<max_x){showToast('按规则走',1000)
								can_go = false};
							}
						 }
						 if (!is_chess(x2,y2)) {
								console.log('点击的位置没有棋子')
          			if (x1 === x2 || y1 === y2) {
          			  console.log('x走')
          			  for (var i = 0;i < Chess.length; i++) {
										// console.log(this.Chess, this.existe)
										if(Chess[i].positionX==x1&&Chess[i].positionY>min_y&&Chess[i].positionY<max_y){num = num + 1};
						        if(Chess[i].positionY==y1&&Chess[i].positionX>min_x&&Chess[i].positionX<max_x){num = num + 1};
          			  }
          			  if (num !== 0) {
										showToast('按规则走',1000)
          			    can_go = false
          			  }
          			  // console.log(this.canGoChess)
          			  break
          			}
							}
						break;
					case 7://兵怎么
						can_go = false
						console.log(x1,y1,x2,y2)
						if (y2>=550 && x1 == x2 && y1 - 100 == y2) {
							can_go = true
						}
						if (y2<550){
							if (y1 == y2 && x1 -100 == x2) {
								can_go = true
							}
							if (y1 == y2 && x1 +100 == x2) {
								can_go = true
							}
							if (x1 == x2 && y1 - 100 == y2) {
								can_go = true
							}
						}
						break;						
				}
				return can_go;
			}  
			//判断这颗棋子是否存在
			function is_chess(x,y){
				//定义existe判断是否这颗棋子还活着默认是死了
				var existe = false;
				for(var i = 0;i<Chess.length;i++){
					//如果某颗棋子的横纵坐标都有并且chess[i][3]任然等于1这个参数是true表示活着
					if(Number(Chess[i].positionX)==x && Number(Chess[i].positionY)==y){existe = true;};
				}
				return existe;
			}
			//判断这颗棋子是否存在绊象腿
			function is_chessCloseege(x1,x2,y1,y2){
				let oldX = x1
				let newX = x2
				let oldY = y1
				let newY = y2
			//定义existe判断是否这颗棋子还活着默认是死了
			var chessCloseege = false;
			for(var i = 0;i<Chess.length;i++){
				let banX = Number(Chess[i].positionX)
				let banY =  Number(Chess[i].positionY)
				// console.log('进入规则象', 'banX', banX, 'banY', banY, 'newX', newX, 'newY', newY, 'oldX', oldX, 'oldY', oldY)
				//如果某颗棋子的横纵坐标都有并且chess[i][3]任然等于1这个参数是true表示活着
				if (oldX + 200 == newX && oldY - 200 == newY && oldX + 100 == banX && oldY - 100 ==banY) {
						showToast('绊象腿',1000)
						chessCloseege = true
        }
        if (oldX + 200 == newX && oldY + 200 == newY && oldX + 100 == banX && oldY + 100 ==banY) {
          showToast('绊象腿',1000)
						chessCloseege = true
        }
        if (oldX - 200 == newX && oldY - 200 == newY && oldX - 100 == banX && oldY - 100 ==banY) {
          showToast('绊象腿',1000)
						chessCloseege = true
        }
        if (oldX - 200 == newX && oldY + 200 == newY && oldX - 100 == banX && oldY + 100 ==banY) {
          showToast('绊象腿',1000)
						chessCloseege = true
        }
			}
			console.log(chessCloseege,'chessCloseege')
			return chessCloseege;
		}
			function is_banMa(x1,x2,y1,y2){
				let oldX = x1
				let newX = x2
				let oldY = y1
				let newY = y2
			//定义existe判断是否这颗棋子还活着默认是死了
			// console.log('绊马腿','(x1,YI)',x1,y1,'(X2,Y2)',x2,y2)
			var banMa = false
			for(var i = 0;i<Chess.length;i++){
				let banX = Number(Chess[i].positionX)
				let banY =  Number(Chess[i].positionY)
				// console.log('进入规则马', 'banX', banX, 'banY', banY, 'newX', newX, 'newY', newY, 'oldX', oldX, 'oldY', oldY)
        if (oldX === banX && oldX + 100 === newX && oldY - 200 === newY && oldY - 100 === banY) {
          showToast('绊马腿',1000)
					banMa = true
					break
        }
        if (oldX === banX && oldX - 100 === newX && oldY - 200 === newY && oldY - 100 === banY) {
					showToast('绊马腿',1000)
					banMa = true
        }
        if (oldY === banY && oldX + 200 === newX && oldY - 100 === newY && oldX + 100 === banX) {
					showToast('绊马腿',1000)
					banMa = true
        }
        if (oldY === banY && oldX + 200 === newX && oldY + 100 === newY && oldX + 100 === banX) {
					showToast('绊马腿',1000)
					banMa = true
        }
        if (oldY === banY && oldX - 200 === newX && oldY + 100 === newY && oldX - 100 === banX) {
					showToast('绊马腿',1000)
					banMa = true
        }
        if (oldY === banY && oldX - 200 === newX && oldY - 100 === newY && oldX - 100 === banX) {
					showToast('绊马腿',1000)
					banMa = true
        }
        if (oldX === banX && oldX + 100 === newX && oldY + 200 === newY && oldY + 100 === banY) {
          showToast('绊马腿',1000)
					banMa = true
        }
        if (oldX === banX && oldX - 100 === newX && oldY + 200 === newY && oldY + 100 === banY) {
          showToast('绊马腿',1000)
					banMa = true
        }
			}
			console.log(banMa,'banMa')
			return banMa;
		}
			function is_face_to_face(x){
				//定义四个参数红色的和黑色的x,y坐标
				var r_x = 0,r_y=0,b_x=0,b_y=0,num=0;
				//定义一个状态
				var state = false;
				for(var i=0;i<Chess.length;i++){
					//chess[i][5]值就是从1到7分别代表不同的棋子,例如1表示军7表示兵卒
					//5表示将帅
					if(Chess[i].piece==5 && Chess[i].whose==1){//如果将帅活着并且1表示红棋
						//获取到这个棋子的横纵坐标
						r_x=Chess[i].x;
						r_y=Chess[i].y;
					}
					if(Chess[i].piece==5 && Chess[i].whose==0){//如果将帅活着并且1表示黑棋
						//获取到当前棋子的横纵坐标
						b_x=Chess[i].x;
						b_y=Chess[i].y;
					}
				}
				//如果红色和黑色的横坐标相同时
				if(r_x == b_x){
					//遍历一遍chess数组
					for(var i = 0;i<Chess.length;i++){
						//如果当前棋子的位置没变并且是红棋落子时
						if(Chess[i].x == r_x){num++;}
					}
					//??有问题
					if(num - 2 == 1 && x != r_x){state = true;}
				}
				return state;
			}
	</script>
</body>
</html>
上一篇:【字符串哈希】Ybt_对称正方形


下一篇:Kiwi Syslog Web Access与Active Directory集成认证