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 13

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

powered by: WebSVN 2.1.0

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