X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=samples%2Fdummy%2Fdummy.cpp;fp=samples%2Fdummy%2Fdummy.cpp;h=a4a68ce22fc872b69d5002501edb4d5dc2e12f23;hp=0000000000000000000000000000000000000000;hb=041c37d1dfc4a94024fe1d329d289e4c59667885;hpb=2ab27eb698cfd57977cc9cc25edcbfbeb3b1b1ee diff --git a/samples/dummy/dummy.cpp b/samples/dummy/dummy.cpp new file mode 100644 index 0000000..a4a68ce --- /dev/null +++ b/samples/dummy/dummy.cpp @@ -0,0 +1,178 @@ +#include +#include + +#include +#include +#include +#include +#include +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()); + + assert(width == 10 && height == 10); //Can't deal with other sized boards + if (colour == "RED") + { + fprintf(stdout, "FBnyBmSsBn\n"); + fprintf(stdout, "BBCMccccnC\n"); + fprintf(stdout, "LSGmnsnsSm\n"); + fprintf(stdout, "sLSBLLssss\n"); + } + else if (colour == "BLUE") + { + fprintf(stdout, "sLSBLLssss\n"); + fprintf(stdout, "LSGmnsnsSm\n"); + fprintf(stdout, "BBCMccccnC\n"); + fprintf(stdout, "FBnyBmSsBn\n"); + } + else + { + return 1; + } + + + + char board[width][height]; + + vector > choices; + + int myPid = (int)(getpid()); + + while (true) + { + //fprintf(stderr, "%s [%d] looping\n", argv[0], myPid); + choices.clear(); + + //fprintf(stderr, "%s Waiting for status line...\n", colour.c_str()); + char c = fgetc(stdin); + while (c != '\n') + { + //fprintf(stderr,"%c",c); + c = fgetc(stdin); + } + //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(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 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); + } +}