-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
147 lines (128 loc) · 3.59 KB
/
index.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="header">
<input id="nx" type="text" value="50">行
<input id="ny" type="text" value="50">列
<button id="set_map" onclick="reload()">设定画布</button>
<button id="start" onclick="stop()">开始</button>
<button id="clean" onclick="clean()">清空</button>
</div>
<div id="map" style="position:relative">
</div>
</body>
<script>
var nx,ny,matrix,running
running = false
dead = false
alive = true
size = 9
function stop(){
if(running){
running = false
$("#start").text("开始")
}
else{
running = true
$("#start").text("暂停")
loop()
}
}
function reload(){
// set matrix and set to dead
nx = $("#nx").val()
ny = $("#ny").val()
$("#map").empty();
matrix = new Array(nx+2)
for (var i = nx+1; i >= 0; i--) {
matrix[i] = new Array(ny+2)
for (var j = ny+1; j >= 0; j--) {
matrix[i][j] = dead;
};
};
//create divs
for (var y = 1; y<=ny ; y++){
for (var x = 1; x <= nx; x++) {
var pix_left = size*(x-1)
var pix_top = size*(y-1)
var pix_id = x+"_"+y
var pix_style = "top: "+pix_top+"px; left: "+pix_left+"px;"
var pix_onclick = "Click("+x+","+y+")"
$("#map").append($("<div/>", { id: pix_id, class: "dead",style:pix_style,onclick:pix_onclick}))
};
};
running = true
stop()
}
function clean(){
reload()
}
function loop() {
if(false == running){
return
}
var matrix_t = new Array(nx+2)
for (var i = nx+1; i >= 0; i--) {
matrix_t[i] = new Array(ny+2)
for (var j = ny+1; j >= 0; j--) {
matrix_t[i][j] = 0;
};
};
for (x=1;x<=nx;x++){
for(y=1;y<=ny;y++){
if (matrix[x][y] == alive){
matrix_t[x-1][y-1]++
matrix_t[x-1][y ]++
matrix_t[x-1][y+1]++
matrix_t[x ][y-1]++
matrix_t[x ][y+1]++
matrix_t[x+1][y-1]++
matrix_t[x+1][y ]++
matrix_t[x+1][y+1]++
}
}
}
for (x=1;x<=nx;x++){
for(y=1;y<=ny;y++){
if (matrix_t[x][y] == 2){
}
else if (matrix_t[x][y] == 3){
matrix[x][y] = alive
}
else{
matrix[x][y] = dead
}
}
}
for (x=1;x<=nx;x++){
for(y=1;y<=ny;y++){
///
id = "#"+x+"_"+y
pix = $(id)
if (matrix[x][y] == alive){
pix.attr("class", "alive")
}
else{
pix.attr("class", "dead")
}
}
}
setTimeout(function () {
loop();
}, 100);
}
function Click(x,y){
//if(true == running){return}
id = "#"+x+"_"+y
pix = $(id)
matrix[x][y] = !matrix[x][y]
pix.toggleClass("alive dead")
}
</script>
</html>