Mostly messing with "forfax" AI
[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
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 == 10 && height == 10); //Can't deal with other sized boards
34         if (colour == "RED")
35         {
36                 fprintf(stdout, "FBnyBmSsBn\n");
37                 fprintf(stdout, "BBCMccccnC\n");
38                 fprintf(stdout, "LSGmnsnsSm\n");
39                 fprintf(stdout, "sLSBLLssss\n");
40         }
41         else if (colour == "BLUE")
42         {
43                 fprintf(stdout, "sLSBLLssss\n");
44                 fprintf(stdout, "LSGmnsnsSm\n");                
45                 fprintf(stdout, "BBCMccccnC\n");
46                 fprintf(stdout, "FBnyBmSsBn\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                 char c = fgetc(stdin);
68                 while (c != '\n')
69                 {
70                         //fprintf(stderr,"%c",c);
71                         c = fgetc(stdin);
72                 }
73                         //fprintf(stderr, "%s Got status, waiting for board line...\n", colour.c_str());
74
75                 //Read in board
76                 for (int y=0; y < height; ++y)
77                 {
78                         for (int x=0; x < width; ++x)
79                         {
80                                 board[x][y] = fgetc(stdin);
81                                 if (board[x][y] == EOF)
82                                         exit(EXIT_SUCCESS);             
83
84                                 if (board[x][y] != '.' && board[x][y] != '*' && board[x][y] != '#' && board[x][y] != '+')
85                                 {       
86                                         choices.push_back(pair<int, int>(x, y));
87                                 }
88                         }
89                         assert(fgetc(stdin) == '\n');
90                 }
91
92                 
93
94                 int dir = 0; int startDir = 0; int choice = rand() % choices.size(); int startChoice = choice;
95                 int x1 = 0; int y1 = 0;
96                 do
97                 {
98                         
99                 
100                         pair<int,int> pear = choices[choice];
101                         x1 = pear.first;
102                         y1 = pear.second;
103                         //fprintf(stderr,"Trying unit at %d %d...\n", x1, y1);
104
105                         if (board[x1][y1] == 'B' || board[x1][y1] == 'F')
106                         {
107                                 choice = (choice+1) % choices.size();
108                                 continue;
109                         }
110                         
111                         int x2 = x1;
112                         int y2 = y1;
113                         dir = rand() % 4; startDir = dir; int lastDir = dir;
114
115                         bool okay = false;
116                         while (!okay)
117                         {
118                                 //fprintf(stderr,"      Trying direction %d...\n", dir);                                
119                                 x2 = x1; y2 = y1;
120                                 switch (dir)
121                                 {
122                                 case 0:
123                                         --y2;
124                                         break;
125                                 case 1:
126                                         ++y2;
127                                         break;
128                                 case 2:
129                                         --x2;
130                                         break;
131                                 case 3:
132                                         ++x2;
133                                         break;
134                                 }
135
136                                 okay = !(x2 < 0 || y2 < 0 || x2 >= width || y2 >= height || (board[x2][y2] != '.' && board[x2][y2] != '*' && board[x2][y2] != '#'));
137                                 if (!okay)
138                                 {
139                                         dir = (dir+1) % 4;
140                                         if (dir == startDir)
141                                                 break;
142                                 }
143                                 
144                         }
145                         
146
147
148                         choice = (choice+1) % choices.size();
149                         if (dir != startDir)
150                                 break;
151                 }
152                 while (choice != startChoice);
153                 
154
155                 string direction="";
156                 switch (dir)
157                 {
158                         case 0:
159                                 direction = "UP";
160                                 break;
161                         case 1:
162                                 direction = "DOWN";
163                                 break;
164                         case 2:
165                                 direction = "LEFT";
166                                 break;
167                         case 3:
168                                 direction = "RIGHT";
169                                 break;
170                 }
171                 printf("%d %d %s\n", x1, y1, direction.c_str());
172                 //fprintf(stderr,"%s Made move, waiting for confirmation line\n", colour.c_str());
173                 while (fgetc(stdin) != '\n'); //Read in result line
174                 //fprintf(stderr, "%s Done turn\n", colour.c_str());
175                 //fprintf(stderr,"%s - %d %d %s\n",colour.c_str(),  x1, y1, direction.c_str() );
176                 //fprintf(stderr, "%s [%d] computed move\n", argv[0], myPid);
177         }
178 }

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