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
|
||||
$(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
|
||||
|
|
|
@ -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");
|
||||
|
@ -20,38 +24,67 @@ send_move(int row, int col)
|
|||
|
||||
int main(void)
|
||||
{
|
||||
int done = 0;
|
||||
int done = 0;
|
||||
srandom(time(NULL));
|
||||
|
||||
while (!done) {
|
||||
int turn_row = 0;
|
||||
int turn_col = 0;
|
||||
/* BEGIN PLAYER-SPECIFIC CODE */
|
||||
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();
|
||||
// 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.
|
||||
|
||||
// 3. Return result
|
||||
send_move(turn_row, turn_col);
|
||||
// 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.
|
||||
|
||||
/* END PLAYER-SPECIFIC CODE */
|
||||
// 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();
|
||||
|
||||
return 0;
|
||||
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);
|
||||
|
||||
/* END PLAYER-SPECIFIC CODE */
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* EOF */
|
||||
|
|
|
@ -27,28 +27,32 @@ char enemyc = 0, ownc = 0;
|
|||
|
||||
char
|
||||
getEnemyChar(char c) {
|
||||
char enemyc;
|
||||
switch(c)
|
||||
{
|
||||
case 'X':
|
||||
enemyc='O'; break;
|
||||
case 'O':
|
||||
enemyc='X'; break;
|
||||
}
|
||||
return enemyc;
|
||||
char enemyc;
|
||||
switch(c)
|
||||
{
|
||||
case 'X':
|
||||
enemyc='O'; break;
|
||||
case 'O':
|
||||
enemyc='X'; break;
|
||||
}
|
||||
return enemyc;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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 *);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue