-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·238 lines (209 loc) · 6.34 KB
/
script.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*eslint-env browser */
//全局变量
mapOffset = 2; //地图大小偏移量
maxSize = 300; //地图大小限制
liveCellColor = 'rgb(0,0,0)'; //活细胞颜色
//deadCellColor = 'rgb(255,255,255)'; //死细胞颜色
wallColor = 'rgb(127,127,127)'; //墙壁颜色
bgColor = 'rgb(255,255,255)'; //背景颜色
gridColor = 'rgb(0,0,0)'; //网格线颜色
//GameMap类
GameMap = {};
GameMap.row = 50;
GameMap.column = 50;
GameMap.dataA = new Array();
GameMap.dataB = new Array();
GameMap.dataWall = new Array();
GameMap.dataFlag = true; //True表示使用dataA False表示使用dataB
GameMap.rate = 0.5; //随机初始化活细胞比率
GameMap.init = function () {
//初始化data
this.dataFlag = true;
this.dataA = new Array();
this.dataB = new Array();
this.dataWall = new Array();
for (var i = 0; i < this.row + 2*mapOffset; i++)
{
this.dataA[i] = new Array();
this.dataB[i] = new Array();
this.dataWall[i] = new Array();
for (var j = 0; j < this.column + 2*mapOffset; j++)
{
this.dataA[i][j] = 0;
this.dataB[i][j] = 0;
this.dataWall[i][j] = false;
}
}
}
GameMap.setSize = function (newSize) {
//设置地图尺寸
if(newSize < 1 || newSize > maxSize)
return;
this.row = newSize;
this.column = newSize;
this.init();
}
GameMap.updateData = function () {
//更新下一代细胞
var counter = 0;
var row = this.row + mapOffset;
var column = this.column + mapOffset;
this.dataFlag = !this.dataFlag;
if (!this.dataFlag) {
for (var i = mapOffset; i < row; i++) {
for (var j = mapOffset; j < column; j++) {
counter = this.dataA[i - 2][j] + this.dataA[i - 1][j] + this.dataA[i + 1][j] + this.dataA[i + 2][j] + this.dataA[i][j - 2] + this.dataA[i][j - 1] + this.dataA[i][j + 1] + this.dataA[i][j + 2];
if (!this.dataWall[i][j])
{
if (counter == 3)
this.dataB[i][j] = 1;
else if (counter != 2)
this.dataB[i][j] = 0;
else
this.dataB[i][j] = this.dataA[i][j];
}
}
}
}
else
{
for (var i = mapOffset; i < row; i++)
{
for (var j = mapOffset; j < column; j++)
{
counter = this.dataB[i - 2][j] + this.dataB[i - 1][j] + this.dataB[i + 1][j] + this.dataB[i + 2][j] + this.dataB[i][j - 2] + this.dataB[i][j - 1] + this.dataB[i][j + 1] + this.dataB[i][j + 2];
if (!this.dataWall[i][j])
{
if (counter == 3)
this.dataA[i][j] = 1;
else if (counter != 2)
this.dataA[i][j] = 0;
else
this.dataA[i][j] = this.dataB[i][j];
}
}
}
}
}
GameMap.randomSet = function () {
//随机初始化
var row = this.row + mapOffset;
var column = this.column + mapOffset;
for (var i = mapOffset; i < row; i++)
{
for (var j = mapOffset; j < column; j++)
{
if (!this.dataWall[i][j])
this.dataA[i][j] = (Math.random() < this.rate) ? 1 : 0;
}
}
}
GameMap.changeCell = function (row, column) {
//转化当前细胞状态(用于响应手动设置操作)
if (!Clock.flag && Clock.beginFlag)
{
this.dataA[row][column] = 1 - this.dataA[row][column];
this.dataB[row][column] = 1 - this.dataB[row][column];
}
}
GameMap.changeWall = function (row, column) {
//设置墙壁
if (!Clock.beginFlag)
{
this.dataWall[row][column] = !this.dataWall[row][column];
}
}
//Clock计时类
Clock = {};
Clock.interval = 100;
Clock.flag = false; //False表示游戏为暂停状态 True为运行状态
Clock.beginFlag = false; //False表示游戏未开始 True为游戏开始
Clock.start = function () {
//开始游戏
Clock.over();
this.beginFlag = true;
GameMap.randomSet();
paint();
this.run();
}
Clock.over = function () {
//结束游戏
if(this.beginFlag)
{
this.stop();
this.beginFlag = false;
paintGrid();
GameMap.init();
}
}
Clock.run = function () {
//触发细胞更新
this.flag = true;
timer = setInterval("paint()", this.interval);
document.getElementById('myState').innerHTML = "STOP";
}
Clock.stop = function () {
//暂停游戏
this.flag = false;
try{
clearInterval(timer);
} catch (e) { }
document.getElementById('myState').innerHTML = 'RUN';
}
function paintGrid() {
//绘制表格
var mx = mycanvas.width / GameMap.column;
var my = mycanvas.height / GameMap.row;
mycontext.fillStyle = bgColor;
mycontext.fillRect(0, 0, mycanvas.width, mycanvas.height);
mycontext.strokeStyle = gridColor;
for (var i = 0; i <= GameMap.row; i++)
{
mycontext.beginPath();
mycontext.moveTo(0, i * my);
mycontext.lineTo(mycanvas.width, i * my);
mycontext.stroke();
}
for (var i = 0; i <= GameMap.column; i++)
{
mycontext.beginPath();
mycontext.moveTo(i * mx, 0);
mycontext.lineTo(i * mx, mycanvas.height);
mycontext.stroke();
}
}
function paint() {
GameMap.updateData();
//根据data绘制canvas
var data;
if(GameMap.dataFlag)
data = GameMap.dataA;
else
data = GameMap.dataB;
paintGrid();
var mx = mycanvas.width / GameMap.column;
var my = mycanvas.height / GameMap.row;
//绘制细胞
var row = GameMap.row + mapOffset;
var column = GameMap.column + mapOffset;
for (var i = mapOffset; i < row; i++)
{
for (var j = mapOffset; j < column; j++)
{
if (GameMap.dataWall[i][j])
{
mycontext.fillStyle = wallColor;
mycontext.fillRect((j - mapOffset) * mx, (i - mapOffset) * my, mx, my);
}
else if (data[i][j])
{
mycontext.fillStyle = liveCellColor;
mycontext.fillRect((j - mapOffset) * mx, (i - mapOffset) * my, mx, my);
}
}
}
}
module.exports.GameMap = GameMap;
module.exports.Clock = Clock;
module.exports.mapOffset = mapOffset;
module.exports.maxSize = maxSize;