Revamped manager program and added manual page
[progcomp2012.git] / samples / dummy / 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         srand(time(NULL));      
23
24         //Read in the colour, and choose a layout
25         
26         int width = 14; int height = 14;
27         
28         string colour; string opponent;
29         cin >> colour; cin >> opponent; cin >> width; cin >> height;
30         fgetc(stdin);
31
32         //fprintf(stderr, "Colour is \"%s\", width and height are (%d, %d), opponent is \"%s\"\n", colour.c_str(), width, height, opponent.c_str());
33
34         assert(width == 10 && height == 10); //Can't deal with other sized boards
35         if (colour == "RED")
36         {
37                 fprintf(stdout, "FB8sB479B8\n");
38                 fprintf(stdout, "BB31555583\n");
39                 fprintf(stdout, "6724898974\n");
40                 fprintf(stdout, "967B669999\n");
41         }
42         else if (colour == "BLUE")
43         {
44                 fprintf(stdout, "967B669999\n");
45                 fprintf(stdout, "6724898974\n");                
46                 fprintf(stdout, "BB31555583\n");
47                 fprintf(stdout, "FB8sB479B8\n");
48         }
49         else
50         {
51                 return 1;
52         }
53
54
55
56         char board[width][height];
57
58         vector<pair<int, int> > choices;
59
60         int myPid = (int)(getpid());
61
62         while (true)
63         {
64                 //fprintf(stderr, "%s [%d] looping\n", argv[0], myPid);
65                 choices.clear();
66
67                 //fprintf(stderr, "%s Waiting for status line...\n", colour.c_str());
68                 char c = fgetc(stdin);
69                 while (c != '\n')
70                 {
71                         //fprintf(stderr,"%c",c);
72                         c = fgetc(stdin);
73                 }
74                         //fprintf(stderr, "%s Got status, waiting for board line...\n", colour.c_str());
75
76                 //Read in board
77                 for (int y=0; y < height; ++y)
78                 {
79                         for (int x=0; x < width; ++x)
80                         {
81                                 board[x][y] = fgetc(stdin);
82                                 if (board[x][y] == EOF)
83                                         exit(EXIT_SUCCESS);             
84
85                                 if (board[x][y] != '.' && board[x][y] != '*' && board[x][y] != '#' && board[x][y] != '+')
86                                 {       
87                                         choices.push_back(pair<int, int>(x, y));
88                                 }
89                         }
90                         assert(fgetc(stdin) == '\n');
91                 }
92
93                 
94
95                 int dir = 0; int startDir = 0; int choice = rand() % choices.size(); int startChoice = choice;
96                 int x1 = 0; int y1 = 0;
97                 do
98                 {
99                         
100                 
101                         pair<int,int> pear = choices[choice];
102                         x1 = pear.first;
103                         y1 = pear.second;
104                         //fprintf(stderr,"Trying unit at %d %d...\n", x1, y1);
105
106                         if (board[x1][y1] == 'B' || board[x1][y1] == 'F')
107                         {
108                                 choice = (choice+1) % choices.size();
109                                 continue;
110                         }
111                         
112                         int x2 = x1;
113                         int y2 = y1;
114                         dir = rand() % 4; startDir = dir; int lastDir = dir;
115
116                         bool okay = false;
117                         while (!okay)
118                         {
119                                 //fprintf(stderr,"      Trying direction %d...\n", dir);                                
120                                 x2 = x1; y2 = y1;
121                                 switch (dir)
122                                 {
123                                 case 0:
124                                         --y2;
125                                         break;
126                                 case 1:
127                                         ++y2;
128                                         break;
129                                 case 2:
130                                         --x2;
131                                         break;
132                                 case 3:
133                                         ++x2;
134                                         break;
135                                 }
136
137                                 okay = !(x2 < 0 || y2 < 0 || x2 >= width || y2 >= height || (board[x2][y2] != '.' && board[x2][y2] != '*' && board[x2][y2] != '#'));
138                                 if (!okay)
139                                 {
140                                         dir = (dir+1) % 4;
141                                         if (dir == startDir)
142                                                 break;
143                                 }
144                                 
145                         }
146                         
147
148
149                         choice = (choice+1) % choices.size();
150                         if (dir != startDir)
151                                 break;
152                 }
153                 while (choice != startChoice);
154                 
155
156                 string direction="";
157                 switch (dir)
158                 {
159                         case 0:
160                                 direction = "UP";
161                                 break;
162                         case 1:
163                                 direction = "DOWN";
164                                 break;
165                         case 2:
166                                 direction = "LEFT";
167                                 break;
168                         case 3:
169                                 direction = "RIGHT";
170                                 break;
171                 }
172                 printf("%d %d %s\n", x1, y1, direction.c_str());
173                 //fprintf(stderr,"%s Made move, waiting for confirmation line\n", colour.c_str());
174                 while (fgetc(stdin) != '\n'); //Read in result line
175                 //fprintf(stderr, "%s Done turn\n", colour.c_str());
176                 //fprintf(stderr,"%s - %d %d %s\n",colour.c_str(),  x1, y1, direction.c_str() );
177                 //fprintf(stderr, "%s [%d] computed move\n", argv[0], myPid);
178         }
179 }

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