dummy.cpp 3.64 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>

#include <vector>
#include <cassert>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

/**
 * A suitably terrible program which combines C style IO with C++ style IO
 * Enjoy!
 * Mwuhahaha
 */

int main(int argc, char ** argv)
{
	setbuf(stdout, NULL);
	setbuf(stdin, NULL);
	

	//Read in the colour, and choose a layout
	
	int width = 14; int height = 14;
	
	string colour; string opponent;
	cin >> colour; cin >> opponent; cin >> width; cin >> height;
	fgetc(stdin);

	//fprintf(stderr, "Colour is \"%s\", width and height are (%d, %d), opponent is \"%s\"\n", colour.c_str(), width, height, opponent.c_str());

Sam Moore's avatar
Sam Moore committed
33
	assert(width == 10 && height == 10); //Can't deal with other sized boards
Sam Moore's avatar
Sam Moore committed
34
35
	if (colour == "RED")
	{
Sam Moore's avatar
Sam Moore committed
36
37
38
39
		fprintf(stdout, "FBnyBmSsBn\n");
		fprintf(stdout, "BBCMccccnC\n");
		fprintf(stdout, "LSGmnsnsSm\n");
		fprintf(stdout, "sLSBLLssss\n");
Sam Moore's avatar
Sam Moore committed
40
41
42
	}
	else if (colour == "BLUE")
	{
Sam Moore's avatar
Sam Moore committed
43
44
45
46
		fprintf(stdout, "sLSBLLssss\n");
		fprintf(stdout, "LSGmnsnsSm\n");		
		fprintf(stdout, "BBCMccccnC\n");
		fprintf(stdout, "FBnyBmSsBn\n");
Sam Moore's avatar
Sam Moore committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	}
	else
	{
		return 1;
	}



	char board[width][height];

	vector<pair<int, int> > choices;

	int myPid = (int)(getpid());

	while (true)
	{
		//fprintf(stderr, "%s [%d] looping\n", argv[0], myPid);
		choices.clear();

Sam Moore's avatar
Sam Moore committed
66
67
68
		//fprintf(stderr, "%s Waiting for status line...\n", colour.c_str());
		char c = fgetc(stdin);
		while (c != '\n')
Sam Moore's avatar
Sam Moore committed
69
		{
Sam Moore's avatar
Sam Moore committed
70
71
			//fprintf(stderr,"%c",c);
			c = fgetc(stdin);
Sam Moore's avatar
Sam Moore committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
		}
			//fprintf(stderr, "%s Got status, waiting for board line...\n", colour.c_str());

		//Read in board
		for (int y=0; y < height; ++y)
		{
			for (int x=0; x < width; ++x)
			{
				board[x][y] = fgetc(stdin);
				if (board[x][y] == EOF)
					exit(EXIT_SUCCESS);		

				if (board[x][y] != '.' && board[x][y] != '*' && board[x][y] != '#' && board[x][y] != '+')
				{	
					choices.push_back(pair<int, int>(x, y));
				}
			}
			assert(fgetc(stdin) == '\n');
		}

		

		int dir = 0; int startDir = 0; int choice = rand() % choices.size(); int startChoice = choice;
		int x1 = 0; int y1 = 0;
		do
		{
			
		
			pair<int,int> pear = choices[choice];
			x1 = pear.first;
			y1 = pear.second;
			//fprintf(stderr,"Trying unit at %d %d...\n", x1, y1);

			if (board[x1][y1] == 'B' || board[x1][y1] == 'F')
			{
				choice = (choice+1) % choices.size();
				continue;
			}
			
			int x2 = x1;
			int y2 = y1;
			dir = rand() % 4; startDir = dir; int lastDir = dir;

			bool okay = false;
			while (!okay)
			{
				//fprintf(stderr,"	Trying direction %d...\n", dir);				
				x2 = x1; y2 = y1;
				switch (dir)
				{
				case 0:
					--y2;
					break;
				case 1:
					++y2;
					break;
				case 2:
					--x2;
					break;
				case 3:
					++x2;
					break;
				}

				okay = !(x2 < 0 || y2 < 0 || x2 >= width || y2 >= height || (board[x2][y2] != '.' && board[x2][y2] != '*' && board[x2][y2] != '#'));
				if (!okay)
				{
					dir = (dir+1) % 4;
					if (dir == startDir)
						break;
				}
				
			}
			


			choice = (choice+1) % choices.size();
			if (dir != startDir)
				break;
		}
		while (choice != startChoice);
		

		string direction="";
		switch (dir)
		{
			case 0:
				direction = "UP";
				break;
			case 1:
				direction = "DOWN";
				break;
			case 2:
				direction = "LEFT";
				break;
			case 3:
				direction = "RIGHT";
				break;
		}
		printf("%d %d %s\n", x1, y1, direction.c_str());
		//fprintf(stderr,"%s Made move, waiting for confirmation line\n", colour.c_str());
		while (fgetc(stdin) != '\n'); //Read in result line
		//fprintf(stderr, "%s Done turn\n", colour.c_str());
		//fprintf(stderr,"%s - %d %d %s\n",colour.c_str(),  x1, y1, direction.c_str() );
		//fprintf(stderr, "%s [%d] computed move\n", argv[0], myPid);
	}
}