begin work on strategic my-player
numX, numO contains number of X/O stones
This commit is contained in:
parent
27d563173e
commit
76981af571
2
Makefile
2
Makefile
|
@ -40,7 +40,7 @@ othello : $(SRC_mcp:.cc=.o)
|
||||||
example-player: players/example-player.o
|
example-player: players/example-player.o
|
||||||
$(CXX) $^ -o $@ $(LDFLAGS)
|
$(CXX) $^ -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
my-player : players/my-player.o
|
my-player : players/my-player.o players/playerlib.o
|
||||||
$(CXX) $^ -o $@ $(LDFLAGS)
|
$(CXX) $^ -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
random-player : players/random-player.o players/playerlib.o
|
random-player : players/random-player.o players/playerlib.o
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
/* -*- Mode: C -*- */
|
/* -*- Mode: C -*- */
|
||||||
|
#define debugprint
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include <mcp.h>
|
#include <mcp.h>
|
||||||
|
|
||||||
static int
|
#include "playerlib.h"
|
||||||
|
|
||||||
|
static int
|
||||||
send_move(int row, int col)
|
send_move(int row, int col)
|
||||||
{
|
{
|
||||||
FILE *f = fdopen(dup(CHILD_OUT_FD), "w");
|
FILE *f = fdopen(dup(CHILD_OUT_FD), "w");
|
||||||
|
@ -20,38 +24,67 @@ send_move(int row, int col)
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int done = 0;
|
int done = 0;
|
||||||
|
srandom(time(NULL));
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
int turn_row = 0;
|
int turn_row = 0;
|
||||||
int turn_col = 0;
|
int turn_col = 0;
|
||||||
/* BEGIN PLAYER-SPECIFIC CODE */
|
/* BEGIN PLAYER-SPECIFIC CODE */
|
||||||
|
|
||||||
// 1. Read state
|
// 1. Read state
|
||||||
char state_buffer[128];
|
char state_buffer[128];
|
||||||
ssize_t bytes = read(CHILD_IN_FD, state_buffer, sizeof(state_buffer));
|
ssize_t bytes = read(CHILD_IN_FD, state_buffer, sizeof(state_buffer));
|
||||||
if (bytes != 65) // invalid number of chars
|
if (bytes != 65) // invalid number of chars
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
// state_buffer enthält jetzt 65 Zeichen ('.' oder 'X' oder 'O'):
|
// state_buffer enthält jetzt 65 Zeichen ('.' oder 'X' oder 'O'):
|
||||||
// * Das ERSTE Zeichen gibt an, welcher Spieler an der Reihe ist.
|
// * Das ERSTE Zeichen gibt an, welcher Spieler an der Reihe ist.
|
||||||
// * Die weiteren 64 Zeichen definieren die Belegung des Feldes.
|
// * Die weiteren 64 Zeichen definieren die Belegung des Feldes.
|
||||||
// * Die Belegung wird reihenweise gegeben, d.h. die Zeichen
|
// * Die Belegung wird reihenweise gegeben, d.h. die Zeichen
|
||||||
// 1 bis 8 definieren die erste Zeile des Feldes, Zeichen 9 bis 17
|
// 1 bis 8 definieren die erste Zeile des Feldes, Zeichen 9 bis 17
|
||||||
// geben die zweite Zeile usw.
|
// geben die zweite Zeile usw.
|
||||||
// * X und O stehen hierbei für die jeweiligen Spieler. Leere Felder
|
// * X und O stehen hierbei für die jeweiligen Spieler. Leere Felder
|
||||||
// sind durch einen Punkt (.) gekennzeichnet.
|
// sind durch einen Punkt (.) gekennzeichnet.
|
||||||
|
|
||||||
// 2. TODO: Strategie hier einfügen. Resultat in turn_row und turn_col speichern.
|
// 2. TODO: Strategie hier einfügen. Resultat in turn_row und turn_col speichern.
|
||||||
|
|
||||||
// 3. Return result
|
//number of stones of X/O
|
||||||
send_move(turn_row, turn_col);
|
unsigned int numX=0, numO=0; //TODO: make it a property of field struct
|
||||||
|
if(!enemyc)
|
||||||
|
{
|
||||||
|
ownc = state_buffer[0];
|
||||||
|
enemyc = getEnemyChar(ownc);
|
||||||
|
}
|
||||||
|
|
||||||
/* END PLAYER-SPECIFIC CODE */
|
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
|
||||||
|
{
|
||||||
|
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef debugprint
|
||||||
|
printf("numX: %d, numO: %d\n", numX, numO);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 3. Return result
|
||||||
|
send_move(turn_row, turn_col);
|
||||||
|
|
||||||
|
/* END PLAYER-SPECIFIC CODE */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* EOF */
|
/* EOF */
|
||||||
|
|
|
@ -27,28 +27,32 @@ char enemyc = 0, ownc = 0;
|
||||||
|
|
||||||
char
|
char
|
||||||
getEnemyChar(char c) {
|
getEnemyChar(char c) {
|
||||||
char enemyc;
|
char enemyc;
|
||||||
switch(c)
|
switch(c)
|
||||||
{
|
{
|
||||||
case 'X':
|
case 'X':
|
||||||
enemyc='O'; break;
|
enemyc='O'; break;
|
||||||
case 'O':
|
case 'O':
|
||||||
enemyc='X'; break;
|
enemyc='X'; break;
|
||||||
}
|
}
|
||||||
return enemyc;
|
return enemyc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize]) {
|
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize], unsigned int *numX, unsigned int *numO) {
|
||||||
for(size_t row=0; row< fieldSize; ++row)
|
for(size_t row=0; row < fieldSize; ++row)
|
||||||
{
|
{
|
||||||
for(size_t col=0; col< fieldSize; ++col)
|
for(size_t col=0; col< fieldSize; ++col)
|
||||||
{
|
{
|
||||||
int index = row*fieldSize+col+1;
|
int index = row*fieldSize+col+1;
|
||||||
gameField[row][col].content = stateBuffer[index];
|
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);
|
//printf("gameField %lu %lu =stateBuffer %d = %c\n", row, col, index, gameField[row][col].content);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -155,9 +159,9 @@ iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t
|
||||||
for (; row < fieldSize && col < fieldSize; ++row, ++col)
|
for (; row < fieldSize && col < fieldSize; ++row, ++col)
|
||||||
{
|
{
|
||||||
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
|
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
|
||||||
#ifdef debugprint
|
#ifdef debugprint
|
||||||
//printf("%d,%d\n", row,col);
|
//printf("%d,%d\n", row,col);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -169,9 +173,9 @@ iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col)
|
||||||
for (; row >= 0 && col >= 0; --row, --col)
|
for (; row >= 0 && col >= 0; --row, --col)
|
||||||
{
|
{
|
||||||
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
|
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
|
||||||
#ifdef debugprint
|
#ifdef debugprint
|
||||||
//printf("%d,%d\n", row,col);
|
//printf("%d,%d\n", row,col);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ struct movesList {
|
||||||
//returns the character used for the enemy's stones
|
//returns the character used for the enemy's stones
|
||||||
extern char getEnemyChar(char);
|
extern char getEnemyChar(char);
|
||||||
//reads the stateBuffer string into a 2d matrix
|
//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
|
//iterates through field in all directions, stores moves into movesList
|
||||||
int findMoves(cell (*)[fieldSize], movesList *);
|
int findMoves(cell (*)[fieldSize], movesList *);
|
||||||
|
|
|
@ -50,6 +50,9 @@ int main(void)
|
||||||
|
|
||||||
// 2. TODO: Strategie hier einfügen. Resultat in turn_row und turn_col speichern.
|
// 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)
|
if(!enemyc)
|
||||||
{
|
{
|
||||||
ownc = state_buffer[0];
|
ownc = state_buffer[0];
|
||||||
|
@ -62,7 +65,7 @@ int main(void)
|
||||||
//C++ version:
|
//C++ version:
|
||||||
auto field = new cell[fieldSize][fieldSize]();
|
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();
|
movesList *moves = new movesList();
|
||||||
|
|
||||||
findMoves(field, moves);
|
findMoves(field, moves);
|
||||||
|
|
Loading…
Reference in a new issue