⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.js

📁 用javascript写的扫雷游戏的源码
💻 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 &nbsp; 宽度: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 + -