ArcIMS.net

using System;
using System.Data;
using System.Configuration;
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Javascript 扫雷

Posted on 2006-04-29 22:10  BaobeI  阅读(704)  评论(0)    收藏  举报
年编写的一个扫雷,一直也没有完善过,先Post出来供大家参考,以后有时间再完善...

代码有两部分组成:HTML 部分

 1<html>
 2    <head>
 3        <title>Game</title>
 4        <script language="javascript" src="game.js"></script>
 5        <script language="javascript">
 6            function StartMineGame()
 7            {
 8                LoadGame();
 9            }

10            function KU()
11            {
12                alert( event.srcElement );
13                return false;
14            }

15        
</script>
16    </head>
17    <body onload="setTimeout( 'StartMineGame()',100 )" oncontextmenu="return false;">
18        <div id="GamePan"></div>
19        <div id="gameP" name="gameP"></div>
20    </body>
21</html>
22

Javascript 部分:

  1/*************************************************************
  2  ** Game        : 扫雷
  3  ** Author        : sicon
  4  ** Date        : 2005.11.24
  5  ** Email        : sicon2002@163.com
  6  ** version    : 1.0
  7  ** copyright    : All Earthman.
  8  ** 1.1        : 考虑已标记为雷时的自动标记
  9*************************************************************/

 10
 11// 注册 一个游戏。
 12var game = new Mine( "GamePan" );
 13function LoadGame()
 14{
 15    game.InitGame();
 16}

 17
 18/**************************************************
 19 ** 游戏代码
 20**************************************************/

 21
 22function Mine( Panel )
 23{
 24    this.PanelId    = Panel;
 25    this.Name        = "game";
 26    this.Version    = "1.0";
 27    this.Colums        = 10;            // 行数
 28    this.Rows        = 10;            // 列数
 29    this.ColumsWidth= 40;            // 行宽
 30    this.RowsWidth  = 40;            // 列宽 
 31    this.Bgcolor    = "#2E8B57";        //已挖
 32    this.ForeColor  = "BUTTONSHADOW";    //未挖
 33    this.MineColor  = "#8B4513";        //标记为雷
 34    this.MineCount  = 0;                //雷数
 35    this.MineLeft    = 0;
 36    this.FontSize    = 5;                //字体大小
 37    this.IsEnd        = false;            //是否结束
 38    this.MineArray  = new Array();        //雷 0:不是 1:是
 39    this.MineAround = new Array();        //周围雷数
 40    this.CellShow   = new Array();        //已挖标记 1:无雷 2:有雷
 41     // this.CellBorder = new Array();
 42    this.StartTime  = new Date();
 43    this.EndTime    = new Date();
 44    this.Score        = 0;
 45    this.ShowStyle  = 1;            // 提示数字样式 0:繁体汉字 1:阿拉伯数字 2:英文 3:简体汉字 4:不提示
 46    this.ShowMine   = false;        // 是否显示雷
 47    
 48    // 初始化游戏
 49    this.InitGame        = sInitGame;
 50    // 初始化雷区
 51    this.InitMineArray    = sInitMineArray;
 52    // 计算周围的雷数
 53    this.CaluMineAround = sCaluMineAround;
 54    // 格内显示内容。
 55    this.GetDisplay        = sGetDisplay;
 56    // 某一网格周围。
 57    this.GetCellBorder    = sGetCellBorder;
 58    // 四周是否有雷
 59    this.IsArroundCellsHasMine = sIsArroundCellsHasMine;
 60    // 点网格事件时
 61    this.RefreshGame    = sRefreshGame;
 62    // 标记雷
 63    this.MarkMineGame    = sMarkMineGame;
 64    // 是否结束
 65    this.IsAllFind        = sIsAllFind;
 66    // 游戏结束
 67    this.GameOver       = sGameOver;
 68    // 刷新
 69    this.Refresh        = sRefresh;
 70    // 标记为雷
 71    this.MarkMine        = sMarkMine;
 72    // 显示分数
 73    this.ShowWhiteExp   = sShowWhiteExp;
 74    // 数字转换
 75    this.ChangeNum2Char = sChangeNum2Char;
 76}

 77
 78function sInitGame()
 79{
 80    this.StartTime = new Date();
 81    // alert( this.StartTime.getTime()  );
 82    this.MineCount  = 0;
 83    this.InitMineArray();
 84    this.CaluMineAround();
 85    this.Refresh();
 86}

 87
 88function sRefresh()
 89{
 90    // 布雷 Panel.
 91    this.IsAllFind();
 92    var GamePan = document.getElementById( this.PanelId ); 
 93    var htmStr;
 94    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\">";
 95    forvar i=0; i<this.Rows; i++ )
 96    {
 97        htmStr += "<tr>";
 98        forvar j=0; j<this.Colums; j++ )
 99        {
100            var Color = this.ForeColor;
101            ifthis.CellShow[i][j] == 1 )
102                Color = this.Bgcolor;
103            ifthis.CellShow[i][j] == 2 )
104                Color = this.MineColor;
105            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>";
106        }

107        htmStr += "</tr>";
108    }

109    htmStr += "</table></td><td valign=\"top\">";
110    htmStr += "<table border=\"0\">"
111    //htmStr += "    <tr><td>难度:5<td></tr>"
112    htmStr += "    <tr><td>高度:10 &nbsp; 宽度:10<td></tr>"
113    htmStr += "    <tr><td>雷数:"+ this.MineCount +" <td></tr>"
114    htmStr += "    <tr><td>剩余雷数:"+ this.MineLeft +" <td></tr>"
115    htmStr += "    <tr><td><td></tr>"
116    htmStr += "    <tr><td><td></tr>"
117    htmStr += "    <tr><td><td></tr>"
118    htmStr += "    <tr><td><span style=\"cursor:hand\" onclick=\"LoadGame()\">[开始] <span onclick=\"javascript:alert('不准停止!')\">[暂停]<td></tr>"
119//    htmStr += "    <tr><td>时间:\""+ (this.EndTime - this.StartTime) +"\"<td></tr>"
120//    htmStr += "    <tr><td>分数:"+ this.Score +"<td></tr>"
121    htmStr += "    <tr><td><td></tr>"
122    htmStr += "    <tr><td>"+ this.ShowWhiteExp() +"<td></tr>"
123    htmStr += "    <tr><td><td></tr>"
124    htmStr += "</table>"
125    htmStr += "</td></tr></table>";
126//    htmStr += this.MineCount;
127    GamePan.innerHTML = htmStr;
128}

129
130function sInitMineArray()
131{    
132    forvar i=0; i<this.Rows; i++ )
133    {
134        this.MineArray[i] = new Array();
135        this.CellShow[i] = new Array();
136        forvar j=0; j<this.Colums; j++ )
137        {
138            var rand = parseInt(Math.random() * 7);
139            this.MineArray[i][j] = ( rand < 2 )?rand:0;
140            this.CellShow[i][j] = 0;  // All Not show.
141            ifthis.MineArray[i][j] == 1 )
142            {
143                this.MineCount ++;
144            }

145        }

146    }

147    this.MineLeft = this.MineCount;
148}

149
150function sCaluMineAround()
151{
152    var sValue;
153    forvar i=0; i<this.Rows; i++ )
154    {
155        this.MineAround[i] = new Array();
156        forvar j=0; j<this.Colums; j++ )
157        {
158            this.MineAround[i][j] = 0;
159            forvar m=i-1; m<=i+1; m++ )
160            {
161                sValue = 0;
162                forvar n=j-1; n<=j+1; n++ )
163                {
164                    try
165                    {
166                        sValue = this.MineArray[m][n] ;
167                    }

168                    catch( e )
169                    {
170                        sValue = 0;
171                    }

172                    if( m==&& n==j )
173                        sValue = 0;
174                    if!isNaN(sValue) )
175                    this.MineAround[i][j] +=  parseInt( sValue );
176                }

177            }

178        }

179    }

180}

181
182function sGetDisplay( i,j )
183{
184    var returnValue = "";
185    ifthis.ShowMine == true )
186        returnValue += "" + this.MineArray[i][j] ;
187    ifthis.CellShow[i][j] == 1 && this.MineAround[i][j] != 0 )
188        returnValue += "<font size="+ this.FontSize +" color=\"#FFFFFF\">" + this.ChangeNum2Char( this.MineAround[i][j] ) + "</font>";
189    return returnValue;
190}

191
192function sGetCellBorder( i,j )
193{
194    ifthis.IsArroundCellsHasMine(i,j) == true && this.CellShow[i][j] != 1)
195    {
196        this.CellShow[i][j] = 1;
197        forvar m=i-1; m<=i+1; m++ )
198        {
199            forvar n=j-1; n<=j+1; n++ )
200            {
201                if( m>=0 && n>=0 && m<this.Rows && n<this.Colums )
202                    this.GetCellBorder( m,n );
203            }

204        }

205    
206    }

207    ifthis.MineArray[i][j] == 1 )
208        this.GameOver();
209    else
210        this.CellShow[i][j] = 1;
211}

212
213function sIsArroundCellsHasMine( i,j )
214{
215    var sValue;
216
217    forvar m=i-1; m<=i+1; m++ )
218    {
219        sValue = 0;
220        forvar n=j-1; n<=j+1; n++ )
221        {
222            try
223            {
224                sValue = this.MineArray[m][n] ;
225            }

226            catch( e )
227            {
228                sValue = 0;
229            }

230    //        if( !isNaN(sValue) && sValue == 1 && this.CellShow[i][j] )
231    //            sValue = 0;
232            if!isNaN(sValue) && sValue==1 )
233                return false;
234        }

235    }

236
237    return true;
238}

239
240function sGameOver()
241{
242    alert( 'Game Over.' );
243    this.InitGame();
244}

245
246function sMarkMine( i,j )
247{
248    ifthis.CellShow[i][j] == 1 )
249        return;
250    ifthis.CellShow[i][j] != 2 )
251    {
252        this.CellShow[i][j] = 2;
253        this.MineLeft--;
254    }

255    else
256    {
257        this.CellShow[i][j] = 0;
258        this.MineLeft++;
259    }

260}

261
262function sIsAllFind()
263{
264    var c = 0;
265    var d = 0;
266    forvar i=0; i<this.Rows; i++ )
267    {
268        forvar j=0; j<this.Colums; j++ )
269        {
270            ifthis.CellShow[i][j] == 2 )
271            {
272                c++;
273            }

274            ifthis.CellShow[i][j] == 1 )
275            {
276                d++;
277            }

278        }

279    }

280
281    if( c > this.MineCount )
282        alert( '小白,俺没有那么多雷!' );
283    if( c == this.MineCount && c+== this.Colums * this.Rows )
284    {
285        this.EndTime = new Date();
286        this.IsEnd = true;
287    }

288}

289
290function sRefreshGame( i,j )
291{
292    this.GetCellBorder( i,j );
293    this.Refresh();
294}

295
296function sMarkMineGame( i,j )
297{
298    this.MarkMine( i,j );
299    this.Refresh();
300}

301
302function sShowWhiteExp()
303{
304    var timeSpan;
305    timeSpan = this.EndTime - this.StartTime;
306    this.Score = Math.ceil( 100000000/(timeSpan * this.MineCount ));
307
308    ifthis.IsEnd == true )
309    {
310        return "<font size=\"8\">You Win!</font><br><font color=\"#FF00ff\">你的分数为:</font><br><font color=\"#FF0000\" size=\"30\">" + this.Score + "</font>";
311        this.IsEnd = false;
312    }

313    else
314        return "";
315}

316
317function sChangeNum2Char( j )
318{
319    var arrNum;
320
321    ifthis.ShowStyle == 4 )
322        return "";
323    arrNum = new Array();
324    arrNum[0= new Array();
325    arrNum[1= new Array();
326    arrNum[2= new Array();
327    arrNum[3= new Array();
328
329    arrNum[0][0= "";
330    arrNum[0][1= "";
331    arrNum[0][2= "";
332    arrNum[0][3= "";
333    arrNum[0][4= "";
334    arrNum[0][5= "";
335    arrNum[0][6= "";
336    arrNum[0][7= "";
337    arrNum[0][8= "";
338    arrNum[0][9= "";
339
340    arrNum[1][0= "0";
341    arrNum[1][1= "1";
342    arrNum[1][2= "2";
343    arrNum[1][3= "3";
344    arrNum[1][4= "4";
345    arrNum[1][5= "5";
346    arrNum[1][6= "6";
347    arrNum[1][7= "7";
348    arrNum[1][8= "8";
349    arrNum[1][9= "9";
350
351    arrNum[2][0= "ONE";
352    arrNum[2][1= "TWO";
353    arrNum[2][2= "THREE";
354    arrNum[2][3= "FOUR";
355    arrNum[2][4= "FIVE";
356    arrNum[2][5= "SIX";
357    arrNum[2][6= "SEVEN";
358    arrNum[2][7= "EIGHT";
359    arrNum[2][8= "NINE";
360    arrNum[2][9= "TEN";
361
362    arrNum[3][0= "O";
363    arrNum[3][1= "";
364    arrNum[3][2= "";
365    arrNum[3][3= "";
366    arrNum[3][4= "";
367    arrNum[3][5= "";
368    arrNum[3][6= "";
369    arrNum[3][7= "";
370    arrNum[3][8= "";
371    arrNum[3][9= "";
372
373    return arrNum[ this.ShowStyle ][j];
374}

375

下载文件