PraktikumOthello/players/playerlib.cc

289 lines
6.8 KiB
C++

//#define debugprint
#include <stdio.h>
#include <stdlib.h>
#include "playerlib.h"
//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
getEnemyChar(char c) {
char enemyc;
switch(c)
{
case 'X':
enemyc='O'; break;
case 'O':
enemyc='X'; break;
}
return enemyc;
}
int
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldWidth]) {
for(size_t row=0; row< fieldHeight; ++row)
{
for(size_t col=0; col< fieldWidth; ++col)
{
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;
}
int
fillMovesList(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t col, unsigned int *ownCount, unsigned int *enemyCount) {
++(field[row][col].timesVisited);
if (field[row][col].content == ownc)
{
++(*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;
}
int
findHorizontalForwardMoves(cell (*field)[fieldWidth], movesList *moves) {
for (size_t row = 0;row < fieldHeight; ++row)
{
//Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0;
for (size_t col = 0;col < fieldWidth; ++col)
{
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
}
}
return 0;
}
int
findHorizontalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) {
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);
}
}
return 0;
}
int
findVerticalForwardMoves(cell (*field)[fieldWidth], movesList *moves) {
for (size_t col = 0;col < fieldHeight; ++col)
{
//Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0;
for (size_t row = 0;row < fieldWidth; ++row)
{
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
}
}
return 0;
}
int
findVerticalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) {
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);
}
}
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<fieldHeight; ++i)
{
#ifdef debugprint
printf("\n---------------------------------\n");
#endif
//Anzahl eigener/gegnerischer Steine
unsigned int ownCount=0, enemyCount=0;
for(int row=0; row<= i && row < fieldHeight; ++row)
{
int col = i-row;
#ifdef debugprint
printf("%d %d\n", row, col);
#endif
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
}
}
for(int i=fieldWidth; i<2*fieldWidth-1; ++i)
{
#ifdef debugprint
printf("i=%d\n", i);
printf("\n---------------------------------\n");
#endif
unsigned int ownCount=0, enemyCount=0;
int col=7;
int row= i-col;
while(row < fieldWidth-1)
{
row= i-col;
#ifdef debugprint
printf("%d %d\n", row, col);
#endif
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
col--;
}
//letzte "Diagonale" hätte Länge 1, kann keinen Zug enthalten
}
return 0;
}
int
findDiagonalTopRightMoves(cell (*field)[fieldWidth], movesList *moves) {
for(int i=2*fieldWidth-2; 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<fieldWidth-1)
{
col= i-row;
#ifdef debugprint
printf("%d %d\n", row, col);
#endif
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
row--;
}
}
for(int i=fieldWidth-1; i>=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;
}