moving enemyc, ownc out of global scope, expilicitly passed with every

function call
This commit is contained in:
Trolli Schmittlauch 2015-03-13 10:09:10 +01:00
parent 4dd39b5e79
commit 88bf2f215a
4 changed files with 62 additions and 65 deletions

View file

@ -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<evalMove> *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<int> curEvals;
@ -121,12 +112,19 @@ findBestMove(gameTree *currentNode, int depth) {
return maxEvalMove(&curMoves);
}
//move
//nFindBestMove(cell (*field)[fieldSize], movesList *movesa) {
// std::vector<evalMove> 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;

View file

@ -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
@ -238,7 +238,7 @@ findDiagonalBottomLeftMoves(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);
}
}
@ -257,7 +257,7 @@ findDiagonalBottomLeftMoves(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);
col--;
}
//letzte "Diagonale" hätte Länge 1, kann keinen Zug enthalten
@ -266,7 +266,7 @@ findDiagonalBottomLeftMoves(cell (*field)[fieldSize], movesList *moves) {
}
int
findDiagonalTopRightMoves(cell (*field)[fieldSize], movesList *moves) {
findDiagonalTopRightMoves(cell (*field)[fieldSize], movesList *moves, char enemyc) {
for(int i=2*fieldSize-2; 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

View file

@ -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);

View file

@ -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");