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

Subversion Repositories connect-6

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

powered by: WebSVN 2.1.0

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