Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Beginner: 8 × 8 or 9 × 9 field with 10 mines
- Intermediate: 16 × 16 field with 40 mines
- Expert: 30 × 16 field with 99 mines
- */
- var field = null;
- var gameOver = false;
- var ammountClicked = 0;
- var fieldSize = null;
- var mines = null;
- function start()
- {
- //Clear clicks
- ammountClicked = 0;
- //Clear the content div, new game inc
- document.getElementById("content").innerHTML = "";
- //Put the smiley as happeh ;
- document.getElementById("smiley").src="images/minesweeper_smiley_happy.png";
- //Clear potential gameOver status
- gameOver = false;
- //The size of the field (SIZE x SIZE)
- fieldSize = document.getElementById("fieldSize").value;
- //Ammount of mines in the field(based on the size of the field)
- mines = ( (fieldSize==8) ? 10 : (fieldSize==9) ? 10 : (fieldSize==16) ? 40 : (fieldSize==30) ? 99 : -1);
- //Create the field and fill it
- createField(fieldSize, mines);
- //Print will hold the table code, it must all be printed at once or it won't work
- var print = "";
- print = "<table border='1px' id='minesweeperTable'>";
- for(var i=0; i<fieldSize; i++)
- {
- print += "<tr>";
- for(var j=0; j<fieldSize; j++)
- {
- print += "<td class='field' id='"+i+";"+j+"' width='25px' height='25px' onclick='clicked(this.id);'></td>";
- }
- print +="</tr>";
- }
- print += "</table>";
- document.getElementById("minesweeper").innerHTML = print;
- /*
- for(var i=0; i<fieldSize; i++)
- {
- for(var j=0; j<fieldSize; j++)
- {
- var ad = i+";"+j;
- document.getElementById(ad).onclick = function(){clicked(this.id, field);};
- }
- }
- */
- }
- function createField(fieldSize, mines)
- { //Create an array, this is the field that this function generates and returns
- //The 30 option is 30 x 16, so got to check for that
- field = new Array(fieldSize);
- for (var i=0; i<fieldSize; i++)
- { field[i] = new Array( (fieldSize==30) ? 16 : fieldSize ); }
- //Put "-" in every field, after this we will add the mines
- for(var i=0; i<fieldSize; i++)
- {
- for(var j=0; j<fieldSize; j++)
- {
- field[i][j] = "-";
- }
- }
- //Add the mines
- for(var i=0; i<mines; i++)
- { //Random[x][y] to place a mine
- var randomX = Math.floor(Math.random()*fieldSize);
- var randomY = Math.floor(Math.random()* ((fieldSize==30) ? 16 : fieldSize) );
- //If there already exists a mine on that spot, decrease i by 1 and continue looping
- if(field[randomX][randomY] != "*")
- {field[randomX][randomY] = "*";}
- else
- {--i;}
- }
- }
- function clicked(id)
- {
- if(gameOver) return;
- ammountClicked++;
- document.getElementById("minesweeper").innerHTML += ammountClicked+" - ";
- var row = id.substring(0,id.indexOf(";"));
- var column = id.substring(id.indexOf(";")+1);
- document.getElementById(id).innerHTML = field[row][column];
- document.getElementById(id).onclick = tst;
- if( (field[row][column]=="*") )
- {
- gameOver = true;
- document.getElementById("smiley").src = "images/minesweeper_smiley_sad.png";
- setTimeout(function(){alert("You hit a bomb! Game over.");},100);
- }
- else if( ammountClicked==(fieldSize*fieldSize-mines) )
- {
- gameOver = true;
- document.getElementById("smiley").src = "images/minesweeper_smiley_veryHappy.png";
- setTimeout(function(){alert("Congratulations! You beat minesweeper!");},100);
- }
- }
- function showAll()
- {
- for(var i=0; i<fieldSize; i++)
- {
- for(var j=0; j<fieldSize; j++)
- {
- document.getElementById(i+";"+j).innerHTML = field[i][j];
- }
- }
- }
- function tst()
- {alert("NOPE");}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement