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

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [BUILD_SCC/] [synth_src/] [main.cpp] - Blame information for rev 11

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

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

powered by: WebSVN 2.1.0

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