12 * A suitably terrible program which combines C style IO with C++ style IO
17 int main(int argc, char ** argv)
23 //Read in the colour, and choose a layout
25 int width = 14; int height = 14;
27 string colour; string opponent;
28 cin >> colour; cin >> opponent; cin >> width; cin >> height;
31 //fprintf(stderr, "Colour is \"%s\", width and height are (%d, %d), opponent is \"%s\"\n", colour.c_str(), width, height, opponent.c_str());
33 assert(width == 14 && height == 14); //Can't deal with other sized boards
36 fprintf(stdout, "FB..........B.\n");
37 fprintf(stdout, "BBCM....cccc.C\n");
38 fprintf(stdout, "LSGmnsBmSsnsSm\n");
39 fprintf(stdout, "sLSBLnLssssnyn\n");
41 else if (colour == "BLUE")
43 fprintf(stdout, "sLSBLnLssssnyn\n");
44 fprintf(stdout, "LSGmnsBmSsnsSm\n");
45 fprintf(stdout, "BBCM....cccc.C\n");
46 fprintf(stdout, "FB..........B.\n");
55 char board[width][height];
57 vector<pair<int, int> > choices;
59 int myPid = (int)(getpid());
63 //fprintf(stderr, "%s [%d] looping\n", argv[0], myPid);
66 // fprintf(stderr, "%s Waiting for status line...\n", colour.c_str());
67 while (fgetc(stdin) != '\n')
69 //fprintf(stderr,".");
71 //fprintf(stderr, "%s Got status, waiting for board line...\n", colour.c_str());
74 for (int y=0; y < height; ++y)
76 for (int x=0; x < width; ++x)
78 board[x][y] = fgetc(stdin);
79 if (board[x][y] == EOF)
82 if (board[x][y] != '.' && board[x][y] != '*' && board[x][y] != '#' && board[x][y] != '+')
84 choices.push_back(pair<int, int>(x, y));
87 assert(fgetc(stdin) == '\n');
92 int dir = 0; int startDir = 0; int choice = rand() % choices.size(); int startChoice = choice;
93 int x1 = 0; int y1 = 0;
98 pair<int,int> pear = choices[choice];
101 //fprintf(stderr,"Trying unit at %d %d...\n", x1, y1);
103 if (board[x1][y1] == 'B' || board[x1][y1] == 'F')
105 choice = (choice+1) % choices.size();
111 dir = rand() % 4; startDir = dir; int lastDir = dir;
116 //fprintf(stderr," Trying direction %d...\n", dir);
134 okay = !(x2 < 0 || y2 < 0 || x2 >= width || y2 >= height || (board[x2][y2] != '.' && board[x2][y2] != '*' && board[x2][y2] != '#'));
146 choice = (choice+1) % choices.size();
150 while (choice != startChoice);
169 printf("%d %d %s\n", x1, y1, direction.c_str());
170 //fprintf(stderr,"%s Made move, waiting for confirmation line\n", colour.c_str());
171 while (fgetc(stdin) != '\n'); //Read in result line
172 //fprintf(stderr, "%s Done turn\n", colour.c_str());
173 //fprintf(stderr,"%s - %d %d %s\n",colour.c_str(), x1, y1, direction.c_str() );
174 //fprintf(stderr, "%s [%d] computed move\n", argv[0], myPid);