JS编写_扫雷

一个简单的扫雷程序,JS编写: 

<script>
var MapX=9,MapY=9,SweepNum=10; Bingo=0; 
var Flag=new Array(); var timer=0;
     
for(var i=1;i<=16;i++){                
     Flag[i]=new Array();         
     for(var j=1;j<=30;j++){Flag[i][j]=0;}
}
	   
var SweepMap=new Array(); 
for(var i=1;i<=16;i++){ 
	SweepMap[i]=new Array();                         
   	  for(var j=1;j<=30;j++){SweepMap[i][j]=0;}
}

function randomCoord(Coord){ 
	if(Coord=="x") return Math.floor((Math.random()*1000%MapX))+1;
	else if(Coord=="y") return Math.floor((Math.random()*1000%MapY))+1;
	
}

function SweepArray(){
	   for (var i=1;i<=SweepNum;i++){
		   var x=randomCoord("x"),y=randomCoord("y");
		   if (SweepMap[x][y]>=0) SweepMap[x][y]=-1;
		  }
		for(var i=1;i<=MapX;i++){               
          for(var j=1;j<=MapY;j++){           
             if (SweepMap[i][j]<0){
				 if (isWarning("cross",i+1,j)&&SweepMap[i+1][j]>=0) {SweepMap[i+1][j]++;}
				 if (isWarning("cross",i-1,j)&&SweepMap[i-1][j]>=0) {SweepMap[i-1][j]++;}
				 if (isWarning("cross",i,j+1)&&SweepMap[i][j+1]>=0) {SweepMap[i][j+1]++;}
				 if (isWarning("cross",i,j-1)&&SweepMap[i][j-1]>=0) {SweepMap[i][j-1]++;}
				 if (isWarning("cross",i+1,j+1)&&SweepMap[i+1][j+1]>=0) {SweepMap[i+1][j+1]++;}
				 if (isWarning("cross",i-1,j-1)&&SweepMap[i-1][j-1]>=0) {SweepMap[i-1][j-1]++;}
				 if (isWarning("cross",i+1,j-1)&&SweepMap[i+1][j-1]>=0) {SweepMap[i+1][j-1]++;}
				 if (isWarning("cross",i-1,j+1)&&SweepMap[i-1][j+1]>=0) {SweepMap[i-1][j+1]++;}
				 }
		  }
		}
}

function isWarning(error,i,j){

	if (error=="cross"){
		if (i<=MapX&&i>0&&j<=MapY&&j>0)  { return 1;}
		else return 0;
		}
	else if (error=="sweep"){
		if(SweepMap[i][j]>=0){ return 1;}
		else{			
			Paint(i,j,1);
			for (var x=1;x<=MapX;x++)
				for (var y=1;y<=MapY;y++){
					if (SweepMap[x][y]<0&&!(x==i&&y==j)){Paint(x,y,2);}
				}
			
			}
			clearInterval(t);
			alert("你踩到雷了..");
			location.reload() ;
		}
}



function ScanPaint(x,y){
	var n=arguments[2]?arguments[2]:"null";  
	<!--这个地方卡了我4个小时,他为什么会把我的x,y当做字符串看不能做int运算????-->
	x=parseInt(x);
	y=parseInt(y);
	
	if(isWarning("cross",x,y)&&SweepMap[x][y]>0){
		 Paint(x,y,0);
		 }
	 if(isWarning("cross",x,y)&&SweepMap[x][y]==0){
		 Paint(x,y,0);
		 if(isWarning("cross",x+1,y)==1&&SweepMap[x+1][y]==0&&Flag[x+1][y]==0) ScanPaint(x+1,y);
		 if(isWarning("cross",x,y+1)==1&&SweepMap[x][y+1]==0&&Flag[x][y+1]==0) ScanPaint(x,y+1);
		 if(isWarning("cross",x-1,y)==1&&SweepMap[x-1][y]==0&&Flag[x-1][y]==0) ScanPaint(x-1,y);
		 if(isWarning("cross",x,y-1)==1&&SweepMap[x][y-1]==0&&Flag[x][y-1]==0) ScanPaint(x,y-1);
		 if(isWarning("cross",x+1,y+1)==1&&SweepMap[x+1][y+1]==0&&Flag[x+1][y+1]==0) ScanPaint(x+1,y+1);
		 if(isWarning("cross",x-1,y-1)==1&&SweepMap[x-1][y-1]==0&&Flag[x-1][y-1]==0) ScanPaint(x-1,y-1);
		 if(isWarning("cross",x+1,y-1)==1&&SweepMap[x+1][y-1]==0&&Flag[x+1][y-1]==0) ScanPaint(x+1,y-1);
		 if(isWarning("cross",x-1,y+1)==1&&SweepMap[x-1][y+1]==0&&Flag[x-1][y+1]==0) ScanPaint(x-1,y+1);
		 
		 if(isWarning("cross",x+1,y)==1&&SweepMap[x+1][y]>=0) {Paint(x+1,y,0);}
		 if(isWarning("cross",x,y+1)==1&&SweepMap[x][y+1]>=0) {Paint(x,y+1,0);}
		 if(isWarning("cross",x-1,y)==1&&SweepMap[x-1][y]>=0) {Paint(x-1,y,0);}
		 if(isWarning("cross",x,y-1)==1&&SweepMap[x][y-1]>=0) {Paint(x,y-1,0);}
		 }
}

function Paint(x,y){
		var isSweep=arguments[2]?arguments[2]:0  
		var test=document.getElementById("test");
		var testtr=test.getElementsByTagName("tr")[x-1];
		var testtd=testtr.getElementsByTagName("td")[y-1];
		
		switch (isSweep)
		{
			case 0:{if (!(Flag[x][y]==1)){var n=SweepMap[x][y]*20+40; Flag[x][y]=-1;} break;}
			case 1:{var n=20;break;}
			case 2:{var n=220;break;}
			case 3:{var n=240; Flag[x][y]=1;break;}
			case 4:{var n=0;   Flag[x][y]=0;break;}
			default:{alert("error!!"); location.reload(); }
			}
		
		testtd.style.background="url(sweep.png) -"+n+"px 0px;";	
}

function Time(){
	var time=document.getElementById("time");
		timer++;
	time.innerHTML=timer;

	}
function Start(){
	document.oncontextmenu=function(evt){
		if (window.event){
			window.event.returnValue=false;}
			else{
				evt.preventDefault();
				}
	}
	t=setInterval("Time()",1000);	
	var test=document.getElementById("test");
	test.onmousedown=function(e){
		var e=e||window.event;
		var o=e.target||e.srcElement;
		a=o.id.split("|");
		if(a[0]>0&&a[1]>0){
			if (e.button==0) {
				if (isWarning("sweep",a[0],a[1])==1){
					Paint(a[0],a[1],0);}
				if (SweepMap[a[0]][a[1]]==0){ScanPaint(a[0],a[1]);}
				}
			 if (e.button==2){
				 if (Flag[a[0]][a[1]]==0) {
					 Paint(a[0],a[1],3);  if(SweepMap[a[0]][a[1]]<0) Bingo++; if (Bingo==SweepNum) {clearInterval(t);alert("You Win!"); location.reload() ;}}		 
				else if (Flag[a[0]][a[1]]==1) {Paint(a[0],a[1],4);  if(SweepMap[a[0]][a[1]]<0) Bingo--;}
			 }
			}
		}		
}

function LevelSelect(x){	
	switch (x)
	{
		case "初级":{MapX=9,MapY=9,SweepNum=10; Bingo=0; break; }
		case "中级":{MapX=16,MapY=16,SweepNum=40; Bingo=0; break;}
		case "高级":{MapX=16,MapY=30,SweepNum=99; Bingo=0; break;}
		default: alert("来选择你的游戏等级,默认初级!");
	}
	document.getElementsByTagName('select')[0].options.length = 0; 
}

function PaintTable(){
	var level=document.getElementById("level");
	var levelIndex=level.selectedIndex;
	LevelSelect(level.options[levelIndex].text);


	var body1=document.getElementById("layout");      
	var table=document.createElement("table");
	table.id="test";
	table.setAttribute('border',"1px");
	
	for(var i=1;i<=MapX;i++){
		var tr=document.createElement("tr");
		table.appendChild(tr);
	table.style.margin="0 auto";
	for(var j=1;j<=MapY;j++){
		var td=document.createElement("td");
		td.style.width="18px";
		td.style.height="18px";
		var s=i+"|"+j;
		td.id=s;
		td.style.background="url(sweep.png)";
		tr.appendChild(td);		
	}
}
	body1.appendChild(table);
	SweepArray();
	Start();
}
</script>

  

 

 

posted on 2012-12-30 04:52  China.Jik  阅读(156)  评论(0)    收藏  举报

导航