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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [XILINX/] [BUILD_SCC_SRCH/] [synth_src/] [threats.cpp] - Blame information for rev 17

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 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 <iostream>
27
#include "./shared.h"
28
//#include "./q.hpp"
29
#include "pico.h"
30
//#include <stdio.h>
31
 
32
/* Bits per threat level */
33
#define BITS_PER_THREAT 6
34
 
35
//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
 
56
static void debug_assert (bool cond, char * msg) {
57
        if (!cond) {
58
                printf("assert failed: %s\n", msg);
59
                assert(0);
60
        }
61
}
62
 
63
#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
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
        #pragma unroll 
196
        #pragma num_iterations(1,3,6)
197
        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
        #pragma unroll 
213
        #pragma num_iterations(1,3,6)
214
        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
        //#pragma unroll 
236
        #pragma num_iterations(1,3,6)
237
                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
/*static*/ AIWEIGHT threat_line(int x, int y, int dx, int dy,Board *b,Board *bwrite,int k,int loop_bound)
260
{
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
        //if (k==1) board_copy(b, bwrite);
280
        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
//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
/*AIMoves*/int ai_threats(Board board[2][16],int depth,int branch,AIMoves moves[2][16]/*,index_array *index*/)
352
{
353
        //#pragma read_write_ports board.data combined 2
354
        //#pragma internal_blockram board
355
        //#pragma no_memory_analysis board
356
 
357
        //#pragma internal_blockram move
358
        //#pragma no_memory_analysis move
359
        index_array  index={0};
360
                #pragma internal_fast index
361
 
362
        /////////* All threat functions work on this board */
363
        /*static*/ Board b;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
364
        //#pragma read_write_ports b.data combined 2
365
        #pragma internal_blockram b
366
        //#pragma multi_buffer b 2
367
        //#pragma read_write_ports b.data separate 1 readonly 2 writeonly
368
        //#pragma no_memory_analysis b
369
        /*static*/ Board bwrite;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}} ;//= NULL;
370
        //#pragma read_write_ports b.data combined 2
371
        #pragma internal_blockram bwrite
372
        //#pragma multi_buffer bwrite 22
373
        //#pragma no_memory_analysis b
374
        ///*static*/ AIMoves moves;//={0,0,0,{{0,0,0}}};
375
        //#pragma read_write_ports moves.data combined 3
376
        //#pragma internal_blockram moves
377
        //#pragma no_memory_analysis moves
378
 
379
        moves[depth][branch].len=0;
380
        //AIMoves moves;
381
        AIWEIGHT u_sum = 0;
382
        int i;
383
 
384
        //b = board_new();
385
        //Board b;
386
        board_copy(&board[depth][branch], &b);
387
 
388
        /* Clear threat tallys */
389
        //for (i = 0; i < connect_k; i++) {
390
        //        threat_counts.data[i][0] = 0;
391
        //        threat_counts.data[i][1] = 0;
392
        //}
393
/*---------------------------------------------------------------------------*/
394
       // /* Horizontal lines */
395
       // for (i = 0; i < board_size; i++)
396
       //         u_sum += threat_line(0, i, 1, 0,&b);
397
 
398
       // /* Vertical lines */
399
       // for (i = 0; i < board_size; i++)
400
       //         u_sum += threat_line(i, 0, 0, 1,&b);
401
 
402
       // /* SE diagonals */
403
       // for (i = 0; i < board_size - connect_k + 1; i++)
404
       //         u_sum += threat_line(i, 0, 1, 1,&b);
405
       // for (i = 1; i < board_size - connect_k + 1; i++)
406
       //         u_sum += threat_line(0, i, 1, 1,&b);
407
 
408
       // /* SW diagonals */
409
       // for (i = connect_k - 1; i < board_size; i++)
410
       //         u_sum += threat_line(i, 0, -1, 1,&b);
411
       // for (i = 1; i < board_size - connect_k + 1; i++)
412
       //         u_sum += threat_line(board_size - 1, i, -1, 1,&b);
413
/*---------------------------------------------------------------------------*/
414
//rewritten for hardware
415
/*---------------------------------------------------------------------------*/
416
        //int id= PICO_initialize_PPA(threat_line);     
417
        //PICO_set_task_overlap(id, 2);
418
        int j;
419
        int arg1,arg2,arg3,arg4,loop_bound,loop_begin;
420
        int k=0;
421
        for(j=0;j<6;j++){
422
                        switch(j){
423
                        case 0:
424
                                {
425
                                loop_begin=0;
426
                                loop_bound=board_size;
427
                                break;
428
                                }
429
                        case 1:
430
                                {
431
                                loop_begin=0;
432
                                loop_bound=board_size;
433
                                break;
434
                                }
435
                        case 2:
436
                                {
437
                                loop_begin=0;
438
                                loop_bound=board_size-connect_k+1;
439
                                break;
440
                                }
441
                        case 3:
442
                                {
443
                                loop_begin=1;
444
                                loop_bound=board_size-connect_k+1;
445
                                break;
446
                                }
447
                        case 4:
448
                                {
449
                                loop_begin=connect_k-1;
450
                                loop_bound=board_size;
451
                                break;
452
                                }
453
                        case 5:
454
                                {
455
                                loop_begin=1;
456
                                loop_bound=board_size-connect_k+1;
457
                                break;
458
                                }
459
                        default:{
460
                                break;
461
                                }
462
                        }
463
                        for (i = loop_begin; i < loop_bound; i++){
464
                                k++;
465
                                switch(j){
466
                                case 0:
467
                                        {
468
                                        arg1=0;
469
                                        arg2=i;
470
                                        arg3=1;
471
                                        arg4=0;
472
                                        break;
473
                                        }
474
                                case 1:
475
                                        {
476
                                        arg1=i;
477
                                        arg2=0;
478
                                        arg3=0;
479
                                        arg4=1;
480
                                        break;
481
                                        }
482
                                case 2:
483
                                        {
484
                                        arg1=i;
485
                                        arg2=0;
486
                                        arg3=1;
487
                                        arg4=1;
488
                                        break;
489
                                        }
490
                                case 3:
491
                                        {
492
                                        arg1=0;
493
                                        arg2=i;
494
                                        arg3=1;
495
                                        arg4=1;
496
                                        break;
497
                                        }
498
                                case 4:
499
                                        {
500
                                        arg1=i;
501
                                        arg2=0;
502
                                        arg3=-1;
503
                                        arg4=1;
504
                                        break;
505
                                        }
506
                                case 5:
507
                                        {
508
                                        arg1=board_size-1;
509
                                        arg2=i;
510
                                        arg3=-1;
511
                                        arg4=1;
512
                                        break;
513
                                        }
514
                                default:{
515
                                        break;
516
                                        }
517
                                }
518
 
519
 
520
                                if (k==1) board_copy(&b, &bwrite);
521
                                u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,k,loop_bound);
522
                        }
523
        }
524
        //PICO_sync_task(id, 1);
525
        //PICO_finalize_PPA(id);
526
/*---------------------------------------------------------------------------*/
527
        //board_copy(&b,&b_marks);
528
                //unsigned int index[max_size]={0};
529
                //#pragma bitsize index 9
530
                //#pragma internal_fast index
531
        AIMoves moves1;
532
        moves1.len=0;
533
        #pragma internal_blockram moves1
534
        /*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves[depth][branch]);
535
        //test(ready);
536
        //streamsort(&moves[depth][branch],&index);
537
        aimoves_sort_bis(moves,depth,branch);
538
        moves1.utility = u_sum;
539
        moves[depth][branch].utility = u_sum;
540
        /*----------------------------
541
                rewritten for hardware
542
        ----------------------------*/
543
        //if (!aimoves_choose(&moves1, move))
544
        //      return 0;
545
        //else return 1;                
546
        //int ret_val;
547
        //AIMove move;
548
        //ret_val=aimoves_choose(&moves[depth][branch], &move/*,&index[0]*/);
549
        //if (!ret_val)
550
        //      return 0;
551
        //else return 1;                
552
        /*----------------------------
553
        end rewritten for hardware
554
        ----------------------------*/
555
        //board_free(b);
556
        //return moves;
557
        return 1;
558
}
559
 
560
//void debug_counts(void)
561
//{
562
//        int i, sum = 0;
563
//
564
//        if (!b)
565
//                return;
566
//
567
//        g_debug("Threat counts (black, white):");
568
//        for (i = 1; i < connect_k; i++) {
569
//                g_debug("%d: %3d %3d", i, threat_counts[i][0],
570
//                        threat_counts[i][1]);
571
//                sum += threat_counts[i][0] * threat_bits(i, b->turn) -
572
//                       threat_counts[i][1] *
573
//                       threat_bits(i, other_player(b->turn));
574
//        }
575
//        if (sum > 0)
576
//                g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
577
//        else if (sum < 0)
578
//                g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
579
//        else
580
//                g_debug("Threat sum: 0");
581
//}
582
 
583
//static int threat_number(int player, int threat,threat_count_array threat_counts)
584
//{
585
//        return threat_counts.data[threat][player] / (connect_k - threat);
586
//}
587
 
588
//AIMoves *ai_priority(const Board *b)
589
//{
590
//        AIMoves *moves;
591
//        int i, j, stage[2] = {1, 1}, mask, bits;
592
//
593
//        moves = ai_threats(b);
594
//
595
//        /* Do not prioritize if we've won */
596
//        if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
597
//                moves->utility = AIW_WIN;
598
//                return moves;
599
//        }
600
//
601
//        /* Find the largest supported threat for each player */
602
//        for (i = 2; i < connect_k; i++) {
603
//                if (threat_number(0, i - 1) >= place_p &&
604
//                    threat_number(0, i) > place_p)
605
//                        stage[0] = i;
606
//                if (threat_number(1, i - 1) >= place_p &&
607
//                    threat_number(1, i) > place_p)
608
//                        stage[1] = i;
609
//        }
610
//
611
//        //if (opt_debug_stage)
612
//        //        g_debug("Stages %d/%d", stage[0], stage[1]);
613
//
614
//        /* Do not prioritize if we're losing */
615
//        if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
616
//                moves->utility = -stage[other_player(b->turn) - 1];
617
//                return moves;
618
//        }
619
//
620
//        /* Threats above the player's stage are no more valuable than the
621
//           stage */
622
//        bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
623
//        mask = bits - 1;
624
//        for (i = 0; i < moves->len; i++) {
625
//                AIWEIGHT w = moves->data[i].weight, w2;
626
//
627
//                if (w < AIW_THREAT_MAX && w >= bits) {
628
//                        w2 = w & mask;
629
//                        w = w & ~mask;
630
//                        for (j = stage[b->turn - 1];
631
//                             w && j < connect_k - place_p + 1; j++) {
632
//                                w = w >> BITS_PER_THREAT;
633
//                                w2 += w & mask;
634
//                        }
635
//                        moves->data[i].weight = w2;
636
//                }
637
//        }
638
//
639
//        /* Stage determines weight */
640
//        moves->utility = stage[b->turn - 1];
641
//        return moves;
642
//}
643
/*AIMoves*/ void ai_marks(Board *b, PIECE minimum,AIMoves *moves)
644
{
645
        //#pragma read_write_ports b.data combined 2
646
        //#pragma internal_blockram b
647
        //#pragma no_memory_analysis b
648
        //AIMoves *moves = aimoves_new();
649
        //AIMoves moves;
650
        //AIMoves moves[361];
651
        AIMove move;
652
        PIECE p;
653
        //moves_fifo.resoet();
654
        AIMove m;
655
        #pragma num_iterations(19,19,19)
656
        for (move.y = 0; move.y < board_size; move.y++)
657
        #pragma num_iterations(19,19,19)
658
                for (move.x = 0; move.x < board_size; move.x++){
659
                        if ((p = piece_at(b, move.x, move.y)) >= minimum) {
660
                                move.weight = p - PIECE_THREAT0;
661
                                aimoves_set(moves, &move);
662
                                //pico_stream_output_queue(move);
663
                                //cout<<"push"<<move.weight<<endl;
664
                        //ready=ready+1;
665
                        //cout<<"READY"<<*ready<<endl;
666
                        }else {
667
                        m.weight =-100;
668
                        //pico_stream_output_queue(m);
669
                                //cout<<"push"<<m.weight<<endl;
670
                        }
671
                        //if((move.y == board_size-1) && (move.x == board_size-1)){
672
                        //      m.weight=-1;m.x=-1;m.y=-1;
673
                        //        /*if((ready))*/ {pico_stream_output_queue(m);/*cout<<"push_finish"<<m.weight<<endl;*/}
674
                        //
675
                        //}
676
                }
677
        //moves_fifo.active=0;
678
        //return moves;
679
        int i;
680
        //for (i=0;i<10;i++) {*ready=i;pico_stream_output_x(i); }
681
        //for (i=0;i<10;i++) {if(!moves_fifo1.full()) moves_fifo1.push(m); }
682
        //for (i=0;i<10;i++) {
683
        //      *ready=i;pico_stream_output_queue(m); 
684
        //      if(i==9){
685
        //              m.weight=-1;
686
        //              pico_stream_output_queue(m);
687
        //      }
688
        //              
689
        //      }
690
 
691
}
692
void streamsort(AIMoves *moves,index_array *index){
693
/* Insertion sort for streaming*/
694
                AIMove val;
695
                AIMove data[361]={{-1},{-1},{-1}};
696
                //unsigned int index[361];
697
                //#pragma bitsize index 9
698
                //#pragma internal_fast index
699
                int i,j,k;
700
                unsigned int len=0;
701
                moves->data[0].weight=-100;
702
//              while(1) {
703
//                      if(!moves_fifo.empty()){ 
704
//                              val=moves_fifo.pop();
705
//                              for(i=0;i<len;i++){
706
//                                      if (list[i].weight < val.weight){
707
//                                      for(j=len-1;j>i-1;j--){
708
//                                              list[j+1]=list[j];
709
//                                      }
710
//                                      break;
711
//                                      }
712
//                              }
713
//                                      list[i]=val;
714
//                                      len++;
715
//                      }
716
//                      else break;
717
//                      //if(!moves_fifo.active && moves_fifo.empty()) break;
718
//              }
719
                //while(1) {
720
                //int count=0;
721
                #pragma num_iterations(1,150,368)
722
                for(k=0;k<368;k++){
723
                        //count++;
724
                        //cout<<count<<endl;
725
                        if (k>6){
726
 
727
                        //if(ready>5){ 
728
                                //val=pico_stream_input_queue();
729
                                //cout<<"popped"<<","<<val.weight<<" "<<val.x<<" "<<val.y<<endl;
730
                                if(val.weight==-1) {moves->len=len;break;}
731
                                else if(val.weight==-100) continue;
732
                                #pragma num_iterations(0,150,361)
733
                                for(i=0;i<len;i++){
734
                                        if (moves->data[i].weight < val.weight){
735
                                        for(j=len-1;j>i-1;j--){
736
                                                moves->data[j+1]=moves->data[j];
737
                                                //index[j+1]=index[j];
738
                                        }
739
                                        break;
740
                                        }
741
                                }
742
                                        index->data[i]=len;
743
                                        moves->data[i]=val;
744
                                        len++;
745
                                //cout<<"STREAMSORT"<<":";
746
                        //}
747
                        /*else*/ //{moves->len=len;break;}
748
                        }
749
                }
750
                moves->len=len;
751
                                //cout<<"STREAMSORT"<<":"<<moves->len<<endl;
752
                                //for(i=0;i<len;i++) cout<<moves->data[i].weight<<",";
753
                                //cout<<endl;
754
                                //for(i=0;i<len;i++) cout<<moves->data[index[i]].weight<<",";
755
                                //cout<<endl;
756
}
757
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
758
{
759
        int dx, dy, count;
760
        #pragma bitsize dx 4
761
        #pragma bitsize dy 4
762
        PIECE p;
763
 
764
        if (!piece_empty(piece_at(b, x, y)))
765
                return FALSE;
766
        for (dy = -1; dy < 2; dy++)
767
                for (dx = -1; dx < 2; dx++) {
768
                        if (!dx && !dy)
769
                                continue;
770
                        count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
771
                        if (count - 1 < dist && p != PIECE_NONE)
772
                                return TRUE;
773
                }
774
        return FALSE;
775
}
776
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random)
777
{
778
        //AIMoves *moves;
779
        AIMove move;
780
 
781
        move.weight = AIW_NONE;
782
        //moves = aimoves_new();
783
        #pragma num_iterations(1,9,19)
784
        for (move.y = 0; move.y < board_size; move.y++){
785
        #pragma num_iterations(1,9,19)
786
                for (move.x = 0; move.x < board_size; move.x++){
787
                        if (is_adjacent(b, move.x, move.y, dist))
788
                                aimoves_append(moves, &move);
789
                }
790
        }
791
        //aimoves_shuffle(moves,current_random);
792
        //return moves;
793
}
794
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
795
{
796
        //#pragma read_write_ports board.data combined 2
797
        #pragma internal_blockram b
798
        //#pragma no_memory_analysis b
799
 
800
        /*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
801
        //#pragma read_write_ports moves.data combined 3
802
        #pragma internal_blockram moves
803
        //#pragma no_memory_analysis moves
804
        //#pragma read_write_ports moves.data combined 3
805
        //#pragma internal_blockram moves
806
        //#pragma no_memory_analysis moves
807
        //AIMove move;
808
        index_array  index={0};
809
        //AIMoves *moves;
810
        moves.len=0;
811
        /* Get all open tiles adjacent to any piece */
812
        /*moves =*/ enum_adjacent(b, 1,&moves,current_random);
813
        if (moves.len){
814
                aimoves_choose(&moves, move/*,&index*/);
815
 
816
                return ;//moves;
817
        }
818
        /* Play in the middle if there are no open adjacent tiles */
819
        move->x = board_size / 2;
820
        move->y = board_size / 2;
821
        move->weight = AIW_NONE;
822
        //aimoves_append(&moves, move);
823
        //aimoves_choose(&moves, move);
824
        //return moves;
825
}

powered by: WebSVN 2.1.0

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