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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [threats.cpp] - Blame information for rev 4

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

Line No. Rev Author Line
1 4 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
//#include <stdio.h>
28
 
29
/* Bits per threat level */
30
#define BITS_PER_THREAT 6
31
 
32
 
33
typedef struct {
34
        int threat[2];
35
        PIECE turn[2];
36
} Line;
37
typedef struct{
38
        int data[MAX_CONNECT_K + 1][2];
39
}threat_count_array;
40
 
41
static AIWEIGHT threat_bits(int threat, PIECE type, Board *b)
42
/* Bit pack the threat value */
43
{
44
        if (threat < 1)
45
                return 0;
46
 
47
        /* No extra value for building sequences over k - p unless it is
48
           enough to win */
49
        if (b->turn == type && connect_k - threat <= b->moves_left)
50
                threat = connect_k - place_p + 1;
51
        else if (threat >= connect_k - place_p)
52
                threat = connect_k - place_p - (type == b->turn);
53
 
54
        return 1 << ((threat - 1) * BITS_PER_THREAT);
55
}
56
 
57
static void threat_mark(int i, int threat, PIECE type,Board *b,Line *line)
58
{
59
        int j, index = 0;
60
 
61
        if (threat <= 0)
62
                return;
63
 
64
        /* No extra value for building sequences over k - p unless it is
65
           enough to win */
66
        if (b->turn == type && connect_k - threat <= b->moves_left)
67
                threat = connect_k - place_p + 1;
68
        else if (threat >= connect_k - place_p)
69
                threat = connect_k - place_p - (type == b->turn);
70
 
71
        /* Do not mark if this threat is dominated by a preceeding threat;
72
           Likewise supress any smaller threats */
73
        for (j = i; j >= 0 && j > i - connect_k; j--)
74
                if (line[j].threat[0] > threat)
75
                        return;
76
                else if (line[j].threat[0] < threat) {
77
                        line[j].threat[0] = 0;
78
                        line[j].threat[1] = 0;
79
                }
80
 
81
        /* Store up to two threats per tile in the line */
82
        if (line[i].threat[index])
83
                index++;
84
        line[i].threat[index] = threat;
85
        line[i].turn[index] = type;
86
}
87
 
88
int threat_window(int x, int y, int dx, int dy,
89
                         PIECE *ptype, int *pdouble,Board *b)
90
{
91
        int minimum, maximum, count = 0;
92
        PIECE p, type = PIECE_NONE;
93
 
94
        /* Check if this tile is empty */
95
        p = piece_at(b, x, y);
96
        if (!piece_empty(p))
97
                return 0;
98
 
99
        /* Push forward the maximum and find the window type */
100
        #pragma unroll
101
        for (maximum = 1; maximum < connect_k; maximum++) {
102
                p = piece_at(b, x + dx * maximum, y + dy * maximum);
103
                if (p == PIECE_ERROR)
104
                        break;
105
                if (!piece_empty(p)) {
106
                        if (type == PIECE_NONE)
107
                                type = p;
108
                        else if (type != p)
109
                                break;
110
                        count++;
111
                }
112
        }
113
        maximum--;
114
 
115
        /* Try to push the entire window back */
116
        #pragma unroll
117
        for (minimum = -1; minimum > -connect_k; minimum--) {
118
                p = piece_at(b, x + dx * minimum, y + dy * minimum);
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 (maximum - minimum > connect_k - 1) {
126
                        p = piece_at(b, x + dx * maximum, y + dy * maximum);
127
                        if (p == type)
128
                                count--;
129
                        maximum--;
130
                }
131
                count++;
132
        }
133
        minimum++;
134
 
135
        /* Push back minimum if we haven't formed a complete window, this window
136
           can't be a double */
137
        if (maximum - minimum < connect_k - 1) {
138
                for (minimum--; minimum > maximum - connect_k; minimum--) {
139
                        p = piece_at(b, x + dx * minimum, y + dy * minimum);
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
                minimum++;
152
        }
153
 
154
        *ptype = type;
155
        if (maximum - minimum >= connect_k - 1)
156
                return count;
157
        return 0;
158
}
159
 
160
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,AIMoves *moves,int k)
161
{
162
 
163
        //#pragma read_write_ports threat_counts.data combined 2
164
        //#pragma internal_blockram threat_counts
165
        //#pragma no_memory_analysis threat_counts
166
 
167
        //#pragma read_write_ports b.data combined 2
168
        #pragma internal_blockram b
169
        #pragma internal_blockram bwrite
170
        //#pragma read_write_ports b.data separate 1 readonly 2 writeonly
171
        //#pragma no_memory_analysis b
172
        /* This is the line of threats currently being processed */
173
        Line line[board_size]={{1},{2}};
174
        #pragma internal_fast line
175
        //#pragma no_memory_analysis line
176
        /* Running tally of threats for both players */
177
        //static int threat_counts[MAX_CONNECT_K + 1][2];
178
        threat_count_array threat_counts={{0}};
179
        #pragma internal_fast threat_counts
180
        //#pragma read_write_ports threat_counts.data combined 2
181
        //#pragma no_memory_analysis threat_counts
182
        if (k==1) board_copy(b, bwrite);
183
        int i;
184
        AIWEIGHT weight = 0;
185
        ///* Clear threat tallys */
186
        //for (i = 0; i < connect_k; i++) {
187
        //        threat_counts.data[i][0] = 1;
188
        //        threat_counts.data[i][1] = 1;
189
        //}
190
 
191
        /* Mark the maximum threat for each */
192
        for (i = 0; x >= 0 && x < board_size && y >= 0 && y < board_size; i++) {
193
                int count[2], tmp, double_threat = 1;
194
                PIECE type[2];
195
 
196
                count[0] = threat_window(x, y, dx, dy, type, &double_threat,bwrite);
197
                count[1] = threat_window(x, y, -dx, -dy, type + 1,
198
                                         &double_threat,bwrite);
199
                if (count[1] > count[0]) {
200
                        tmp = count[1];
201
                        count[1] = count[0];
202
                        count[0] = tmp;
203
                        tmp = type[1];
204
                        type[1] = type[0];
205
                        type[0] = tmp;
206
                }
207
                line[i].threat[0] = 0;
208
                line[i].threat[1] = 0;
209
                threat_mark(i, count[0], type[0],bwrite,&line[0]);
210
                if (double_threat)
211
                        threat_mark(i, count[1], type[1],bwrite,&line[0]);
212
                x += dx;
213
                y += dy;
214
        }
215
 
216
        /* Commit stored line values to the board */
217
        x -= dx;
218
        y -= dy;
219
        for (i--; x >= 0 && x < board_size && y >= 0 && y < board_size; i--) {
220
                AIWEIGHT bits[2];
221
                PIECE p;
222
 
223
                bits[0] = threat_bits(line[i].threat[0], line[i].turn[0],bwrite);
224
                bits[1] = threat_bits(line[i].threat[1], line[i].turn[1],bwrite);
225
                p = piece_at(bwrite, x, y);
226
                if (piece_empty(p) && line[i].threat[0]) {
227
                        threat_counts.data[line[i].threat[0]][line[i].turn[0] - 1]++;
228
                        if (line[i].threat[1])
229
                                threat_counts.data[line[i].threat[1]]
230
                                             [line[i].turn[1] - 1]++;
231
                        if (p >= PIECE_THREAT0)
232
                                place_threat(bwrite, x, y, p - PIECE_THREAT0 +
233
                                             bits[0] + bits[1]);
234
                        else
235
                                place_threat(bwrite, x, y, bits[0] + bits[1]);
236
                }
237
                if (bwrite->turn != line[i].turn[0])
238
                        bits[0] = -bits[0];
239
                if (bwrite->turn != line[i].turn[1])
240
                        bits[1] = -bits[1];
241
                weight += bits[0] + bits[1];
242
                x -= dx;
243
                y -= dy;
244
        }
245
        return weight;
246
}
247
 
248
/*AIMoves*/int ai_threats(Board *board,AIMove *move)
249
{
250
        //#pragma read_write_ports board.data combined 2
251
        #pragma internal_blockram board
252
        //#pragma no_memory_analysis board
253
 
254
        //#pragma internal_blockram move
255
        //#pragma no_memory_analysis move
256
 
257
        /////////* All threat functions work on this board */
258
        /*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
259
        //#pragma read_write_ports b.data combined 2
260
        #pragma internal_blockram b
261
        //#pragma read_write_ports b.data separate 1 readonly 2 writeonly
262
        //#pragma no_memory_analysis b
263
        /*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
264
        //#pragma read_write_ports b.data combined 2
265
        #pragma internal_blockram bwrite
266
        //#pragma no_memory_analysis b
267
        /*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
268
        //#pragma read_write_ports moves.data combined 3
269
        #pragma internal_blockram moves
270
        //#pragma no_memory_analysis moves
271
 
272
        moves.len=0;
273
        //AIMoves moves;
274
        AIWEIGHT u_sum = 0;
275
        int i;
276
 
277
        //b = board_new();
278
        //Board b;
279
        board_copy(board, &b);
280
 
281
        /* Clear threat tallys */
282
        //for (i = 0; i < connect_k; i++) {
283
        //        threat_counts.data[i][0] = 0;
284
        //        threat_counts.data[i][1] = 0;
285
        //}
286
/*---------------------------------------------------------------------------*/
287
       // /* Horizontal lines */
288
       // for (i = 0; i < board_size; i++)
289
       //         u_sum += threat_line(0, i, 1, 0,&b);
290
 
291
       // /* Vertical lines */
292
       // for (i = 0; i < board_size; i++)
293
       //         u_sum += threat_line(i, 0, 0, 1,&b);
294
 
295
       // /* SE diagonals */
296
       // for (i = 0; i < board_size - connect_k + 1; i++)
297
       //         u_sum += threat_line(i, 0, 1, 1,&b);
298
       // for (i = 1; i < board_size - connect_k + 1; i++)
299
       //         u_sum += threat_line(0, i, 1, 1,&b);
300
 
301
       // /* SW diagonals */
302
       // for (i = connect_k - 1; i < board_size; i++)
303
       //         u_sum += threat_line(i, 0, -1, 1,&b);
304
       // for (i = 1; i < board_size - connect_k + 1; i++)
305
       //         u_sum += threat_line(board_size - 1, i, -1, 1,&b);
306
/*---------------------------------------------------------------------------*/
307
//rewritten for hardware
308
/*---------------------------------------------------------------------------*/
309
        int j;
310
        int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
311
        int k=0;
312
        for(j=0;j<6;j++){
313
                        switch(j){
314
                        case 0:
315
                                {
316
                                loop_begin=0;
317
                                loop_bound=board_size;
318
                                break;
319
                                }
320
                        case 1:
321
                                {
322
                                loop_begin=0;
323
                                loop_bound=board_size;
324
                                break;
325
                                }
326
                        case 2:
327
                                {
328
                                loop_begin=0;
329
                                loop_bound=board_size-connect_k+1;
330
                                break;
331
                                }
332
                        case 3:
333
                                {
334
                                loop_begin=1;
335
                                loop_bound=board_size-connect_k+1;
336
                                break;
337
                                }
338
                        case 4:
339
                                {
340
                                loop_begin=connect_k-1;
341
                                loop_bound=board_size;
342
                                break;
343
                                }
344
                        case 5:
345
                                {
346
                                loop_begin=1;
347
                                loop_bound=board_size-connect_k+1;
348
                                break;
349
                                }
350
                        default:{
351
                                break;
352
                                }
353
                        }
354
                        for (i = loop_begin; i < loop_bound; i++){
355
                                k++;
356
                                switch(j){
357
                                case 0:
358
                                        {
359
                                        arg1=0;
360
                                        arg2=i;
361
                                        arg3=1;
362
                                        arg4=0;
363
                                        break;
364
                                        }
365
                                case 1:
366
                                        {
367
                                        arg1=i;
368
                                        arg2=0;
369
                                        arg3=0;
370
                                        arg4=1;
371
                                        break;
372
                                        }
373
                                case 2:
374
                                        {
375
                                        arg1=i;
376
                                        arg2=0;
377
                                        arg3=1;
378
                                        arg4=1;
379
                                        break;
380
                                        }
381
                                case 3:
382
                                        {
383
                                        arg1=0;
384
                                        arg2=i;
385
                                        arg3=1;
386
                                        arg4=1;
387
                                        break;
388
                                        }
389
                                case 4:
390
                                        {
391
                                        arg1=i;
392
                                        arg2=0;
393
                                        arg3=-1;
394
                                        arg4=1;
395
                                        break;
396
                                        }
397
                                case 5:
398
                                        {
399
                                        arg1=board_size-1;
400
                                        arg2=i;
401
                                        arg3=-1;
402
                                        arg4=1;
403
                                        break;
404
                                        }
405
                                default:{
406
                                        break;
407
                                        }
408
                                }
409
 
410
 
411
                                u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,&moves,k);
412
                        }
413
        }
414
/*---------------------------------------------------------------------------*/
415
        //board_copy(&b,&b_marks);
416
        /*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves);
417
        moves.utility = u_sum;
418
        if (!aimoves_choose(&moves, move))
419
                return 0;
420
        else return 1;
421
        //board_free(b);
422
        //return moves;
423
        //return 0;
424
}
425
 
426
//void debug_counts(void)
427
//{
428
//        int i, sum = 0;
429
//
430
//        if (!b)
431
//                return;
432
//
433
//        g_debug("Threat counts (black, white):");
434
//        for (i = 1; i < connect_k; i++) {
435
//                g_debug("%d: %3d %3d", i, threat_counts[i][0],
436
//                        threat_counts[i][1]);
437
//                sum += threat_counts[i][0] * threat_bits(i, b->turn) -
438
//                       threat_counts[i][1] *
439
//                       threat_bits(i, other_player(b->turn));
440
//        }
441
//        if (sum > 0)
442
//                g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
443
//        else if (sum < 0)
444
//                g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
445
//        else
446
//                g_debug("Threat sum: 0");
447
//}
448
 
449
//static int threat_number(int player, int threat,threat_count_array threat_counts)
450
//{
451
//        return threat_counts.data[threat][player] / (connect_k - threat);
452
//}
453
 
454
//AIMoves *ai_priority(const Board *b)
455
//{
456
//        AIMoves *moves;
457
//        int i, j, stage[2] = {1, 1}, mask, bits;
458
//
459
//        moves = ai_threats(b);
460
//
461
//        /* Do not prioritize if we've won */
462
//        if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
463
//                moves->utility = AIW_WIN;
464
//                return moves;
465
//        }
466
//
467
//        /* Find the largest supported threat for each player */
468
//        for (i = 2; i < connect_k; i++) {
469
//                if (threat_number(0, i - 1) >= place_p &&
470
//                    threat_number(0, i) > place_p)
471
//                        stage[0] = i;
472
//                if (threat_number(1, i - 1) >= place_p &&
473
//                    threat_number(1, i) > place_p)
474
//                        stage[1] = i;
475
//        }
476
//
477
//        //if (opt_debug_stage)
478
//        //        g_debug("Stages %d/%d", stage[0], stage[1]);
479
//
480
//        /* Do not prioritize if we're losing */
481
//        if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
482
//                moves->utility = -stage[other_player(b->turn) - 1];
483
//                return moves;
484
//        }
485
//
486
//        /* Threats above the player's stage are no more valuable than the
487
//           stage */
488
//        bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
489
//        mask = bits - 1;
490
//        for (i = 0; i < moves->len; i++) {
491
//                AIWEIGHT w = moves->data[i].weight, w2;
492
//
493
//                if (w < AIW_THREAT_MAX && w >= bits) {
494
//                        w2 = w & mask;
495
//                        w = w & ~mask;
496
//                        for (j = stage[b->turn - 1];
497
//                             w && j < connect_k - place_p + 1; j++) {
498
//                                w = w >> BITS_PER_THREAT;
499
//                                w2 += w & mask;
500
//                        }
501
//                        moves->data[i].weight = w2;
502
//                }
503
//        }
504
//
505
//        /* Stage determines weight */
506
//        moves->utility = stage[b->turn - 1];
507
//        return moves;
508
//}
509
/*AIMoves*/ void ai_marks(Board *b, PIECE minimum,AIMoves *moves)
510
{
511
        //#pragma read_write_ports b.data combined 2
512
        #pragma internal_blockram b
513
        //#pragma no_memory_analysis b
514
        //AIMoves *moves = aimoves_new();
515
        //AIMoves moves;
516
        //AIMoves moves[361];
517
        AIMove move;
518
        PIECE p;
519
        for (move.y = 0; move.y < board_size; move.y++)
520
                for (move.x = 0; move.x < board_size; move.x++)
521
                        if ((p = piece_at(b, move.x, move.y)) >= minimum) {
522
                                move.weight = p - PIECE_THREAT0;
523
                                aimoves_set(moves, &move);
524
                        }
525
        //return moves;
526
}
527
 
528
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
529
{
530
        int dx, dy, count;
531
        #pragma bitsize dx 4
532
        #pragma bitsize dy 4
533
        PIECE p;
534
 
535
        if (!piece_empty(piece_at(b, x, y)))
536
                return FALSE;
537
        for (dy = -1; dy < 2; dy++)
538
                for (dx = -1; dx < 2; dx++) {
539
                        if (!dx && !dy)
540
                                continue;
541
                        count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
542
                        if (count - 1 < dist && p != PIECE_NONE)
543
                                return TRUE;
544
                }
545
        return FALSE;
546
}
547
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random)
548
{
549
        //AIMoves *moves;
550
        AIMove move;
551
 
552
        move.weight = AIW_NONE;
553
        //moves = aimoves_new();
554
        for (move.y = 0; move.y < board_size; move.y++)
555
                for (move.x = 0; move.x < board_size; move.x++)
556
                        if (is_adjacent(b, move.x, move.y, dist))
557
                                aimoves_append(moves, &move);
558
        //aimoves_shuffle(moves,current_random);
559
        //return moves;
560
}
561
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
562
{
563
        //#pragma read_write_ports board.data combined 2
564
        #pragma internal_blockram b
565
        //#pragma no_memory_analysis b
566
 
567
        /*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
568
        //#pragma read_write_ports moves.data combined 3
569
        #pragma internal_blockram moves
570
        //#pragma no_memory_analysis moves
571
        //#pragma read_write_ports moves.data combined 3
572
        //#pragma internal_blockram moves
573
        //#pragma no_memory_analysis moves
574
        //AIMove move;
575
        //AIMoves *moves;
576
        moves.len=0;
577
        /* Get all open tiles adjacent to any piece */
578
        /*moves =*/ enum_adjacent(b, 1,&moves,current_random);
579
        if (moves.len){
580
                aimoves_choose(&moves, move);
581
 
582
                return ;//moves;
583
        }
584
        /* Play in the middle if there are no open adjacent tiles */
585
        move->x = board_size / 2;
586
        move->y = board_size / 2;
587
        move->weight = AIW_NONE;
588
        //aimoves_append(&moves, move);
589
        //aimoves_choose(&moves, move);
590
        //return moves;
591
}

powered by: WebSVN 2.1.0

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