?? game.js
字號:
/*************************************************************
** Game : 掃雷
** Author : sicon
** Date : 2005.11.24
** Email : sicon2002@163.com
** version : 1.0
** copyright : All Earthman.
** 1.1 : 考慮已標記為雷時的自動標記
*************************************************************/
// 注冊 一個游戲。
var game = new Mine( "GamePan" );
function LoadGame()
{
game.InitGame();
}
/**************************************************
** 游戲代碼
**************************************************/
function Mine( Panel )
{
this.PanelId = Panel;
this.Name = "game";
this.Version = "1.0";
this.Colums = 10; // 行數
this.Rows = 10; // 列數
this.ColumsWidth= 40; // 行寬
this.RowsWidth = 40; // 列寬
this.Bgcolor = "#2E8B57"; //已挖
this.ForeColor = "BUTTONSHADOW"; //未挖
this.MineColor = "#8B4513"; //標記為雷
this.MineCount = 0; //雷數
this.MineLeft = 0;
this.FontSize = 5; //字體大小
this.IsEnd = false; //是否結束
this.MineArray = new Array(); //雷 0:不是 1:是
this.MineAround = new Array(); //周圍雷數
this.CellShow = new Array(); //已挖標記 1:無雷 2:有雷
// this.CellBorder = new Array();
this.StartTime = new Date();
this.EndTime = new Date();
this.Score = 0;
this.ShowStyle = 1; // 提示數字樣式 0:繁體漢字 1:阿拉伯數字 2:英文 3:簡體漢字 4:不提示
this.ShowMine = false; // 是否顯示雷
// 初始化游戲
this.InitGame = sInitGame;
// 初始化雷區
this.InitMineArray = sInitMineArray;
// 計算周圍的雷數
this.CaluMineAround = sCaluMineAround;
// 格內顯示內容。
this.GetDisplay = sGetDisplay;
// 某一網格周圍。
this.GetCellBorder = sGetCellBorder;
// 四周是否有雷
this.IsArroundCellsHasMine = sIsArroundCellsHasMine;
// 點網格事件時
this.RefreshGame = sRefreshGame;
// 標記雷
this.MarkMineGame = sMarkMineGame;
// 是否結束
this.IsAllFind = sIsAllFind;
// 游戲結束
this.GameOver = sGameOver;
// 刷新
this.Refresh = sRefresh;
// 標記為雷
this.MarkMine = sMarkMine;
// 顯示分數
this.ShowWhiteExp = sShowWhiteExp;
// 數字轉換
this.ChangeNum2Char = sChangeNum2Char;
}
function sInitGame()
{
this.StartTime = new Date();
// alert( this.StartTime.getTime() );
this.MineCount = 0;
this.InitMineArray();
this.CaluMineAround();
this.Refresh();
}
function sRefresh()
{
// 布雷 Panel.
this.IsAllFind();
var GamePan = document.getElementById( this.PanelId );
var htmStr;
htmStr = "<table border=\"5\"><tr><td align=\"center\" bgcolor=\"#F5F5DC\"><font size=\"7\" color=\"#2F4F4F\"><b>Sweep Mine 1.0</b></font></td></tr><tr><td><table cellspacing='1' cellpadding='1' border=\"0\">";
for( var i=0; i<this.Rows; i++ )
{
htmStr += "<tr>";
for( var j=0; j<this.Colums; j++ )
{
var Color = this.ForeColor;
if( this.CellShow[i][j] == 1 )
Color = this.Bgcolor;
if( this.CellShow[i][j] == 2 )
Color = this.MineColor;
htmStr += "<td bgcolor=\""+ Color +"\" width=\""+ this.ColumsWidth +"\" height=\""+ this.RowsWidth +"\" oncontextmenu='"+ this.Name +".MarkMineGame("+i+","+j+")' onclick='"+ this.Name +".RefreshGame("+i+","+j+")' align=\"center\" valign=\"center\">"+ this.GetDisplay( i,j ) +"</td>";
}
htmStr += "</tr>";
}
htmStr += "</table></td><td valign=\"top\">";
htmStr += "<table border=\"0\">"
//htmStr += " <tr><td>難度:5<td></tr>"
htmStr += " <tr><td>高度:10 寬度:10<td></tr>"
htmStr += " <tr><td>雷數:"+ this.MineCount +" <td></tr>"
htmStr += " <tr><td>剩余雷數:"+ this.MineLeft +" <td></tr>"
htmStr += " <tr><td><td></tr>"
htmStr += " <tr><td><td></tr>"
htmStr += " <tr><td><td></tr>"
htmStr += " <tr><td><span style=\"cursor:hand\" onclick=\"LoadGame()\">[開始] <span onclick=\"javascript:alert('不準停止!')\">[暫停]<td></tr>"
// htmStr += " <tr><td>時間:\""+ (this.EndTime - this.StartTime) +"\"<td></tr>"
// htmStr += " <tr><td>分數:"+ this.Score +"<td></tr>"
htmStr += " <tr><td><td></tr>"
htmStr += " <tr><td>"+ this.ShowWhiteExp() +"<td></tr>"
htmStr += " <tr><td><td></tr>"
htmStr += "</table>"
htmStr += "</td></tr></table>";
// htmStr += this.MineCount;
GamePan.innerHTML = htmStr;
}
function sInitMineArray()
{
for( var i=0; i<this.Rows; i++ )
{
this.MineArray[i] = new Array();
this.CellShow[i] = new Array();
for( var j=0; j<this.Colums; j++ )
{
var rand = parseInt(Math.random() * 7);
this.MineArray[i][j] = ( rand < 2 )?rand:0;
this.CellShow[i][j] = 0; // All Not show.
if( this.MineArray[i][j] == 1 )
{
this.MineCount ++;
}
}
}
this.MineLeft = this.MineCount;
}
function sCaluMineAround()
{
var sValue;
for( var i=0; i<this.Rows; i++ )
{
this.MineAround[i] = new Array();
for( var j=0; j<this.Colums; j++ )
{
this.MineAround[i][j] = 0;
for( var m=i-1; m<=i+1; m++ )
{
sValue = 0;
for( var n=j-1; n<=j+1; n++ )
{
try
{
sValue = this.MineArray[m][n] ;
}
catch( e )
{
sValue = 0;
}
if( m==i && n==j )
sValue = 0;
if( !isNaN(sValue) )
this.MineAround[i][j] += parseInt( sValue );
}
}
}
}
}
function sGetDisplay( i,j )
{
var returnValue = "";
if( this.ShowMine == true )
returnValue += "" + this.MineArray[i][j] ;
if( this.CellShow[i][j] == 1 && this.MineAround[i][j] != 0 )
returnValue += "<font size="+ this.FontSize +" color=\"#FFFFFF\">" + this.ChangeNum2Char( this.MineAround[i][j] ) + "</font>";
return returnValue;
}
function sGetCellBorder( i,j )
{
if( this.IsArroundCellsHasMine(i,j) == true && this.CellShow[i][j] != 1)
{
this.CellShow[i][j] = 1;
for( var m=i-1; m<=i+1; m++ )
{
for( var n=j-1; n<=j+1; n++ )
{
if( m>=0 && n>=0 && m<this.Rows && n<this.Colums )
this.GetCellBorder( m,n );
}
}
}
if( this.MineArray[i][j] == 1 )
this.GameOver();
else
this.CellShow[i][j] = 1;
}
function sIsArroundCellsHasMine( i,j )
{
var sValue;
for( var m=i-1; m<=i+1; m++ )
{
sValue = 0;
for( var n=j-1; n<=j+1; n++ )
{
try
{
sValue = this.MineArray[m][n] ;
}
catch( e )
{
sValue = 0;
}
// if( !isNaN(sValue) && sValue == 1 && this.CellShow[i][j] )
// sValue = 0;
if( !isNaN(sValue) && sValue==1 )
return false;
}
}
return true;
}
function sGameOver()
{
alert( 'Game Over.' );
this.InitGame();
}
function sMarkMine( i,j )
{
if( this.CellShow[i][j] == 1 )
return;
if( this.CellShow[i][j] != 2 )
{
this.CellShow[i][j] = 2;
this.MineLeft--;
}
else
{
this.CellShow[i][j] = 0;
this.MineLeft++;
}
}
function sIsAllFind()
{
var c = 0;
var d = 0;
for( var i=0; i<this.Rows; i++ )
{
for( var j=0; j<this.Colums; j++ )
{
if( this.CellShow[i][j] == 2 )
{
c++;
}
if( this.CellShow[i][j] == 1 )
{
d++;
}
}
}
if( c > this.MineCount )
alert( '小白,俺沒有那么多雷!' );
if( c == this.MineCount && c+d == this.Colums * this.Rows )
{
this.EndTime = new Date();
this.IsEnd = true;
}
}
function sRefreshGame( i,j )
{
this.GetCellBorder( i,j );
this.Refresh();
}
function sMarkMineGame( i,j )
{
this.MarkMine( i,j );
this.Refresh();
}
function sShowWhiteExp()
{
var timeSpan;
timeSpan = this.EndTime - this.StartTime;
this.Score = Math.ceil( 100000000/(timeSpan * this.MineCount ));
if( this.IsEnd == true )
{
return "<font size=\"8\">You Win!</font><br><font color=\"#FF00ff\">你的分數為:</font><br><font color=\"#FF0000\" size=\"30\">" + this.Score + "</font>";
this.IsEnd = false;
}
else
return "";
}
function sChangeNum2Char( j )
{
var arrNum;
if( this.ShowStyle == 4 )
return "";
arrNum = new Array();
arrNum[0] = new Array();
arrNum[1] = new Array();
arrNum[2] = new Array();
arrNum[3] = new Array();
arrNum[0][0] = "零";
arrNum[0][1] = "壹";
arrNum[0][2] = "貮";
arrNum[0][3] = "叁";
arrNum[0][4] = "肆";
arrNum[0][5] = "伍";
arrNum[0][6] = "陸";
arrNum[0][7] = "柒";
arrNum[0][8] = "捌";
arrNum[0][9] = "玖";
arrNum[1][0] = "0";
arrNum[1][1] = "1";
arrNum[1][2] = "2";
arrNum[1][3] = "3";
arrNum[1][4] = "4";
arrNum[1][5] = "5";
arrNum[1][6] = "6";
arrNum[1][7] = "7";
arrNum[1][8] = "8";
arrNum[1][9] = "9";
arrNum[2][0] = "ONE";
arrNum[2][1] = "TWO";
arrNum[2][2] = "THREE";
arrNum[2][3] = "FOUR";
arrNum[2][4] = "FIVE";
arrNum[2][5] = "SIX";
arrNum[2][6] = "SEVEN";
arrNum[2][7] = "EIGHT";
arrNum[2][8] = "NINE";
arrNum[2][9] = "TEN";
arrNum[3][0] = "O";
arrNum[3][1] = "一";
arrNum[3][2] = "二";
arrNum[3][3] = "三";
arrNum[3][4] = "四";
arrNum[3][5] = "五";
arrNum[3][6] = "六";
arrNum[3][7] = "七";
arrNum[3][8] = "八";
arrNum[3][9] = "九";
return arrNum[ this.ShowStyle ][j];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -