@wsl233/dsh-gomoku 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ /* 五子棋 - 棋盘核心逻辑
2
+ * 棋盘状态、落子、撤销、四方向胜负判定。
3
+ * 无禁手自由规则:任意方向连成五子或以上即获胜。
4
+ */
5
+ window.Gomoku = window.Gomoku || {};
6
+ (function (G) {
7
+ 'use strict';
8
+
9
+ var SIZE = 19;
10
+ var BLACK = 1;
11
+ var WHITE = 2;
12
+ var DRAW = 3;
13
+ var DIRS = [[1, 0], [0, 1], [1, 1], [1, -1]];
14
+
15
+ function idx(x, y) {
16
+ return y * SIZE + x;
17
+ }
18
+
19
+ function inBounds(x, y) {
20
+ return x >= 0 && x < SIZE && y >= 0 && y < SIZE;
21
+ }
22
+
23
+ /* 判断 (x, y) 上的棋子是否构成五连(或更长)。 */
24
+ function hasFive(board, x, y, color) {
25
+ for (var d = 0; d < 4; d++) {
26
+ var dx = DIRS[d][0];
27
+ var dy = DIRS[d][1];
28
+ var count = 1;
29
+ for (var s = -1; s <= 1; s += 2) {
30
+ var nx = x + dx * s;
31
+ var ny = y + dy * s;
32
+ while (inBounds(nx, ny) && board[idx(nx, ny)] === color) {
33
+ count++;
34
+ nx += dx * s;
35
+ ny += dy * s;
36
+ }
37
+ }
38
+ if (count >= 5) return true;
39
+ }
40
+ return false;
41
+ }
42
+
43
+ /* 返回构成胜利的整条连线(连六、连七会完整返回),否则返回 null。 */
44
+ function winLineAt(board, x, y, color) {
45
+ for (var d = 0; d < 4; d++) {
46
+ var dx = DIRS[d][0];
47
+ var dy = DIRS[d][1];
48
+ var line = [[x, y]];
49
+ for (var s = -1; s <= 1; s += 2) {
50
+ var nx = x + dx * s;
51
+ var ny = y + dy * s;
52
+ while (inBounds(nx, ny) && board[idx(nx, ny)] === color) {
53
+ line.push([nx, ny]);
54
+ nx += dx * s;
55
+ ny += dy * s;
56
+ }
57
+ }
58
+ if (line.length >= 5) {
59
+ line.sort(function (a, b) {
60
+ return (a[0] * dx + a[1] * dy) - (b[0] * dx + b[1] * dy);
61
+ });
62
+ var out = [];
63
+ for (var i = 0; i < line.length; i++) {
64
+ out.push({ x: line[i][0], y: line[i][1] });
65
+ }
66
+ return out;
67
+ }
68
+ }
69
+ return null;
70
+ }
71
+
72
+ function Game() {
73
+ this.reset();
74
+ }
75
+
76
+ Game.prototype.reset = function () {
77
+ this.board = new Uint8Array(SIZE * SIZE);
78
+ this.moves = [];
79
+ this.turn = BLACK;
80
+ this.winner = 0; // 0=进行中, 1=黑胜, 2=白胜, 3=和棋
81
+ this.winLine = null;
82
+ };
83
+
84
+ /* 落子。成功返回 {x, y, color},非法或已终局返回 null。 */
85
+ Game.prototype.place = function (x, y) {
86
+ if (this.winner) return null;
87
+ if (!inBounds(x, y)) return null;
88
+ var i = idx(x, y);
89
+ if (this.board[i] !== 0) return null;
90
+
91
+ var color = this.turn;
92
+ this.board[i] = color;
93
+ var move = { x: x, y: y, color: color };
94
+ this.moves.push(move);
95
+
96
+ var line = winLineAt(this.board, x, y, color);
97
+ if (line) {
98
+ this.winner = color;
99
+ this.winLine = line;
100
+ } else if (this.moves.length >= SIZE * SIZE) {
101
+ this.winner = DRAW;
102
+ } else {
103
+ this.turn = color === BLACK ? WHITE : BLACK;
104
+ }
105
+ return move;
106
+ };
107
+
108
+ /* 撤销最后一步。返回被撤销的走法,空棋盘返回 null。 */
109
+ Game.prototype.undo = function () {
110
+ var m = this.moves.pop();
111
+ if (!m) return null;
112
+ this.board[idx(m.x, m.y)] = 0;
113
+ this.winner = 0;
114
+ this.winLine = null;
115
+ this.turn = m.color;
116
+ return m;
117
+ };
118
+
119
+ Game.prototype.lastMove = function () {
120
+ return this.moves.length ? this.moves[this.moves.length - 1] : null;
121
+ };
122
+
123
+ /* 重放前 count 步,返回新的棋盘数组(不修改当前对局)。 */
124
+ function replayBoard(moves, count) {
125
+ var b = new Uint8Array(SIZE * SIZE);
126
+ var n = Math.min(count, moves.length);
127
+ for (var i = 0; i < n; i++) {
128
+ b[idx(moves[i].x, moves[i].y)] = moves[i].color;
129
+ }
130
+ return b;
131
+ }
132
+
133
+ /* 第 ply 手之后轮到谁走(ply = 已落子数)。 */
134
+ function moverAt(ply) {
135
+ return ply % 2 === 0 ? BLACK : WHITE;
136
+ }
137
+
138
+ function coordText(x, y) {
139
+ return '(' + (x + 1) + ', ' + (y + 1) + ')';
140
+ }
141
+
142
+ function colorName(color) {
143
+ if (color === BLACK) return '黑方';
144
+ if (color === WHITE) return '白方';
145
+ return '';
146
+ }
147
+
148
+ G.Board = {
149
+ SIZE: SIZE,
150
+ BLACK: BLACK,
151
+ WHITE: WHITE,
152
+ DRAW: DRAW,
153
+ DIRS: DIRS,
154
+ idx: idx,
155
+ inBounds: inBounds,
156
+ hasFive: hasFive,
157
+ winLineAt: winLineAt,
158
+ Game: Game,
159
+ replayBoard: replayBoard,
160
+ moverAt: moverAt,
161
+ coordText: coordText,
162
+ colorName: colorName
163
+ };
164
+ })(window.Gomoku);
@@ -0,0 +1,242 @@
1
+ /* 五子棋 - 棋型评估
2
+ * 对每条线做棋型模式匹配,并通过 make/unmake 增量维护双方总分。
3
+ * 评估值统一约定为「黑方分 - 白方分」。
4
+ */
5
+ window.Gomoku = window.Gomoku || {};
6
+ (function (G) {
7
+ 'use strict';
8
+
9
+ var SIZE = 19;
10
+ var DIRS = [[1, 0], [0, 1], [1, 1], [1, -1]];
11
+
12
+ /* 棋型分值:从高到低。字符串中 1=己方, 0=空, 2=对方或边界。 */
13
+ var PATTERNS = [
14
+ ['11111', 10000000], // 连五
15
+ ['011110', 1000000], // 活四
16
+ ['11110', 100000], // 冲四 / 眠四
17
+ ['01111', 100000],
18
+ ['11011', 100000],
19
+ ['10111', 100000],
20
+ ['11101', 100000],
21
+ ['01110', 80000], // 活三
22
+ ['010110', 80000], // 跳活三
23
+ ['011010', 80000],
24
+ ['21110', 5000], // 眠三
25
+ ['01112', 5000],
26
+ ['10011', 5000], // 跳眠三
27
+ ['11001', 5000],
28
+ ['10101', 5000],
29
+ ['001100', 800], // 活二
30
+ ['01010', 800],
31
+ ['010010', 800],
32
+ ['211000', 150], // 眠二
33
+ ['000112', 150],
34
+ ['010100', 150],
35
+ ['001010', 150],
36
+ ['00100', 50] // 单子
37
+ ];
38
+
39
+ /* 胜率映射温度:分值每 T 对应 sigmoid 的一个单位。
40
+ * 棋型分跨度很大(活三 8 万、活四 100 万),T 取 10 万可避免一个活三就显示 100%。 */
41
+ var WIN_RATE_T = 100000;
42
+
43
+ function scoreString(str) {
44
+ var total = 0;
45
+ for (var p = 0; p < PATTERNS.length; p++) {
46
+ var pat = PATTERNS[p][0];
47
+ var val = PATTERNS[p][1];
48
+ var from = 0;
49
+ var i;
50
+ while ((i = str.indexOf(pat, from)) !== -1) {
51
+ total += val;
52
+ from = i + 1;
53
+ }
54
+ }
55
+ return total;
56
+ }
57
+
58
+ /* 提取经过 (x,y)、方向 (dx,dy) 的整条线,同时生成双方视角的字符串。 */
59
+ function lineStrings(board, x, y, dx, dy) {
60
+ var sx = x;
61
+ var sy = y;
62
+ while (sx - dx >= 0 && sx - dx < SIZE && sy - dy >= 0 && sy - dy < SIZE) {
63
+ sx -= dx;
64
+ sy -= dy;
65
+ }
66
+ var a = '2'; // 黑方视角
67
+ var b = '2'; // 白方视角
68
+ var px = sx;
69
+ var py = sy;
70
+ while (px >= 0 && px < SIZE && py >= 0 && py < SIZE) {
71
+ var v = board[py * SIZE + px];
72
+ if (v === 0) {
73
+ a += '0';
74
+ b += '0';
75
+ } else if (v === 1) {
76
+ a += '1';
77
+ b += '2';
78
+ } else {
79
+ a += '2';
80
+ b += '1';
81
+ }
82
+ px += dx;
83
+ py += dy;
84
+ }
85
+ return [a + '2', b + '2'];
86
+ }
87
+
88
+ /* 经过 (x,y) 的四条线对双方的得分。 */
89
+ function scoresAround(board, x, y) {
90
+ var s1 = 0;
91
+ var s2 = 0;
92
+ for (var d = 0; d < 4; d++) {
93
+ var pair = lineStrings(board, x, y, DIRS[d][0], DIRS[d][1]);
94
+ s1 += scoreString(pair[0]);
95
+ s2 += scoreString(pair[1]);
96
+ }
97
+ return [s1, s2];
98
+ }
99
+
100
+ /* 线编号:四种方向共 112 条线。 */
101
+ function lineKey(x, y, d) {
102
+ if (d === 0) return y; // 横线 0..18
103
+ if (d === 1) return 19 + x; // 竖线 19..37
104
+ if (d === 2) return 38 + (y - x + 18); // ↘ 线 38..74
105
+ return 75 + (x + y); // ↙ 线 75..111
106
+ }
107
+
108
+ var LINE_COUNT = 112;
109
+ var EMPTY_LINE_SCORE = 0; // 全空线的棋型分为 0(没有 "00100" 之类的单子)
110
+
111
+ function Engine(board, cells) {
112
+ this.board = board;
113
+ this.scores = [0, 0, 0]; // scores[1] 黑, scores[2] 白
114
+ this.lineScores = [new Float64Array(LINE_COUNT), new Float64Array(LINE_COUNT)];
115
+ this.cellX = new Int8Array(SIZE * SIZE);
116
+ this.cellY = new Int8Array(SIZE * SIZE);
117
+ this.cellCount = 0;
118
+ this.depth = 0;
119
+ this.records = [];
120
+
121
+ if (cells) {
122
+ var n = Math.min(cells.length, SIZE * SIZE);
123
+ for (var i = 0; i < n; i++) {
124
+ this.cellX[i] = cells[i].x;
125
+ this.cellY[i] = cells[i].y;
126
+ }
127
+ this.cellCount = n;
128
+ }
129
+ this._initScores();
130
+ }
131
+
132
+ Engine.prototype._initScores = function () {
133
+ this.scores[1] = 0;
134
+ this.scores[2] = 0;
135
+ var self = this;
136
+ var board = this.board;
137
+
138
+ function addLine(sx, sy, dx, dy) {
139
+ var pair = lineStrings(board, sx, sy, dx, dy);
140
+ var key = lineKey(sx, sy, dx === 1 && dy === 0 ? 0 : (dx === 0 ? 1 : (dy === 1 ? 2 : 3)));
141
+ var sa = scoreString(pair[0]);
142
+ var sb = scoreString(pair[1]);
143
+ self.lineScores[0][key] = sa;
144
+ self.lineScores[1][key] = sb;
145
+ self.scores[1] += sa;
146
+ self.scores[2] += sb;
147
+ }
148
+
149
+ var i;
150
+ // 横线
151
+ for (i = 0; i < SIZE; i++) addLine(0, i, 1, 0);
152
+ // 竖线
153
+ for (i = 0; i < SIZE; i++) addLine(i, 0, 0, 1);
154
+ // ↘ 对角线:从上边出发,再从左边出发(跳过角点)
155
+ for (i = 0; i < SIZE; i++) addLine(i, 0, 1, 1);
156
+ for (i = 1; i < SIZE; i++) addLine(0, i, 1, 1);
157
+ // ↙ 对角线:从底边出发,再从左边出发(跳过角点)
158
+ for (i = 0; i < SIZE; i++) addLine(i, SIZE - 1, 1, -1);
159
+ for (i = 0; i < SIZE - 1; i++) addLine(0, i, 1, -1);
160
+ };
161
+
162
+ /* 落子并维护增量分数。 */
163
+ Engine.prototype.make = function (x, y, side) {
164
+ var board = this.board;
165
+ board[y * SIZE + x] = side;
166
+
167
+ var rec = this.records[this.depth];
168
+ if (!rec) {
169
+ rec = {
170
+ keys: new Uint8Array(8),
171
+ sides: new Uint8Array(8),
172
+ prev: new Float64Array(8)
173
+ };
174
+ this.records[this.depth] = rec;
175
+ }
176
+
177
+ var k = 0;
178
+ for (var d = 0; d < 4; d++) {
179
+ var key = lineKey(x, y, d);
180
+ var pair = lineStrings(board, x, y, DIRS[d][0], DIRS[d][1]);
181
+ var pairScore = [scoreString(pair[0]), scoreString(pair[1])];
182
+ for (var s = 1; s <= 2; s++) {
183
+ var arr = this.lineScores[s - 1];
184
+ var prev = arr[key];
185
+ var after = pairScore[s - 1];
186
+ arr[key] = after;
187
+ this.scores[s] += after - prev;
188
+ rec.keys[k] = key;
189
+ rec.sides[k] = s;
190
+ rec.prev[k] = prev;
191
+ k++;
192
+ }
193
+ }
194
+
195
+ this.cellX[this.cellCount] = x;
196
+ this.cellY[this.cellCount] = y;
197
+ this.cellCount++;
198
+ this.depth++;
199
+ };
200
+
201
+ /* 撤销落子,恢复分数。 */
202
+ Engine.prototype.unmake = function (x, y) {
203
+ this.board[y * SIZE + x] = 0;
204
+ this.depth--;
205
+ var rec = this.records[this.depth];
206
+ for (var k = 7; k >= 0; k--) {
207
+ var s = rec.sides[k];
208
+ var key = rec.keys[k];
209
+ var arr = this.lineScores[s - 1];
210
+ this.scores[s] -= (arr[key] - rec.prev[k]);
211
+ arr[key] = rec.prev[k];
212
+ }
213
+ this.cellCount--;
214
+ };
215
+
216
+ /* 黑方视角的静态评估值。 */
217
+ Engine.prototype.value = function () {
218
+ return this.scores[1] - this.scores[2];
219
+ };
220
+
221
+ /* 黑方视角分值 → 黑方胜率(百分比)。 */
222
+ function winRateFromBlackScore(score) {
223
+ if (score >= 1e8) return 100;
224
+ if (score <= -1e8) return 0;
225
+ var p = 100 / (1 + Math.exp(-score / WIN_RATE_T));
226
+ if (p < 0.5) return 0.5;
227
+ if (p > 99.5) return 99.5;
228
+ return p;
229
+ }
230
+
231
+ G.Evaluate = {
232
+ PATTERNS: PATTERNS,
233
+ WIN_RATE_T: WIN_RATE_T,
234
+ scoreString: scoreString,
235
+ lineStrings: lineStrings,
236
+ scoresAround: scoresAround,
237
+ lineKey: lineKey,
238
+ LINE_COUNT: LINE_COUNT,
239
+ Engine: Engine,
240
+ winRateFromBlackScore: winRateFromBlackScore
241
+ };
242
+ })(window.Gomoku);