<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 //EN">
<html>
<!--
Se prohibe el uso total o parcial del contenido de este fichero con
fines comerciales o no comerciales sin autorizaci車n previa del autor.
http://www.inmensia.com/juegos
-->
<head>
<title>Minesweeper v1.0</title>
<meta name="keywords" content="games,free,juegos,javascript" />
</head>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial;
font-size: 12px;
color: #FFFFFF;
background: #000000;
}
.div-game {
margin: 5% auto;
width: 480px;
height: 420px;
}
.div-score {
position: relative;
top: 0px;
left: 0px;
height: 30px;
color: #000000;
background: #C0C0C0;
border-top: 1px solid #FFFFFF;
border-right: 1px solid #FFFFFF;
}
.img-mines, .div-mines, .img-time, .img-smiley, .div-time, .lb-level, .bn-reset {
position: absolute;
}
.img-mines {
top: 6px;
left: 10px;
}
.div-mines {
top: 6px;
left: 30px;
width: 40px;
font-family: Verdana;
font-size: 14px;
font-weight: bold;
text-align: left;
color: #0000CF;
background: transparent;
}
.img-time {
top: 6px;
left: 80px;
}
.div-time {
top: 6px;
left: 100px;
width: 60px;
font-family: Verdana;
font-size: 14px;
font-weight: bold;
text-align: left;
color: #007F00;
background: transparent;
}
.img-smiley {
top: 6px;
left: 240px;
}
.lb-level {
top: 4px;
left: 330px;
width: 80px;
font-family: Arial;
font-size: 12px;
}
.bn-reset {
top: 3px;
left: 420px;
width: 50px;
font-family: Arial;
font-size: 12px;
text-align: center;
}
.div-board {
position: relative;
width: 480px;
height: 320px;
color: #000000;
background: #7F7F7F;
border-top: 1px solid #000000;
border-bottom: 1px solid #FFFFFF;
border-left: 1px solid #FFFFFF;
}
.cell {
position: absolute;
overflow: hidden;
}
.div-adsense {
position: relative;
width: 100%;
height: 60px;
text-align: center;
}
.div-addfreestats {
position: absolute;
left: 0px;
top: 0px;
display: none;
}
</style>
<body>
<div id="div-game" class="div-game">
<div id="div-score" class="div-score">
<img id="img-mines" class="img-mines" src="mines.png" />
<div id="div-mines" class="div-mines"></div>
<img id="img-time" class="img-time" src="clock.png" />
<div id="div-time" class="div-time">00:00</div>
<img id="img-smiley" class="img-smiley" />
<select id="lb-level" class="lb-level" onchange="game.restart()" name="bn-reset"/>
<option selected value="easy">Easy</option>
<option value="normal">Normal</option>
<option value="advanced">Advanced</option>
<option value="hard">Hard</option>
<option value="expert">Expert</option>
</select>
<input id="bn-reset" type="button" class="bn-reset" onclick="game.restart()" value="Play!" name="bn-reset"/>
</div>
<div id="div-board" class="div-board"></div>
</div>
<script type="text/javascript">
//
// Game
//
function Game() {
var self = this;
this.board = new Board();
this.startTime = null;
this.run = function() {
self.start();
}
this.start = function() {
self.hideSmiley();
self.createBoard();
self.registerMouse();
self.startTimer();
self.updateScore();
}
this.restart = function() {
self.board.destroyImgs();
self.start();
}
this.onMouseDown = function(e) {
var img = getMouseObject(e);
if ( self.board.isCell(img) ){
if ( getMouseButton(e) == 1)
self.onLeftClick(img.mRow, img.mCol);
else
self.onRightClick(img.mRow, img.mCol);
}
self.updateScore();
return(false);
}
this.onLeftClick = function(row, col) {
if ( self.board.isFlag(row, col) )
return;
if ( self.board.isMine(row, col) ) {
self.looseGame(row, col);
return;
}
self.board.flip(row, col);
if (0 == self.board.downs)
self.winGame();
}
this.onRightClick = function(row, col) {
if ( self.board.isDown(row, col) )
return;
self.board.changeState(row, col);
if (self.board.flags == self.board.mines)
self.tryWinGame();
}
this.tryWinGame = function() {
if ( self.board.testFlags() )
self.winGame();
}
this.winGame = function() {
self.showSmiley("happy");
self.board.showFlags();
self.endGame();
}
this.looseGame = function(row, col) {
self.showSmiley("sad");
self.board.showMines(row, col);
self.endGame();
}
this.endGame = function() {
self.stopTimer();
self.unregisterMouse();
}
this.updateScore = function() {
document.getElementById("div-mines").innerHTML = String(self.board.mines - self.board.flags);
}
this.showSmiley = function(pic) {
var img = document.getElementById("img-smiley");
img.src = self.board.getImgSrc(pic);
img.style.visibility = "visible";
}
this.hideSmiley = function() {
document.getElementById("img-smiley").style.visibility = "hidden";
}
this.startTimer = function() {
self.startTime = new Date().getTime();
self.timer();
}
this.stopTimer = function() {
self.startTime = null;
}
this.timer = function() {
if (self.startTime) {
var diff = Math.floor( ( new Date().getTime() - self.startTime) / 1000);
var mins = "0" + String( Math.floor(diff / 60) );
var secs = "0" + String(diff % 60);
document.getElementById("div-time").innerHTML =
mins.substring(mins.length - 2) + ":" + secs.substring(secs.length - 2);
setTimeout(self.timer, 1000);
}
}
this.registerMouse = function() {
self.board.div.onmousedown = self.onMouseDown;
self.board.div.onclick = function(){return false;};
self.board.div.ondblclick = function(){return false;};
self.board.div.oncontextmenu = function(){return false;};
}
this.unregisterMouse = function() {
self.board.div.onmousedown = null;
}
this.createBoard = function() {
switch( document.getElementById("lb-level").value ) {
case "easy" : self.board.create(10, 10, 10); break;
case "normal" : self.board.create(15, 15, 25); break;
case "advanced": self.board.create(20, 20, 50); break;
case "hard" : self.board.create(20, 25, 100); break;
case "expert" : self.board.create(20, 30, 150); break;
}
}
}
//
// Board
//
function Board() {
var self = this;
this.div = document.getElementById("div-board");
this.cells = null;
this.rows = 0;
this.cols = 0;
this.mines = 0;
this.downs = 0;
this.flags = 0;
this.imgClass = "cell";
this.imgWidth = 16;
this.imgHeight = 16;
this.imgURL = "";
this.imgExt = ".png";
this.create = function(rows, cols, mines) {
self.cells = null;
self.rows = rows;
self.cols = cols;
self.mines = mines;
self.downs = (self.rows * self.cols) - self.mines;
self.flags = 0;
self.createCells();
self.putMines();
self.createImgs();
}
this.createCells = function() {
self.cells = new Array(self.rows);
for (var row = 0; row != self.rows; ++ row) {
self.cells[row] = new Array(self.cols);
for (var col = 0; col != self.cols; ++ col)
self.cells[row][col] = new Cell();
}
}
this.putMines = function() {
for (var mine = 0; mine != self.mines; ++ mine)
self.putRandMine();
}
this.putRandMine = function() {
var row, col;
do{
row = rand(self.rows);
col = rand(self.cols);
}while( self.isMine(row, col) );
self.putMine(row, col);
self.roundMine(row, col);
}
this.putMine = function(row, col) {
self.cells[row][col].value = 'm';
}
this.roundMine = function(row, col) {
for (var r = Math.max(row - 1, 0); r <= Math.min(row + 1, self.rows - 1); ++ r)
for (var c = Math.max(col - 1, 0); c <= Math.min(col + 1, self.cols - 1); ++ c)
if ( self.isMine(r, c) == false)
++ self.cells[r][c].value;
}
this.createImgs = function() {
for (var row = 0; row != self.rows; ++ row)
for (var col = 0; col != sel