/********************************************************************************
* This example program in C illustrates how the IO works with the CodeCup System.
* This example is written by Marcel Vlastuin for the CodeCup game 2009 Pillars.
* Feel free to modify or use this code. The use of this code is not compulsary.
*********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define EMPTY 0
#define OCCUPIED 1
int board[10][10];
void read_pillars(void);
void process_opponents_move(char *text_input);
void do_own_move(void);
int main (void) {
char text_input[6];
srand(time(0));
read_pillars();
scanf("%s", text_input);
if (strcmp(text_input, "Start") == 0) goto red_starts;
while (1) {
process_opponents_move(text_input);
red_starts:
do_own_move();
scanf("%s", text_input);
if (strcmp(text_input, "Quit") == 0) break;
}
return 0;
}
/********************************************************************************
* In this example program the tiles and pillars are stored in board[10][10].
* It is up to you to use this or not.
* In the function read_pillars the board will be initialized and will the
* pillars be read in.
*********************************************************************************/
void read_pillars(void) {
int i, j;
char text_input[3];
for (i = 0; i < 10; ++i) for (j = 0; j < 10; ++j) board[i][j] = EMPTY;
for (i = 0; i < 10; ++i) {
scanf("%s", text_input);
board[text_input[0] - 'A'][text_input[1] - 'a'] = OCCUPIED;
}
}
/********************************************************************************
* In the function process_opponents_move the opoonent's move is read and processed.
*********************************************************************************/
void process_opponents_move(char *text_input) {
int i, j, ibegin, iend, jbegin, jend;
ibegin = text_input[0] - 'A';
jbegin = text_input[1] - 'a';
iend = text_input[2] - 'A';
jend = text_input[3] - 'a';
for (i = ibegin; i <= iend; ++i) for (j = jbegin; j <= jend; ++j) board[i][j] = OCCUPIED;
}
/********************************************************************************
* In the function do_own_move you have to calculate on basis of the positions
* on the board what the best move is.
* In this example only a simple random player is programmed.
* It's up to you to fill the body of this function with the best you can achieve!
* Good luck!
*********************************************************************************/
void do_own_move(void) {
// Generate a random number from 0 up to 99:
int i = rand() % 10, j = rand() % 10;
while (board[i][j] == OCCUPIED) {
if (i < 9) ++i;
else {i = 0; if (++j == 10) j = 0;}
}
// If you get here you've found an empty spot:
// Then you pick the found empty spot to perform your random move:
board[i][j] = OCCUPIED;
printf("%c%c%c%c\n", i + 'A', j + 'a', i + 'A', j + 'a');
fflush(stdout);
return;
}
syntax highlighted by Code2HTML, v. 0.9.1