摘要:消消樂 遊戲 javascrip版本 Part4-2(半完成品)
昨天終於把瘦身完美的完成了~
但是卻意外的發現另一個bug......
學過軟工,就知道"知識非瀑布式"。
因為你之後學習的知識可能會影響和改變你以前所學的。
因此當我的瘦身完成後,我很高興的隨便亂點,他還真的讓我隨便消除!
我才發現,原來我歸零的部分放錯地方了,應該說少放......
因為可以點的地方一直不會歸零,所以第一次都沒問題,第二次以後進入無歸零狀態,
圖案很歡樂的消來消去......
還好把這個修理完畢,
這樣我就可以安心的往下一個目標前進!
希望今天可以弄完判斷沒有可按的功能!
以下是目前為止應該沒問題的程式碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TTD</title>
</head>
<script type="text/javascript">
var color = new Array(6);
for(var p=0;p<6;p++)
{
color[p]=new Array(6);
}
var pic = new Array(6);
for(var q=0;q<6;q++)
{
pic[q]=new Array(6);
}
function initial()
{
color[0][0] = Math.floor(1 + Math.random() * 4);
color[0][1] = Math.floor(1 + Math.random() * 4);
color[0][2] = Math.floor(1 + Math.random() * 4);
color[0][3] = Math.floor(1 + Math.random() * 4);
color[0][4] = Math.floor(1 + Math.random() * 4);
color[0][5] = Math.floor(1 + Math.random() * 4);
color[1][0] = Math.floor(1 + Math.random() * 4);
color[1][1] = Math.floor(1 + Math.random() * 4);
color[1][2] = Math.floor(1 + Math.random() * 4);
color[1][3] = Math.floor(1 + Math.random() * 4);
color[1][4] = Math.floor(1 + Math.random() * 4);
color[1][5] = Math.floor(1 + Math.random() * 4);
color[2][0] = Math.floor(1 + Math.random() * 4);
color[2][1] = Math.floor(1 + Math.random() * 4);
color[2][2] = Math.floor(1 + Math.random() * 4);
color[2][3] = Math.floor(1 + Math.random() * 4);
color[2][4] = Math.floor(1 + Math.random() * 4);
color[2][5] = Math.floor(1 + Math.random() * 4);
color[3][0] = Math.floor(1 + Math.random() * 4);
color[3][1] = Math.floor(1 + Math.random() * 4);
color[3][2] = Math.floor(1 + Math.random() * 4);
color[3][3] = Math.floor(1 + Math.random() * 4);
color[3][4] = Math.floor(1 + Math.random() * 4);
color[3][5] = Math.floor(1 + Math.random() * 4);
color[4][0] = Math.floor(1 + Math.random() * 4);
color[4][1] = Math.floor(1 + Math.random() * 4);
color[4][2] = Math.floor(1 + Math.random() * 4);
color[4][3] = Math.floor(1 + Math.random() * 4);
color[4][4] = Math.floor(1 + Math.random() * 4);
color[4][5] = Math.floor(1 + Math.random() * 4);
color[5][0] = Math.floor(1 + Math.random() * 4);
color[5][1] = Math.floor(1 + Math.random() * 4);
color[5][2] = Math.floor(1 + Math.random() * 4);
color[5][3] = Math.floor(1 + Math.random() * 4);
color[5][4] = Math.floor(1 + Math.random() * 4);
color[5][5] = Math.floor(1 + Math.random() * 4);
pic[0][0]="pic00";
pic[0][1]="pic01";
pic[0][2]="pic02";
pic[0][3]="pic03";
pic[0][4]="pic04";
pic[0][5]="pic05";
pic[1][0]="pic10";
pic[1][1]="pic11";
pic[1][2]="pic12";
pic[1][3]="pic13";
pic[1][4]="pic14";
pic[1][5]="pic15";
pic[2][0]="pic20";
pic[2][1]="pic21";
pic[2][2]="pic22";
pic[2][3]="pic23";
pic[2][4]="pic24";
pic[2][5]="pic25";
pic[3][0]="pic30";
pic[3][1]="pic31";
pic[3][2]="pic32";
pic[3][3]="pic33";
pic[3][4]="pic34";
pic[3][5]="pic35";
pic[4][0]="pic40";
pic[4][1]="pic41";
pic[4][2]="pic42";
pic[4][3]="pic43";
pic[4][4]="pic44";
pic[4][5]="pic45";
pic[5][0]="pic50";
pic[5][1]="pic51";
pic[5][2]="pic52";
pic[5][3]="pic53";
pic[5][4]="pic54";
pic[5][5]="pic55";
for(var i = 0; i < 6; i++)
{
for(var j=0;j<6;j++){
switch(color[i][j])
{
case 1:
document.getElementById(pic[i][j]).src="red.png";
break;
case 2:
document.getElementById(pic[i][j]).src="green.png";
break;
case 3:
document.getElementById(pic[i][j]).src="blue.png";
break;
case 4:
document.getElementById(pic[i][j]).src="yellow.png";
break;
}
}
}
}
var element=0;
var isSame = new Array(6);
for(var t=0;t<6;t++)
{
isSame[t]=new Array(6);
}
var row=0;
var col=0;
function clkPic(obj)
{
element=obj.id
var strrow=element[3];
var strcol=element[4];
row=parseInt(strrow,10);
col=parseInt(strcol,10);
for(var o=0;o<6;o++)
{
for(var g=0;g<6;g++)
{
isSame[o][g]=false;
}
}
isSame[row][col]=true;
//Three to disppeared
cnttop(row,col);
cntleft(row,col);
cntdown(row,col);
cntright(row,col);
var isThree=connect1+connect2+connect3+connect4;
if(isThree>=2)
{
disppear();
}
//restart
for(var o=0;o<6;o++)
{
for(var g=0;g<6;g++)
{
isSame[o][g]=false;
}
}
connect1 = 0;
connect2 = 0;
connect3 = 0;
connect4 = 0;
isThree = 0;
row = 0;
col = 0;
}
var tempSame = new Array(6);
for(var h=0;h<6;h++)
{
tempSame[h] = new Array(6);
}
var tempColor = new Array(6);
for(var f=0;f<6;f++)
{
tempColor[f] = new Array(6);
}
function disppear()
{
for(var y=0;y<6;y++)
{
for(var s=0;s<6;s++)
{
if(isSame[y][s]==true)
{
var idis=pic[y][s];
//document.getElementById(idis).style.display="none";
document.getElementById(idis).src="empty.png";
//ToDrop
dropt(y,s);
}
}
}
for(var b=0;b<6;b++)
{
for(var l=0;l<6;l++)
{
if(isSame[b][l]==true)
{
document.getElementById(pic[b][l]).src="empty.png";
color[b][l]=8;//not any of the colors
}
}
}
for(var o=0;o<6;o++)
{
for(var g=0;g<6;g++)
{
isSame[o][g]=false;
}
}
//restart
connect1 = 0;
connect2 = 0;
connect3 = 0;
connect4 = 0;
isThree = 0;
row = 0;
col = 0;
//toThin :)
thin(); //if there is an empty cols
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
if(color[i][j]==7)
color[i][j]=8;
}
}
//restart
for(var o=0;o<6;o++)
{
for(var g=0;g<6;g++)
{
isSame[o][g]=false;
}
}
}
var temp2S=new Array(6);
for(var i=0;i<6;i++)
{
temp2S[i]=new Array(6);
}
var temp2C=new Array(6);
for(var j=0;j<6;j++)
{
temp2C[j]=new Array(6);
}
function thin()
{
//fix the col, and if row0~5 of color is 8 -> empty col, then combind.
if(color[0][0]==8&&color[1][0]==8&&color[2][0]==8&&color[3][0]==8&&color[4][0]==8&&color[5][0]==8)
{
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
temp2S[i][j]=isSame[i][j];
temp2C[i][j]=color[i][j];
}
}
for(var t=0;t<6;t++)
{
for(var r=0;r<6;r++)
{
var postis=(r+1);
if(postis<=5)
{
isSame[t][r]=temp2S[t][postis];
color[t][r]=temp2C[t][postis];
}
else
{
//the last cols
color[t][5]=7;
}
}
}
for(var k=0;k<6;k++)
{
for(var l=0;l<6;l++)
{
switch(color[k][l])
{
case 1:
document.getElementById(pic[k][l]).src="red.png";
break;
case 2:
document.getElementById(pic[k][l]).src="green.png";
break;
case 3:
document.getElementById(pic[k][l]).src="blue.png";
break;
case 4:
document.getElementById(pic[k][l]).src="yellow.png";
break;
case 7:
document.getElementById(pic[k][l]).src="empty.png";
break;
case 8:
document.getElementById(pic[k][l]).src="empty.png";
break;
}
}
}
}
if(color[0][1]==8&&color[1][1]==8&&color[2][1]==8&&color[3][1]==8&&color[4][1]==8&&color[5][1]==8)
{
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
temp2S[i][j]=isSame[i][j];
temp2C[i][j]=color[i][j];
}
}
for(var t=0;t<6;t++)
{
for(var r=1;r<6;r++)
{
var postis=(r+1);
if(postis<=5)
{
isSame[t][r]=temp2S[t][postis];
color[t][r]=temp2C[t][postis];
}
else
{
//the last cols
color[t][5]=7;
}
}
}
for(var k=0;k<6;k++)
{
for(var l=0;l<6;l++)
{
switch(color[k][l])
{
case 1:
document.getElementById(pic[k][l]).src="red.png";
break;
case 2:
document.getElementById(pic[k][l]).src="green.png";
break;
case 3:
document.getElementById(pic[k][l]).src="blue.png";
break;
case 4:
document.getElementById(pic[k][l]).src="yellow.png";
break;
case 7:
document.getElementById(pic[k][l]).src="empty.png";
break;
case 8:
document.getElementById(pic[k][l]).src="empty.png";
break;
}
}
}
}
if(color[0][2]==8&&color[1][2]==8&&color[2][2]==8&&color[3][2]==8&&color[4][2]==8&&color[5][2]==8)
{
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
temp2S[i][j]=isSame[i][j];
temp2C[i][j]=color[i][j];
}
}
for(var t=0;t<6;t++)
{
for(var r=2;r<6;r++)
{
var postis=(r+1);
if(postis<=5)
{
isSame[t][r]=temp2S[t][postis];
color[t][r]=temp2C[t][postis];
}
else
{
//the last cols
color[t][5]=7;
}
}
}
for(var k=0;k<6;k++)
{
for(var l=0;l<6;l++)
{
switch(color[k][l])
{
case 1:
document.getElementById(pic[k][l]).src="red.png";
break;
case 2:
document.getElementById(pic[k][l]).src="green.png";
break;
case 3:
document.getElementById(pic[k][l]).src="blue.png";
break;
case 4:
document.getElementById(pic[k][l]).src="yellow.png";
break;
case 7:
document.getElementById(pic[k][l]).src="empty.png";
break;
case 8:
document.getElementById(pic[k][l]).src="empty.png";
break;
}
}
}
}
if(color[0][3]==8&&color[1][3]==8&&color[2][3]==8&&color[3][3]==8&&color[4][3]==8&&color[5][3]==8)
{
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
temp2S[i][j]=isSame[i][j];
temp2C[i][j]=color[i][j];
}
}
for(var t=0;t<6;t++)
{
for(var r=3;r<6;r++)
{
var postis=(r+1);
if(postis<=5)
{
isSame[t][r]=temp2S[t][postis];
color[t][r]=temp2C[t][postis];
}
else
{
//the last cols
color[t][5]=7;
}
}
}
for(var k=0;k<6;k++)
{
for(var l=0;l<6;l++)
{
switch(color[k][l])
{
case 1:
document.getElementById(pic[k][l]).src="red.png";
break;
case 2:
document.getElementById(pic[k][l]).src="green.png";
break;
case 3:
document.getElementById(pic[k][l]).src="blue.png";
break;
case 4:
document.getElementById(pic[k][l]).src="yellow.png";
break;
case 7:
document.getElementById(pic[k][l]).src="empty.png";
break;
case 8:
document.getElementById(pic[k][l]).src="empty.png";
break;
}
}
}
}
if(color[0][4]==8&&color[1][4]==8&&color[2][4]==8&&color[3][4]==8&&color[4][4]==8&&color[5][4]==8)
{
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
temp2S[i][j]=isSame[i][j];
temp2C[i][j]=color[i][j];
}
}
for(var t=0;t<6;t++)
{
for(var r=4;r<6;r++)
{
var postis=(r+1);
if(postis<=5)
{
isSame[t][r]=temp2S[t][postis];
color[t][r]=temp2C[t][postis];
}
else
{
//the last cols
color[t][5]=7;
}
}
}
for(var k=0;k<6;k++)
{
for(var l=0;l<6;l++)
{
switch(color[k][l])
{
case 1:
document.getElementById(pic[k][l]).src="red.png";
break;
case 2:
document.getElementById(pic[k][l]).src="green.png";
break;
case 3:
document.getElementById(pic[k][l]).src="blue.png";
break;
case 4:
document.getElementById(pic[k][l]).src="yellow.png";
break;
case 7:
document.getElementById(pic[k][l]).src="empty.png";
break;
case 8:
document.getElementById(pic[k][l]).src="empty.png";
break;
}
}
}
}
if(color[0][5]==8&&color[1][5]==8&&color[2][5]==8&&color[3][5]==8&&color[4][5]==8&&color[5][5]==8)
{
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
temp2S[i][j]=isSame[i][j];
temp2C[i][j]=color[i][j];
}
}
for(var t=0;t<6;t++)
{
for(var r=5;r<6;r++)
{
var postis=(r+1);
if(postis<=5)
{
isSame[t][r]=temp2S[t][postis];
color[t][r]=temp2C[t][postis];
}
else
{
//the last cols
color[t][5]=7;
}
}
}
for(var k=0;k<6;k++)
{
for(var l=0;l<6;l++)
{
switch(color[k][l])
{
case 1:
document.getElementById(pic[k][l]).src="red.png";
break;
case 2:
document.getElementById(pic[k][l]).src="green.png";
break;
case 3:
document.getElementById(pic[k][l]).src="blue.png";
break;
case 4:
document.getElementById(pic[k][l]).src="yellow.png";
break;
case 7:
document.getElementById(pic[k][l]).src="empty.png";
break;
case 8:
document.getElementById(pic[k][l]).src="empty.png";
break;
}
}
}
}
//if there is much to thin
for(var i=0;i<6;i++)
{
for(var j=0;j<6;j++)
{
if(color[i][j]==8)
return thin();
}
}
}
function dropt(rows,cols)
{
if(rows>=0)
{
var pres=(rows-1);
if(pres>=0)
{
//test if the pre is False
if(isSame[pres][cols]==false)
{
//change the pre and that be clicked
//temp=a;
//a=b;
//b=temp;
for(var d=0;d<6;d++)
{
for(var z=0;z<6;z++)
{
tempColor[d][z]=color[d][z];
tempSame[d][z]=isSame[d][z];
}
}
isSame[rows][cols]=isSame[pres][cols];
isSame[pres][cols]=tempSame[rows][cols];
color[rows][cols]=color[pres][cols];
color[pres][cols]=tempColor[rows][cols];
//Then show the result
switch(color[rows][cols])
{
case 1:
document.getElementById(pic[rows][cols]).src="red.png";
break;
case 2:
document.getElementById(pic[rows][cols]).src="green.png";
break;
case 3:
document.getElementById(pic[rows][cols]).src="blue.png";
break;
case 4:
document.getElementById(pic[rows][cols]).src="yellow.png";
break;
case 8:
document.getElementById(pic[rows][cols]).src="empty.png";
break;
}
}
}
return dropt(pres,cols);
}
}
var connect1=0;
function cnttop(rows,cols)
{
if(color[rows][cols]!=8)
{
var tops=(rows-1);
if(tops>=0)
{
if(isSame[tops][cols]!=true)
{
if(color[tops][cols]==color[row][col])
{
isSame[tops][cols]=true;
connect1++;
cnttop(tops,cols);
cntleft(tops,cols);
cntright(tops,cols);
}
}
}
}
else
{
connect1=0;
document.getElementById(pic[rows][cols]).src="empty.png";
}
}
var connect2=0;
function cntleft(rows,cols)
{
if(color[rows][cols]!=8)
{
var lefts=(cols-1);
if(lefts>=0)
{
if(isSame[rows][lefts]!=true)
{
if(color[rows][lefts]==color[row][col])
{
isSame[rows][lefts]=true;
connect2++;
cnttop(rows,lefts);
cntleft(rows,lefts);
cntdown(rows,lefts);
}
}
}
}
else
{
document.getElementById(pic[rows][cols]).src="empty.png";
}
}
var connect3=0;
function cntdown(rows,cols)
{
if(color[rows][cols]!=8)
{
var downs=(rows+1);
if(downs<=5)
{
if(isSame[downs][cols]!=true)
{
if(color[downs][cols]==color[row][col])
{
isSame[downs][cols]=true;
connect3++;
cntright(downs,cols);
cntleft(downs,cols);
cntdown(downs,cols);
}
}
}
}
else
{
document.getElementById(pic[rows][cols]).src="empty.png";
}
}
var connect4=0;
function cntright(rows,cols)
{
if(color[rows][cols]!=8)
{
var rights=(cols+1);
if(rights<=5)
{
if(isSame[rows][rights]!=true)
{
if(color[rows][rights]==color[row][col])
{
isSame[rows][rights]=true;
connect4++;
cntright(rows,rights);
cnttop(rows,rights);
cntdown(rows,rights);
}
}
}
}
else
{
document.getElementById(pic[rows][cols]).src="empty.png";
}
}
</script>
<body onload="initial()">
<div align="center">
<table width="480" border="0">
<tr>
<td><img src="blue.png" width="80" height="80" id="pic00" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic01" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic02" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic03" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic04" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic05" onclick="clkPic(this)"/></td>
</tr>
<tr>
<td><img src="blue.png" width="80" height="80" id="pic10" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic11" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic12" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic13" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic14" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic15" onclick="clkPic(this)"/></td>
</tr>
<tr>
<td><img src="blue.png" width="80" height="80" id="pic20" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic21" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic22" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic23" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic24" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic25" onclick="clkPic(this)"/></td>
</tr>
<tr>
<td><img src="blue.png" width="80" height="80" id="pic30" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic31" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic32" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic33" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic34" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic35" onclick="clkPic(this)"/></td>
</tr>
<tr>
<td><img src="blue.png" width="80" height="80" id="pic40" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic41" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic42" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic43" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic44" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic45" onclick="clkPic(this)"/></td>
</tr>
<tr>
<td><img src="blue.png" width="80" height="80" id="pic50" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic51" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic52" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic53" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic54" onclick="clkPic(this)"/></td>
<td><img src="blue.png" width="80" height="80" id="pic55" onclick="clkPic(this)"/></td>
</tr>
</table>
</div>
<div>
<input type="button" id="btnReplay" value="重玩" style="visibility:hidden;"/>
</div>
</body>
</html>
雖然很凌亂,但總算是鬆了一口氣。
瘦身的技巧就在於要一直判斷還有沒有需要瘦的?也就是color=8的時候。
已經加入重玩的button,但之後要先隱藏~
找機會、時間著手判斷是否無法消掉了!