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 12

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 12 sumanta.ch
                #pragma bitsize index 9
360
                #pragma internal_fast index
361 4 sumanta.ch
 
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 7 sumanta.ch
        //#pragma multi_buffer b 2
367 4 sumanta.ch
        //#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 8 sumanta.ch
        //#pragma multi_buffer bwrite 22
373 4 sumanta.ch
        //#pragma no_memory_analysis b
374 10 sumanta.ch
        ///*static*/ AIMoves moves;//={0,0,0,{{0,0,0}}};
375 4 sumanta.ch
        //#pragma read_write_ports moves.data combined 3
376 12 sumanta.ch
        //#pragma internal_blockram moves
377 4 sumanta.ch
        //#pragma no_memory_analysis moves
378
 
379 10 sumanta.ch
        moves->len=0;
380 4 sumanta.ch
        //AIMoves moves;
381
        AIWEIGHT u_sum = 0;
382
        int i;
383
 
384
        //b = board_new();
385
        //Board b;
386
        board_copy(board, &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 7 sumanta.ch
        //int id= PICO_initialize_PPA(threat_line);     
417
        //PICO_set_task_overlap(id, 2);
418 4 sumanta.ch
        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 8 sumanta.ch
                                if (k==1) board_copy(&b, &bwrite);
521 7 sumanta.ch
                                u_sum += threat_line(arg1, arg2, arg3, arg4,&b,&bwrite,k,loop_bound);
522 4 sumanta.ch
                        }
523
        }
524 7 sumanta.ch
        //PICO_sync_task(id, 1);
525
        //PICO_finalize_PPA(id);
526 4 sumanta.ch
/*---------------------------------------------------------------------------*/
527
        //board_copy(&b,&b_marks);
528 10 sumanta.ch
                //unsigned int index[max_size]={0};
529 12 sumanta.ch
                //#pragma bitsize index 9
530
                //#pragma internal_fast index
531 7 sumanta.ch
        AIMoves moves1;
532 10 sumanta.ch
        moves1.len=0;
533 7 sumanta.ch
        #pragma internal_blockram moves1
534 10 sumanta.ch
        /*moves = */ ai_marks(&bwrite, PIECE_THREAT(1),&moves1);
535 7 sumanta.ch
        //test(ready);
536 10 sumanta.ch
        streamsort(moves,index);
537
        moves1.utility = u_sum;
538
        moves->utility = u_sum;
539 7 sumanta.ch
        /*----------------------------
540
                rewritten for hardware
541
        ----------------------------*/
542
        //if (!aimoves_choose(&moves1, move))
543
        //      return 0;
544
        //else return 1;                
545 10 sumanta.ch
        //int ret_val;
546
        //ret_val=aimoves_choose(&moves1, move,&index[0]);
547
        //if (!ret_val)
548
        //      return 0;
549
        //else return 1;                
550 7 sumanta.ch
        /*----------------------------
551
        end rewritten for hardware
552
        ----------------------------*/
553 4 sumanta.ch
        //board_free(b);
554
        //return moves;
555 11 sumanta.ch
        return 1;
556 4 sumanta.ch
}
557
 
558
//void debug_counts(void)
559
//{
560
//        int i, sum = 0;
561
//
562
//        if (!b)
563
//                return;
564
//
565
//        g_debug("Threat counts (black, white):");
566
//        for (i = 1; i < connect_k; i++) {
567
//                g_debug("%d: %3d %3d", i, threat_counts[i][0],
568
//                        threat_counts[i][1]);
569
//                sum += threat_counts[i][0] * threat_bits(i, b->turn) -
570
//                       threat_counts[i][1] *
571
//                       threat_bits(i, other_player(b->turn));
572
//        }
573
//        if (sum > 0)
574
//                g_debug("Threat sum: %d (10^%.2f)", sum, log10((double)sum));
575
//        else if (sum < 0)
576
//                g_debug("Threat sum: %d (-10^%.2f)", sum, log10((double)-sum));
577
//        else
578
//                g_debug("Threat sum: 0");
579
//}
580
 
581
//static int threat_number(int player, int threat,threat_count_array threat_counts)
582
//{
583
//        return threat_counts.data[threat][player] / (connect_k - threat);
584
//}
585
 
586
//AIMoves *ai_priority(const Board *b)
587
//{
588
//        AIMoves *moves;
589
//        int i, j, stage[2] = {1, 1}, mask, bits;
590
//
591
//        moves = ai_threats(b);
592
//
593
//        /* Do not prioritize if we've won */
594
//        if (threat_counts[connect_k - place_p + 1][b->turn - 1]) {
595
//                moves->utility = AIW_WIN;
596
//                return moves;
597
//        }
598
//
599
//        /* Find the largest supported threat for each player */
600
//        for (i = 2; i < connect_k; i++) {
601
//                if (threat_number(0, i - 1) >= place_p &&
602
//                    threat_number(0, i) > place_p)
603
//                        stage[0] = i;
604
//                if (threat_number(1, i - 1) >= place_p &&
605
//                    threat_number(1, i) > place_p)
606
//                        stage[1] = i;
607
//        }
608
//
609
//        //if (opt_debug_stage)
610
//        //        g_debug("Stages %d/%d", stage[0], stage[1]);
611
//
612
//        /* Do not prioritize if we're losing */
613
//        if (stage[b->turn - 1] <= stage[other_player(b->turn) - 1]) {
614
//                moves->utility = -stage[other_player(b->turn) - 1];
615
//                return moves;
616
//        }
617
//
618
//        /* Threats above the player's stage are no more valuable than the
619
//           stage */
620
//        bits = 1 << (stage[b->turn - 1] * BITS_PER_THREAT);
621
//        mask = bits - 1;
622
//        for (i = 0; i < moves->len; i++) {
623
//                AIWEIGHT w = moves->data[i].weight, w2;
624
//
625
//                if (w < AIW_THREAT_MAX && w >= bits) {
626
//                        w2 = w & mask;
627
//                        w = w & ~mask;
628
//                        for (j = stage[b->turn - 1];
629
//                             w && j < connect_k - place_p + 1; j++) {
630
//                                w = w >> BITS_PER_THREAT;
631
//                                w2 += w & mask;
632
//                        }
633
//                        moves->data[i].weight = w2;
634
//                }
635
//        }
636
//
637
//        /* Stage determines weight */
638
//        moves->utility = stage[b->turn - 1];
639
//        return moves;
640
//}
641
/*AIMoves*/ void ai_marks(Board *b, PIECE minimum,AIMoves *moves)
642
{
643
        //#pragma read_write_ports b.data combined 2
644 12 sumanta.ch
        //#pragma internal_blockram b
645 4 sumanta.ch
        //#pragma no_memory_analysis b
646
        //AIMoves *moves = aimoves_new();
647
        //AIMoves moves;
648
        //AIMoves moves[361];
649
        AIMove move;
650
        PIECE p;
651 7 sumanta.ch
        //moves_fifo.resoet();
652
        AIMove m;
653
        #pragma num_iterations(19,19,19)
654 4 sumanta.ch
        for (move.y = 0; move.y < board_size; move.y++)
655 7 sumanta.ch
        #pragma num_iterations(19,19,19)
656
                for (move.x = 0; move.x < board_size; move.x++){
657 4 sumanta.ch
                        if ((p = piece_at(b, move.x, move.y)) >= minimum) {
658
                                move.weight = p - PIECE_THREAT0;
659
                                aimoves_set(moves, &move);
660 7 sumanta.ch
                                pico_stream_output_queue(move);
661
                                //cout<<"push"<<move.weight<<endl;
662
                        //ready=ready+1;
663
                        //cout<<"READY"<<*ready<<endl;
664
                        }else {
665
                        m.weight =-100;
666
                        pico_stream_output_queue(m);
667
                                //cout<<"push"<<m.weight<<endl;
668
                        }
669
                        //if((move.y == board_size-1) && (move.x == board_size-1)){
670
                        //      m.weight=-1;m.x=-1;m.y=-1;
671
                        //        /*if((ready))*/ {pico_stream_output_queue(m);/*cout<<"push_finish"<<m.weight<<endl;*/}
672
                        //
673
                        //}
674
                }
675
        //moves_fifo.active=0;
676 4 sumanta.ch
        //return moves;
677 7 sumanta.ch
        int i;
678
        //for (i=0;i<10;i++) {*ready=i;pico_stream_output_x(i); }
679
        //for (i=0;i<10;i++) {if(!moves_fifo1.full()) moves_fifo1.push(m); }
680
        //for (i=0;i<10;i++) {
681
        //      *ready=i;pico_stream_output_queue(m); 
682
        //      if(i==9){
683
        //              m.weight=-1;
684
        //              pico_stream_output_queue(m);
685
        //      }
686
        //              
687
        //      }
688
 
689 4 sumanta.ch
}
690 7 sumanta.ch
void streamsort(AIMoves *moves,unsigned int *index){
691
/* Insertion sort for streaming*/
692
                AIMove val;
693
                AIMove data[361]={{-1},{-1},{-1}};
694
                //unsigned int index[361];
695 12 sumanta.ch
                //#pragma bitsize index 9
696
                //#pragma internal_fast index
697 7 sumanta.ch
                int i,j,k;
698
                unsigned int len=0;
699
                moves->data[0].weight=-100;
700
//              while(1) {
701
//                      if(!moves_fifo.empty()){ 
702
//                              val=moves_fifo.pop();
703
//                              for(i=0;i<len;i++){
704
//                                      if (list[i].weight < val.weight){
705
//                                      for(j=len-1;j>i-1;j--){
706
//                                              list[j+1]=list[j];
707
//                                      }
708
//                                      break;
709
//                                      }
710
//                              }
711
//                                      list[i]=val;
712
//                                      len++;
713
//                      }
714
//                      else break;
715
//                      //if(!moves_fifo.active && moves_fifo.empty()) break;
716
//              }
717
                //while(1) {
718
                //int count=0;
719 8 sumanta.ch
                #pragma num_iterations(1,150,368)
720
                for(k=0;k<368;k++){
721 7 sumanta.ch
                        //count++;
722
                        //cout<<count<<endl;
723 8 sumanta.ch
                        if (k>6){
724 7 sumanta.ch
 
725
                        //if(ready>5){ 
726
                                val=pico_stream_input_queue();
727
                                //cout<<"popped"<<","<<val.weight<<" "<<val.x<<" "<<val.y<<endl;
728
                                if(val.weight==-1) {moves->len=len;break;}
729
                                else if(val.weight==-100) continue;
730
                                #pragma num_iterations(0,150,361)
731
                                for(i=0;i<len;i++){
732 11 sumanta.ch
                                        if (moves->data[i].weight < val.weight){
733 7 sumanta.ch
                                        for(j=len-1;j>i-1;j--){
734 11 sumanta.ch
                                                moves->data[j+1]=moves->data[j];
735
                                                //index[j+1]=index[j];
736 7 sumanta.ch
                                        }
737
                                        break;
738
                                        }
739
                                }
740
                                        index[i]=len;
741 11 sumanta.ch
                                        moves->data[i]=val;
742 7 sumanta.ch
                                        len++;
743
                                //cout<<"STREAMSORT"<<":";
744
                        //}
745
                        /*else*/ //{moves->len=len;break;}
746
                        }
747
                }
748
                moves->len=len;
749
                                //cout<<"STREAMSORT"<<":"<<moves->len<<endl;
750
                                //for(i=0;i<len;i++) cout<<moves->data[i].weight<<",";
751
                                //cout<<endl;
752
                                //for(i=0;i<len;i++) cout<<moves->data[index[i]].weight<<",";
753
                                //cout<<endl;
754
}
755 4 sumanta.ch
static gboolean is_adjacent( Board *b, BCOORD x, BCOORD y, int dist)
756
{
757
        int dx, dy, count;
758
        #pragma bitsize dx 4
759
        #pragma bitsize dy 4
760
        PIECE p;
761
 
762
        if (!piece_empty(piece_at(b, x, y)))
763
                return FALSE;
764
        for (dy = -1; dy < 2; dy++)
765
                for (dx = -1; dx < 2; dx++) {
766
                        if (!dx && !dy)
767
                                continue;
768
                        count = count_pieces(b, x, y, PIECE_NONE, dx, dy, &p);
769
                        if (count - 1 < dist && p != PIECE_NONE)
770
                                return TRUE;
771
                }
772
        return FALSE;
773
}
774
/*AIMoves **/ void enum_adjacent(Board *b, int dist,AIMoves *moves,unsigned int current_random)
775
{
776
        //AIMoves *moves;
777
        AIMove move;
778
 
779
        move.weight = AIW_NONE;
780
        //moves = aimoves_new();
781 7 sumanta.ch
        #pragma num_iterations(1,9,19)
782
        for (move.y = 0; move.y < board_size; move.y++){
783
        #pragma num_iterations(1,9,19)
784
                for (move.x = 0; move.x < board_size; move.x++){
785 4 sumanta.ch
                        if (is_adjacent(b, move.x, move.y, dist))
786
                                aimoves_append(moves, &move);
787 7 sumanta.ch
                }
788
        }
789 4 sumanta.ch
        //aimoves_shuffle(moves,current_random);
790
        //return moves;
791
}
792
/*AIMoves **/void ai_adjacent( Board *b, AIMove *move,unsigned int current_random)
793
{
794
        //#pragma read_write_ports board.data combined 2
795
        #pragma internal_blockram b
796
        //#pragma no_memory_analysis b
797
 
798
        /*static*/ AIMoves moves;//={{0,0,0,{{0,0,0}}}};
799
        //#pragma read_write_ports moves.data combined 3
800
        #pragma internal_blockram moves
801
        //#pragma no_memory_analysis moves
802
        //#pragma read_write_ports moves.data combined 3
803
        //#pragma internal_blockram moves
804
        //#pragma no_memory_analysis moves
805
        //AIMove move;
806 7 sumanta.ch
        unsigned int index[1]={0};
807 4 sumanta.ch
        //AIMoves *moves;
808
        moves.len=0;
809
        /* Get all open tiles adjacent to any piece */
810
        /*moves =*/ enum_adjacent(b, 1,&moves,current_random);
811
        if (moves.len){
812 7 sumanta.ch
                aimoves_choose(&moves, move,&index[0]);
813 4 sumanta.ch
 
814
                return ;//moves;
815
        }
816
        /* Play in the middle if there are no open adjacent tiles */
817
        move->x = board_size / 2;
818
        move->y = board_size / 2;
819
        move->weight = AIW_NONE;
820
        //aimoves_append(&moves, move);
821
        //aimoves_choose(&moves, move);
822
        //return moves;
823
}

powered by: WebSVN 2.1.0

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