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

Subversion Repositories connect-6

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 sumanta.ch
/*
2
   connect6.cpp
3
   June 9, 2011
4
   This file contains the game AI
5
   By Kevin Nam
6
 
7
 */
8
 
9
//#include <time.h>
10
//#include <stdlib.h>
11
//
12
//#include "util.h"
13
//#include "connect6.h"
14
//#include<stdio.h>
15
#include "./shared.h"
16
//#ifdef PICO_SYSC_SIM
17
#include "pico.h"
18
//#endif
19
 
20
// Subtract this many points for moves at the edges.
21
#define EDGEPENALTY 5
22
 
23
using namespace std;
24
 
25
/*  The cost function simply counts all of the consecutive stones of same colour in
26
    every direction from the spot for which the points is being calculated.
27
 
28
Ex:
29
 
30
.DDLL
31
.DLDD
32
DXDDD
33
...D.
34
 
35
Above, X is the spot being calculated.
36
The points would be 2 (above) + 2(topright) + 3(right) + 1 (left) = 8.
37
It treats opponent's stones and own stones with equal weighting.
38
 
39
Return 0 if the spot y,x is already taken, else return the calculated value
40
 
41
 */
42
void move_to_ascii(int x,int y, char *move){
43
    if (y >= 10){
44
        move[0] = '1';
45
        y -= 10;
46
    } else {
47
        move[0] = '0';
48
    }
49
    if      (y == 0) move[1] = '0';
50
    else if (y == 1) move[1] = '1';
51
    else if (y == 2) move[1] = '2';
52
    else if (y == 3) move[1] = '3';
53
    else if (y == 4) move[1] = '4';
54
    else if (y == 5) move[1] = '5';
55
    else if (y == 6) move[1] = '6';
56
    else if (y == 7) move[1] = '7';
57
    else if (y == 8) move[1] = '8';
58
    else if (y == 9) move[1] = '9';
59
 
60
    // Do same for x.
61
    if (x >= 10){
62
        move[2] = '1';
63
        x -= 10;
64
    } else {
65
        move[2] = '0';
66
    }
67
    if      (x == 0) move[3] = '0';
68
    else if (x == 1) move[3] = '1';
69
    else if (x == 2) move[3] = '2';
70
    else if (x == 3) move[3] = '3';
71
    else if (x == 4) move[3] = '4';
72
    else if (x == 5) move[3] = '5';
73
    else if (x == 6) move[3] = '6';
74
    else if (x == 7) move[3] = '7';
75
    else if (x == 8) move[3] = '8';
76
    else if (x == 9) move[3] = '9';
77
 
78
}
79
 
80
static int char_to_int(short x){
81
        if(x>=48)
82
                return x-48;
83
        else
84
                return 0;
85
}
86
 
87
 
88
/*
89
   The AI Function that calls the cost function for every spot on the board.
90
   It returns the location with the highest points. In the event of a tie, randomly decide.
91
   Input the board and the colour being played.
92
   Puts the move (in ASCII chars) inside move[4]. This is from [1 ... 19]
93
   Puts the move (in integers) in moveY and moveX. This is from [0 ... 18]
94
 */
95
//void backup_move(Board *board, AIMoves *moves,AIMove *move){
96
////when the threat doesn't return any good move
97
////put in a single function to parition and speedup synthesis
98
//      //#pragma read_write_ports board.data combined 2
99
//      //#pragma internal_blockram myboard
100
//      //#pragma no_memory_analysis myboard
101
//      //#pragma read_write_ports moves.data combined 3
102
//      //#pragma internal_blockram moves
103
//      //#pragma no_memory_analysis moves
104
//                      move->x=-1;
105
//                      move->y=-1;
106
//                        if (!aimoves_choose(moves, move)) {
107
//                               // aimoves_free(moves);
108
//                              //moves->len=0;
109
//                            //    /*moves = */ai_adjacent(board,moves);
110
//                                aimoves_choose(moves, move);
111
//                        }
112
//}
113
int id;
114
int connect6ai_synth(int firstmove,char movein[8], char colour, char moveout[8]){
115
        //#ifdef PICO_SYSC_SIM
116
        //id= PICO_initialize_PPA(ai_threats);  
117
        //PICO_set_task_overlap(id, 2);
118
        //#endif
119
        #pragma bitsize firstmove 17
120
        char moveoutm[8];
121
        #pragma internal_blockram moveoutm
122
        short x,y,highx = 0;
123
        //Board *myboard ;
124
        static Board myboard;//={0,0,0,0,0,0,0,0,0,0,0,{{0}}};
125
        //#pragma read_write_ports board.data combined 2
126
        //#pragma preserve_array myboard.data 
127
        #pragma internal_blockram myboard
128
        //#pragma multi_buffer myboard 2
129
        //#pragma no_memory_analysis myboard
130
        static unsigned int current_random = 10;
131
        AIMove move,move_threat,move_adj;
132
        //#pragma internal_blockram move
133
        //#pragma no_memory_analysis move
134
        if(firstmove==0||firstmove==1) {
135
                        //my_srandom(1,&current_random);
136
                        new_game(&myboard,board_size);
137
        }
138
        if(firstmove==0) myboard.moves_left=1;
139
        else myboard.moves_left=2;
140
 
141
        //-------------------------------------------------------------------------
142
        if((firstmove >= 1)){
143
                //update the board
144
                y = char_to_int(movein[0])*10 + char_to_int(movein[1]) - 1;
145
                x = char_to_int(movein[2])*10 + char_to_int(movein[3]) - 1;
146
                if(colour==68){//'D')
147
                        //myboard[y][x] = (char)2;//76;//'L';
148
        place_piece_type(&myboard,x,y,PIECE_WHITE);
149
        myboard.turn=PIECE_BLACK;
150
                }else{
151
                        //board[y][x] = (char)1;//68;//'D';
152
        place_piece_type(&myboard,x,y,PIECE_BLACK);
153
        myboard.turn=PIECE_WHITE;
154
                }
155
        }
156
        if((firstmove >=3)){
157
                //update the board
158
                y = char_to_int(movein[4])*10 + char_to_int(movein[5]) - 1;
159
                x = char_to_int(movein[6])*10 + char_to_int(movein[7]) - 1;
160
                if(colour==68){//'D')
161
                        //board[y][x] = (char)2;//76;//'L';
162
        place_piece_type(&myboard,x,y,PIECE_WHITE);
163
        myboard.turn=PIECE_BLACK;
164
                }else{
165
                        //board[y][x] = (char)1;//68;//'D';
166
        place_piece_type(&myboard,x,y,PIECE_BLACK);
167
        myboard.turn=PIECE_WHITE;
168
                }
169
        }
170
        int i;
171
        #pragma bitsize i 6
172
 
173
        //#pragma num_iterations(1,2,2)
174
        //#pragma unroll
175
        Player player;
176
        player.depth=2;
177
        player.branch=2;
178
        for(i=myboard.moves_left;i>0;i--){
179
                  //aimoves_free(&moves);
180
                        move.x=-1;
181
                        move.y=-1;
182
 
183
                        if (!search(&myboard,&move_threat,&player)){
184
                                //aimoves_free(&moves);
185
                                //moves.len=0;
186
                                ai_adjacent(&myboard,&move_adj,current_random);
187
                                move.x=move_adj.x;
188
                                move.y=move_adj.y;
189
                        }else{
190
                                move.x=move_threat.x;
191
                                move.y=move_threat.y;
192
                        }
193
 
194
                        //backup_move(&myboard,&moves,&move);
195
        //printf("DEBUG1:%d ",move.x);
196
        // Modify the board based on current move.
197
        place_piece_type(&myboard,move.x,move.y,myboard.turn);
198
        /// Convert the int coordinates to corresponding ASCII chars
199
 
200
        //if(firstmove==0)
201
        //move_to_ascii(move.x+1,move.y+1,&moveout[0]);         
202
        //else if(myboard.moves_left==2)
203
        //move_to_ascii(move.x+1,move.y+1,&moveout[0]);         
204
        //else
205
        //move_to_ascii(move.x+1,move.y+1,&moveout[4]);         
206
        if(firstmove==0)
207
        move_to_ascii(move.x+1,move.y+1,&moveoutm[0]);
208
        else
209
        move_to_ascii(move.x+1,move.y+1,&moveoutm[8-4*i]);
210
        myboard.moves_left--;
211
        }
212
        if(firstmove==0){
213
        #pragma unroll
214
        for(i=0;i<4;i++)
215
                moveout[i]=moveoutm[i];
216
        }else{
217
        #pragma unroll
218
        for(i=0;i<8;i++)
219
                moveout[i]=moveoutm[i];
220
        }
221
        //if(firstmove==0){
222
        //moveout[0]=moveoutm[0];
223
        //moveout[1]=moveoutm[1];
224
        //moveout[2]=moveoutm[2];
225
        //moveout[3]=moveoutm[3];
226
        //}else{
227
        //moveout[0]=moveoutm[0];
228
        //moveout[1]=moveoutm[1];
229
        //moveout[2]=moveoutm[2];
230
        //moveout[3]=moveoutm[3];
231
        //moveout[4]=moveoutm[4];
232
        //moveout[5]=moveoutm[5];
233
        //moveout[6]=moveoutm[6];
234
        //moveout[7]=moveoutm[7];
235
        //}
236
 
237
        ////-------------------------------------------------------------------------
238
        //if(firstmove>=1){
239
        ////aimoves_free(&moves);
240
        //moves.len=0;
241
        //myboard.moves_left=1;
242
        ///*moves=*/ai_threats(&myboard,&moves);
243
        //              move.x=-1;
244
        //              move.y=-1;
245
        //                if (!aimoves_choose(&moves, &move)) {
246
        //                        //aimoves_free(&moves);
247
        //                      moves.len=0;
248
        //                        /*moves = */ai_adjacent(&myboard,&moves);
249
        //                        aimoves_choose(&moves, &move);
250
        //                }
251
        //              //backup_move(&myboard,&moves,&move);
252
        ////printf("DEBUG2%d\n",move.x);
253
        //// Modify the board based on current move.
254
        //place_piece_type(&myboard,move.x,move.y,myboard.turn);
255
        //
256
        //      /// Convert the int coordinates to corresponding ASCII chars
257
        //move_to_ascii(move.x+1,move.y+1,&moveout[4]);         
258
        //}
259
        //#ifdef PICO_SYSC_SIM
260
        //PICO_sync_task(id, 1);
261
        //PICO_finalize_PPA(id);
262
        //#endif
263
        return 0;
264
}
265
 
266
 
267
 

powered by: WebSVN 2.1.0

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