2015-03-07 17:18:50 +01:00
|
|
|
/* -*- Mode: C -*- */
|
2015-03-11 23:48:18 +01:00
|
|
|
//#define debugprint
|
2015-03-07 17:18:50 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2015-03-11 23:47:02 +01:00
|
|
|
//for seeding rng with cummrent time
|
|
|
|
#include <time.h>
|
2015-03-07 17:18:50 +01:00
|
|
|
|
|
|
|
#include <mcp.h>
|
|
|
|
|
|
|
|
#include "playerlib.h"
|
|
|
|
|
|
|
|
static int
|
|
|
|
send_move(int row, int col)
|
|
|
|
{
|
|
|
|
FILE *f = fdopen(dup(CHILD_OUT_FD), "w");
|
|
|
|
fprintf(f, "%u,%u", row, col);
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-11 11:28:20 +01:00
|
|
|
|
2015-03-07 17:18:50 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2015-03-11 15:52:48 +01:00
|
|
|
int done = 0;
|
2015-03-11 23:47:02 +01:00
|
|
|
srandom(time(NULL));
|
2015-03-11 15:52:48 +01:00
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
int turn_row = 0;
|
|
|
|
int turn_col = 0;
|
|
|
|
/* BEGIN PLAYER-SPECIFIC CODE */
|
|
|
|
|
|
|
|
// 1. Read state
|
|
|
|
char state_buffer[128];
|
|
|
|
ssize_t bytes = read(CHILD_IN_FD, state_buffer, sizeof(state_buffer));
|
|
|
|
if (bytes != 65) // invalid number of chars
|
|
|
|
abort();
|
|
|
|
|
|
|
|
// state_buffer enthält jetzt 65 Zeichen ('.' oder 'X' oder 'O'):
|
|
|
|
// * Das ERSTE Zeichen gibt an, welcher Spieler an der Reihe ist.
|
|
|
|
// * Die weiteren 64 Zeichen definieren die Belegung des Feldes.
|
|
|
|
// * Die Belegung wird reihenweise gegeben, d.h. die Zeichen
|
|
|
|
// 1 bis 8 definieren die erste Zeile des Feldes, Zeichen 9 bis 17
|
|
|
|
// geben die zweite Zeile usw.
|
|
|
|
// * X und O stehen hierbei für die jeweiligen Spieler. Leere Felder
|
|
|
|
// sind durch einen Punkt (.) gekennzeichnet.
|
|
|
|
|
|
|
|
// 2. TODO: Strategie hier einfügen. Resultat in turn_row und turn_col speichern.
|
|
|
|
|
|
|
|
if(!enemyc)
|
|
|
|
{
|
|
|
|
ownc = state_buffer[0];
|
|
|
|
enemyc = getEnemyChar(ownc);
|
|
|
|
}
|
2015-03-10 13:54:11 +01:00
|
|
|
|
2015-03-11 15:52:48 +01:00
|
|
|
//pure C:
|
2015-03-11 23:29:56 +01:00
|
|
|
//struct cell (*field)[fieldSize] = malloc(fieldSize*fieldSize*sizeof(struct cell));
|
2015-03-07 17:18:50 +01:00
|
|
|
|
2015-03-11 15:52:48 +01:00
|
|
|
//C++ version:
|
2015-03-11 23:29:56 +01:00
|
|
|
auto field = new cell[fieldSize][fieldSize]();
|
2015-03-07 17:18:50 +01:00
|
|
|
|
2015-03-11 15:52:48 +01:00
|
|
|
readStateBuffer(state_buffer, field); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[]
|
|
|
|
movesList *moves = new movesList();
|
2015-03-11 23:26:52 +01:00
|
|
|
|
2015-03-12 11:50:39 +01:00
|
|
|
findMoves(field, moves);
|
2015-03-11 23:26:52 +01:00
|
|
|
|
2015-03-11 23:47:02 +01:00
|
|
|
#ifdef debugprint
|
2015-03-11 23:26:52 +01:00
|
|
|
printf("\nZüge:\n");
|
2015-03-11 15:52:48 +01:00
|
|
|
for (unsigned int i=0; i<moves->movesNumber; ++i)
|
|
|
|
{
|
|
|
|
printf("%d,%d\n",moves->list[i].turnRow,moves->list[i].turnCol);
|
|
|
|
}
|
2015-03-11 23:47:02 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if(!moves->movesNumber)
|
|
|
|
{
|
|
|
|
turn_row=0;
|
|
|
|
turn_col=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int r = rand() % moves->movesNumber;
|
|
|
|
turn_row=moves->list[r].turnRow;
|
|
|
|
turn_col=moves->list[r].turnCol;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef debugprint
|
|
|
|
printf("======================\nGewählter Zug: %d,%d\n======================\n", turn_row, turn_col);
|
|
|
|
#endif
|
2015-03-11 15:52:48 +01:00
|
|
|
|
|
|
|
// 3. Return result
|
2015-03-11 23:47:02 +01:00
|
|
|
send_move(turn_row, turn_col);
|
2015-03-11 23:29:56 +01:00
|
|
|
//for(int i=0; i<fieldSize; ++i)
|
2015-03-11 15:52:48 +01:00
|
|
|
// delete [] field[i];
|
|
|
|
delete [] field;
|
|
|
|
delete [] moves;
|
|
|
|
|
|
|
|
/* END PLAYER-SPECIFIC CODE */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-03-07 17:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|