OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [ai/] [threats.c] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 sumanta.ch
 
2
/*
3
 
4
connectk -- a program to play the connect-k family of games
5
Copyright (C) 2007 Michael Levin
6
 
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License
9
as published by the Free Software Foundation; either version 2
10
of the License, or (at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 
21
*/
22
 
23
#include "config.h"
24
#include <math.h>
25
#include <glib.h>
26
#include "../shared.h"
27
 
28
/* Bits per threat level */
29
#define BITS_PER_THREAT 6
30
 
31
/* All threat functions work on this board */
32
static Board *b = NULL;
33
 
34
/* This is the line of threats currently being processed */
35
static struct {
36
        int threat[2];
37
        PIECE turn[2];
38
} line[MAX_BOARD_SIZE];
39
 
40
/* Running tally of threats for both players */
41
static int threat_counts[MAX_CONNECT_K + 1][2];
42
 
43
static AIWEIGHT threat_bits(int threat, PIECE type)
44
/* Bit pack the threat value */
45
{
46
        if (threat < 1)
47
                return 0;
48
 
49
        /* No extra value for building sequences over k - p unless it is
50
           enough to win */
51
        if (b->turn == type && connect_k - threat <= b->moves_left)
52
                threat = connect_k - place_p + 1;
53
        else if (threat >= connect_k - place_p)
54
                threat = connect_k - place_p - (type == b->turn);
55
 
56
        return 1 << ((threat - 1) * BITS_PER_THREAT);
57
}
58
 
59
static void threat_mark(int i, int threat, PIECE type)
60
{
61
        int j, index = 0;
62
 
63
        if (threat <= 0)
64
                return;
65
 
66
        /* No extra value for building sequences over k - p unless it is
67
           enough to win */
68
        if (b->turn == type && connect_k - threat <= b->moves_left)
69
                threat = connect_k - place_p + 1;
70
        else if (threat >= connect_k - place_p)
71
                threat = connect_k - place_p - (type == b->turn);
72
 
73
        /* Do not mark if this threat is dominated by a preceeding threat;
74
           Likewise supress any smaller threats */
75
        for (j = i; j >= 0 && j > i - connect_k; j--)
76
                if (line[j].threat[0] > threat)
77
                        return;
78
                else if (line[j].threat[0] < threat) {
79
                        line[j].threat[0] = 0;
80
                        line[j].threat[1] = 0;
81
                }
82
 
83
        /* Store up to two threats per tile in the line */
84
        if (line[i].threat[index])
85
                index++;
86
        line[i].threat[index] = threat;
87
        line[i].turn[index] = type;
88
}
89
 
90
static int threat_window(int x, int y, int dx, int dy,
91
                         PIECE *ptype, int *pdouble)
92
{
93
        int min, max, count = 0;
94
        PIECE p, type = PIECE_NONE;
95
 
96
        /* Check if this tile is empty */
97
        p = piece_at(b, x, y);
98
        if (!piece_empty(p))
99
                return 0;
100
 
101
        /* Push forward the max and find the window type */
102
        for (max = 1; max < connect_k; max++) {
103
                p = piece_at(b, x + dx * max, y + dy * max);
104
                if (p == PIECE_ERROR)
105
                        break;
106
                if (!piece_empty(p)) {
107
                        if (type == PIECE_NONE)
108
                                type = p;
109
                        else if (type != p)
110
                                break;
111
                        count++;
112
                }
113
        }
114
        max--;
115
 
116
        /* Try to push the entire window back */
117
        for (min = -1; min > -connect_k; min--) {
118
                p = piece_at(b, x + dx * min, y + dy * min);
119
                if (p == PIECE_ERROR || piece_empty(p))
120
                        break;
121
                if (type == PIECE_NONE)
122
                        type = p;
123
                else if (type != p)
124
                        break;
125
                if (max - min > connect_k - 1) {
126
                        p = piece_at(b, x + dx * max, y + dy * max);
127
                        if (p == type)
128
                                count--;
129
                        max--;
130
                }
131
                count++;
132
        }
133
        min++;
134
 
135
        /* Push back min if we haven't formed a complete window, this window
136
           can't be a double */
137
        if (max - min < connect_k - 1) {
138
                for (min--; min > max - connect_k; min--) {
139
                        p = piece_at(b, x + dx * min, y + dy * min);
140
                        if (p == PIECE_ERROR)
141
                                break;
142
                        if (!piece_empty(p)) {
143
                                if (type != p)
144
                                        break;
145
                                if (type == PIECE_NONE)
146
                                        type = p;
147
                                count++;
148
                        }
149
                }
150
                *pdouble = 0;
151
                min++;
152
        }
153
 
154
        *ptype = type;
155
        if (max - min >= connect_k - 1)
156
                return count;
157
        return 0;
158
}
159
 
160
static AIWEIGHT threat_line(int x, int y, int dx, int dy)
161
{
162
        int i;
163
        AIWEIGHT weight = 0;
164
 
165
        /* Mark the maximum threat for each */
166
        for (i = 0; x >= 0 && x < board_size && y >= 0 && y < board_size; i++) {
167
                int count[2], tmp, double_threat = 1;
168
                PIECE type[2];
169
 
170
                count[0] = threat_window(x, y, dx, dy, type, &double_threat);
171
                count[1] = threat_window(x, y, -dx, -dy, type + 1,
172
                                         &double_threat);
173
                if (count[1] > count[0]) {
174
                        tmp = count[1];
175
                        count[1] = count[0];
176
                        count[0] = tmp;
177
                        tmp = type[1];
178
                        type[1] = type[0];
179
                        type[0] = tmp;
180
                }
181
                line[i].threat[0] = 0;
182
                line[i].threat[1] = 0;
183
                threat_mark(i, count[0], type[0]);
184
                if (double_threat)
185
                        threat_mark(i, count[1], type[1]);
186
                x += dx;
187
                y += dy;
188
        }
189
 
190
        /* Commit stored line values to the board */
191
        x -= dx;
192
        y -= dy;
193
        for (i--; x >= 0 && x < board_size && y >= 0 && y < board_size; i--) {
194
                AIWEIGHT bits[2];
195
                PIECE p;
196
 
197
                bits[0] = threat_bits(line[i].threat[0], line[i].turn[0]);
198
                bits[1] = threat_bits(line[i].threat[1], line[i].turn[1]);
199
                p = piece_at(b, x, y);
200
                if (piece_empty(p) && line[i].threat[0]) {
201
                        threat_counts[line[i].threat[0]][line[i].turn[0] - 1]++;
202
                        if (line[i].threat[1])
203
                                threat_counts[line[i].threat[1]]
204
                                             [line[i].turn[1] - 1]++;
205
                        if (p >= PIECE_THREAT0)
206
                                place_threat(b, x, y, p - PIECE_THREAT0 +
207
                                             bits[0] + bits[1]);
208
                        else
209
                                place_threat(b, x, y, bits[0] + bits[1]);
210
                }
211
                if (b->turn != line[i].turn[0])
212
                        bits[0] = -bits[0];
213
                if (b->turn != line[i].turn[1])
214
                        bits[1] = -bits[1];
215
                weight += bits[0] + bits[1];
216
                x -= dx;
217
                y -= dy;
218
        }
219
 
220
        return weight;
221
}
222
 
223
AIMoves *ai_threats(const Board *original)
224
{
225
        AIMoves *moves;
226
        AIWEIGHT u_sum = 0;
227
        int i;
228
 
229
        b = board_new();
230
        board_copy(original, b);
231
 
232
        /* Clear threat tallys */
233
        for (i = 0; i < connect_k; i++) {
234
                threat_counts[i][0] = 0;
235
                threat_counts[i][1] = 0;
236
        }
237
 
238
        /* Horizontal lines */
239
        for (i = 0; i < board_size; i++)
240
                u_sum += threat_line(0, i, 1, 0);
241
 
242
        /* Vertical lines */
243
        for (i = 0; i < board_size; i++)
244
                u_sum += threat_line(i, 0, 0, 1);
245
 
246
        /* SE diagonals */
247
        for (i = 0; i < board_size - connect_k + 1; i++)
248
                u_sum += threat_line(i, 0, 1, 1);
249
        for (i = 1; i < board_size - connect_k + 1; i++)
250
                u_sum += threat_line(0, i, 1, 1);
251
 
252
        /* SW diagonals */
253
        for (i = connect_k - 1; i < board_size; i++)
254
                u_sum += threat_line(i, 0, -1, 1);
255
        for (i = 1; i < board_size - connect_k + 1; i++)
256
                u_sum += threat_line(board_size - 1, i, -1, 1);
257
 
258
        moves = ai_marks(b, PIECE_THREAT(1));
259
        moves->utility = u_sum;
260
        board_free(b);
261
        return moves;
262
}
263
 
264
void debug_counts(void)
265
{
266
        int i, sum = 0;
267
 
268
        if (!b)
269
                return;
270
 
271
        g_debug("Threat counts (black, white):");
272
        for (i = 1; i < connect_k; i++) {
273
                g_debug("%d: %3d %3d", i, threat_counts[i][0],
274
                        threat_counts[i][1]);
275
                sum += threat_counts[i][0] * threat_bits(i, b->turn) -
276
                       threat_counts[i][1] *
277
                       threat_bits(i, other_player(b->turn));
278
        }
279
        if (sum > 0)
280
                g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
281
        else if (sum < 0)
282
                g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
283
        else
284
                g_debug("Threat sum: 0");
285
}
286
 
287
static int threat_number(int player, int threat)
288
{
289
        return threat_counts[threat][player] / (connect_k - threat);
290
}
291
 
292
AIMoves *ai_priority(const Board *b)
293
{
294
        AIMoves *moves;
295
        int i, j, stage[2] = {1, 1}, mask, bits;
296
 
297
        moves = ai_threats(b);
298
 
299
        /* Do not prioritize if we've won */
300
        if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
301
                moves->utility = AIW_WIN;
302
                return moves;
303
        }
304
 
305
        /* Find the largest supported threat for each player */
306
        for (i = 2; i < connect_k; i++) {
307
                if (threat_number(0, i - 1) >= place_p &&
308
                    threat_number(0, i) > place_p)
309
                        stage[0] = i;
310
                if (threat_number(1, i - 1) >= place_p &&
311
                    threat_number(1, i) > place_p)
312
                        stage[1] = i;
313
        }
314
 
315
        if (opt_debug_stage)
316
                g_debug("Stages %d/%d", stage[0], stage[1]);
317
 
318
        /* Do not prioritize if we're losing */
319
        if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
320
                moves->utility = -stage[other_player(b->turn) - 1];
321
                return moves;
322
        }
323
 
324
        /* Threats above the player's stage are no more valuable than the
325
           stage */
326
        bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
327
        mask = bits - 1;
328
        for (i = 0; i < moves->len; i++) {
329
                AIWEIGHT w = moves->data[i].weight, w2;
330
 
331
                if (w < AIW_THREAT_MAX && w >= bits) {
332
                        w2 = w & mask;
333
                        w = w & ~mask;
334
                        for (j = stage[b->turn - 1];
335
                             w && j < connect_k - place_p + 1; j++) {
336
                                w = w >> BITS_PER_THREAT;
337
                                w2 += w & mask;
338
                        }
339
                        moves->data[i].weight = w2;
340
                }
341
        }
342
 
343
        /* Stage determines weight */
344
        moves->utility = stage[b->turn - 1];
345
        return moves;
346
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.