• 原声js 五子棋 源码


    Welcome to use MarkDown

    	<style type="text/css">
    		.box{
    			 600px;
    			height: 600px;
    			background: url(img/bak.jpg) no-repeat;
    			background-size: cover;
    			margin: 30px auto;
    			border: 1px solid #000000;
    		}
    	</style>
    
    		<div class="box">
    			<canvas id="can" width="600" height="600px"></canvas>
    		</div>
    
    		<script type="text/javascript">
    			var maps = [];
    				for(var i=0;i<15;i++){
    					maps[i] = [];
    					for(var j=0;j<15;j++){
    					maps[i][j] = 0;
    					}
    			}
    			//制作棋盘
    			var can = document.getElementById("can");
    			
    			var ctx = can.getContext('2d');
    			
    			var isblack = true;
    			
    			ctx.strokeStyle ="#000";
    			
    			for(var i=0;i<15;i++){
    				for(var j=0;j<15;j++){
    					ctx.strokeRect(j*40,i*40,40,40)
    				}
    			}
    			
    			//制作棋子
    			var black = new Image();
    			var white  = new Image();
    			black.src = "img/black.png"
    			white.src = "img/white.png"
    			
    			
    			
    			can.addEventListener('click',function(ev){
    				var dx = ev.clientX  -  can.offsetLeft;
    				var dy = ev.clientY - can.offsetTop;
    				
    //				console.log(dx,dy)
    
    				var row =0;
    				var col =0;
    				
    				if(dx%40<20){
    					row = Math.floor(dx/40)
    					
    				}else{
    					row = Math.ceil(dx/40)
    				}
    				
    				if(dy%40<20){
    					col = Math.floor(dy/40)
    					
    				}else{
    					col = Math.ceil(dy/40)
    				}
    				
    //				console.log(row,col)
    				
    				
    				
    				
    				if(isblack){
    					
    					
    					ctx.drawImage(black,row*40-20,col*40-20)
    					isblack = false;
    					
    					maps[row][col] = 2 //黑
    					
    					iswin(2,row,col)
    				}else{
    					ctx.drawImage(white,row*40-20,col*40-20)
    					isblack = true;
    					
    					maps[row][col] = 1  //白
    					
    					iswin(1,row,col)
    				}
    				
    				//判断输赢
    				function iswin(t,row,col){
    					var orgrow = row ;   //将目标点存起来
    					var orgcol = col ;
    					var total =1;
    					
    					//垂直方向
    					while(col-1>0&&maps[row][col-1] == t){
    						total++;
    						col--;
    					}
    					
    					row = orgrow ;
    					col = orgcol;
    					
    					while(col+1<15&&maps[row][col+1] == t){
    						
    						col++;
    						total++;
    					}
    					console.log(total)
    					if(total>=5){
    						if(t==1){
    							setTimeout(function(){alert('白子赢了')},500)
    						}else{
    							
    							setTimeout(function(){alert('黑子赢了')},500);
    						}
    					}
    					
    					
    					//水平方向
    					row=orgrow;
    			    	col=orgcol;
    			    	total=1;
    			    	while(row-1>0&&maps[row-1][col]==t)
    			    	{
    			    		row--;
    			    		total++;
    			    	}
    			    	row=orgrow;
    			    	col=orgcol;
    			    	while(row+1<15&&maps[row+1][col]==t)
    			    	{
    			    		row++;
    			    		total++;
    			    	}
    			    	if(total>=5)
    			    	{
    			    		if(t==1)
    			    		alert("白子赢");
    			    		else
    			    		alert("黑子赢");
    			    	}
    		    	
    		    			
    		    		//左下和右上
    		    		row = orgrow ;
    					col = orgcol;
    					total =1;
    		    		while(row+1<15&&col-1>0&&maps[row+1][col-1]==t){
    		    				row++;
    		    				col--;
    		    				total++;
    		    		}
    		    		row = orgrow ;
    					col = orgcol;
    		    		while(row-1>0&&col+1<15&&maps[row-1][col+1] == t){
    		    			row--;
    		    			col++;
    		    			total++;
    		    		}
    		    		if(total>=5){
    		    			if(t==1){
    							setTimeout(function(){alert('白子赢了')},500)
    						}else{
    							
    							setTimeout(function(){alert('黑子赢了')},500);
    						}
    		    		}
    		    		
    		    		//右下和左上
    		    		row = orgrow ;
    					col = orgcol;
    		    		total=1;
    		    		while(row+1<15&&col+1<15&&maps[row+1][col+1]==t){
    		    				row++;
    		    				col++;
    		    				total++;
    		    		}
    		    		row = orgrow ;
    					col = orgcol;
    					
    		    		while(row-1>0&&col-1>0&&maps[row-1][col-1] == t){
    		    			row--;
    		    			col--;
    		    			total++;
    		    		}
    		    		if(total>=5){
    		    			if(t==1){
    							setTimeout(function(){alert('白子赢了')},500)
    						}else{
    							
    							setTimeout(function(){alert('黑子赢了')},500);
    						}
    		    		}
    		    		//至此所有方向游戏都结束呢
    		    	
    				}
    				
    			})
    			
    		</script>
    
    欢迎各位大虾指正
  • 相关阅读:
    android studio快捷键大全
    HTML5规范尘埃落定,5个开发工具推荐
    javascript:设置URL参数的方法,适合多条件查询
    MyBatis 多表联合查询及优化
    js动态向页面中添加表格
    mysql装完电脑里面没mysql相关服务
    javascript 实现一个网页,然后计算出有多少剩余时间的倒计时程序
    xcode于Archive当产生安装包遇到ld: library not found for -lPods
    计算机网络 2. 应用层
    Android手游《》斗地主完整的源代码(支持单机和网络对战)
  • 原文地址:https://www.cnblogs.com/he-zhi/p/6886571.html
Copyright © 2020-2023  润新知