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 18

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
// The AI has as much time as it wants, but moves after 1 second. Default is to wait 2 seconds
32
#define AI_WAIT_TIME 0.1
33
 
34
// FPGA has 1 second to make its move
35
#define MOVE_TIME_LIMIT 0.1
36
 
37
using namespace std;
38
extern "C" int main(int argc, char **argv);
39
// commandline option: -port <serialport>
40
int main(int argc, char **argv){
41
    //for verification two runs and a reference board
42
    int i,j,k;
43
    char ref_board[19][19] = {{ 0 }};
44
 
45
    char board[19][19] = {{ 0 }};
46
    char move[4];
47
    char moveport[8]={0};
48
    char moveportout[8]={0};
49
    int movecount=0;
50
    int y,x;
51
    char winning_colour;
52
 
53
#ifdef EMUL
54
    // Get the serial port
55
        int port = select_com_port(argc,argv);
56
#endif
57
    // Get software AI's colour
58
    char AI_colour = select_AI_colour(argc,argv);
59
    char FPGA_colour;
60
#ifndef EMUL
61
        int id = PICO_initialize_PPA(connect6ai_synth);
62
#endif
63
    // Take care of the first few moves (including sending the colour)
64
    if (AI_colour == 'D'){
65
        FPGA_colour = 'L';
66
#ifdef EMUL
67
        write(port, "L",1);
68
#endif
69
 
70
        wait(AI_WAIT_TIME);
71
 
72
        // AI makes a move
73
        connect6ai(board,AI_colour,move);
74
        movecount++;
75
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
76
#ifdef EMUL
77
        write(port,&move[0],1);
78
        write(port,&move[1],1);
79
        write(port,&move[2],1);
80
        write(port,&move[3],1);
81
#endif
82
        print_board(board);
83
        print_board_file(board);
84
 
85
        moveport[0]=move[0];
86
        moveport[1]=move[1];
87
        moveport[2]=move[2];
88
        moveport[3]=move[3];
89
 
90
        moveport[4]=0;
91
        moveport[5]=0;
92
        moveport[6]=0;
93
        moveport[7]=0;
94
 
95
    } else {
96
        FPGA_colour = 'D';
97
#ifdef EMUL
98
        write(port, "D",1);
99
#endif 
100
        wait(MOVE_TIME_LIMIT);
101
 
102
        move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
103
        ////////// Get Opponent's move
104
#ifdef EMUL
105
        read(port,&move[0],1);
106
        read(port,&move[1],1);
107
        read(port,&move[2],1);
108
        read(port,&move[3],1);
109
#endif
110
        // FPGA makes a move
111
        connect6ai_synth(movecount,moveport,FPGA_colour,moveportout);
112
        movecount++;
113
#ifndef EMUL
114
        move[0]=moveportout[0];move[1]=moveportout[1];move[2]=moveportout[2];move[3]=moveportout[3];
115
        //connect6ai_golden(board,FPGA_colour,move);
116
#endif
117
        if (move[0] == 0 || move[1] == 0 || move[2] == 0 || move[3] == 0){
118
            cout<<"FPGA has not completed a move in 1 second. Exiting."<<endl;
119
            return 0;
120
        }
121
        cout<<"FPGA MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
122
        y = char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
123
        x = char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
124
        if (check_move_validity(board,y,x) < 0) return 0;
125
        board[y][x] = FPGA_colour;
126
        print_board(board);
127
        print_board_file(board);
128
        wait(AI_WAIT_TIME);
129
 
130
 
131
        // AI makes a move
132
        connect6ai(board,AI_colour,move);
133
        movecount++;
134
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
135
#ifdef EMUL
136
        write(port,&move[0],1);
137
        write(port,&move[1],1);
138
        write(port,&move[2],1);
139
        write(port,&move[3],1);
140
#endif
141
        moveport[0]=move[0];
142
        moveport[1]=move[1];
143
        moveport[2]=move[2];
144
        moveport[3]=move[3];
145
        // AI makes a move
146
        connect6ai(board,AI_colour,move);
147
        movecount++;
148
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
149
#ifdef EMUL
150
        write(port,&move[0],1);
151
        write(port,&move[1],1);
152
        write(port,&move[2],1);
153
        write(port,&move[3],1);
154
#endif  
155
        moveport[4]=move[0];
156
        moveport[5]=move[1];
157
        moveport[6]=move[2];
158
        moveport[7]=move[3];
159
        print_board_file(board);
160
    }
161
 
162
    // Alternate between receiving and sending moves
163
    while(1){
164
        wait(MOVE_TIME_LIMIT);
165
 
166
        // Get Opponent's move
167
        move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
168
#ifdef EMUL
169
        read(port,&move[0],1);
170
        read(port,&move[1],1);
171
        read(port,&move[2],1);
172
        read(port,&move[3],1);
173
#endif
174
        connect6ai_synth(movecount,moveport,FPGA_colour,moveportout);
175
        movecount++;
176
#ifndef EMUL
177
        move[0]=moveportout[0];move[1]=moveportout[1];move[2]=moveportout[2];move[3]=moveportout[3];
178
#endif
179
        //connect6ai_golden(board,FPGA_colour,move);
180
 
181
        cout<<"FPGA MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
182
        if (move[0] == 0 || move[1] == 0 || move[2] == 0 || move[3] == 0){
183
            cout<<"FPGA has not completed a move in 1 second. Exiting."<<endl;
184
            break;
185
        }
186
        y = char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
187
        x = char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
188
        if (check_move_validity(board,y,x) < 0) break;
189
        board[y][x] = FPGA_colour;
190
        winning_colour = check_for_win(board);
191
        if (winning_colour == AI_colour){
192
            cout<<"AI has won!" << movecount << " moves " << "Exiting."<<endl;
193
            break;
194
        } else if (winning_colour == FPGA_colour){
195
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
196
            break;
197
        }
198
        if (check_board_full(board) < 0){
199
            cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
200
                break;
201
        }
202
        // Get Opponent's move
203
        move[0] = 0; move[1] = 0; move[2] = 0; move[3] = 0;
204
#ifdef EMUL
205
        read(port,&move[0],1);
206
        read(port,&move[1],1);
207
        read(port,&move[2],1);
208
        read(port,&move[3],1);
209
#endif
210
        ////connect6ai_synth(board,FPGA_colour,move);
211
        movecount++;
212
#ifndef EMUL
213
        move[0]=moveportout[4];move[1]=moveportout[5];move[2]=moveportout[6];move[3]=moveportout[7];
214
#endif
215
        //connect6ai_golden(board,FPGA_colour,move);
216
        cout<<"FPGA MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
217
        if (move[0] == 0 || move[1] == 0 || move[2] == 0 || move[3] == 0){
218
            cout<<"FPGA has not completed a move in 1 second. Exiting."<<endl;
219
            break;
220
        }
221
        y = char_to_int(move[0])*10 + char_to_int(move[1]) - 1;
222
        x = char_to_int(move[2])*10 + char_to_int(move[3]) - 1;
223
        if (check_move_validity(board,y,x) < 0) break;
224
        board[y][x] = FPGA_colour;
225
        winning_colour = check_for_win(board);
226
        if (winning_colour == AI_colour){
227
            cout<<"AI has won! " << movecount << " moves " << "Exiting."<<endl;
228
            break;
229
        } else if (winning_colour == FPGA_colour){
230
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
231
            break;
232
        }
233
        if (check_board_full(board) < 0) {
234
                cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
235
                break;
236
        }
237
        print_board(board);
238
        print_board_file(board);
239
 
240
        wait(AI_WAIT_TIME);
241
 
242
        // AI makes a move
243
        connect6ai(board,AI_colour,move);
244
        movecount++;
245
        cout<<"AI MOVE: "<<move[0]<<move[1]<<move[2]<<move[3]<<endl;
246
        //if(movecount >=20) return 0 ; //reducing length of simulation
247
        winning_colour = check_for_win(board);
248
        if (winning_colour == AI_colour){
249
            cout<<"AI has won! " << movecount << " moves " << "Exiting."<<endl;
250
            break;
251
        } else if (winning_colour == FPGA_colour){
252
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
253
            break;
254
        }
255
        if (check_board_full(board) < 0){
256
                cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
257
                 break;
258
        }
259
#ifdef EMUL
260
        write(port,&move[0],1);
261
        write(port,&move[1],1);
262
        write(port,&move[2],1);
263
        write(port,&move[3],1);
264
#endif
265
        moveport[0]=move[0];
266
        moveport[1]=move[1];
267
        moveport[2]=move[2];
268
        moveport[3]=move[3];
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
        winning_colour = check_for_win(board);
274
        if (winning_colour == AI_colour){
275
            cout<<"AI has won! " << movecount << " moves " << "Exiting."<<endl;
276
            break;
277
        } else if (winning_colour == FPGA_colour){
278
            cout<<"FPGA has won! " << movecount << " moves " << "Exiting."<<endl;
279
            break;
280
        }
281
        if (check_board_full(board) < 0) {
282
            cout << "TIE "  << movecount << " moves " << "Exiting."<<endl;
283
                break;
284
        }
285
#ifdef EMUL
286
        write(port,&move[0],1);
287
        write(port,&move[1],1);
288
        write(port,&move[2],1);
289
        write(port,&move[3],1);
290
#endif
291
        moveport[4]=move[0];
292
        moveport[5]=move[1];
293
        moveport[6]=move[2];
294
        moveport[7]=move[3];
295
        print_board(board);
296
        print_board_file(board);
297
    }
298
 
299
#ifndef EMUL
300
PICO_finalize_PPA(id);
301
#endif
302
 
303
    return 0;
304
 
305
}

powered by: WebSVN 2.1.0

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