Advertisement
Guest User

Untitled

a guest
Jan 21st, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Beginner: 8 × 8 or 9 × 9 field with 10 mines
  3. Intermediate: 16 × 16 field with 40 mines
  4. Expert: 30 × 16 field with 99 mines
  5. */
  6.  
  7. var field = null;
  8. var gameOver = false;
  9. var ammountClicked = 0;
  10. var fieldSize = null;
  11. var mines = null;
  12.  
  13. function start()
  14. {
  15.     //Clear clicks
  16.     ammountClicked = 0;
  17.    
  18.     //Clear the content div, new game inc
  19.     document.getElementById("content").innerHTML = "";
  20.    
  21.     //Put the smiley as happeh ;
  22.     document.getElementById("smiley").src="images/minesweeper_smiley_happy.png";
  23.    
  24.     //Clear potential gameOver status
  25.     gameOver = false;
  26.    
  27.     //The size of the field (SIZE x SIZE)
  28.     fieldSize = document.getElementById("fieldSize").value;
  29.    
  30.     //Ammount of mines in the field(based on the size of the field)
  31.     mines = ( (fieldSize==8) ? 10 : (fieldSize==9) ? 10 : (fieldSize==16) ? 40 : (fieldSize==30) ? 99 : -1);
  32.    
  33.     //Create the field and fill it
  34.     createField(fieldSize, mines);
  35.    
  36.     //Print will hold the table code, it must all be printed at once or it won't work
  37.     var print = "";
  38.    
  39.     print = "<table border='1px' id='minesweeperTable'>";
  40.     for(var i=0; i<fieldSize; i++)
  41.     {
  42.         print += "<tr>";
  43.         for(var j=0; j<fieldSize; j++)
  44.         {
  45.             print += "<td class='field' id='"+i+";"+j+"' width='25px' height='25px' onclick='clicked(this.id);'></td>";
  46.         }
  47.         print +="</tr>";
  48.     }
  49.     print += "</table>";
  50.     document.getElementById("minesweeper").innerHTML = print;
  51.    
  52.     /*
  53.         for(var i=0; i<fieldSize; i++)
  54.         {
  55.         for(var j=0; j<fieldSize; j++)
  56.         {
  57.             var ad = i+";"+j;
  58.             document.getElementById(ad).onclick = function(){clicked(this.id, field);};
  59.         }
  60.         }
  61.     */
  62.    
  63.    
  64.    
  65. }
  66.  
  67.  
  68. function createField(fieldSize, mines)
  69. {   //Create an array, this is the field that this function generates and returns
  70.  
  71.  
  72.     //The 30 option is 30 x 16, so got to check for that
  73.     field = new Array(fieldSize);
  74.    
  75.     for (var i=0; i<fieldSize; i++)
  76.     { field[i] = new Array( (fieldSize==30) ? 16 : fieldSize ); }
  77.    
  78.     //Put "-" in every field, after this we will add the mines
  79.     for(var i=0; i<fieldSize; i++)
  80.     {
  81.         for(var j=0; j<fieldSize; j++)
  82.         {
  83.             field[i][j] = "-";
  84.         }
  85.     }
  86.    
  87.     //Add the mines
  88.     for(var i=0; i<mines; i++)
  89.     {   //Random[x][y] to place a mine
  90.         var randomX = Math.floor(Math.random()*fieldSize);
  91.         var randomY = Math.floor(Math.random()* ((fieldSize==30) ? 16 : fieldSize) );
  92.        
  93.         //If there already exists a mine on that spot, decrease i by 1 and continue looping
  94.         if(field[randomX][randomY] != "*")
  95.             {field[randomX][randomY] = "*";}
  96.         else
  97.             {--i;}
  98.     }
  99. }
  100.  
  101.  
  102. function clicked(id)
  103. {
  104.     if(gameOver) return;
  105.    
  106.     ammountClicked++;
  107.     document.getElementById("minesweeper").innerHTML += ammountClicked+" - ";
  108.    
  109.     var row = id.substring(0,id.indexOf(";"));
  110.     var column = id.substring(id.indexOf(";")+1);
  111.    
  112.     document.getElementById(id).innerHTML = field[row][column];
  113.     document.getElementById(id).onclick = tst;
  114.    
  115.     if( (field[row][column]=="*") )
  116.     {
  117.         gameOver = true;
  118.         document.getElementById("smiley").src = "images/minesweeper_smiley_sad.png";
  119.         setTimeout(function(){alert("You hit a bomb! Game over.");},100);  
  120.     }
  121.     else if( ammountClicked==(fieldSize*fieldSize-mines) )
  122.     {
  123.         gameOver = true;
  124.         document.getElementById("smiley").src = "images/minesweeper_smiley_veryHappy.png";
  125.         setTimeout(function(){alert("Congratulations! You beat minesweeper!");},100);
  126.     }
  127. }
  128.  
  129. function showAll()
  130. {
  131.     for(var i=0; i<fieldSize; i++)
  132.     {
  133.         for(var j=0; j<fieldSize; j++)
  134.         {
  135.             document.getElementById(i+";"+j).innerHTML = field[i][j];
  136.         }
  137.     }
  138. }
  139.  
  140.  
  141. function tst()
  142. {alert("NOPE");}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement