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 11

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 7 sumanta.ch
//#include <iostream>
27 4 sumanta.ch
#include "./shared.h"
28 7 sumanta.ch
//#include "./q.hpp"
29
#include "pico.h"
30 4 sumanta.ch
//#include <stdio.h>
31
 
32
/* Bits per threat level */
33
#define BITS_PER_THREAT 6
34
 
35 7 sumanta.ch
//FIFO(x,int);
36
//#pragma fifo_length x 24
37
//#pragma no_inter_loop_stream_analysis pico_stream_input_x
38
//#pragma no_inter_loop_stream_analysis pico_stream_output_x
39
//#pragma no_inter_task_stream_analysis pico_stream_input_x
40
//#pragma no_inter_task_stream_analysis pico_stream_output_x
41
/*--------------------------------------------------------------------*/
42
#ifdef PICO_SYNTH
43
#define Q_ASSERT(_cond, _msg)
44
//#include <iostream>
45
//#include "pico.h"
46
//#include "q.hpp"
47
//#include "./shared.h"
48
using namespace std;
49
#else
50
/* not synthesizable */
51
#include <iostream>
52
#include <sstream>
53
#include <string>
54
#include <assert.h>
55 4 sumanta.ch
 
56 7 sumanta.ch
static void debug_assert (bool cond, char * msg) {
57
        if (!cond) {
58
                printf("assert failed: %s\n", msg);
59
                assert(0);
60
        }
61
}
62 4 sumanta.ch
 
63 7 sumanta.ch
#define Q_ASSERT(_cond, _msg) debug_assert(_cond, _msg)
64
#endif
65
#define max_size 361
66
#define ptr_bw 32
67
//FIFO(queue,AIMove);
68
//#pragma no_inter_loop_stream_analysis pico_stream_input_queue
69
//#pragma no_inter_loop_stream_analysis pico_stream_output_queue
70
//#pragma no_inter_task_stream_analysis pico_stream_input_queue
71
//#pragma no_inter_task_stream_analysis pico_stream_output_queue
72
 
73
//FIFO(queue,AIMove);
74
//FIFO_INTERFACE(queue,AIMove);
75
//#pragma fifo_length pico_stream_input_queue 800
76
//#pragma fifo_length pico_stream_output_queue 800
77
//#pragma read_write_port queue separate
78
//#pragma bandwidth pico_stream_input_queue 1
79
//#pragma bandwidth pico_stream_output_queue 1
80
//////template <class tp=AIMove, int max_size=128, int ptr_bw=32>
81
////
82
////  /* pop front of queue, returning the front data */
83
////  /* q is corrupted if pop when empty */
84
////  AIMove q::pop (){
85
////    /* assert that before pop, queue is not empty (underflow check) */
86
////    Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
87
////                    "queue underflowed");
88
////    AIMove d = pico_stream_input_queue();
89
////    //cout <<"pop: "<<head<<":"<<tail<<":"<<wrapped<<endl;
90
////    if (head == max_size-1) {
91
////      head = 0;
92
////      wrapped = false;
93
////    } else {
94
////      head = head + 1;
95
////    }
96
////    return d;
97
////  }
98
////
99
////  /* push data into back of queue */
100
////  /* q is corrupted if push when full */
101
////   void q::push (AIMove d){
102
////    pico_stream_output_queue(d);
103
////    if (tail == max_size-1) {
104
////      tail = 0;
105
////      wrapped = true;
106
////    } else {
107
////      tail = tail + 1;
108
////    }
109
////    /* assert that after push, queue is not empty (overflow check) */
110
////    Q_ASSERT((!wrapped && (head < tail)) || (wrapped && (head >= tail)),
111
////                    "Queue overflowed") ;
112
////    //cout <<"push: "<<head<<":"<<tail<<":"<<wrapped<<endl;
113
////  }
114
////
115
////  /* return current size of the queue */
116
////  int q::size (){
117
////    if (wrapped) {
118
////      return (max_size - head) + (tail - 0);
119
////    } else {
120
////      return tail - head;
121
////    }
122
////  }
123
/////*--------------------------------------------------------------------*/
124
////void test(int ready){
125
////    int i;
126
////    //for (i=0;i<10;i++) if(ready>1) pico_stream_input_x(); 
127
////    //for (i=0;i<10;i++) {if(!moves_fifo1.empty()) moves_fifo1.pop(); }
128
////    AIMove m;
129
////    while(1) {
130
////            if(ready>1) m=pico_stream_input_queue(); 
131
////            if(m.weight==-1) break;
132
////    }
133
////}
134
////q moves_fifo1;
135
 
136 4 sumanta.ch
static AIWEIGHT threat_bits(int threat, PIECE type, Board *b)
137
/* Bit pack the threat value */
138
{
139
        if (threat < 1)
140
                return 0;
141
 
142
        /* No extra value for building sequences over k - p unless it is
143
           enough to win */
144
        if (b->turn == type && connect_k - threat <= b->moves_left)
145
                threat = connect_k - place_p + 1;
146
        else if (threat >= connect_k - place_p)
147
                threat = connect_k - place_p - (type == b->turn);
148
 
149
        return 1 << ((threat - 1) * BITS_PER_THREAT);
150
}
151
 
152
static void threat_mark(int i, int threat, PIECE type,Board *b,Line *line)
153
{
154
        int j, index = 0;
155
 
156
        if (threat <= 0)
157
                return;
158
 
159
        /* No extra value for building sequences over k - p unless it is
160
           enough to win */
161
        if (b->turn == type && connect_k - threat <= b->moves_left)
162
                threat = connect_k - place_p + 1;
163
        else if (threat >= connect_k - place_p)
164
                threat = connect_k - place_p - (type == b->turn);
165
 
166
        /* Do not mark if this threat is dominated by a preceeding threat;
167
           Likewise supress any smaller threats */
168
        for (j = i; j >= 0 && j > i - connect_k; j--)
169
                if (line[j].threat[0] > threat)
170
                        return;
171
                else if (line[j].threat[0] < threat) {
172
                        line[j].threat[0] = 0;
173
                        line[j].threat[1] = 0;
174
                }
175
 
176
        /* Store up to two threats per tile in the line */
177
        if (line[i].threat[index])
178
                index++;
179
        line[i].threat[index] = threat;
180
        line[i].turn[index] = type;
181
}
182
 
183
int threat_window(int x, int y, int dx, int dy,
184
                         PIECE *ptype, int *pdouble,Board *b)
185
{
186
        int minimum, maximum, count = 0;
187
        PIECE p, type = PIECE_NONE;
188
 
189
        /* Check if this tile is empty */
190
        p = piece_at(b, x, y);
191
        if (!piece_empty(p))
192
                return 0;
193
 
194
        /* Push forward the maximum and find the window type */
195 8 sumanta.ch
        #pragma unroll 
196
        #pragma num_iterations(1,3,6)
197 4 sumanta.ch
        for (maximum = 1; maximum < connect_k; maximum++) {
198
                p = piece_at(b, x + dx * maximum, y + dy * maximum);
199
                if (p == PIECE_ERROR)
200
                        break;
201
                if (!piece_empty(p)) {
202
                        if (type == PIECE_NONE)
203
                                type = p;
204
                        else if (type != p)
205
                                break;
206
                        count++;
207
                }
208
        }
209
        maximum--;
210
 
211
        /* Try to push the entire window back */
212 8 sumanta.ch
        #pragma unroll 
213
        #pragma num_iterations(1,3,6)
214 4 sumanta.ch
        for (minimum = -1; minimum > -connect_k; minimum--) {
215
                p = piece_at(b, x + dx * minimum, y + dy * minimum);
216
                if (p == PIECE_ERROR || piece_empty(p))
217
                        break;
218
                if (type == PIECE_NONE)
219
                        type = p;
220
                else if (type != p)
221
                        break;
222
                if (maximum - minimum > connect_k - 1) {
223
                        p = piece_at(b, x + dx * maximum, y + dy * maximum);
224
                        if (p == type)
225
                                count--;
226
                        maximum--;
227
                }
228
                count++;
229
        }
230
        minimum++;
231
 
232
        /* Push back minimum if we haven't formed a complete window, this window
233
           can't be a double */
234
        if (maximum - minimum < connect_k - 1) {
235 8 sumanta.ch
        //#pragma unroll 
236
        #pragma num_iterations(1,3,6)
237 4 sumanta.ch
                for (minimum--; minimum > maximum - connect_k; minimum--) {
238
                        p = piece_at(b, x + dx * minimum, y + dy * minimum);
239
                        if (p == PIECE_ERROR)
240
                                break;
241
                        if (!piece_empty(p)) {
242
                                if (type != p)
243
                                        break;
244
                                if (type == PIECE_NONE)
245
                                        type = p;
246
                                count++;
247
                        }
248
                }
249
                *pdouble = 0;
250
                minimum++;
251
        }
252
 
253
        *ptype = type;
254
        if (maximum - minimum >= connect_k - 1)
255
                return count;
256
        return 0;
257
}
258
 
259 7 sumanta.ch
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound)
260 4 sumanta.ch
{
261
 
262
        //#pragma read_write_ports threat_counts.data combined 2
263
        //#pragma internal_blockram threat_counts
264
        //#pragma no_memory_analysis threat_counts
265
 
266
        //#pragma read_write_ports b.data combined 2
267
        //#pragma read_write_ports b.data separate 1 readonly 2 writeonly
268
        //#pragma no_memory_analysis b
269
        /* This is the line of threats currently being processed */
270
        Line line[board_size]={{1},{2}};
271
        #pragma internal_fast line
272
        //#pragma no_memory_analysis line
273
        /* Running tally of threats for both players */
274
        //static int threat_counts[MAX_CONNECT_K + 1][2];
275
        threat_count_array threat_counts={{0}};
276
        #pragma internal_fast threat_counts
277
        //#pragma read_write_ports threat_counts.data combined 2
278
        //#pragma no_memory_analysis threat_counts
279 8 sumanta.ch
        //if (k==1) board_copy(b, bwrite);
280 4 sumanta.ch
        int i;
281
        AIWEIGHT weight = 0;
282
        ///* Clear threat tallys */
283
        //for (i = 0; i < connect_k; i++) {
284
        //        threat_counts.data[i][0] = 1;
285
        //        threat_counts.data[i][1] = 1;
286
        //}
287
 
288
        /* Mark the maximum threat for each */
289
        for (i = 0; x >= 0 && x < board_size && y >= 0 && y < board_size; i++) {
290
                int count[2], tmp, double_threat = 1;
291
                PIECE type[2];
292
 
293
                count[0] = threat_window(x, y, dx, dy, type, &double_threat,bwrite);
294
                count[1] = threat_window(x, y, -dx, -dy, type + 1,
295
                                         &double_threat,bwrite);
296
                if (count[1] > count[0]) {
297
                        tmp = count[1];
298
                        count[1] = count[0];
299
                        count[0] = tmp;
300
                        tmp = type[1];
301
                        type[1] = type[0];
302
                        type[0] = tmp;
303
                }
304
                line[i].threat[0] = 0;
305
                line[i].threat[1] = 0;
306
                threat_mark(i, count[0], type[0],bwrite,&line[0]);
307
                if (double_threat)
308
                        threat_mark(i, count[1], type[1],bwrite,&line[0]);
309
                x += dx;
310
                y += dy;
311
        }
312
 
313
        /* Commit stored line values to the board */
314
        x -= dx;
315
        y -= dy;
316
        for (i--; x >= 0 && x < board_size && y >= 0 && y < board_size; i--) {
317
                AIWEIGHT bits[2];
318
                PIECE p;
319
 
320
                bits[0] = threat_bits(line[i].threat[0], line[i].turn[0],bwrite);
321
                bits[1] = threat_bits(line[i].threat[1], line[i].turn[1],bwrite);
322
                p = piece_at(bwrite, x, y);
323
                if (piece_empty(p) && line[i].threat[0]) {
324
                        threat_counts.data[line[i].threat[0]][line[i].turn[0] - 1]++;
325
                        if (line[i].threat[1])
326
                                threat_counts.data[line[i].threat[1]]
327
                                             [line[i].turn[1] - 1]++;
328
                        if (p >= PIECE_THREAT0)
329
                                place_threat(bwrite, x, y, p - PIECE_THREAT0 +
330
                                             bits[0] + bits[1]);
331
                        else
332
                                place_threat(bwrite, x, y, bits[0] + bits[1]);
333
                }
334
                if (bwrite->turn != line[i].turn[0])
335
                        bits[0] = -bits[0];
336
                if (bwrite->turn != line[i].turn[1])
337
                        bits[1] = -bits[1];
338
                weight += bits[0] + bits[1];
339
                x -= dx;
340
                y -= dy;
341
        }
342
        return weight;
343
}
344
 
345 7 sumanta.ch
FIFO(queue,AIMove);
346
//FIFO_INTERFACE(queue,AIMove);
347
#pragma fifo_length pico_stream_input_queue 800
348
#pragma fifo_length pico_stream_output_queue 800
349
#pragma bandwidth pico_stream_input_queue 1
350
#pragma bandwidth pico_stream_output_queue 1
351 10 sumanta.ch
/*AIMoves*/int ai_threats(Board *board,AIMoves *moves,unsigned int *index)
352 4 sumanta.ch
{
353
        //#pragma read_write_ports board.data combined 2
354 7 sumanta.ch
        //#pragma internal_blockram board
355 4 sumanta.ch
        //#pragma no_memory_analysis board
356
 
357
        //#pragma internal_blockram move
358
        //#pragma no_memory_analysis move
359
 
360
        /////////* All threat functions work on this board */
361
        /*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
362
        //#pragma read_write_ports b.data combined 2
363
        #pragma internal_blockram b
364 7 sumanta.ch
        //#pragma multi_buffer b 2
365 4 sumanta.ch
        //#pragma read_write_ports b.data separate 1 readonly 2 writeonly
366
        //#pragma no_memory_analysis b
367
        /*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
368
        //#pragma read_write_ports b.data combined 2
369
        #pragma internal_blockram bwrite
370 8 sumanta.ch
        //#pragma multi_buffer bwrite 22
371 4 sumanta.ch
        //#pragma no_memory_analysis b
372 10 sumanta.ch
        ///*static*/ AIMoves moves;//={0,0,0,{{0,0,0}}};
373 4 sumanta.ch
        //#pragma read_write_ports moves.data combined 3
374
        #pragma internal_blockram moves
375
        //#pragma no_memory_analysis moves
376
 
377 10 sumanta.ch
        moves->len=0;
378 4 sumanta.ch
        //AIMoves moves;
379
        AIWEIGHT u_sum = 0;
380
        int i;
381
 
382
        //b = board_new();
383
        //Board b;
384
        board_copy(board, &b);
385
 
386
        /* Clear threat tallys */
387
        //for (i = 0; i < connect_k; i++) {
388
        //        threat_counts.data[i][0] = 0;
389
        //        threat_counts.data[i][1] = 0;
390
        //}
391
/*---------------------------------------------------------------------------*/
392
       // /* Horizontal lines */
393
       // for (i = 0; i < board_size; i++)
394
       //         u_sum += threat_line(0, i, 1, 0,&b);
395
 
396
       // /* Vertical lines */
397
       // for (i = 0; i < board_size; i++)
398
       //         u_sum += threat_line(i, 0, 0, 1,&b);
399
 
400
       // /* SE diagonals */
401
       // for (i = 0; i < board_size - connect_k + 1; i++)
402
       //         u_sum += threat_line(i, 0, 1, 1,&b);
403
       // for (i = 1; i < board_size - connect_k + 1; i++)
404
       //         u_sum += threat_line(0, i, 1, 1,&b);
405
 
406
       // /* SW diagonals */
407
       // for (i = connect_k - 1; i < board_size; i++)
408
       //         u_sum += threat_line(i, 0, -1, 1,&b);
409
       // for (i = 1; i < board_size - connect_k + 1; i++)
410
       //         u_sum += threat_line(board_size - 1, i, -1, 1,&b);
411
/*---------------------------------------------------------------------------*/
412
//rewritten for hardware
413
/*---------------------------------------------------------------------------*/
414 7 sumanta.ch
        //int id= PICO_initialize_PPA(threat_line);     
415
        //PICO_set_task_overlap(id, 2);
416 4 sumanta.ch
        int j;
417
        int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
418
        int k=0;
419
        for(j=0;j<6;j++){
420
                        switch(j){
421
                        case 0:
422
                                {
423
                                loop_begin=0;
424
                                loop_bound=board_size;
425
                                break;
426
                                }
427
                        case 1:
428
                                {
429
                                loop_begin=0;
430
                                loop_bound=board_size;
431
                                break;
432
                                }
433
                        case 2:
434
                                {
435
                                loop_begin=0;
436
                                loop_bound=board_size-connect_k+1;
437
                                break;
438
                                }
439
                        case 3:
440
                                {
441
                                loop_begin=1;
442
                                loop_bound=board_size-connect_k+1;
443
                                break;
444
                                }
445
                        case 4:
446
                                {
447
                                loop_begin=connect_k-1;
448
                                loop_bound=board_size;
449
                                break;
450
                                }
451
                        case 5:
452
                                {
453
                                loop_begin=1;
454
                                loop_bound=board_size-connect_k+1;
455
                                break;
456
                                }
457
                        default:{
458
                                break;
459
                                }
460
                        }
461
                        for (i = loop_begin; i < loop_bound; i++){
462
                                k++;
463
                                switch(j){
464
                                case 0:
465
                                        {
466
                                        arg1=0;
467
                                        arg2=i;
468
                                        arg3=1;
469
                                        arg4=0;
470
                                        break;
471
                                        }
472
                                case 1:
473
                                        {
474
                                        arg1=i;
475
                                        arg2=0;
476
                                        arg3=0;
477
                                        arg4=1;
478
                                        break;
479
                                        }
480
                                case 2:
481
                                        {
482
                                        arg1=i;
483
                                        arg2=0;
484
                                        arg3=1;
485
                                        arg4=1;
486
                                        break;
487
                                        }
488
                                case 3:
489
                                        {
490
                                        arg1=0;
491
                                        arg2=i;
492
                                        arg3=1;
493
                                        arg4=1;
494
                                        break;
495
                                        }
496
                                case 4:
497
                                        {
498
                                        arg1=i;
499
                                        arg2=0;
500
                                        arg3=-1;
501
                                        arg4=1;
502
                                        break;
503
                                        }
504
                                case 5:
505
                                        {
506
                                        arg1=board_size-1;
507
                                        arg2=i;
508
                                        arg3=-1;
509
                                        arg4=1;
510
                                        break;
511
                                        }
512
                                default:{
513
                                        break;
514
                                        }
515
                                }
516
 
517
 
518 8 sumanta.ch
                                if (k==1) board_copy(&b, &bwrite);
519 7 sumanta.ch
                                u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,k,loop_bound);
520 4 sumanta.ch
                        }
521
        }
522 7 sumanta.ch
        //PICO_sync_task(id, 1);
523
        //PICO_finalize_PPA(id);
524 4 sumanta.ch
/*---------------------------------------------------------------------------*/
525
        //board_copy(&b,&b_marks);
526 10 sumanta.ch
                //unsigned int index[max_size]={0};
527 7 sumanta.ch
                #pragma bitsize index 9
528
                #pragma internal_fast index
529
        AIMoves moves1;
530 10 sumanta.ch
        moves1.len=0;
531 7 sumanta.ch
        #pragma internal_blockram moves1
532 10 sumanta.ch
        /*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves1);
533 7 sumanta.ch
        //test(ready);
534 10 sumanta.ch
        streamsort(moves,index);
535
        moves1.utility = u_sum;
536
        moves->utility = u_sum;
537 7 sumanta.ch
        /*----------------------------
538
                rewritten for hardware
539
        ----------------------------*/
540
        //if (!aimoves_choose(&moves1, move))
541
        //      return 0;
542
        //else return 1;                
543 10 sumanta.ch
        //int ret_val;
544
        //ret_val=aimoves_choose(&moves1, move,&index[0]);
545
        //if (!ret_val)
546
        //      return 0;
547
        //else return 1;                
548 7 sumanta.ch
        /*----------------------------
549
        end rewritten for hardware
550
        ----------------------------*/
551 4 sumanta.ch
        //board_free(b);
552
        //return moves;
553 11 sumanta.ch
        return 1;
554 4 sumanta.ch
}
555
 
556
//void debug_counts(void)
557
//{
558
//        int i, sum = 0;
559
//
560
//        if (!b)
561
//                return;
562
//
563
//        g_debug("Threat counts (black, white):");
564
//        for (i = 1; i < connect_k; i++) {
565
//                g_debug("%d: %3d %3d", i, threat_counts[i][0],
566
//                        threat_counts[i][1]);
567
//                sum += threat_counts[i][0] * threat_bits(i, b->turn) -
568
//                       threat_counts[i][1] *
569
//                       threat_bits(i, other_player(b->turn));
570
//        }
571
//        if (sum > 0)
572
//                g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
573
//        else if (sum < 0)
574
//                g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
575
//        else
576
//                g_debug("Threat sum: 0");
577
//}
578
 
579
//static int threat_number(int player, int threat,threat_count_array threat_counts)
580
//{
581
//        return threat_counts.data[threat][player] / (connect_k - threat);
582
//}
583
 
584
//AIMoves *ai_priority(const Board *b)
585
//{
586
//        AIMoves *moves;
587
//        int i, j, stage[2] = {1, 1}, mask, bits;
588
//
589
//        moves = ai_threats(b);
590
//
591
//        /* Do not prioritize if we've won */
592
//        if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
593
//                moves->utility = AIW_WIN;
594
//                return moves;
595
//        }
596
//
597
//        /* Find the largest supported threat for each player */
598
//        for (i = 2; i < connect_k; i++) {
599
//                if (threat_number(0, i - 1) >= place_p &&
600
//                    threat_number(0, i) > place_p)
601
//                        stage[0] = i;
602
//                if (threat_number(1, i - 1) >= place_p &&
603
//                    threat_number(1, i) > place_p)
604
//                        stage[1] = i;
605
//        }
606
//
607
//        //if (opt_debug_stage)
608
//        //        g_debug("Stages %d/%d", stage[0], stage[1]);
609
//
610
//        /* Do not prioritize if we're losing */
611
//        if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
612
//                moves->utility = -stage[other_player(b->turn) - 1];
613
//                return moves;
614
//        }
615
//
616
//        /* Threats above the player's stage are no more valuable than the
617
//           stage */
618
//        bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
619
//        mask = bits - 1;
620
//        for (i = 0; i < moves->len; i++) {
621
//                AIWEIGHT w = moves->data[i].weight, w2;
622
//
623
//                if (w < AIW_THREAT_MAX && w >= bits) {
624
//                        w2 = w & mask;
625
//                        w = w & ~mask;
626
//                        for (j = stage[b->turn - 1];
627
//                             w && j < connect_k - place_p + 1; j++) {
628
//                                w = w >> BITS_PER_THREAT;
629
//                                w2 += w & mask;
630
//                        }
631
//                        moves->data[i].weight = w2;
632
//                }
633
//        }
634
//
635
//        /* Stage determines weight */
636
//        moves->utility = stage[b->turn - 1];
637
//        return moves;
638
//}
639
/*AIMoves*/ void ai_marks(Board *b, PIECE minimum,AIMoves *moves)
640
{
641
        //#pragma read_write_ports b.data combined 2
642
        #pragma internal_blockram b
643
        //#pragma no_memory_analysis b
644
        //AIMoves *moves = aimoves_new();
645
        //AIMoves moves;
646
        //AIMoves moves[361];
647
        AIMove move;
648
        PIECE p;
649 7 sumanta.ch
        //moves_fifo.resoet();
650
        AIMove m;
651
        #pragma num_iterations(19,19,19)
652 4 sumanta.ch
        for (move.y = 0; move.y < board_size; move.y++)
653 7 sumanta.ch
        #pragma num_iterations(19,19,19)
654
                for (move.x = 0; move.x < board_size; move.x++){
655 4 sumanta.ch
                        if ((p = piece_at(b, move.x, move.y)) >= minimum) {
656
                                move.weight = p - PIECE_THREAT0;
657
                                aimoves_set(moves, &move);
658 7 sumanta.ch
                                pico_stream_output_queue(move);
659
                                //cout<<"push"<<move.weight<<endl;
660
                        //ready=ready+1;
661
                        //cout<<"READY"<<*ready<<endl;
662
                        }else {
663
                        m.weight =-100;
664
                        pico_stream_output_queue(m);
665
                                //cout<<"push"<<m.weight<<endl;
666
                        }
667
                        //if((move.y == board_size-1) && (move.x == board_size-1)){
668
                        //      m.weight=-1;m.x=-1;m.y=-1;
669
                        //        /*if((ready))*/ {pico_stream_output_queue(m);/*cout<<"push_finish"<<m.weight<<endl;*/}
670
                        //
671
                        //}
672
                }
673
        //moves_fifo.active=0;
674 4 sumanta.ch
        //return moves;
675 7 sumanta.ch
        int i;
676
        //for (i=0;i<10;i++) {*ready=i;pico_stream_output_x(i); }
677
        //for (i=0;i<10;i++) {if(!moves_fifo1.full()) moves_fifo1.push(m); }
678
        //for (i=0;i<10;i++) {
679
        //      *ready=i;pico_stream_output_queue(m); 
680
        //      if(i==9){
681
        //              m.weight=-1;
682
        //              pico_stream_output_queue(m);
683
        //      }
684
        //              
685
        //      }
686
 
687 4 sumanta.ch
}
688 7 sumanta.ch
void streamsort(AIMoves *moves,unsigned int *index){
689
/* Insertion sort for streaming*/
690
                AIMove val;
691
                AIMove data[361]={{-1},{-1},{-1}};
692
                //unsigned int index[361];
693
                #pragma bitsize index 9
694
                #pragma internal_fast index
695
                int i,j,k;
696
                unsigned int len=0;
697
                moves->data[0].weight=-100;
698
//              while(1) {
699
//                      if(!moves_fifo.empty()){ 
700
//                              val=moves_fifo.pop();
701
//                              for(i=0;i<len;i++){
702
//                                      if (list[i].weight < val.weight){
703
//                                      for(j=len-1;j>i-1;j--){
704
//                                              list[j+1]=list[j];
705
//                                      }
706
//                                      break;
707
//                                      }
708
//                              }
709
//                                      list[i]=val;
710
//                                      len++;
711
//                      }
712
//                      else break;
713
//                      //if(!moves_fifo.active && moves_fifo.empty()) break;
714
//              }
715
                //while(1) {
716
                //int count=0;
717 8 sumanta.ch
                #pragma num_iterations(1,150,368)
718
                for(k=0;k<368;k++){
719 7 sumanta.ch
                        //count++;
720
                        //cout<<count<<endl;
721 8 sumanta.ch
                        if (k>6){
722 7 sumanta.ch
 
723
                        //if(ready>5){ 
724
                                val=pico_stream_input_queue();
725
                                //cout<<"popped"<<","<<val.weight<<" "<<val.x<<" "<<val.y<<endl;
726
                                if(val.weight==-1) {moves->len=len;break;}
727
                                else if(val.weight==-100) continue;
728
                                #pragma num_iterations(0,150,361)
729
                                for(i=0;i<len;i++){
730 11 sumanta.ch
                                        if (moves->data[i].weight < val.weight){
731 7 sumanta.ch
                                        for(j=len-1;j>i-1;j--){
732 11 sumanta.ch
                                                moves->data[j+1]=moves->data[j];
733
                                                //index[j+1]=index[j];
734 7 sumanta.ch
                                        }
735
                                        break;
736
                                        }
737
                                }
738
                                        index[i]=len;
739 11 sumanta.ch
                                        moves->data[i]=val;
740 7 sumanta.ch
                                        len++;
741
                                //cout<<"STREAMSORT"<<":";
742
                        //}
743
                        /*else*/ //{moves->len=len;break;}
744
                        }
745
                }
746
                moves->len=len;
747
                                //cout<<"STREAMSORT"<<":"<<moves->len<<endl;
748
                                //for(i=0;i<len;i++) cout<<moves->data[i].weight<<",";
749
                                //cout<<endl;
750
                                //for(i=0;i<len;i++) cout<<moves->data[index[i]].weight<<",";
751
                                //cout<<endl;
752
}
753 4 sumanta.ch
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
754
{
755
        int dx, dy, count;
756
        #pragma bitsize dx 4
757
        #pragma bitsize dy 4
758
        PIECE p;
759
 
760
        if (!piece_empty(piece_at(b, x, y)))
761
                return FALSE;
762
        for (dy = -1; dy < 2; dy++)
763
                for (dx = -1; dx < 2; dx++) {
764
                        if (!dx && !dy)
765
                                continue;
766
                        count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
767
                        if (count - 1 < dist && p != PIECE_NONE)
768
                                return TRUE;
769
                }
770
        return FALSE;
771
}
772
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random)
773
{
774
        //AIMoves *moves;
775
        AIMove move;
776
 
777
        move.weight = AIW_NONE;
778
        //moves = aimoves_new();
779 7 sumanta.ch
        #pragma num_iterations(1,9,19)
780
        for (move.y = 0; move.y < board_size; move.y++){
781
        #pragma num_iterations(1,9,19)
782
                for (move.x = 0; move.x < board_size; move.x++){
783 4 sumanta.ch
                        if (is_adjacent(b, move.x, move.y, dist))
784
                                aimoves_append(moves, &move);
785 7 sumanta.ch
                }
786
        }
787 4 sumanta.ch
        //aimoves_shuffle(moves,current_random);
788
        //return moves;
789
}
790
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
791
{
792
        //#pragma read_write_ports board.data combined 2
793
        #pragma internal_blockram b
794
        //#pragma no_memory_analysis b
795
 
796
        /*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
797
        //#pragma read_write_ports moves.data combined 3
798
        #pragma internal_blockram moves
799
        //#pragma no_memory_analysis moves
800
        //#pragma read_write_ports moves.data combined 3
801
        //#pragma internal_blockram moves
802
        //#pragma no_memory_analysis moves
803
        //AIMove move;
804 7 sumanta.ch
        unsigned int index[1]={0};
805 4 sumanta.ch
        //AIMoves *moves;
806
        moves.len=0;
807
        /* Get all open tiles adjacent to any piece */
808
        /*moves =*/ enum_adjacent(b, 1,&moves,current_random);
809
        if (moves.len){
810 7 sumanta.ch
                aimoves_choose(&moves, move,&index[0]);
811 4 sumanta.ch
 
812
                return ;//moves;
813
        }
814
        /* Play in the middle if there are no open adjacent tiles */
815
        move->x = board_size / 2;
816
        move->y = board_size / 2;
817
        move->weight = AIW_NONE;
818
        //aimoves_append(&moves, move);
819
        //aimoves_choose(&moves, move);
820
        //return moves;
821
}

powered by: WebSVN 2.1.0

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