用Javascript编写数独程序

最近一直在玩数独。比如下面这个。当点击右下的8时,所有的8会被框起来。但还是觉得不够智能。

所以我还是想改进一下,增加一点功能:这样更加直观,界面大概是这样的。先写个PC版的,然后在写个小程序或者APP。

我们安装下面的步骤来完成这个项目。

1、界面完成(Javascript)

2、开始按钮(先自动创建下面的新局)

3、点击格的事件(若空格就弹出数字选择,若有数字就框选同数字

4、最后一个格子校验正确。每次填数字均就这个格进行校验。

最后这个是我们模仿的结果图。来自于微信小程序 数独

构想的加强版

第一步:先构建棋盘。

<!-- head,css -->
<style type="text/css">	
	html,body,ul,li div{margin: 0;padding:0;}
	.clearfix:after{content:"";display:block;clear:both;}
	.clearfix{zoom:1;}
	.box_bg  {width: 400px;height:396px;background-color: #e1c285;margin: 0 auto;padding-top: 10px; }
	.box_bg ul {width: 380px;margin: 0 auto;border:2px solid #ab8d17;background-color: #fff;}
	.box_bg ul li {display: block;float: left;width: 40px;height: 40px;border:1px solid #d6c079;line-height: 40px;text-align: center;font-size:26px;color:#383838;font-weight: bold;}
	.box_bg ul li.border_right{border-right:2px solid #c19502;}
	.box_bg ul li.border_bottom{border-bottom:2px solid #c19502;}
</style> 
<script type="text/javascript" src="/assets/libs/jquery/dist/jquery.min.js"></script>  
<!-- body -->
<div id="shudubox" class="box_bg"></div>
<!-- js -->
<script type="text/javascript">
	var data = []; 
	for (var i = 0; i < 9; i++) {
		for (var j = 0; j < 9; j++) {
			data[i*9+j]=j+1;
		}
	} 
	console.log(data); //初始化数据

	var str_box = "" ;
	$(function(argument) { 
		str_box += "<ul class='clearfix'>";
		for (var i = 0; i < 9; i++) { 
			for (var j = 0; j < 9; j++) { 
				str_box += "<li class='"+ (( i==2 || i==5  )?"border_bottom":"") + (((j+1)%3==0 && j!=8)?" border_right":"")  +"'>"  +"</li>";
			} 
		}
		str_box += "</ul>"; 
		$("#shudubox").html(str_box);   //画盘子
	}); 
 
</script>

第二步。增加数独题目:

var startdata ="400179008003800000000300090300000741700000002924000003040008000000007100100253009"; //一道中级题目
	data = startdata.split("");
	console.log(data);  
	$(function(argument) { 
		var shuduboxli = $("#shudubox li") ;
		for (var i = 0; i < shuduboxli.length; i++) {
			$(shuduboxli[i]).html((data[i]=="0")?"":data[i]);
		}
	});

第三步,编写相关事件:

单元格点击事件。如果有数字就显示相同数字。如是可填区域就显示数字盘。可填区域的判断是根据原题数组。

$("#shudubox li").click(function(argument) {
			var li = this; 
			var lin =   parseInt($(li).html());
			if(lin >0 ){
				light_select(lin);  //如果有数字 ,就高亮同数字
			}

			var liindex =  parseInt(data[($(li).index())]);
			if( liindex==0 ){
				showselectbox($(li).index());  //如果原题中为空就运行填或修改数字
			}			 
		});
	function light_select(n) {
		console.log(n);//将数据里的相同数字设置为on
		var shuduboxli = $("#shudubox li") ;
		$("#shudubox .on").removeClass("on");
		for (var i = 0; i < shuduboxli.length; i++) {
			var lin =  parseInt($(shuduboxli[i]).html());
			if(lin==n){
				$(shuduboxli[i]).addClass("on");
			} 
		}
		$("#selectbox").css("display","none");
	}

 

	function showselectbox(index) {
		console.log(index); //显示选择数字盘
		$("#selectbox").attr("targetid",index);
		$("#selectbox").css("display","block");
		var shuduboxlix = $("#shudubox li").eq(index) ;
		$("#selectbox").offset({left: $(shuduboxlix).offset().left,top:  $(shuduboxlix).offset().top}) 
	}

表盘点击事件:

	$("#selectbox li").click(function(argument) {
			var li = this;  
			var lin =  parseInt($(li).html());
			if(lin >0 ){
				//点击的格子填充这个数字 fill(lin)
				$("#shudubox li").eq($("#selectbox").attr("targetid")).html(lin);   
			}else{ 
				if($(li).html()=="清除" || $(li).html()=="" ){  
						$("#shudubox li").eq($("#selectbox").attr("targetid")).html(""); 
					} 
			}
			$("#selectbox").css("display","none");
	});

由于增加了选择数字表盘:head区域完整代码为

<!-- head,css -->
<style type="text/css">	
	html,body,ul,li div{margin: 0;padding:0;}
	.clearfix:after{content:"";display:block;clear:both;}
	.clearfix{zoom:1;}
	.box_bg  {width: 400px;height: 396px;background-color: #7f5a0f;margin: 0 auto;padding-top: 20px;padding-bottom: 20px;  }
	.box_bg ul {width: 380px;margin: 0 auto;border:2px solid #ab8d17;background-color: #fff;}
	.box_bg ul li {display: block;float: left;width: 40px;height: 40px;border:1px solid #d6c079;line-height: 40px;text-align: center;font-size:26px;color:#383838;font-weight: bold;}
	.box_bg ul li.border_right{border-right:2px solid #c19502;}
	.box_bg ul li.border_bottom{border-bottom:2px solid #c19502;}
	.box_bg .on{background-color: #ffe000;}
	 
	#selectbox {width: 115px;height: 152px;background-color: #deecfd;margin: 0 auto;     position: absolute; display: none;  }
	 
	#selectbox ul {width: 112px;margin: 0 auto;border:2px solid #deecfd;}
	#selectbox ul li {display: block;float: left;width: 35px;height: 35px;border:1px solid #d6c079;line-height: 35px;text-align: center;font-size:20px;color:#383838;font-weight: bold;}
	#selectbox ul li.border_right{border-right:2px solid #c19502;}
	#selectbox ul li.border_bottom{border-bottom:2px solid #c19502;}
</style> 
<script type="text/javascript" src="/assets/libs/jquery/dist/jquery.min.js"></script>

body区域代码:

<div id="shudubox" class="box_bg"></div>
<div id="selectbox" targetid="" >
	<ul>
		<li>1</li>
		<li>2</li>
		<li>3</li>
		<li>4</li>
		<li>5</li>
		<li>6</li>
		<li>7</li>
		<li>8</li>
		<li>9</li>
		<li style="    font-size: 17px;">清除</li><li></li><li style="    font-size: 17px;">关闭</li>
	</ul>
</div>

第几步:编写检查程序

	//检查
	function check_rule(targetid,n) { 

		var current_H = parseInt(targetid/9) ; //行
		var current_L = parseInt(targetid%9) ;  //列
		var current_B = parseInt(current_H/3) *3 + parseInt(current_L/3);//块
		console.log(current_H + "," +current_L + "," +current_B + " "  );


		//先检查同行是否有相等的
		var samei = -1;
		for (var i = 0; i < data.length; i++) {
			if(parseInt(i/9) ==  current_H){
				if (data[i] == n){
					samei=i;
				}
			}
			if(parseInt(i%9) ==  current_L){
				if (data[i] == n){
					samei=i;
				}
			}
			if(parseInt(parseInt(i/9)/3) *3 + parseInt(parseInt(i%9)/3) ==  current_B){
				if (data[i] == n){
					samei=i;
				}
			} 
		}

		if(samei>-1){
				//检查是否通过red1
			var shuduboxli = $("#shudubox li") ;
			$(shuduboxli[samei]).addClass("red1"); 
			// for (var i = 0; i < shuduboxli.length; i++) {
			// 	var lin =  parseInt($(shuduboxli[i]).html());
			// 	if(lin==n){
			// 		$(shuduboxli[i]).addClass("red1"); 
			// 	} 
			// }
			setTimeout(function(argument) {
						$(shuduboxli).removeClass("red1");
					},2000);
			return false;
		}
		console.log(samei);
		return true;

		
	}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
是一种经典的游戏,在计算机科学中也有很多人尝试使用编程语言来编写程序。其中,Python是一种常用的编程语言,比较适合用来编写程序。 首先,编写程序需要定义数的数据结构。常用的方式是使用二维数组来表示数的九宫格,每个格子用数字0~9来表示,其中0代表着该格子为空。 其次,需要编写求解算法。一般而言,数问题可以使用回溯算法来解决。具体的实现过程可以在每次填完一个数字后,递归地填下一个位置。如果填到了最后一个位置,就代表求解成功;如果某个位置填不进任何数字了,就需要回溯到上一个位置重新填。这个过程需要进行一定的剪枝,以提高求解的效率。 除了求解算法之外,还需要编写的生成算法。一般而言,生成数的过程是从一个空的九宫格开始,并随机填入一些数字,然后用求解算法验证这个数是否有解。如果有解,就再填入一些数字,直到生成一个完整的数。 最后,需要编写用户界面。这可以使用Python提供的GUI库来实现。用户界面可以包括一个可交互的数棋盘,用户可以通过鼠标或者键盘来填写数字,求解数或者生成新的数。为了提高用户体验,还可以添加一些提示和错误检查等功能。 总之,Python编写程序是一项比较有挑战性的任务,需要对Python编程语言和数算法有一定的了解。不过,一旦掌握了这些基本技能,编写高效而优美的数程序就不在话下了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东宇科技

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

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

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

打赏作者

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

抵扣说明:

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

余额充值