纯js 数独游戏

<head>
<script>
	delayTime=10;
	board=[];
	maplistc = [];
	maplistr = [];
	maplistk = [];
	emptylist = [];
	notEmptyList={};
	
	run=false;
	
	function refresh(){
	
		if(document.getElementById("t")!=null)
			document.getElementById("t").remove();
			
		if(document.getElementById("b1")!=null)
			document.getElementById("b1").remove();
		
		if(document.getElementById("b2")!=null)
			document.getElementById("b2").remove();
			
		if(document.getElementById("s")!=null)
			document.getElementById("s").remove();
	
		document.write("<table id='t'>");
		
		for(var i=0;i<board.length;i++){
		
			document.write("<tr>");
		
			for(var j=0;j<board[i].length;j++){
			
				var clazz = notEmptyList[i+" "+j]?
							"width:50px;height:50px;border:1px solid black;line-height:50px;text-align:center;background-color:yellow;"
							:
							"width:50px;height:50px;border:1px solid black;line-height:50px;text-align:center;";
			
				if(board[i][j]=="."){
					document.write("<td style='"+clazz+"'></td>");
				}else{
					document.write("<td style='"+clazz+"'>"+board[i][j]+"</td>");
				}
			}
			
			document.write("</tr>");
		}
		
		document.write("</table>");
		
		if(!run){
			
			document.write("<select id='s'>");
			for(var i=0;i<=17;i++){
				document.write("<option>"+i+"</option>");
			}
			document.write("</select>");
		
			document.write("<button id='b1' onclick='reset()'>刷新</button> <button id='b2' onclick='startRun()'>开始</button>");
			
		}
		
	}

	function  getIndex(i, j) {

		if (i >= 0 && i <= 2 && j >= 0 && j <= 2) {
			return 0;
		} else if (i >= 0 && i <= 2 && j >= 3 && j <= 5) {
			return 1;
		} else if (i >= 0 && i <= 2 && j >= 6 && j <= 8) {
			return 2;
		} else if (i >= 3 && i <= 5 && j >= 0 && j <= 2) {
			return 3;
		} else if (i >= 3 && i <= 5 && j >= 3 && j <= 5) {
			return 4;
		} else if (i >= 3 && i <= 5 && j >= 6 && j <= 8) {
			return 5;
		} else if (i >= 6 && i <= 8 && j >= 0 && j <= 2) {
			return 6;
		} else if (i >= 6 && i <= 8 && j >= 3 && j <= 5) {
			return 7;
		} else {
			return 8;
		}

	}
	
	function solveSudoku() {

		maplistc = [{},{},{},{},{},{},{},{},{}];
		maplistr = [{},{},{},{},{},{},{},{},{}];
		maplistk = [{},{},{},{},{},{},{},{},{}];
		emptylist = [];

		for (var i = 0; i < 9; i++) {
			
			for(var j=1;j<=9;j++){
				maplistc[i][j]=0;
				maplistr[i][j]=0;
				maplistk[i][j]=0;
			}

		}

		for (var i = 0; i < board.length; i++) {

			for (var j = 0; j < board[i].length; j++) {

				if (board[i][j] != '.') {
					maplistc[i][board[i][j]]=1;
					maplistr[j][board[i][j]]=1;
					maplistk[getIndex(i, j)][board[i][j]]=1;
				} else {
					emptylist.push([ i, j ]);
				}

			}

		}
		
		//insert(0, emptylist);
		
		delayInsert(0,emptylist);

	}
	
	function delayInsert(index,list){
		
		var arr = list[index];

		var i = arr[0], j = arr[1], n=1;;
		
		if(board[i][j]=="."){
			n=1;
		}else if(board[i][j]<9){
			n=board[i][j]+1;
		}else{
			n=-1;
		}
		
		
		if(n!=-1){
		
			board[i][j] = n;
			
			if (
				maplistc[i][n]==0
				&& maplistr[j][n]==0
				&& maplistk[getIndex(i, j)][n]==0) {
				
				
				
				maplistc[i][n]=1;
				maplistr[j][n]=1;
				maplistk[getIndex(i, j)][n]=1;
				
				if (index >= list.length - 1) {
				
					console.log("over");
					run=false;
					
				}else{
				
					setTimeout(function(){
						delayInsert(index+1,emptylist);
					},delayTime);
				}
				
				
			}else{
				
				setTimeout(function(){
						delayInsert(index,emptylist);
				},delayTime);
				
			}
			
		
		}else{
		
			board[i][j] = ".";
			
			arr = list[index-1];
			i=arr[0];
			j=arr[1];
			n=board[i][j]
			
			maplistc[i][n]=0;
			maplistr[j][n]=0;
			maplistk[getIndex(i, j)][n]=0;
			
			
			setTimeout(function(){
						delayInsert(index-1,emptylist);
			},delayTime);
			
		}
		
		refresh();
		
	}
	
	function insert( index,  list) {

		var arr = list[index];

		var i = arr[0], j = arr[1];

		for (var n = 1; n <= 9; n++) {

			if (
				maplistc[i][n]==0
				&& maplistr[j][n]==0
				&& maplistk[getIndex(i, j)][n]==0) {

				board[i][j] = n+"";
				
				maplistc[i][n]=1;
				maplistr[j][n]=1;
				maplistk[getIndex(i, j)][n]=1;
				
				//refresh();

				if (index >= list.length - 1) {
				
					return true;
				} else {

					if (insert(index + 1, list, maplistc, maplistr, maplistk)) {
						return true;
					} else {
						
						board[i][j] = ".";
						maplistc[i][n]=0;
						maplistr[j][n]=0;
						maplistk[getIndex(i, j)][n]=0;
						
					}
				}
			}

		}

		return false;
	}

	
	function createBoard(nmb) {
	
		notEmptyList={};
		
		var maplistc = [{},{},{},{},{},{},{},{},{}];
		var maplistr = [{},{},{},{},{},{},{},{},{}];
		var maplistk = [{},{},{},{},{},{},{},{},{}];
		
		for (var i = 0; i < 9; i++) {
			for(var j=1;j<=9;j++){
				maplistc[i][j]=0;
				maplistr[i][j]=0;
				maplistk[i][j]=0;
			}
		}
		
		var result = [];
		
		for(var i=0;i<9;i++) {
			
			result[i]=['.','.','.','.','.','.','.','.','.'];
			
		}
		
		//var nmb =0;
		
		while(nmb>0) {
			
			var x = Math.floor(Math.random()*9);
			var y = Math.floor(Math.random()*9);
			
			if(result[x][y]=='.') {
				var tnmb = Math.floor(Math.random()*9)+1;
				if(maplistc[x][tnmb]==0
				&&maplistr[y][tnmb]==0
				&&maplistk[getIndex(x, y)][tnmb]==0
				) {
					result[x][y]=tnmb+"";
					maplistc[x][tnmb]=1;
					maplistr[y][tnmb]=1;
					maplistk[getIndex(x, y)][tnmb]=1;
					nmb--;	
					
					notEmptyList[x+" "+y]=1;
				}	
			}
		}
		return result;
	}
	
	
	board = createBoard();
	
	refresh();
	
	//solveSudoku(board);
	
	function reset(){
		
		var nmb = document.getElementById('s').value;
	
		board = createBoard(nmb);
		refresh();
	}
	
	function startRun(){
		solveSudoku();
		run=true;
	}

</script>
</head>
<body>
	
</body>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值