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

Subversion Repositories connect-6

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 sumanta.ch
/*  main.cpp
2
    June 9,2011
3
 
4
    Software connect6 AI program.
5
    Have your board polling for its colour before starting this program.
6
 
7
   commandline option:
8
   -port <serialport>
9
   Ex: "./connect6 -port /dev/ttyUSB0"
10
 
11
   By: Kevin Nam
12
*/
13
 
14
#include <iostream>
15
#include <stdlib.h>
16
#include <stdio.h>
17
#include <string.h>
18
#include <unistd.h>
19
#include <fcntl.h>
20
#include <errno.h>
21
#include <termios.h>
22
#include <sys/time.h>
23
#include "util.h"
24
#include "connect6.h"
25
#include "connect6_synth.h"
26
#ifndef EMUL
27
#include "pico.h"
28
#endif
29
#include "shared.h"
30
 
31
#ifndef EMUL
32
// The AI has as much time as it wants, but moves after 1 second. Default is to wait 2 seconds
33
#define AI_WAIT_TIME 0.1
34
 
35
// FPGA has 1 second to make its move
36
#define MOVE_TIME_LIMIT 0.1
37
#endif
38
 
39
#ifdef EMUL
40
// The AI has as much time as it wants, but moves after 1 second. Default is to wait 2 seconds
41
#define AI_WAIT_TIME 0.1
42
 
43
// FPGA has 1 second to make its move
44
#define MOVE_TIME_LIMIT 1
45
#endif
46
using namespace std;
47
extern "C" int main(int argc, char **argv);
48
// commandline option: -port <serialport>
49
int main(int argc, char **argv){
50
    //for verification two runs and a reference board
51
    int i,j,k;
52
    char ref_board[19][19] = {{ 0 }};
53
 
54
    char board[19][19] = {{ 0 }};
55
    char move[4];
56
    char moveport[8]={0};
57
    char moveportout[8]={0};
58
    int movecount=0;
59
    int y,x;
60
    char winning_colour;
61
 
62
#ifdef EMUL
63
    // Get the serial port
64
        int port = select_com_port(argc,argv);
65
#endif
66
    // Get software AI's colour
67
    char AI_colour = select_AI_colour(argc,argv);
68
    char FPGA_colour;
69
#ifndef EMUL
70
        int id = PICO_initialize_PPA(connect6ai_synth);
71
#endif
72
    // Take care of the first few moves (including sending the colour)
73
    if (AI_colour == 'D'){
74
        FPGA_colour = 'L';
75
#ifdef EMUL
76
        write(port, "L",1);
77
#endif
78
 
79
        wait(AI_WAIT_TIME);
80
 
81
        // AI makes a move
82
        connect6ai(board,AI_colour,move);
83
        movecount++;
84
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
85
#ifdef EMUL
86
        write(port,&move[0],1);
87
        write(port,&move[1],1);
88
        write(port,&move[2],1);
89
        write(port,&move[3],1);
90
#endif
91
        print_board(board);
92
        print_board_file(board);
93
 
94
        moveport[0]=move[0];
95
        moveport[1]=move[1];
96
        moveport[2]=move[2];
97
        moveport[3]=move[3];
98
 
99
        moveport[4]=0;
100
        moveport[5]=0;
101
        moveport[6]=0;
102
        moveport[7]=0;
103
 
104
    } else {
105
        FPGA_colour = 'D';
106
#ifdef EMUL
107
        write(port, "D",1);
108
#endif 
109
        wait(MOVE_TIME_LIMIT);
110
 
111
        move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
112
        ////////// Get Opponent's move
113
#ifdef EMUL
114
        read(port,&move[0],1);
115
        read(port,&move[1],1);
116
        read(port,&move[2],1);
117
        read(port,&move[3],1);
118
#endif
119
        // FPGA makes a move
120
        connect6ai_synth(movecount,moveport,FPGA_colour,moveportout);
121
        movecount++;
122
#ifndef EMUL
123
        move[0]=moveportout[0];move[1]=moveportout[1];move[2]=moveportout[2];move[3]=moveportout[3];
124
        //connect6ai_golden(board,FPGA_colour,move);
125
#endif
126
        if (move[0] == 0 || move[1] == 0 || move[2] == 0 || move[3] == 0){
127
            cout<<"FPGA has not completed a move in 1 second. Exiting."<<endl;
128
            return 0;
129
        }
130
        cout<<"FPGA MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
131
        y = char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
132
        x = char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
133
        if (check_move_validity(board,y,x) < 0) return 0;
134
        board[y][x] = FPGA_colour;
135
        print_board(board);
136
        print_board_file(board);
137
#ifdef EMUL
138
        if(move[0]!=moveportout[0]||move[1]!=moveportout[1]||move[2]!=moveportout[2]||move[3]!=moveportout[3]) {
139
                printf("EMULATION FAIL: BEHAVIOUR Doesn't match \"%c%c%c%c\" X \"%c%c%c%c\"\n",move[0],move[1],move[2],move[3],moveportout[0],moveportout[1],moveportout[2],moveportout[3]);
140
                //exit(1);
141
        }
142
#endif
143
        wait(AI_WAIT_TIME);
144
 
145
 
146
        // AI makes a move
147
        connect6ai(board,AI_colour,move);
148
        movecount++;
149
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
150
#ifdef EMUL
151
        write(port,&move[0],1);
152
        write(port,&move[1],1);
153
        write(port,&move[2],1);
154
        write(port,&move[3],1);
155
#endif
156
        moveport[0]=move[0];
157
        moveport[1]=move[1];
158
        moveport[2]=move[2];
159
        moveport[3]=move[3];
160
        // AI makes a move
161
        connect6ai(board,AI_colour,move);
162
        movecount++;
163
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
164
#ifdef EMUL
165
        write(port,&move[0],1);
166
        write(port,&move[1],1);
167
        write(port,&move[2],1);
168
        write(port,&move[3],1);
169
#endif  
170
        moveport[4]=move[0];
171
        moveport[5]=move[1];
172
        moveport[6]=move[2];
173
        moveport[7]=move[3];
174
        print_board_file(board);
175
    }
176
 
177
    // Alternate between receiving and sending moves
178
    while(1){
179
        wait(MOVE_TIME_LIMIT);
180
 
181
        // Get Opponent's move
182
        move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
183
#ifdef EMUL
184
        read(port,&move[0],1);
185
        read(port,&move[1],1);
186
        read(port,&move[2],1);
187
        read(port,&move[3],1);
188
#endif
189
        connect6ai_synth(movecount,moveport,FPGA_colour,moveportout);
190
        movecount++;
191
#ifndef EMUL
192
        move[0]=moveportout[0];move[1]=moveportout[1];move[2]=moveportout[2];move[3]=moveportout[3];
193
#endif
194
        //connect6ai_golden(board,FPGA_colour,move);
195
 
196
        cout<<"FPGA MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
197
        if (move[0] == 0 || move[1] == 0 || move[2] == 0 || move[3] == 0){
198
            cout<<"FPGA has not completed a move in 1 second. Exiting."<<endl;
199
            break;
200
        }
201
        y = char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
202
        x = char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
203
#ifdef EMUL
204
        if(move[0]!=moveportout[0]||move[1]!=moveportout[1]||move[2]!=moveportout[2]||move[3]!=moveportout[3]) {
205
                printf("EMULATION FAIL: BEHAVIOUR Doesn't match \"%c%c%c%c\" X \"%c%c%c%c\"\n",move[0],move[1],move[2],move[3],moveportout[0],moveportout[1],moveportout[2],moveportout[3]);
206
                //exit(1);
207
        }
208
#endif
209
        if (check_move_validity(board,y,x) < 0) break;
210
        board[y][x] = FPGA_colour;
211
        winning_colour = check_for_win(board);
212
        if (winning_colour == AI_colour){
213
            cout<<"AI has won!" << movecount << " moves " << "Exiting."<<endl;
214
            break;
215
        } else if (winning_colour == FPGA_colour){
216
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
217
            break;
218
        }
219
        if (check_board_full(board) < 0){
220
            cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
221
                break;
222
        }
223
        // Get Opponent's move
224
        move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
225
#ifdef EMUL
226
        read(port,&move[0],1);
227
        read(port,&move[1],1);
228
        read(port,&move[2],1);
229
        read(port,&move[3],1);
230
#endif
231
        ////connect6ai_synth(board,FPGA_colour,move);
232
        movecount++;
233
#ifndef EMUL
234
        move[0]=moveportout[4];move[1]=moveportout[5];move[2]=moveportout[6];move[3]=moveportout[7];
235
#endif
236
        //connect6ai_golden(board,FPGA_colour,move);
237
        cout<<"FPGA MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
238
        if (move[0] == 0 || move[1] == 0 || move[2] == 0 || move[3] == 0){
239
            cout<<"FPGA has not completed a move in 1 second. Exiting."<<endl;
240
            break;
241
        }
242
        y = char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
243
        x = char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
244
#ifdef EMUL
245
        if(move[0]!=moveportout[4]||move[1]!=moveportout[5]||move[2]!=moveportout[6]||move[3]!=moveportout[7]) {
246
                printf("EMULATION FAIL: BEHAVIOUR Doesn't match \"%c%c%c%c\" X \"%c%c%c%c\"\n",move[0],move[1],move[2],move[3],moveportout[4],moveportout[5],moveportout[6],moveportout[7]);
247
                //exit(1);
248
        }
249
#endif
250
        if (check_move_validity(board,y,x) < 0) break;
251
        board[y][x] = FPGA_colour;
252
        winning_colour = check_for_win(board);
253
        if (winning_colour == AI_colour){
254
            cout<<"AI has won! " << movecount << " moves " << "Exiting."<<endl;
255
            break;
256
        } else if (winning_colour == FPGA_colour){
257
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
258
            break;
259
        }
260
        if (check_board_full(board) < 0) {
261
                cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
262
                break;
263
        }
264
        print_board(board);
265
        print_board_file(board);
266
 
267
        wait(AI_WAIT_TIME);
268
 
269
        // AI makes a move
270
        connect6ai(board,AI_colour,move);
271
        movecount++;
272
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
273
        //if(movecount >=20) return 0 ; //reducing length of simulation
274
        winning_colour = check_for_win(board);
275
        if (winning_colour == AI_colour){
276
            cout<<"AI has won! " << movecount << " moves " << "Exiting."<<endl;
277
            break;
278
        } else if (winning_colour == FPGA_colour){
279
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
280
            break;
281
        }
282
        if (check_board_full(board) < 0){
283
                cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
284
                 break;
285
        }
286
#ifdef EMUL
287
        write(port,&move[0],1);
288
        write(port,&move[1],1);
289
        write(port,&move[2],1);
290
        write(port,&move[3],1);
291
#endif
292
        moveport[0]=move[0];
293
        moveport[1]=move[1];
294
        moveport[2]=move[2];
295
        moveport[3]=move[3];
296
        // AI makes a move
297
        connect6ai(board,AI_colour,move);
298
        movecount++;
299
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
300
        winning_colour = check_for_win(board);
301
        if (winning_colour == AI_colour){
302
            cout<<"AI has won! " << movecount << " moves " << "Exiting."<<endl;
303
            break;
304
        } else if (winning_colour == FPGA_colour){
305
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
306
            break;
307
        }
308
        if (check_board_full(board) < 0) {
309
            cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
310
                break;
311
        }
312
#ifdef EMUL
313
        write(port,&move[0],1);
314
        write(port,&move[1],1);
315
        write(port,&move[2],1);
316
        write(port,&move[3],1);
317
#endif
318
        moveport[4]=move[0];
319
        moveport[5]=move[1];
320
        moveport[6]=move[2];
321
        moveport[7]=move[3];
322
        print_board(board);
323
        print_board_file(board);
324
    }
325
 
326
#ifndef EMUL
327
PICO_finalize_PPA(id);
328
#endif
329
 
330
    return 0;
331
 
332
}

powered by: WebSVN 2.1.0

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