js 自制扫雷
<html> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"type="text/javascript"></script> <script> /** * author:xiaojiang * time:2013/1/6 */ $(function(){ Array.prototype.hasItem = function(item){ var i = 0; for(i in this) if(this[i] === item) return true; return false; } var width = 10, height = 10, bombNum = 10, bombArr = [], mapArr = []; _isBomb = false, i = 0, j = 0, temp = '', html = ''; var _$table = $("<table></table>"); while(i < bombNum){ //生成n个随机数 temp = Math.floor(Math.random()*(width*height)); if(!bombArr.hasItem(temp)){ bombArr.push(temp); i++; } } html = '<table border="1">'; for(var i=0; i<width;i++){ //生成地图数组 html += '<tr height="20px">'; mapArr[i] = new Array(); for(j =0; j<height;j++){ _id = i*10 + j; _isBomb = bombArr.hasItem(_id) ? true : false; mapArr[i][j] = {id:_id,isBomb:_isBomb,around:[]}; html += '<td width="20px"></td>'; } html += '</tr>'; } html += '</table>'; var _i = _j = 0, _$content = $("#contain"); _$content.append(html); _$content.find("td").click(function(e){ if($(this).data("hasClick")) return ; $(this).data("hasClick",true); _i = $(e.target).parent()[0].rowIndex; _j = e.target.cellIndex; if(!mapArr[_i][_j].isBomb){ var _bombs = getRound(_i,_j); if(_bombs === 0){ $(this).css({background:"red"}); clickRound(_i,_j); }else{ $(this).css({background:"red"}).html(_bombs); } }else{ alert('失败'); } }); function getRound(i,j){ var bombNum = 0; for(var _i = -1; _i < 2; _i++){ for(var _j = -1; _j<2; _j++){ if(_i == 0 && _j == 0) continue; if( (_i+i < 0 || _j+j < 0) || ((_i+i) >= mapArr.length || (_j+j) >= mapArr[0].length) ) continue; mapArr[i][j]['around'].push([_i+i,_j+j]); if(mapArr[_i+i][_j+j].isBomb) bombNum ++; } } return bombNum; } function clickRound(i,j){ var aroundArr = mapArr[i][j]['around']; for(var y in aroundArr) _$content.find("tr").eq(aroundArr[y][0]).find('td').eq(aroundArr[y][1]).click(); } window.rightclick = function(e){ event.returnValue = false; } }); </script> <div id="contain" oncontextmenu="rightclick()"> </div> </html>
生命只有一次。

浙公网安备 33010602011771号