2015-03-07 17:18:50 +01:00
|
|
|
/* -*- Mode: C -*- */
|
2015-03-12 15:23:47 +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-12 15:23:47 +01:00
|
|
|
#include <time.h>
|
2015-03-13 04:06:26 +01:00
|
|
|
#include <vector>
|
2015-03-07 17:18:50 +01:00
|
|
|
|
|
|
|
#include <mcp.h>
|
|
|
|
|
2015-03-12 15:23:47 +01:00
|
|
|
#include "playerlib.h"
|
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
int
|
|
|
|
buildTree(gameTree *currentNode, cell (*field)[fieldSize], movesList *moves, int depth, char enemyc) {
|
|
|
|
if (depth == 0 || moves->movesNumber == 0)
|
|
|
|
return 0;
|
|
|
|
currentNode->numChildMoves = moves->movesNumber;
|
|
|
|
currentNode->childMove = new gameTree[maxMoves]();
|
2015-03-12 22:52:29 +01:00
|
|
|
//switch stone char
|
2015-03-13 04:06:26 +01:00
|
|
|
ownc = getEnemyChar(enemyc);
|
|
|
|
|
|
|
|
//#ifdef debugprint
|
|
|
|
// printf("Depth %d: enemy: %c\n", depth, enemyc);
|
|
|
|
//#endif
|
|
|
|
for(size_t i=0; i<moves->movesNumber; ++i)
|
2015-03-12 22:52:29 +01:00
|
|
|
{
|
2015-03-13 04:06:26 +01:00
|
|
|
currentNode->childMove[i].nodeMove = moves->list[i];
|
2015-03-12 22:52:29 +01:00
|
|
|
size_t fieldMemSize = fieldSize*fieldSize*sizeof(cell);
|
|
|
|
auto futureField = new cell[fieldSize][fieldSize]();
|
|
|
|
memcpy(futureField, field, fieldMemSize);
|
2015-03-13 04:06:26 +01:00
|
|
|
futureField[(moves->list[i].turnRow-1)][(moves->list[i].turnCol-1)].content=enemyc;
|
2015-03-12 22:52:29 +01:00
|
|
|
movesList *futureMoves = new movesList();
|
|
|
|
findMoves(futureField, futureMoves);
|
2015-03-13 04:06:26 +01:00
|
|
|
buildTree(&(currentNode->childMove[i]), futureField, futureMoves, depth-1, getEnemyChar(enemyc));
|
2015-03-12 22:52:29 +01:00
|
|
|
delete [] futureMoves;
|
|
|
|
delete [] futureField;
|
|
|
|
}
|
2015-03-13 04:06:26 +01:00
|
|
|
return 1;
|
2015-03-12 22:52:29 +01:00
|
|
|
}
|
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
move
|
|
|
|
maxEvalMove(std::vector<evalMove> *curMoves) {
|
|
|
|
evalMove maxM;
|
|
|
|
maxM.evaluation = -9;
|
|
|
|
#ifdef debugprint
|
|
|
|
printf("Foo\n");
|
|
|
|
#endif
|
|
|
|
for(size_t i = 0; i < curMoves->size(); ++i)
|
|
|
|
{
|
|
|
|
#ifdef debugprint
|
|
|
|
printf("bar %d %d\n", maxM.evaluation, curMoves->at(i).evaluation);
|
|
|
|
#endif
|
|
|
|
if(curMoves->at(i).evaluation > maxM.evaluation)
|
|
|
|
{
|
|
|
|
#ifdef debugprint
|
|
|
|
printf("baz\n");
|
|
|
|
#endif
|
|
|
|
maxM.evaluation = curMoves->at(i).evaluation;
|
|
|
|
maxM.m = curMoves->at(i).m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxM.m;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
maxInt(std::vector<int> *evaluations) {
|
|
|
|
int maxE = -10;
|
|
|
|
for(size_t i = 0; i<evaluations->size(); ++i)
|
|
|
|
{
|
|
|
|
if(evaluations->at(i) > maxE)
|
|
|
|
{
|
|
|
|
maxE = evaluations->at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//MiniMax Algorithmus
|
|
|
|
int
|
|
|
|
eval(gameTree *currentNode, int depth) {
|
|
|
|
if (depth == 0 || !currentNode->numChildMoves)
|
|
|
|
{
|
|
|
|
//gut: hohe Mobilität, Eckzüge. Schlecht: Züge auf Felder direkt vor der Ecke
|
|
|
|
return ((-1)+currentNode->numChildMoves + 5*isCornerField(currentNode->nodeMove) - 2*isCOrXField(currentNode->nodeMove));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> curEvals;
|
|
|
|
for(size_t i = 0; i < currentNode->numChildMoves; ++i)
|
|
|
|
{
|
|
|
|
int curEval = -1 * eval(&(currentNode->childMove[i]), depth-1);
|
|
|
|
curEvals.push_back(curEval);
|
|
|
|
}
|
|
|
|
return maxInt(&curEvals);
|
|
|
|
}
|
|
|
|
|
|
|
|
move
|
|
|
|
findBestMove(gameTree *currentNode, int depth) {
|
|
|
|
std::vector<evalMove> curMoves;
|
|
|
|
for(size_t i = 0; i < currentNode->numChildMoves; ++i)
|
|
|
|
{
|
|
|
|
evalMove m;
|
|
|
|
m.evaluation = -1 * eval(&(currentNode->childMove[i]), depth-1);
|
|
|
|
m.m = currentNode->childMove[i].nodeMove;
|
|
|
|
#ifdef debugprint
|
|
|
|
printf("findBest %d: %d, %d\n", m.evaluation, m.m.turnRow, m.m.turnCol);
|
|
|
|
#endif
|
|
|
|
curMoves.push_back(m);
|
|
|
|
}
|
|
|
|
return maxEvalMove(&curMoves);
|
|
|
|
}
|
2015-03-12 22:52:29 +01:00
|
|
|
|
2015-03-12 17:09:34 +01:00
|
|
|
|
2015-03-07 17:18:50 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2015-03-12 15:23:47 +01:00
|
|
|
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
|
2015-03-12 22:52:29 +01:00
|
|
|
ownc = state_buffer[0];
|
|
|
|
|
2015-03-12 15:23:47 +01:00
|
|
|
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);
|
2015-03-12 22:52:29 +01:00
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
// unsigned int *numOfEnemyMoves = numNextMoves(field, moves);
|
2015-03-12 22:52:29 +01:00
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
// 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]);
|
|
|
|
// }
|
2015-03-12 22:52:29 +01:00
|
|
|
|
2015-03-12 15:56:23 +01:00
|
|
|
if(!(moves->movesNumber))
|
2015-03-12 15:23:47 +01:00
|
|
|
{
|
|
|
|
turn_row=0;
|
|
|
|
turn_col=0;
|
2015-03-13 04:06:26 +01:00
|
|
|
#ifdef debugprint
|
|
|
|
printf("Nope\n");
|
|
|
|
#endif
|
2015-03-12 15:23:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
if((numX+numO)>5*fieldSize*fieldSize/6)
|
|
|
|
endgame = 1;
|
|
|
|
gameTree *treeRoot = new gameTree();
|
|
|
|
treeRoot->nodeMove.turnRow = 0;
|
|
|
|
treeRoot->nodeMove.turnCol = 0;
|
|
|
|
buildTree(treeRoot, field, moves, 3, getEnemyChar(enemyc));
|
|
|
|
move finalMove = findBestMove(treeRoot, 3);
|
2015-03-12 15:23:47 +01:00
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
turn_row=finalMove.turnRow;
|
|
|
|
turn_col=finalMove.turnCol;
|
2015-03-12 15:23:47 +01:00
|
|
|
#ifdef debugprint
|
2015-03-13 04:06:26 +01:00
|
|
|
printf("Getätigter Zug: %d,%d\n", turn_row, turn_col);
|
2015-03-12 15:23:47 +01:00
|
|
|
#endif
|
2015-03-13 04:06:26 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 15:23:47 +01:00
|
|
|
|
|
|
|
// 3. Return result
|
|
|
|
send_move(turn_row, turn_col);
|
|
|
|
|
2015-03-13 04:06:26 +01:00
|
|
|
//free(numOfEnemyMoves);
|
2015-03-12 22:52:29 +01:00
|
|
|
delete [] field;
|
|
|
|
delete [] moves;
|
2015-03-12 15:23:47 +01:00
|
|
|
/* END PLAYER-SPECIFIC CODE */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-03-07 17:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|