diff --git a/players/playerlib.cc b/players/playerlib.cc index b8074b1..fb71b3e 100644 --- a/players/playerlib.cc +++ b/players/playerlib.cc @@ -1,3 +1,4 @@ +//#define debugprint #include #include @@ -6,6 +7,10 @@ //determines whether move or not and adds them to a movesList int fillMovesList(cell (*)[fieldWidth], movesList *, size_t, size_t, unsigned int *, unsigned int *); +int iterDiagForwards(cell (*)[fieldWidth], movesList *, size_t, size_t); + +int iterDiagBackwards(cell (*)[fieldWidth], movesList *, int, int); + char enemyc = 0, ownc = 0; char @@ -29,7 +34,9 @@ readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldWidth]) { { int index = row*fieldHeight+col+1; gameField[row][col].content = stateBuffer[index]; + #ifdef debugprint //printf("gameField %lu %lu =stateBuffer %d = %c\n", row, col, index, gameField[row][col].content); + #endif } } return 0; @@ -42,25 +49,33 @@ fillMovesList(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t co { ++(*ownCount); *enemyCount = 0; +#ifdef debugprint printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount); +#endif } if(field[row][col].content == enemyc && ownCount) { ++(*enemyCount); +#ifdef debugprint printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount); +#endif } if(field[row][col].content == '.') { if (*ownCount && *enemyCount > 0) { +#ifdef debugprint printf("BAM!\n"); +#endif moves->list[moves->movesNumber].turnRow = row+1; moves->list[moves->movesNumber].turnCol = col+1; ++(moves->movesNumber); } *ownCount = 0; *enemyCount = 0; +#ifdef debugprint printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount); +#endif } return 0; } @@ -120,3 +135,154 @@ findVerticalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) { } return 0; } + +int +iterDiagForwards(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t col) { + //Anzahl eigener/gegnerischer Steine + unsigned int ownCount = 0, enemyCount = 0; + for (; row < fieldWidth && col < fieldHeight; ++row, ++col) + { + fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + #ifdef debugprint + //printf("%d,%d\n", row,col); + #endif + } + return 0; +} + +int +iterDiagBackwards(cell (*field)[fieldWidth], movesList *moves, int row, int col) { + //Anzahl eigener/gegnerischer Steine + unsigned int ownCount = 0, enemyCount = 0; + for (; row >= 0 && col >= 0; --row, --col) + { + fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + #ifdef debugprint + //printf("%d,%d\n", row,col); + #endif + } + return 0; +} + +int +findDiagonalBottomRightMoves(cell (*field)[fieldWidth], movesList *moves) { + for (size_t col = 0; col < fieldHeight; ++col) + { +#ifdef debugprint + printf("F%d\n",col); + printf("\n---------------------------------\n"); +#endif + iterDiagForwards(field, moves, 0, col); + if(col != 0) + { + //2. Mal aufrufen, da Matrix symmetrisch zu transponierter (?) +#ifdef debugprint + printf("\n---------------------------------\n"); +#endif + iterDiagForwards(field, moves, col, 0); + } + } + return 0; +} + +int +findDiagonalTopLeftMoves(cell (*field)[fieldWidth], movesList *moves) { + for (int col = fieldHeight-1; col >= 0; --col) + { +#ifdef debugprint + printf("F%d\n",col); + printf("\n---------------------------------\n"); +#endif + iterDiagBackwards(field, moves, fieldHeight-1, col); + if(col != fieldHeight-1) + { +#ifdef debugprint + printf("\n---------------------------------\n"); +#endif + iterDiagBackwards(field, moves, col, fieldWidth-1); + } + } + return 0; +} + +int +findDiagonalBottomLeftMoves(cell (*field)[fieldWidth], movesList *moves) { + for(unsigned int i=0; i=fieldWidth; --i) + { +#ifdef debugprint + printf("i=%d\n", i); + printf("\n---------------------------------\n"); +#endif + unsigned int ownCount=0, enemyCount=0; + int row=7; + int col= i-row; + while(col=0; --i) + { +#ifdef debugprint + printf("\n---------------------------------\n"); +#endif + //Anzahl eigener/gegnerischer Steine + unsigned int ownCount=0, enemyCount=0; + for(int col=0; col<=i; ++col) + { + int row = i-col; +#ifdef debugprint + printf("%d %d\n", row, col); +#endif + fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + } + } + return 0; +} diff --git a/players/playerlib.h b/players/playerlib.h index c455eb1..15f6421 100644 --- a/players/playerlib.h +++ b/players/playerlib.h @@ -1,7 +1,9 @@ #ifndef PLAYERLIB_H #define PLAYERLIB_H -const unsigned int fieldHeight=8, fieldWidth=8; +const int fieldHeight=8; +const int fieldWidth=fieldHeight; //spätere Annahme: Feld quadratisch + extern char enemyc; //initially 0, contains char of enemy's stone extern char ownc; struct cell { @@ -15,7 +17,7 @@ struct move { struct movesList { unsigned int movesNumber = 0; - move *list = new move[fieldWidth*fieldHeight-4](); + move *list = new move[100](); //eigentlich maximal 8*8-4 Lösungen, aber manche doppelt? -> Reserve }; //returns the character used for the enemy's stones @@ -28,5 +30,9 @@ int findHorizontalForwardMoves(cell (*)[fieldWidth], movesList *); int findHorizontalBackwardMoves(cell (*)[fieldWidth], movesList *); int findVerticalForwardMoves(cell (*)[fieldWidth], movesList *); int findVerticalBackwardMoves(cell (*)[fieldWidth], movesList *); +int findDiagonalTopLeftMoves(cell (*)[fieldWidth], movesList *); +int findDiagonalBottomRightMoves(cell (*)[fieldWidth], movesList *); +int findDiagonalBottomLeftMoves(cell (*)[fieldWidth], movesList *); +int findDiagonalTopRightMoves(cell (*)[fieldWidth], movesList *); #endif diff --git a/players/random-player.cc b/players/random-player.cc index 8489699..b6c1692 100644 --- a/players/random-player.cc +++ b/players/random-player.cc @@ -1,4 +1,5 @@ /* -*- Mode: C -*- */ +#define debugprint #include #include @@ -20,7 +21,6 @@ send_move(int row, int col) } - int main(void) { int done = 0; @@ -61,11 +61,23 @@ int main(void) readStateBuffer(state_buffer, field); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[] movesList *moves = new movesList(); + findHorizontalForwardMoves(field, moves); findHorizontalBackwardMoves(field, moves); findVerticalForwardMoves(field, moves); findVerticalBackwardMoves(field, moves); + findDiagonalBottomRightMoves(field, moves); + findDiagonalTopLeftMoves(field, moves); +#ifdef debugprint + printf("BottomLeft\n"); +#endif + findDiagonalBottomLeftMoves(field, moves); +#ifdef debugprint + printf("TopRight\n"); +#endif + findDiagonalTopRightMoves(field, moves); + printf("\nZüge:\n"); for (unsigned int i=0; imovesNumber; ++i) { printf("%d,%d\n",moves->list[i].turnRow,moves->list[i].turnCol);