PraktikumOthello/players/my-player.cc

149 lines
3.6 KiB
C++
Raw Normal View History

2015-03-07 17:18:50 +01:00
/* -*- Mode: C -*- */
#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>
#include <time.h>
2015-03-07 17:18:50 +01:00
#include <mcp.h>
#include "playerlib.h"
static int
2015-03-07 17:18:50 +01:00
send_move(int row, int col)
{
FILE *f = fdopen(dup(CHILD_OUT_FD), "w");
fprintf(f, "%u,%u", row, col);
fclose(f);
return 0;
}
unsigned int*
numNextMoves(cell (*field)[fieldSize], movesList *moves) {
//switch stone char
char tmpown = ownc, tmpenemy = enemyc;
char enemyc = getEnemyChar(tmpenemy);
char ownc = getEnemyChar(tmpown);
#ifdef debugprint
printf("Own: %c Enemy: %c\n", ownc, enemyc);
#endif
unsigned int *numberOfEnemyMoves= new unsigned int[maxMoves]();
for (size_t moveNum=0; moveNum < moves->movesNumber; ++moveNum)
{
size_t fieldMemSize = fieldSize*fieldSize*sizeof(cell);
//cell (*futureField)[fieldSize] = (cell (*)[fieldSize]) malloc(fieldMemSize);
auto futureField = new cell[fieldSize][fieldSize]();
memcpy(futureField, field, fieldMemSize);
futureField[(moves->list[moveNum].turnRow-1)][(moves->list[moveNum].turnCol-1)].content=enemyc;
printf("--------------------\nfutureField\n");
#ifdef debugprint
for(size_t n=0;n<fieldSize;++n)
{
for(size_t m=0; m<fieldSize; ++m)
{
printf("%c ", futureField[n][m].content);
}
printf("\n");
}
#endif
movesList *futureMoves = new movesList();
findMoves(futureField, futureMoves);
numberOfEnemyMoves[moveNum] = futureMoves->movesNumber;
delete [] futureMoves;
delete [] futureField;
//free(futureField);
}
return numberOfEnemyMoves;
}
2015-03-12 17:09:34 +01:00
2015-03-07 17:18:50 +01:00
int main(void)
{
int done = 0;
srandom(time(NULL));
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.
//number of stones of X/O
unsigned int numX=0, numO=0; //TODO: make it a property of field struct
ownc = state_buffer[0];
if(!enemyc)
{
enemyc = getEnemyChar(ownc);
}
auto field = new cell[fieldSize][fieldSize]();
readStateBuffer(state_buffer, field, &numX, &numO); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[]
movesList *moves = new movesList();
findMoves(field, moves);
unsigned int *numOfEnemyMoves = numNextMoves(field, moves);
for(size_t i=0; i<moves->movesNumber; ++i)
{
printf("Moves %d %d -> %d enemy moves\n", moves->list[i].turnRow, moves->list[i].turnCol, numOfEnemyMoves[i]);
}
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("numX: %d, numO: %d\n", numX, numO);
printf("%d %d\n", sizeof(field), sizeof(cell[fieldSize][fieldSize]));
2015-03-12 17:09:34 +01:00
move te;
te.turnRow=2;
te.turnCol=1;
printf("IsCorner? %d\n", isCOrXField(te));
#endif
// 3. Return result
send_move(turn_row, turn_col);
delete [] field;
delete [] moves;
/* END PLAYER-SPECIFIC CODE */
}
return 0;
2015-03-07 17:18:50 +01:00
}
/* EOF */