// Matt Michie
// mmichie@linux.com
// ACM entry
// Thanks / greets go out to:
// PI the soundtrack
// Astral Projection
// ezeitler
// dcook
// the milkman
// truefluke
// This program sucks.  I ran out of time so I'll submit what
// I have.
// Although if i wake up early maybe i'll work on it (yeah right).
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int **Table;
int **Boxes;

int width = 0;
int height = 0;

FILE *log;

void SetupArrays(void)
{
	int i, j;

	Table = (int **)malloc(sizeof(int) * height);
    Boxes = (int **)malloc(sizeof(int) * height);

	for (i=0; i < height; i++) {
		Table[i] = (int *)malloc(sizeof(int) * width);
        Boxes[i] = (int *)malloc(sizeof(int) * width);
	}
	
	for (i=0; i < height; i++)
		for (j=0; j < width; j++) {
			Table[i][j] = 0;
			Boxes[i][j] = 0;
		}

}

char *rmtrail(char *str)
{
    int i;

    if (0 != (i = strlen(str))) {
		while (--i >= 0) {
		    if (!isspace(str[i]))
			break;
		}
		str[++i] = (char)NULL;
    }
    return str;
}

void RegisterOpponentMove(char *input)
{
	const char delimiters[] = " ,-";
	
	char buffer[50];
    char *x_coord1, *x_coord2;
    char *y_coord1, *y_coord2;
	int x1, y1, x2, y2;

	strncpy(buffer, input, 45);

	strtok (buffer, delimiters);
	x_coord1 = strtok (NULL, delimiters);
	y_coord1 = strtok (NULL, delimiters);
	x_coord2 = strtok (NULL, delimiters);
	y_coord2 = strtok (NULL, delimiters);

	x1 = atoi(x_coord1);
	y1 = atoi(y_coord1);
	x2 = atoi(x_coord2);
	y2 = atoi(y_coord2);

	// Vertical = 1
	// Horizonatal = 2
	// Both = 3
    if (y1 == y2) {
		if (Table[x1][y1] == 1)
			Table[x1][y1] = 3;
		else
			Table[x1][y1] = 2;
	}
	else {
		if (Table[x1][y1] == 2)
			Table[x1][y1] = 3;
		else
			Table[x1][y1] = 1;
	}

	//fprintf(log, "%d %d got %d\n", x1, y1, Table[x1][y1]);
}


int FindNoTwoBox(x, y)
{
	int x1, y1, nsides;

	nsides = 0;
	if (x < width - 1)
		x1 = x + 1;
	else
		x1 = x;
	if (y < height - 1)
		y1 = y + 1;
	else
		y1 = y;
	if ((Table[x][y] == 2) || (Table[x][y] == 3))
		nsides++;
	if ((Table[x][y1] == 3) || (Table[x][y1] == 2))
		nsides++;
    if ((Table[x][y] == 3) || (Table[x][y] == 1))
		nsides++;
    if ((Table[x1][y] == 3) || (Table[x1][y] == 1))
		nsides++;

	if (nsides == 2) 
		return 333;
	else
		return 0;
}

int FindThreeSidedBox (char *zeroside) 
{
	int x, x1, y, y1, nsides;
	//char zeroside[50];

	for (x = 0; x < width-1; x++) {
		for (y = 0; y < height-1; y++) {
			nsides = 0;
			x1 = x + 1;
			y1 = y + 1;

		    if ((Table[x][y] == 2) || (Table[x][y] == 3))
				nsides++;
			else
				sprintf(zeroside, "%d,%d-%d,%d", x,y,x1,y);

			if ((Table[x][y1] == 3) || (Table[x][y1] == 2))
				nsides++;
			else
				sprintf(zeroside, "%d,%d-%d,%d", x,y1,x1,y1);

            if ((Table[x][y] == 3) || (Table[x][y] == 1))
	            nsides++;
            else 
	            sprintf(zeroside, "%d,%d-%d,%d", x,y,x,y1);

            if ((Table[x1][y] == 3) || (Table[x1][y] == 1))
	            nsides++;
            else
	            sprintf(zeroside, "%d,%d-%d,%d", x1,y,x1,y1);

			if (nsides == 3) 
				return 666;
		}
	}
	return 0;
}

// Full of UGLY HACKS!!!!!!!!!!
void MakeMove()
{
	int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
	int horizontal = 0;
    int count = 0;
    char ZeroSide[50], tmp[50];
	int num = 0;

    num = FindThreeSidedBox((char*)&ZeroSide);
    
	if (num == 666) {
		fprintf(stdout, "%s\n", ZeroSide);
		sprintf(tmp, "opponent %s", ZeroSide);
		RegisterOpponentMove(tmp);
		fflush(stdout);
		return;
	}

Fucked:
	count = 0;
	if ((int) ((double) rand() / ((double) RAND_MAX + 1) * 2) == 0) {
		do { // Vertical
			x1 = rand() % width;
			y1 = rand() % (height-1);
			x2 = x1;
			y2 = y1;
			y2++;

			horizontal = 0;
			count++;
			//num = FindNoTwoBox(x1, y1);
            
		    if ( (count > 2*width*height))
				goto Fucked;
		} while ( (Table[x1][y1] == 1) || (Table[x1][y1] == 3) );
	}
	else {
		do { // Horizontal
			x1 = rand() % (width - 1);
			x2 = x1 + 1;
			y1 = rand() % height;
			y2 = y1;

			horizontal = 1;	
			count++;
			//num = FindNoTwoBox(x1, y1);

            if ( (count > 2*width*height) )
				goto Fucked;
	    } while ( (Table[x1][y1] == 2) || (Table[x1][y1] == 3) ); 
	}
   
   	if (horizontal == 1) {
		if (Table[x1][y1] == 1)
			Table[x1][y1] = 3;
		else
			Table[x1][y1] = 2;
	}
	else {
		if (Table[x1][y1] == 2)
			Table[x1][y1] = 3;
		else
			Table[x1][y1] = 1;
	}
	
	printf("%d,%d-%d,%d\n", x1, y1, x2, y2);
    fflush(stdout);
}


int main(int argc, char *argv[])
{
    char *input;
    //char filename[50];

    if (argc < 3) {
		printf("Not enough arguments!\n");
		exit(1);
    }

	srand(time(NULL));
    
	width = atoi(argv[1]);
    height = atoi(argv[2]);

	//sprintf(filename, "mylog.%d", getpid());
	//log = fopen(filename, "w");

	SetupArrays();

    input = (char *) malloc(40 * sizeof(char));

    fgets(input, 30, stdin);
	input = rmtrail(input);

	while (strcmp(input, "quit") != 0) {
		if (strcmp(input, "move") == 0)
		    MakeMove();
		else if (strncmp(input, "opponent", 8) == 0) {
		    RegisterOpponentMove(input);
		}

    	fgets(input, 30, stdin);
		input = rmtrail(input);
    }

	free (Table);
	//fclose(log);

    return 0;
}
