begin work on strategic my-player

numX, numO contains number of X/O stones
master
Trolli Schmittlauch 2015-03-12 15:23:47 +01:00
parent 27d563173e
commit 76981af571
5 changed files with 87 additions and 47 deletions

View File

@ -40,7 +40,7 @@ othello : $(SRC_mcp:.cc=.o)
example-player: players/example-player.o
$(CXX) $^ -o $@ $(LDFLAGS)
my-player : players/my-player.o
my-player : players/my-player.o players/playerlib.o
$(CXX) $^ -o $@ $(LDFLAGS)
random-player : players/random-player.o players/playerlib.o

View File

@ -1,14 +1,18 @@
/* -*- Mode: C -*- */
#define debugprint
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <mcp.h>
static int
#include "playerlib.h"
static int
send_move(int row, int col)
{
FILE *f = fdopen(dup(CHILD_OUT_FD), "w");
@ -21,6 +25,7 @@ send_move(int row, int col)
int main(void)
{
int done = 0;
srandom(time(NULL));
while (!done) {
int turn_row = 0;
@ -44,6 +49,34 @@ int main(void)
// 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
if(!enemyc)
{
ownc = state_buffer[0];
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);
if(!moves->movesNumber)
{
turn_row=0;
turn_col=0;
}
else
{
}
#ifdef debugprint
printf("numX: %d, numO: %d\n", numX, numO);
#endif
// 3. Return result
send_move(turn_row, turn_col);

View File

@ -39,16 +39,20 @@ getEnemyChar(char c) {
}
int
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize]) {
for(size_t row=0; row< fieldSize; ++row)
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize], unsigned int *numX, unsigned int *numO) {
for(size_t row=0; row < fieldSize; ++row)
{
for(size_t col=0; col< fieldSize; ++col)
{
int index = row*fieldSize+col+1;
gameField[row][col].content = stateBuffer[index];
#ifdef debugprint
if (stateBuffer[index] == 'X')
++(*numX);
if (stateBuffer[index] == 'O')
++(*numO);
#ifdef debugprint
//printf("gameField %lu %lu =stateBuffer %d = %c\n", row, col, index, gameField[row][col].content);
#endif
#endif
}
}
return 0;
@ -155,9 +159,9 @@ iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t
for (; row < fieldSize && col < fieldSize; ++row, ++col)
{
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
#ifdef debugprint
#ifdef debugprint
//printf("%d,%d\n", row,col);
#endif
#endif
}
return 0;
}
@ -169,9 +173,9 @@ iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col)
for (; row >= 0 && col >= 0; --row, --col)
{
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
#ifdef debugprint
#ifdef debugprint
//printf("%d,%d\n", row,col);
#endif
#endif
}
return 0;
}

View File

@ -22,7 +22,7 @@ struct movesList {
//returns the character used for the enemy's stones
extern char getEnemyChar(char);
//reads the stateBuffer string into a 2d matrix
extern int readStateBuffer(char*, cell (*)[fieldSize]);
extern int readStateBuffer(char*, cell (*)[fieldSize], unsigned int *, unsigned int *);
//iterates through field in all directions, stores moves into movesList
int findMoves(cell (*)[fieldSize], movesList *);

View File

@ -50,6 +50,9 @@ int main(void)
// 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
if(!enemyc)
{
ownc = state_buffer[0];
@ -62,7 +65,7 @@ int main(void)
//C++ version:
auto field = new cell[fieldSize][fieldSize]();
readStateBuffer(state_buffer, field); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[]
readStateBuffer(state_buffer, field, &numX, &numO); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[]
movesList *moves = new movesList();
findMoves(field, moves);