PraktikumOthello/players/playerlib.cc

289 lines
6.7 KiB
C++
Raw Normal View History

2015-03-11 23:26:52 +01:00
//#define debugprint
2015-03-07 17:18:50 +01:00
#include <stdio.h>
#include <stdlib.h>
#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 iterDiagForwards(cell (*)[fieldSize], movesList *, size_t, size_t);
2015-03-11 23:26:52 +01:00
int iterDiagBackwards(cell (*)[fieldSize], movesList *, int, int);
2015-03-11 23:26:52 +01:00
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;
2015-03-10 13:54:11 +01:00
}
int
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize]) {
for(size_t row=0; row< fieldSize; ++row)
2015-03-10 13:54:11 +01:00
{
for(size_t col=0; col< fieldSize; ++col)
2015-03-10 13:54:11 +01:00
{
int index = row*fieldSize+col+1;
2015-03-10 13:54:11 +01:00
gameField[row][col].content = stateBuffer[index];
2015-03-11 23:26:52 +01:00
#ifdef debugprint
2015-03-10 13:54:11 +01:00
//printf("gameField %lu %lu =stateBuffer %d = %c\n", row, col, index, gameField[row][col].content);
2015-03-11 23:26:52 +01:00
#endif
2015-03-10 13:54:11 +01:00
}
}
return 0;
}
int
fillMovesList(cell (*field)[fieldSize], 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;
2015-03-11 23:26:52 +01:00
#ifdef debugprint
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
2015-03-11 23:26:52 +01:00
#endif
}
if(field[row][col].content == enemyc && ownCount)
{
++(*enemyCount);
2015-03-11 23:26:52 +01:00
#ifdef debugprint
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
2015-03-11 23:26:52 +01:00
#endif
}
if(field[row][col].content == '.')
{
if (*ownCount && *enemyCount > 0)
{
2015-03-11 23:26:52 +01:00
#ifdef debugprint
printf("BAM!\n");
2015-03-11 23:26:52 +01:00
#endif
moves->list[moves->movesNumber].turnRow = row+1;
moves->list[moves->movesNumber].turnCol = col+1;
++(moves->movesNumber);
}
*ownCount = 0;
*enemyCount = 0;
2015-03-11 23:26:52 +01:00
#ifdef debugprint
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
2015-03-11 23:26:52 +01:00
#endif
}
return 0;
}
int
findHorizontalForwardMoves(cell (*field)[fieldSize], movesList *moves) {
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);
}
}
return 0;
}
int
findHorizontalBackwardMoves(cell (*field)[fieldSize], 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)[fieldSize], movesList *moves) {
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);
}
}
return 0;
}
int
findVerticalBackwardMoves(cell (*field)[fieldSize], 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;
}
2015-03-11 23:26:52 +01:00
int
iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col) {
2015-03-11 23:26:52 +01:00
//Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0;
for (; row < fieldSize && col < fieldSize; ++row, ++col)
2015-03-11 23:26:52 +01:00
{
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
#ifdef debugprint
//printf("%d,%d\n", row,col);
#endif
}
return 0;
}
int
iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col) {
2015-03-11 23:26:52 +01:00
//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)[fieldSize], movesList *moves) {
for (size_t col = 0; col < fieldSize; ++col)
2015-03-11 23:26:52 +01:00
{
#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)[fieldSize], movesList *moves) {
for (int col = fieldSize-1; col >= 0; --col)
2015-03-11 23:26:52 +01:00
{
#ifdef debugprint
printf("F%d\n",col);
printf("\n---------------------------------\n");
#endif
iterDiagBackwards(field, moves, fieldSize-1, col);
if(col != fieldSize-1)
2015-03-11 23:26:52 +01:00
{
#ifdef debugprint
printf("\n---------------------------------\n");
#endif
iterDiagBackwards(field, moves, col, fieldSize-1);
2015-03-11 23:26:52 +01:00
}
}
return 0;
}
int
findDiagonalBottomLeftMoves(cell (*field)[fieldSize], movesList *moves) {
for(int i=0; i<fieldSize; ++i)
2015-03-11 23:26:52 +01:00
{
#ifdef debugprint
printf("\n---------------------------------\n");
#endif
//Anzahl eigener/gegnerischer Steine
unsigned int ownCount=0, enemyCount=0;
for(int row=0; row<= i && row < fieldSize; ++row)
2015-03-11 23:26:52 +01:00
{
int col = i-row;
#ifdef debugprint
printf("%d %d\n", row, col);
#endif
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
}
}
for(int i=fieldSize; i<2*fieldSize-1; ++i)
2015-03-11 23:26:52 +01:00
{
#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 < fieldSize-1)
2015-03-11 23:26:52 +01:00
{
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)[fieldSize], movesList *moves) {
for(int i=2*fieldSize-2; i>=fieldSize; --i)
2015-03-11 23:26:52 +01:00
{
#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<fieldSize-1)
2015-03-11 23:26:52 +01:00
{
col= i-row;
#ifdef debugprint
printf("%d %d\n", row, col);
#endif
fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
row--;
}
}
for(int i=fieldSize-1; i>=0; --i)
2015-03-11 23:26:52 +01:00
{
#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;
}