<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MineSweeper</title>
<style type="text/css">
body{border:0px; margin:0px; padding:0px; font-size:12px; background-color:#ccc;}
div{border:0px;}
img{border:0px;}
.titleFont{font-size:18px;}
.button{margin:5px;width:150px;}
.textSetting{width:150px; border: 1px solid #999999;}
.cellStyle IMG:Hover{cursor:hand;}
.mineTable{background-color:#ccc;border:solid 2px #999999;}
.mineHeaderCell{width:33%;text-align:center;padding-top:5px;}
.winFont{color:Red;}
.loseFont{color:Blue;}
.savedGame{float:left;border: dotted 1px #999999; margin:3px;} .savedGameMouseOver{float:left;border: solid 1px #E63C74; margin:3px;}
.savedCell{background-color:#9D9D9D;} .savedCellFlag{background-color:#952C28;} .savedCellAsk{background-color:#060606;} .savedCellEmpty{background-color:#C0C0C0;} .savedCellMineYes{background-color:#7E7E7E;} .savedCellMineNo{background-color:#5B1918;} .savedCellMine{background-color:#E51F1F;}
.savedCell1{background-color:#0306C7;} .savedCell2{background-color:#0E780C;} .savedCell3{background-color:#E6070A;} .savedCell4{background-color:#00035E;} .savedCell5{background-color:#740D11;} .savedCell6{background-color:#287378;} .savedCell7{background-color:#000000;} .savedCell8{background-color:#7E7E7E;}
</style>
<script type="text/javascript">
//==========Class Begin==========
//CellInfo
//M:mine S:status N:number
//M false-none true-mine
//S 0-normal 1-flag 2-ask 3-open 4-mine_yes 5-mine_no 6-mine
//N 0-close >0-show mine number
function CellInfo(m,s,n){
this.M = m;
this.S = s;
this.N = n;
}
CellInfo.prototype.Clone = function(){
return new CellInfo(this.M, this.S, this.N);
}
//MineInfo
function MineInfo(x,y,mine,isData){
this.X = x;
this.Y = y;
this.Mine = mine;
this.Data = null;
if(isData != null && isData == false) return;
this.BuildData();
this.GenerateMine();
}
MineInfo.prototype.BuildData = function(){
this.Data = new Array();
for(var x=0; x<this.X; x++){
this.Data[x] = new Array();
for(var y=0; y<this.Y; y++){
this.Data[x][y] = new CellInfo(false,0,0);
}
}
}
MineInfo.prototype.CleanMine = function(){
if(this.Data == null) return;
for(var x=0; x<this.X; x++)
for(var y=0; y<this.Y; y++)
this.Data[x][y].M = false;
}
MineInfo.prototype.GenerateMine = function(){
if(this.Data == null) return;
if(this.Mine > this.X * this.Y) return;
var mine=0;
while(mine<this.Mine){
var x = parseInt(Math.random() *10000) % this.X;
var y = parseInt(Math.random() *10000) % this.Y;
if(!this.Data[x][y].M){
this.Data[x][y].M = true;
mine++;
}
}
}
MineInfo.prototype.Clone = function(){
var mineInfo = new MineInfo(this.X, this.Y, this.Mine, false);
mineInfo.Data = new Array();
for(var x=0; x<this.X; x++){
mineInfo.Data[x] = new Array();
for(var y=0; y<this.Y; y++){
mineInfo.Data[x][y] = this.Data[x][y].Clone();
}
}
return mineInfo;
}
//InfoProcess
function InfoProcess(info,winHandler, loseHandler, cellChangeHandler, mineChangeHandler, context){
this.Info = info;
this.CurrentMine = info.Mine;
this.WinHandler = winHandler;
this.LoseHandler = loseHandler;
this.CellChangeHandler = cellChangeHandler;
this.MineChangeHandler = mineChangeHandler;
this.Context = context;
}
InfoProcess.prototype.HandlerIsValidated = function(handler){
return !(handler == null || typeof(handler) != "function");
}
InfoProcess.prototype.CellIsValidated = function(x, y){
return !(x < 0 || x >= this.Info.X || y < 0 || y >= this.Info.Y);
}
InfoProcess.prototype.OnCellChange = function(x,y){
if(this.HandlerIsValidated(this.CellChangeHandler))
this.CellChangeHandler(this.Info,x,y,this.Context);
}
InfoProcess.prototype.OnLose = function(x,y){
if(this.HandlerIsValidated(this.LoseHandler))
this.LoseHandler(this.Info,x,y,this.Context);
}
InfoProcess.prototype.OnMineChange = function(x,y){
if(this.HandlerIsValidated(this.MineChangeHandler))
this.MineChangeHandler(this.CurrentMine,x,y,this.Context);
}
InfoProcess.prototype.OnWin = function(x,y){
if(this.HandlerIsValidated(this.WinHandler))
this.WinHandler(x,y,this.Context);
}
InfoProcess.prototype.GetMineNumber = function(x,y){
var result = 0;
if(this.CellIsValidated(x-1, y-1) && this.Info.Data[x-1][y-1].M)
result++;
if(this.CellIsValidated(x-1, y) && this.Info.Data[x-1][y].M)
result++;
if(this.CellIsValidated(x-1, y+1) && this.Info.Data[x-1][y+1].M)
result++;
if (this.CellIsValidated(x, y-1) && this.Info.Data[x][y-1].M)
result++;
if (this.CellIsValidated(x, y+1) && this.Info.Data[x][y+1].M)
result++;
if (this.CellIsValidated(x+1, y-1) && this.Info.Data[x+1][y-1].M)
result++;
if (this.CellIsValidated(x+1, y) && this.Info.Data[x+1][y].M)
result++;
if (this.CellIsValidated(x+1, y + 1) && this.Info.Data[x+1][y+1].M)
result++;
return result++;
}
InfoProcess.prototype.SerachMine = function(x,y){
if(!this.CellIsValidated(x,y))
return;
if(this.Info.Data[x][y].S != 0)
return;
if(!this.Info.Data[x][y].M)
{
var mineNumber = this.GetMineNumber(x,y);
if (mineNumber == 0)
{
this.Info.Data[x][y].S = 3;
this.OnCellChange(x,y);
this.SerachMine(x-1, y-1);
this.SerachMine(x-1, y);
this.SerachMine(x-1, y + 1);
this.SerachMine(x, y - 1);
this.SerachMine(x, y + 1);
this.SerachMine(x + 1, y - 1);
this.SerachMine(x + 1, y );
this.SerachMine(x + 1, y + 1);
}
else
{
this.Info.Data[x][y].S = 3;
this.Info.Data[x][y].N = mineNumber;
this.OnCellChange(x,y);
}
}
else //mine game over
{
this.Info.Data[x][y].S = 6;
this.OnCellChange(x,y);
this.SetLose(x,y);
}
}
InfoProcess.prototype.SetLose = function(x,y){
for(var m=0; m<this.Info.X; m++)
for(var n=0; n<this.Info.Y; n++)
if (this.Info.Data[m][n].M)
if (this.Info.Data[m][n].S != 1 && this.Info.Data[m][n].S != 6){
this.Info.Data[m][n].S = 4;
this.OnCellChange(m,n);
}
else if(this.Info.Data[m][n].S == 1){
this.Info.Data[m][n].S = 5;
this.OnCellChange(m,n);
}
this.OnLose(x,y);
}
InfoProcess.prototype.JudgeWin = function(){
if (this.CurrentMine != 0)
return false;
for(var x = 0; x < this.Info.X; x++)
for(var y = 0; y < this.Info.Y; y++)
if(this.Info.Data[x][y].M && this.Info.Data[x][y].S != 1)
return false;
return true;
}
InfoProcess.prototype.MarkMine = func