From 88bf2f215a9eb54c01b28feaa8012694f9fe3fde Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Fri, 13 Mar 2015 10:09:10 +0100 Subject: [PATCH] moving enemyc, ownc out of global scope, expilicitly passed with every function call --- players/my-player.cc | 28 ++++++------ players/playerlib.cc | 92 ++++++++++++++++++++-------------------- players/playerlib.h | 4 +- players/random-player.cc | 3 +- 4 files changed, 62 insertions(+), 65 deletions(-) diff --git a/players/my-player.cc b/players/my-player.cc index d40e8e6..9980edb 100644 --- a/players/my-player.cc +++ b/players/my-player.cc @@ -29,7 +29,7 @@ buildTree(gameTree *currentNode, cell (*field)[fieldSize], movesList *moves, int currentNode->numChildMoves = moves->movesNumber; currentNode->childMove = new gameTree[maxMoves](); //switch stone char - ownc = getEnemyChar(enemyc); + char ownc = getEnemyChar(enemyc); //#ifdef debugprint // printf("Depth %d: enemy: %c\n", depth, enemyc); @@ -42,7 +42,7 @@ buildTree(gameTree *currentNode, cell (*field)[fieldSize], movesList *moves, int memcpy(futureField, field, fieldMemSize); futureField[(moves->list[i].turnRow-1)][(moves->list[i].turnCol-1)].content=enemyc; movesList *futureMoves = new movesList(); - findMoves(futureField, futureMoves); + findMoves(futureField, futureMoves, enemyc); buildTree(&(currentNode->childMove[i]), futureField, futureMoves, depth-1, getEnemyChar(enemyc)); delete [] futureMoves; delete [] futureField; @@ -54,19 +54,10 @@ move maxEvalMove(std::vector *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; } @@ -93,7 +84,7 @@ 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)); + return (currentNode->numChildMoves + 5*isCornerField(currentNode->nodeMove) - 2*isCOrXField(currentNode->nodeMove)); } std::vector curEvals; @@ -121,12 +112,19 @@ findBestMove(gameTree *currentNode, int depth) { return maxEvalMove(&curMoves); } +//move +//nFindBestMove(cell (*field)[fieldSize], movesList *movesa) { +// std::vector curMoves; + + int main(void) { int done = 0; srandom(time(NULL)); + char enemyc=0, ownc=0; + while (!done) { int turn_row = 0; int turn_col = 0; @@ -162,7 +160,7 @@ int main(void) readStateBuffer(state_buffer, field, &numX, &numO); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[] movesList *moves = new movesList(); - findMoves(field, moves); + findMoves(field, moves, enemyc); // unsigned int *numOfEnemyMoves = numNextMoves(field, moves); @@ -187,8 +185,8 @@ int main(void) gameTree *treeRoot = new gameTree(); treeRoot->nodeMove.turnRow = 0; treeRoot->nodeMove.turnCol = 0; - buildTree(treeRoot, field, moves, 3, getEnemyChar(enemyc)); - move finalMove = findBestMove(treeRoot, 3); + buildTree(treeRoot, field, moves, 1, getEnemyChar(enemyc)); + move finalMove = findBestMove(treeRoot, 1); turn_row=finalMove.turnRow; turn_col=finalMove.turnCol; diff --git a/players/playerlib.cc b/players/playerlib.cc index 33f7b49..e15aa06 100644 --- a/players/playerlib.cc +++ b/players/playerlib.cc @@ -6,25 +6,24 @@ #include "playerlib.h" //determines whether move or not and adds them to a movesList -int fillMovesList(cell (*)[fieldSize], movesList *, size_t, size_t, unsigned int *, unsigned int *); +int fillMovesList(cell (*)[fieldSize], movesList *, size_t, size_t, unsigned int *, unsigned int *, char); -int iterDiagForwards(cell (*)[fieldSize], movesList *, size_t, size_t); +int iterDiagForwards(cell (*)[fieldSize], movesList *, size_t, size_t, char); -int iterDiagBackwards(cell (*)[fieldSize], movesList *, int, int); +int iterDiagBackwards(cell (*)[fieldSize], movesList *, int, int, char); //adds all hotizontally-forward found moves to movesList -int findHorizontalForwardMoves(cell (*)[fieldSize], movesList *); -int findHorizontalBackwardMoves(cell (*)[fieldSize], movesList *); -int findVerticalForwardMoves(cell (*)[fieldSize], movesList *); -int findVerticalBackwardMoves(cell (*)[fieldSize], movesList *); -int findDiagonalTopLeftMoves(cell (*)[fieldSize], movesList *); -int findDiagonalBottomRightMoves(cell (*)[fieldSize], movesList *); -int findDiagonalBottomLeftMoves(cell (*)[fieldSize], movesList *); -int findDiagonalTopRightMoves(cell (*)[fieldSize], movesList *); +int findHorizontalForwardMoves(cell (*)[fieldSize], movesList *, char); +int findHorizontalBackwardMoves(cell (*)[fieldSize], movesList *, char); +int findVerticalForwardMoves(cell (*)[fieldSize], movesList *, char); +int findVerticalBackwardMoves(cell (*)[fieldSize], movesList *, char); +int findDiagonalTopLeftMoves(cell (*)[fieldSize], movesList *, char); +int findDiagonalBottomRightMoves(cell (*)[fieldSize], movesList *, char); +int findDiagonalBottomLeftMoves(cell (*)[fieldSize], movesList *, char); +int findDiagonalTopRightMoves(cell (*)[fieldSize], movesList *, char); -char enemyc = 0, ownc = 0; unsigned int endgame=0; char @@ -61,8 +60,9 @@ readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize], unsigned } int -fillMovesList(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col, unsigned int *ownCount, unsigned int *enemyCount) { +fillMovesList(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col, unsigned int *ownCount, unsigned int *enemyCount, char enemyc) { ++(field[row][col].timesVisited); + char ownc = getEnemyChar(enemyc); if (field[row][col].content == ownc) { ++(*ownCount); @@ -99,68 +99,68 @@ fillMovesList(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col } int -findHorizontalForwardMoves(cell (*field)[fieldSize], movesList *moves) { +findHorizontalForwardMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for (size_t row = 0;row < fieldSize; ++row) { //Anzahl eigener/gegnerischer Steine unsigned int ownCount = 0, enemyCount = 0; for (size_t col = 0;col < fieldSize; ++col) { - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); } } return 0; } int -findHorizontalBackwardMoves(cell (*field)[fieldSize], movesList *moves) { +findHorizontalBackwardMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for (int row = 7; row >= 0; --row) { //Anzahl eigener/gegnerischer Steine unsigned int ownCount = 0, enemyCount = 0; for (int col = 7; col >= 0; --col) { - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); } } return 0; } int -findVerticalForwardMoves(cell (*field)[fieldSize], movesList *moves) { +findVerticalForwardMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for (size_t col = 0;col < fieldSize; ++col) { //Anzahl eigener/gegnerischer Steine unsigned int ownCount = 0, enemyCount = 0; for (size_t row = 0;row < fieldSize; ++row) { - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); } } return 0; } int -findVerticalBackwardMoves(cell (*field)[fieldSize], movesList *moves) { +findVerticalBackwardMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for (int col = 7; col >= 0; --col) { //Anzahl eigener/gegnerischer Steine unsigned int ownCount = 0, enemyCount = 0; for (int row = 7; row >= 0; --row) { - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); } } return 0; } int -iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col) { +iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col, char enemyc) { //Anzahl eigener/gegnerischer Steine unsigned int ownCount = 0, enemyCount = 0; for (; row < fieldSize && col < fieldSize; ++row, ++col) { - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); #ifdef debugprint //printf("%d,%d\n", row,col); #endif @@ -169,12 +169,12 @@ iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t } int -iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col) { +iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col, char enemyc) { //Anzahl eigener/gegnerischer Steine unsigned int ownCount = 0, enemyCount = 0; for (; row >= 0 && col >= 0; --row, --col) { - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); #ifdef debugprint //printf("%d,%d\n", row,col); #endif @@ -183,48 +183,48 @@ iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col) } int -findDiagonalBottomRightMoves(cell (*field)[fieldSize], movesList *moves) { +findDiagonalBottomRightMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for (size_t col = 0; col < fieldSize; ++col) { #ifdef debugprint printf("F%d\n",col); printf("\n---------------------------------\n"); #endif - iterDiagForwards(field, moves, 0, col); + iterDiagForwards(field, moves, 0, col, enemyc); if(col != 0) { //2. Mal aufrufen, da Matrix symmetrisch zu transponierter (?) #ifdef debugprint printf("\n---------------------------------\n"); #endif - iterDiagForwards(field, moves, col, 0); + iterDiagForwards(field, moves, col, 0, enemyc); } } return 0; } int -findDiagonalTopLeftMoves(cell (*field)[fieldSize], movesList *moves) { +findDiagonalTopLeftMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for (int col = fieldSize-1; col >= 0; --col) { #ifdef debugprint printf("F%d\n",col); printf("\n---------------------------------\n"); #endif - iterDiagBackwards(field, moves, fieldSize-1, col); + iterDiagBackwards(field, moves, fieldSize-1, col, enemyc); if(col != fieldSize-1) { #ifdef debugprint printf("\n---------------------------------\n"); #endif - iterDiagBackwards(field, moves, col, fieldSize-1); + iterDiagBackwards(field, moves, col, fieldSize-1, enemyc); } } return 0; } int -findDiagonalBottomLeftMoves(cell (*field)[fieldSize], movesList *moves) { +findDiagonalBottomLeftMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { for(int i=0; i=fieldSize; --i) { #ifdef debugprint @@ -282,7 +282,7 @@ findDiagonalTopRightMoves(cell (*field)[fieldSize], movesList *moves) { #ifdef debugprint printf("%d %d\n", row, col); #endif - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); row--; } } @@ -299,22 +299,22 @@ findDiagonalTopRightMoves(cell (*field)[fieldSize], movesList *moves) { #ifdef debugprint printf("%d %d\n", row, col); #endif - fillMovesList(field, moves, row, col, &ownCount, &enemyCount); + fillMovesList(field, moves, row, col, &ownCount, &enemyCount, enemyc); } } return 0; } int -findMoves(cell (*field)[fieldSize], movesList *moves) { - return findHorizontalForwardMoves(field, moves) + - findHorizontalBackwardMoves(field, moves) + - findVerticalForwardMoves(field, moves) + - findVerticalBackwardMoves(field, moves) + - findDiagonalBottomRightMoves(field, moves) + - findDiagonalTopLeftMoves(field, moves) + - findDiagonalBottomLeftMoves(field, moves) + - findDiagonalTopRightMoves(field, moves); +findMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) { + return findHorizontalForwardMoves(field, moves, enemyc) + + findHorizontalBackwardMoves(field, moves, enemyc) + + findVerticalForwardMoves(field, moves, enemyc) + + findVerticalBackwardMoves(field, moves, enemyc) + + findDiagonalBottomRightMoves(field, moves, enemyc) + + findDiagonalTopLeftMoves(field, moves, enemyc) + + findDiagonalBottomLeftMoves(field, moves, enemyc) + + findDiagonalTopRightMoves(field, moves, enemyc); } int diff --git a/players/playerlib.h b/players/playerlib.h index f3ff553..3aa21d0 100644 --- a/players/playerlib.h +++ b/players/playerlib.h @@ -4,8 +4,6 @@ const int fieldSize=8, maxMoves=100; // =fieldHeight=fieldWidth extern unsigned int endgame; -extern char enemyc; //initially 0, contains char of enemy's stone -extern char ownc; struct cell { char content = '.'; unsigned short int timesVisited = 0; @@ -38,7 +36,7 @@ extern char getEnemyChar(char); 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 *); +int findMoves(cell (*)[fieldSize], movesList *, char); //returns 1 if move on corner field int isCornerField(move); diff --git a/players/random-player.cc b/players/random-player.cc index 079b737..245160d 100644 --- a/players/random-player.cc +++ b/players/random-player.cc @@ -27,6 +27,7 @@ int main(void) { int done = 0; srandom(time(NULL)); + char enemyc=0, ownc=0; while (!done) { int turn_row = 0; @@ -68,7 +69,7 @@ int main(void) readStateBuffer(state_buffer, field, &numX, &numO); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[] movesList *moves = new movesList(); - findMoves(field, moves); + findMoves(field, moves, enemyc); #ifdef debugprint printf("\nZüge:\n");