forked from lihongxun945/gobang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
325 lines (267 loc) · 6.71 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var S = require("./score.js");
var R = require("./role.js");
var W = require("./win.js");
var Board = function(container, status) {
this.container = container;
this.status = status;
this.step = this.container.width() * 0.065;
this.offset = this.container.width() * 0.044;
this.steps = []; //存储
this.started = false;
var self = this;
this.container.on("click", function(e) {
if(self.lock || !self.started) return;
var y = e.offsetX, x = e.offsetY;
x = Math.floor((x+self.offset)/self.step) - 1;
y = Math.floor((y+self.offset)/self.step) - 1;
self.set(x, y, 1);
});
this.worker = new Worker("./dist/bridge.js?r="+(+new Date()));
this.worker.onmessage = function(e) {
self._set(e.data[0], e.data[1], R.com);
self.lock = false;
self.setStatus("电脑下子("+e.data[0]+","+e.data[1]+"), 用时"+((new Date() - self.time)/1000)+"秒");
}
this.setStatus("请点击开始按钮");
}
Board.prototype.start = function() {
if(this.started) return;
this.initBoard();
this.board[7][7] = R.com;
this.steps.push([7, 7]);
this.draw();
this.setStatus("欢迎加入五子棋游戏");
this.started = true;
this.worker.postMessage({
type: "START"
});
}
Board.prototype.stop = function() {
if(!this.started) return;
this.setStatus("请点击开始按钮");
this.started = false;
}
Board.prototype.initBoard = function() {
this.board = [];
for(var i=0;i<15;i++) {
var row = [];
for(var j=0;j<15;j++) {
row.push(0);
}
this.board.push(row);
}
this.steps = [];
}
Board.prototype.draw = function() {
var container = this.container;
var board = this.board;
container.find(".chessman, .indicator").remove();
for(var i=0;i<board.length;i++) {
for(var j=0;j<board[i].length;j++) {
if(board[i][j] != 0) {
var chessman = $("<div class='chessman'></div>").appendTo(container);
if(board[i][j] == 2) chessman.addClass("black");
chessman.css("top", this.offset + i*this.step);
chessman.css("left", this.offset + j*this.step);
}
}
}
if(this.steps.length > 0) {
var lastStep = this.steps[this.steps.length-1];
$("<div class='indicator'></div>")
.appendTo(container)
.css("top", this.offset + this.step * lastStep[0])
.css("left", this.offset + this.step * lastStep[1])
}
}
Board.prototype._set = function(x, y, role) {
this.board[x][y] = role;
this.steps.push([x,y]);
this.draw();
var winner = W(this.board);
var self = this;
if(winner == R.com) {
$.alert("电脑赢了!", function() {
self.stop();
});
} else if (winner == R.hum) {
$.alert("恭喜你赢了!", function() {
self.stop();
});
}
}
Board.prototype.set = function(x, y, role) {
if(this.board[x][y] !== 0) {
throw new Error("此位置不为空");
}
this._set(x, y, role);
this.com(x, y, role);
}
Board.prototype.com = function(x, y, role) {
this.lock = true;
this.time = new Date();
this.worker.postMessage({
type: "GO",
x: x,
y: y
});
this.setStatus("电脑正在思考...");
}
Board.prototype.setStatus = function(s) {
this.status.text(s);
}
Board.prototype.back = function(step) {
if(this.lock) {
this.setStatus("电脑正在思考,请稍等..");
return;
}
step = step || 1;
while(step && this.steps.length >= 2) {
var s = this.steps.pop();
this.board[s[0]][s[1]] = R.empty;
s = this.steps.pop();
this.board[s[0]][s[1]] = R.empty;
step --;
}
this.draw();
this.worker.postMessage({
type: "BACK"
});
}
var b = new Board($("#board"), $(".status"));
$("#start").click(function() {
b.start();
});
$("#fail").click(function() {
$.confirm("确定认输吗?", function() {
b.stop();
});
});
$("#back").click(function() {
b.back();
});
},{"./role.js":2,"./score.js":3,"./win.js":4}],2:[function(require,module,exports){
module.exports = {
com: 2,
hum: 1,
empty: 0,
reverse: function(r) {
return r == 1 ? 2 : 1;
}
}
},{}],3:[function(require,module,exports){
/*
* 棋型表示
* 用一个6位数表示棋型,从高位到低位分别表示
* 连五,活四,眠四,活三,活二/眠三,活一/眠二, 眠一
*/
module.exports = {
ONE: 10,
TWO: 100,
THREE: 1000,
FOUR: 100000,
FIVE: 1000000,
BLOCKED_ONE: 1,
BLOCKED_TWO: 10,
BLOCKED_THREE: 100,
BLOCKED_FOUR: 10000
}
},{}],4:[function(require,module,exports){
var R = require("./role.js");
var isFive = function(board, p, role) {
var len = board.length;
var count = 1;
var reset = function() {
count = 1;
}
for(var i=p[1]+1;true;i++) {
if(i>=len) break;
var t = board[p[0]][i];
if(t !== role) break;
count ++;
}
for(var i=p[1]-1;true;i--) {
if(i<0) break;
var t = board[p[0]][i];
if(t !== role) break;
count ++;
}
if(count >= 5) return true;
//纵向
reset();
for(var i=p[0]+1;true;i++) {
if(i>=len) {
break;
}
var t = board[i][p[1]];
if(t !== role) break;
count ++;
}
for(var i=p[0]-1;true;i--) {
if(i<0) {
break;
}
var t = board[i][p[1]];
if(t !== role) break;
count ++;
}
if(count >= 5) return true;
// \\
reset();
for(var i=1;true;i++) {
var x = p[0]+i, y = p[1]+i;
if(x>=len || y>=len) {
break;
}
var t = board[x][y];
if(t !== role) break;
count ++;
}
for(var i=1;true;i++) {
var x = p[0]-i, y = p[1]-i;
if(x<0||y<0) {
break;
}
var t = board[x][y];
if(t !== role) break;
count ++;
}
if(count >= 5) return true;
// \/
reset();
for(var i=1; true;i++) {
var x = p[0]+i, y = p[1]-i;
if(x<0||y<0||x>=len||y>=len) {
break;
}
var t = board[x][y];
if(t !== role) break;
count ++;
}
for(var i=1;true;i++) {
var x = p[0]-i, y = p[1]+i;
if(x<0||y<0||x>=len||y>=len) {
break;
}
var t = board[x][y];
if(t !== role) break;
count ++;
}
if(count >= 5) return true;
return false;
}
var w = function(board) {
for(var i=0;i<board.length;i++) {
for(var j=0;j<board[i].length;j++) {
var t = board[i][j];
if(t !== R.empty) {
var r = isFive(board, [i, j], t);
if(r) return t;
}
}
}
return false;
}
module.exports = w;
},{"./role.js":2}]},{},[1]);