-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmines.htm
More file actions
131 lines (117 loc) · 3.16 KB
/
mines.htm
File metadata and controls
131 lines (117 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport"
content="width=200; initial-scale=1.0; maximum-scale=1.0; user-scalable=yes;" />
<title>mine sweeper</title>
<style type="text/css">
<!--
.cell {
width: 18px;
height: 18px;
float:left;
display: inline;
text-align:center;
font-size: 12px;
font-weight: bold;
color:blue;
background:transparent url('tile1.gif') no-repeat;
}
-->
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
google.setOnLoadCallback(function() {
$.fn.extend({
MineSweeper: function(config){
var opened = [];
var mines = [];
var is_mine = function(x, y){
return mines[y * config.y_count + x];
}
var get_value = function(x, y){
if(is_mine(x, y)){
return "X";
}else{
var count = 0;
for(i = -1; i < 2; i++){
for(j = -1; j < 2; j++){
if(is_mine(x + i, y + j)){
count++;
}
}
}
if(count == 0){
return "";
}else{
return count;
}
}
}
var open_cell = function(x, y){
if(! ( x >= 0 && x < config.x_count && y >= 0 && y < config.y_count) ) return;
cell = $("#" + x + "_" + y);
if(! cell) return;
id = y * config.y_count + x;
if(opened[id]) return;
var v = get_value(x, y);
cell.css("background", "url('tile2.gif')");
cell.text(v);
opened[id] = 1;
if(v == ""){
open_cell(x, y - 1);
open_cell(x + 1, y);
open_cell(x, y + 1);
open_cell(x - 1, y);
}
return (v == "X");
}
var open_all_cell = function(){
for(x = 0; x < config.x_count; x++){
for( y = 0; y < config.y_count; y++){
if(is_mine(x, y)){
open_cell(x, y);
}
}
}
}
map = $("#map", this);
for(x = 0; x < config.x_count; x++){
for(y = 0; y < config.y_count; y++){
var cell = document.createElement('div');
cell.id = x + "_" + y;
cell.innerHTML = " ";
cell.className = "cell";
$(cell).appendTo(map);
$(cell).bind("click", {x: x, y: y}, function(e){
if( open_cell(e.data.x, e.data.y) ){
open_all_cell();
alert("game over");
}
return false;
});
}
}
for(i = 0; i < config.mine_count; i++){
while(true){
var rand = Math.floor( Math.random() * config.x_count * config.y_count );
if(! mines[rand] ){
mines[rand] = 1;
break;
}
}
}
},
});
$("#minesweeper").MineSweeper({x_count:17, y_count:17, mine_count:20});
});
</script>
</head>
<body>
<div id="minesweeper">
<div id="map" style="width:320px;">
</div>
</div>
</body>
</html>