JS:
var i, winloss, hw, mines, tdCounter, trCounter, tdClass;
// radios = document.getElementById("form").childNodes;
//table = document.getElementById("game");
var difficulty = "";
var table;
i=0;
winloss="";
tdCounter = 0;
function askNewGame() {
alert("You " + winloss +" To play again, choose a difficulty.");
}
//var div = document.getElementById("div2");
function makeGame(hw, mines, tdClass) {
table = document.createElement("table");
table.id = "game";
document.getElementById("div2").appendChild(table);
for (trCounter=0;trCounter<hw;trCounter++) {
var tr = table.insertRow(trCounter);
tr.id = ("row" + trCounter.toString());
for (tdCounter=0;tdCounter<hw;tdCounter++) {
var td = document.getElementById(("row" + trCounter.toString())).insertCell(tdCounter);
td.className = tdClass;
td.innerHTML = "<strong></strong>";
//document.getElementById("row" + trCounter.toString()).appendChild(td);
}
}
genMines();
/*if (tableChildren) {
tableChildren = table.childNodes;
}
else {
var tableChildren = table.childNodes;
}*/
}
function newGame(difficulty) {
switch (difficulty) {
case 1:
hw=5;
mines=5;
tdClass = "beginner";
break;
case 2:
hw=7;
mines=20;
tdClass = "easy";
break;
case 3:
hw=10;
mines=25;
tdClass = "medium";
break;
case 4:
hw=20;
mines=100;
tdClass = "hard";
break;
case 5:
hw=30;
mines=450;
tdClass = "expert";
break;
}
makeGame(hw, mines, tdClass);
}
function gameOver() {
winloss = "Lost.";
for (var e=0;e<tds.length;e++) {
if (tds[e].getAttribute("name") === "mined") {
/*var bomb = document.createElement("img");
bomb.setAttribute("src", "mine.png");
tds[e].appendChild(bomb);*/
tds[e].innerHTML = "<img src='mine.png' />";
}
}
askNewGame();
}
function clearTable() {
if (table) {
/*if (table.childNodes !== undefined) {
if (tableChildren) {
tableChildren = table.childNodes;
}
else {
var tableChildren = table.childNodes;
}
for (i=0;i<tableChildren.length;i++) {
table.removeChild(tableChildren[i]);
}
}*/
table.parentNode.removeChild(table);
}
}
function genMines() {
var tds = table.getElementsByTagName("td");
var mineCount;
for (mineCount=0;mineCount<=mines;mineCount++) {
var index = Math.floor(Math.random()*((hw*hw)-1) + 0);
tds[index].setAttribute("onclick", "gameOver();");
tds[index].setAttribute("name", "mined");
}
}
/*function aC() {
if (allChildren) {
allChildren = table.childNodes;
}
else {
var allChildren = [];
}
}*/
HTML:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Minesweeper</title> <script src="minesweeper.js" type="text/javascript"></script> <link rel="stylesheet" href="minesweeper.css" type="text/css" /> </head> <body> <div> <form id="form"> <input type="radio" name="radio" value="1" onclick="clearTable();newGame(1);" /><label> - BEGINNER</label> <input type="radio" name="radio" value="2" onclick="clearTable();newGame(2);" /><label> - EASY</label> <input type="radio" name="radio" value="3" onclick="clearTable();newGame(3);" /><label> - MEDIUM</label> <input type="radio" name="radio" value="4" onclick="clearTable();newGame(4);" /><label> - HARD</label> <input type="radio" name="radio" value="5" onclick="clearTable();newGame(5);" /><label> - EXPERT</label> </form> </div> <div id="div2"> <strong>Minesweeper</strong> <!--<table id="game"> </table>--> </div> </body> </html>
CSS:
td {
border: 3px solid #555;
}
.beginner {
width: 100px;
height: 100px;
}
.easy {
width: 71px;
height: 71px;
}
.medium {
width: 50px;
height: 50px;
}
.hard {
width: 25px;
height: 25px;
}
.expert {
width: 16px;
height: 16px;
}
.beginner img {
width: 100px;
height: 100px;
}
.easy img {
width: 71px;
height: 71px;
}
.medium img {
width: 50px;
height: 50px;
}
.hard img {
width: 25px;
height: 25px;
}
.expert img {
width: 16px;
height: 16px;
}
I know that I have the image in the same file.

New Topic/Question
Reply


MultiQuote




|