JavaSrcipt 五子棋

HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>五子棋</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
		
		body{
			padding-top: 100px;
			
		}
		
        .main {
            width: 600px;
            height: 600px;
            margin: 0 auto;
            background-color: burlywood;
        }

        .col {
            position: relative;

            width: 40px;
            height: 40px;
            box-sizing: border-box;
            border: 1px solid #000;
            border-collapse: collapse;
            /*border-radius: 20px;*/

        }

        .row {
            position: relative;
            display: flex;
            height: 40px;
            /*background-color: brown;*/
        }

        .col-action {
            background-color: blue;
        }

        .col-actionA {
            /*background-color: white;*/

        }

        .col-actionB {
            /*background-color: black;*/

        }
        
        .col-actionA::before{
            content: "";
            position: absolute;
            width: 30px;
            height: 30px;
            background-color: white;
            border-radius: 99px;
            top:4.5px;
            left:4.5px;
            box-shadow: 0 0 2px rgba(0,0,0,0.5);

        }
        .col-actionB::before{
            content: "";
            width: 30px;
            height: 30px;
            background-color: black;
            border-radius: 99px;
            position: absolute;
            top:4.5px;
            left:4.5px;
            box-shadow: 0 0 2px rgba(128,128,128,0.5);
        }
		
		.hq{
			width: 600px;
            height: 600px;
            margin: 0 auto;
		}

    </style>
	
	<script type="text/javascript" src="js/demo03.js" ></script>
	
</head>
<body>

<div class="main">
	
    <div class="qipan" id="qipan">


    </div>
	<div class="hq"><button id="hq">悔棋</button></div>
</div>

</body>
</html>

JavaSrcipt


window.onload = function(){
	
	var busz = new Array();
	
	//div单击事件
	var ansj = function () {
            	
        const x = this.getAttribute("col");
        const y = this.getAttribute("row");
//              console.log(x, y, nowPlayer)
				
        if (nowPlayer) {
            qjck[this.getAttribute("row")][this.getAttribute("col")] = 1;
            this.classList.add("col-actionA");
            nowPlayer = !nowPlayer;
        } else {
            qjck[this.getAttribute("row")][this.getAttribute("col")] = 2;
            this.classList.add("col-actionB");
            nowPlayer = !nowPlayer;
        }
        
        busz.push(this);
        
        var js = pdsl(y,x);
				
		if(js)
		{
			setTimeout(function(){
				alert("游戏结束");
				location.reload();  //刷新浏览器
					
			},50);
					
		}
                
        this.onclick = null;
                
    }
	
	//判断是否结束
	var pdsl = function(x,y){

		var sx=1,zy=1,zs=1,ys=1,t=1;
	
		//上
		for(t=1;t<=5;t++){
			
			if(x-t < 0)
				break;		
			
			console.log("上"+zy);
			if(qjck[x-t][y]==qjck[x][y] && qjck[x-t][y]!=0)
				sx++;
			else
				break;
		}
		
		//下
		for(t=1;t<=5;t++){
			
			if(Number(x)+t >= 10)
				break;
			
			console.log("下"+zy);
			if(qjck[Number(x)+t][y]==qjck[Number(x)][y] && qjck[Number(x)+t][y]!=0)
				sx++;
			else
				break;
		}
		
		//左
		for(t=1;t<=5;t++){
			
			if(y-t < 0)
				break;		
			
			console.log("左"+zy);
			if(qjck[x][y-t]==qjck[x][y] && qjck[x][y-t]!=0)
				zy++;
			else
				break;
		}
		
		//右
		for(t=1;t<=5;t++){
			
			if(Number(y)+t >= 10)
				break;
			
			console.log("右"+zy);
			if(qjck[x][Number(y)+t]==qjck[x][y] && qjck[x][Number(y)+t]!=0)
				zy++;
			else
				break;
		}
	
		//上左
		for(t=1;t<=5;t++){
			
			if(x-t < 0)
				break;		
			
			console.log("上左"+zy);
			if(qjck[x-t][y-t]==qjck[x][y] && qjck[x-t][y-t]!=0)
				zs++;
			else
				break;
		}	
		
		//下右
		for(t=1;t<=5;t++){
			
			if(Number(x)+t >= 10 || Number(y)+t >= 10)
				break;		
			
			console.log("下右"+zy);
			if(qjck[Number(x)+t][Number(y)+t]==qjck[x][y] && qjck[Number(x)+t][Number(y)+t]!=0)
				zs++;
			else
				break;
		}
		
		//上右
		for(t=1;t<=5;t++){
			
			if(x-t < 0 || Number(y)+t >= 10)
				break;		
			
			console.log("上右"+zy);
			if(qjck[x-t][Number(y)+t]==qjck[x][y] && qjck[x-t][Number(y)+t]!=0)
				ys++;
			else
				break;
		}	
		
		//下左
		for(t=1;t<=5;t++){
			
			if(Number(x)+t >= 10 || y-t < 0)
				break;		
			
			console.log("下右"+zy);
			if(qjck[Number(x)+t][y-t]==qjck[x][y] && qjck[Number(x)+t][y-t]!=0)
				ys++;
			else
				break;
		}
		
		
		console.log(sx + " " + zy + " " + zs + " " + ys);
		
		if(sx == 5 || zy==5 || zs==5 || ys==5)
			return true;
		else
			return false;
		
	}

    var nowPlayer = 0;

	//棋盘数组
    var qjck = Array();
	
	//div
    var piece = document.createElement("div");
    piece.id = "piece";

	//得到div
    var qipan = document.getElementById("qipan");

	//生成棋盘
    for (let r = 0; r < 15; r++) {
        let newrow = document.createElement("div");
        newrow.id = "row" + r;
        newrow.classList.add("row")

        let arrCol = Array()
        qjck.push(arrCol)

        for (let c = 0; c < 15; c++) {
            arrCol.push(0)
            let newcol = document.createElement("div");

            newcol.id = "col" + c;

            newcol.classList.add("col");

            newcol.setAttribute("row", r);
            newcol.setAttribute("col", c)
            newrow.appendChild(newcol)

            newcol.onclick = ansj;

        }


        // console.log(newrow)
        qipan.appendChild(newrow)
    }
    
    //悔棋
    var hq = document.getElementById("hq");
    
    hq.onclick = function(){
    	
    	if(busz.length <= 0)
    		return;
    	
    	var divt = busz.pop();
    	divt.onclick = ansj;
    	
    	divt.classList.remove("col-actionA");
    	divt.classList.remove("col-actionB");
    	
    	qjck[divt.getAttribute("row")][divt.getAttribute("col")] = 0;
    	
    	nowPlayer = !nowPlayer;
    	
//  	console.log(qjck);
    	console.log(divt);
    }
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布伽思索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值