Javascript clearance game example

Source: Internet
Author: User

<HTML>
<HEAD>
<TITLE> Javascript mines </TITLE>

<Style>
Input. no {
Background-color: eeeeee;
Border: none;
Width: 20;
Height: 20;
Color: blue;
}
Input. ye {
Background-color: eeeeee;
Border: none;
Width: 20;
Height: 20;
Color: red;
}
Input {
Width: 20;
Height: 20;
Color: red;
}
Input. mode0 {
Width: 50;
Height: 30;
Border-style: inset
}
Input. mode1 {
Width: 50;
Height: 30;
COLOR: GREEN;
}
</Style>
</HEAD>
<Body bgcolor = "#000000" type = "codeph" text = "/codeph">
<Div ID = dilei style = "position: absolute; left: 0; top: 5">
<Table border = 0 CELLSPACING = 0 CELLPADDING = 0>
<Script language = "JavaScript">
<! --
Var x, y = 0;
While (document. write ("<tr>"), y ++ <20)
For (x = 0; x ++ <20; document. write ("<td> <font size =-5>
<Input ID = "+ x +" _ "+ y +" type = button> "))
;

// -->
</SCRIPT>
</TABLE>
</Div>
<Div ID = ToolBar style = "position: absolute; top: 5; left: 401;
/* Height: 300; */background-color: green; width: 200 ">
<Input id = Mode type = button value = "Dig mine" class = mode1
Onclick = "javascript: modeTab ();">
<Nobr>
<Span id = modenow style = "color: blue"> the square can be opened now </span>
</Div>
</BODY>
<Script language = "JavaScript">
<! -- // 34
Var Row, Col, I = 0;
Var RowSize = 20, ColSize = 20;
Var mineNum = 45; // number of mines
Var mode = 1;


Var mineX = new Array (mineNum), mineY = new Array (mineNum );
Var Mine = new Array (RowSize), Decouv = new Array (RowSize );
For (I = 0; I <20; I ++ ){
Mine [I] = new Array (ColSize );
Decouv [I] = new Array (ColSize );
}


// Initialization
 
For (Row = 1; Row <= RowSize; Row ++ ){
For (Col = 1; Col <= ColSize; Col ++ ){
Mine [Row-1] [Col-1] = 0;
Decouv [Row-1] [Col-1] = 0;
}
};
// Math. random ()
I = 0;
While (I <mineNum ){
MineX [I] = Math. round (Math. random () * 1000) % RowSize;
MineY [I] = Math. round (Math. random () * 1000) % ColSize;
If (Mine [mineX [I] [mineY [I] = 9) continue;
Mine [mineX [I] [mineY [I] = 9;
Decouv [mineX [I] [mineY [I] = 9;
I ++;
}
For (I = 0; I <mineNum; I ++ ){
For (j = 1; j <= 8; j ++ ){
Switch (j ){
Case 1:
DivIndexX = mineX [I]-1; divIndexY = mineY [I]-1;
Break;
Case 2:
DivIndexX = mineX [I]-1; divIndexY = mineY [I];
Break;
Case 3:
DivIndexX = mineX [I]-1; divIndexY = mineY [I] + 1;
Break;
Case 4:
DivIndexX = mineX [I]; divIndexY = mineY [I]-1;
Break;
Case 5:
DivIndexX = mineX [I]; divIndexY = mineY [I] + 1;
Break;
Case 6:
DivIndexX = mineX [I] + 1; divIndexY = mineY [I]-1;
Break;
Case 7:
DivIndexX = mineX [I] + 1; divIndexY = mineY [I];
Break;
Case 8:
DivIndexX = mineX [I] + 1; divIndexY = mineY [I] + 1;
}
If (divIndexX <0 | divIndexY <0 | divIndexX> = RowSize | divIndexY> = ColSize) continue;
Decouv [divIndexX] [divIndexY] + = 1;
Mine [divIndexX] [divIndexY] = Decouv [divIndexX] [divIndexY];
}
}
// Mode switch

Function modeTab (){
If (mode = 1 ){
Mode = 0;
Document. all ("Mode"). className = "mode0 ";
Document. all ("Mode"). value = "probe ";
Document. all ("modenow"). innerText = "Now you can mark the minefield" + mode;
} Else {
Mode = 1;
Document. all ("Mode"). className = "mode1 ";
Document. all ("Mode"). value = "mining ";
Document. all ("modenow"). innerText = "you can uncover the square now" + mode;
}
}

 

Function TRY (){
Var SRCElem = event. srcElement;
If (SRCElem. tagName! = "INPUT" | SRCElem. className = "mode0" |
SRCElem. className = "mode1") return;
If (SRCElem. className = "no" | SRCElem. className = "ye") return;
TryDown (SRCElem );
}
Function tryDown (Elem ){
Var indexX, indexY;
Var ss = Elem. id. split ("_");
IndexX = ss [0]. valueOf ()-1;
IndexY = ss [1]. valueOf ()-1;

If (mode = 0 ){
If (Elem. value = ""){
Elem. value = "! ";
Mine [indexX] [indexY] = Decouv [indexX] [indexY];
Decouv [indexX] [indexY] = 18;
Return ;}
Else {
Elem. value = "";
Decouv [indexX] [indexY] = Mine [indexX] [indexY];
Return;
}
}
If (Decouv [indexX] [indexY]> 8 ){
If (Decouv [indexX] [indexY]> = 18)
Return;
Elem. value = "*";
Elem. className = "ye ";
Decouv [indexX] [indexY] = 9
Return;
}
Elem. className = "no ";
Elem. value = "" + Mine [indexX] [indexY];
If (Decouv [indexX] [indexY]> 0 ){
Decouv [indexX] [indexY] = 9
Return;
}
If (Decouv [indexX] [indexY] = 0)
{
Elem. value = "";
TryGo (indexX, indexY );
}
Return;
}
Function TRY2 (Elm ){
Elm. value = "! ";
}
Function judge4 (x, y ){
If (Decouv [x] [y]! =-1 & Decouv [x] [y]! = 0 ){
Return 0 ;}
Var I = 0, j = 0, divIndexX, divIndexY;
For (I = 1; I <= 4; I ++ ){
Switch (I ){//
Case 1: // up
DivIndexX = x; divIndexY = Y-1; j = 1;
Break;
Case 2: // left
DivIndexX = X-1; divIndexY = y; j = 2;
Break;
Case 3: // down
DivIndexX = x; divIndexY = y + 1; j = 3;
Break;
Case 4: // right
DivIndexX = x + 1; divIndexY = y; j = 4;
}
If (divIndexX> = 0 & divIndexY> = 0 & divIndexX <RowSize & divIndexY
<ColSize & Decouv [divIndexX] [divIndexY]> = 0 & Decouv [divIndexX] [divIndexY] <= 8 ){
Return j;
}
}
Return 0;
}
Function tryGo (x, y ){
Var stackX = new Array (), stackY = new Array ();
Var I = 0;
Var test = 50;
Var divIndexX = x, divIndexY = y;
StackX [I] = divIndexX; stackY [I] = divIndexY;
While (test ){
Var Elem;
Eval ("Elem = document. all (" "+ (divIndexX + 1) +" _ "+ (divIndexY + 1) + "")");
Elem. className = "no ";
If (Decouv [divIndexX] [divIndexY] <= 0)
Decouv [divIndexX] [divIndexY] =-1;
Else {
Elem. value = "" + Mine [divIndexX] [divIndexY];
Decouv [divIndexX] [divIndexY] = 9;
}

Switch (judge4 (divIndexX, divIndexY )){
Case 1: // up
DivIndexY --;
Break;
Case 2: // left
DivIndexX --;
Break;
Case 3: // down
DivIndexY ++;
Break;
Case 4: // right
DivIndexX ++;
Break;
Case 0:
I = I-1;
If (I <0 ){
Return;
}
DivIndexX = stackX [I]; divIndexY = stackY [I];
Continue;
}
I = I + 1; stackX [I] = divIndexX; stackY [I] = divIndexY;
}
}
Document. onclick = TRY;
// -->
</SCRIPT>
</HTML>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.