4f9c33fb4abbefb5c58353dc84d89b5c07486fa9
[progcomp2012.git] / samples / dummy.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <vector>
5 #include <cassert>
6 #include <string>
7 #include <iostream>
8 #include <sstream>
9 using namespace std;
10
11 /**
12  * A suitably terrible program which combines C style IO with C++ style IO
13  * Enjoy!
14  * Mwuhahaha
15  */
16
17 int main(int argc, char ** argv)
18 {
19         setbuf(stdout, NULL);
20         setbuf(stdin, NULL);
21         
22
23         //Read in the colour, and choose a layout
24         
25         int width = 14; int height = 14;
26         
27         string colour; string opponent;
28         cin >> colour; cin >> opponent; cin >> width; cin >> height;
29         fgetc(stdin);
30
31         //fprintf(stderr, "Colour is \"%s\", width and height are (%d, %d), opponent is \"%s\"\n", colour.c_str(), width, height, opponent.c_str());
32
33         assert(width == 14 && height == 14); //Can't deal with other sized boards
34         if (colour == "RED")
35         {
36                 fprintf(stdout, "FB..........B.\n");
37                 fprintf(stdout, "BBCM....cccc.C\n");
38                 fprintf(stdout, "LSGmnsBmSsnsSm\n");
39                 fprintf(stdout, "sLSBLnLssssnyn\n");
40         }
41         else if (colour == "BLUE")
42         {
43                 fprintf(stdout, "sLSBLnLssssnyn\n");
44                 fprintf(stdout, "LSGmnsBmSsnsSm\n");
45                 fprintf(stdout, "BBCM....cccc.C\n");
46                 fprintf(stdout, "FB..........B.\n");
47         }
48         else
49         {
50                 return 1;
51         }
52
53
54
55         char board[width][height];
56
57         vector<pair<int, int> > choices;
58
59         int myPid = (int)(getpid());
60
61         while (true)
62         {
63                 //fprintf(stderr, "%s [%d] looping\n", argv[0], myPid);
64                 choices.clear();
65
66         //      fprintf(stderr, "%s Waiting for status line...\n", colour.c_str());
67                 while (fgetc(stdin) != '\n')
68                 {
69                         //fprintf(stderr,".");
70                 }
71                         //fprintf(stderr, "%s Got status, waiting for board line...\n", colour.c_str());
72
73                 //Read in board
74                 for (int y=0; y < height; ++y)
75                 {
76                         for (int x=0; x < width; ++x)
77                         {
78                                 board[x][y] = fgetc(stdin);
79                                 if (board[x][y] == EOF)
80                                         exit(EXIT_SUCCESS);             
81
82                                 if (board[x][y] != '.' && board[x][y] != '*' && board[x][y] != '#' && board[x][y] != '+')
83                                 {       
84                                         choices.push_back(pair<int, int>(x, y));
85                                 }
86                         }
87                         assert(fgetc(stdin) == '\n');
88                 }
89
90                 
91
92                 int dir = 0; int startDir = 0; int choice = rand() % choices.size(); int startChoice = choice;
93                 int x1 = 0; int y1 = 0;
94                 do
95                 {
96                         
97                 
98                         pair<int,int> pear = choices[choice];
99                         x1 = pear.first;
100                         y1 = pear.second;
101                         //fprintf(stderr,"Trying unit at %d %d...\n", x1, y1);
102
103                         if (board[x1][y1] == 'B' || board[x1][y1] == 'F')
104                         {
105                                 choice = (choice+1) % choices.size();
106                                 continue;
107                         }
108                         
109                         int x2 = x1;
110                         int y2 = y1;
111                         dir = rand() % 4; startDir = dir; int lastDir = dir;
112
113                         bool okay = false;
114                         while (!okay)
115                         {
116                                 //fprintf(stderr,"      Trying direction %d...\n", dir);                                
117                                 x2 = x1; y2 = y1;
118                                 switch (dir)
119                                 {
120                                 case 0:
121                                         --y2;
122                                         break;
123                                 case 1:
124                                         ++y2;
125                                         break;
126                                 case 2:
127                                         --x2;
128                                         break;
129                                 case 3:
130                                         ++x2;
131                                         break;
132                                 }
133
134                                 okay = !(x2 < 0 || y2 < 0 || x2 >= width || y2 >= height || (board[x2][y2] != '.' && board[x2][y2] != '*' && board[x2][y2] != '#'));
135                                 if (!okay)
136                                 {
137                                         dir = (dir+1) % 4;
138                                         if (dir == startDir)
139                                                 break;
140                                 }
141                                 
142                         }
143                         
144
145
146                         choice = (choice+1) % choices.size();
147                         if (dir != startDir)
148                                 break;
149                 }
150                 while (choice != startChoice);
151                 
152
153                 string direction="";
154                 switch (dir)
155                 {
156                         case 0:
157                                 direction = "UP";
158                                 break;
159                         case 1:
160                                 direction = "DOWN";
161                                 break;
162                         case 2:
163                                 direction = "LEFT";
164                                 break;
165                         case 3:
166                                 direction = "RIGHT";
167                                 break;
168                 }
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);
175         }
176 }

UCC git Repository :: git.ucc.asn.au