-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-random-coverage-test.html
More file actions
172 lines (127 loc) · 2.94 KB
/
js-random-coverage-test.html
File metadata and controls
172 lines (127 loc) · 2.94 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js随机值覆盖测试</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,minimal-ui" />
<meta name="renderer" content="webkit">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telphone=no,email=no">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="apple-mobile-web-app-title" content="">
<style>
html,
body {
margin:0;
padding:0;
width:100%;
height:100%;
font-size: 16px;
}
#container {
position: relative;
height: 400px;
}
.node {
position: absolute;
width: 24px;
height: 25px;
border-right: 1px solid black;
border-bottom: 1px solid black;
}
.node.alive {
background-color: lawngreen;
}
.horizontal-1 {
border-top: 1px solid black;
}
.vertical-1 {
border-left: 1px solid black;
}
h1, #tip {
margin-left: 1em;
}
</style>
<script>
window.onload = function(){
init();
setTimeout(loop, interval);
}
const col = 15;
const row = 15;
const total = col*row; // 总数
const initAliveNum = 1;// 每代数量
const interval = 200; // 刷新间隔
let generation = 1; // 代数
let cellList = [];
function Cell(x, y) {
this.x = x;
this.y = y;
this.isAlive = 0;
}
function loop() {
generate();
render();
setTimeout(loop, interval);
generation++;
console.log(generation);
}
function init() {
let x = 0, y = 0;
for(let i = 0; i < total; i++) {
//console.log(x, y);
cellList.push(new Cell(x, y));
y++;
if(y >= col) {
x++;
y = 0;
}
}
}
function generate() {
let list = [];
for(let i = 0; i < total; i++) {
list.push(i);
}
for(let i = 0; i < initAliveNum; i++) {
let index = Math.floor((Math.random()*list.length-1)+1);
let num = list[index];
let cell = cellList[num];
if(cell === undefined) {
throw new Error('cell超出界限,num:'+num+' index:'+index);
break;
}
cell.isAlive = 1;
list.splice(index, 1);
}
}
function render() {
let elWrap = document.getElementById("container");
let fragment = document.createDocumentFragment();
for (let i = 1; i <= row; i++) {
for (let j = 1; j <= col; j++) {
let el = document.createElement("div");
let cell = cellList[(i-1)*col+(j-1)];
if(cell === undefined) {
throw new Error('cell超出界限, index:'+(i-1)*col+(j-1));
break;
}
el.className = "node" + " " + "horizontal-" + (cell.x+1) + " " + "vertical-" + (cell.y+1) + " " + "node-" + (cell.x+1) + "-" + (cell.y+1);
el.style.top = ((cell.x+1) * 25) + "px";
el.style.left = ((cell.y+1) * 24) + "px";
fragment.appendChild(el);
if(cell.isAlive) {
el.className += " alive";
}
}
}
elWrap.innerHTML = "";
elWrap.appendChild(fragment);
}
</script>
<body>
<h1>js随机值覆盖测试</h1>
<div id="container"></div>
</body>
</html>