replace fieldWidth and fieldHeight with fieldSize (field is a square)

This commit is contained in:
Trolli Schmittlauch 2015-03-11 23:29:56 +01:00
parent 8ac7a59a34
commit 48400b8ce2
3 changed files with 48 additions and 49 deletions

View file

@ -5,11 +5,11 @@
#include "playerlib.h" #include "playerlib.h"
//determines whether move or not and adds them to a movesList //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 fillMovesList(cell (*)[fieldSize], movesList *, size_t, size_t, unsigned int *, unsigned int *);
int iterDiagForwards(cell (*)[fieldWidth], movesList *, size_t, size_t); int iterDiagForwards(cell (*)[fieldSize], movesList *, size_t, size_t);
int iterDiagBackwards(cell (*)[fieldWidth], movesList *, int, int); int iterDiagBackwards(cell (*)[fieldSize], movesList *, int, int);
char enemyc = 0, ownc = 0; char enemyc = 0, ownc = 0;
@ -27,12 +27,12 @@ getEnemyChar(char c) {
} }
int int
readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldWidth]) { readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldSize]) {
for(size_t row=0; row< fieldHeight; ++row) for(size_t row=0; row< fieldSize; ++row)
{ {
for(size_t col=0; col< fieldWidth; ++col) for(size_t col=0; col< fieldSize; ++col)
{ {
int index = row*fieldHeight+col+1; int index = row*fieldSize+col+1;
gameField[row][col].content = stateBuffer[index]; gameField[row][col].content = stateBuffer[index];
#ifdef debugprint #ifdef debugprint
//printf("gameField %lu %lu =stateBuffer %d = %c\n", row, col, index, gameField[row][col].content); //printf("gameField %lu %lu =stateBuffer %d = %c\n", row, col, index, gameField[row][col].content);
@ -43,7 +43,7 @@ readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldWidth]) {
} }
int int
fillMovesList(cell (*field)[fieldWidth], 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) {
++(field[row][col].timesVisited); ++(field[row][col].timesVisited);
if (field[row][col].content == ownc) if (field[row][col].content == ownc)
{ {
@ -81,12 +81,12 @@ fillMovesList(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t co
} }
int int
findHorizontalForwardMoves(cell (*field)[fieldWidth], movesList *moves) { findHorizontalForwardMoves(cell (*field)[fieldSize], movesList *moves) {
for (size_t row = 0;row < fieldHeight; ++row) for (size_t row = 0;row < fieldSize; ++row)
{ {
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0; unsigned int ownCount = 0, enemyCount = 0;
for (size_t col = 0;col < fieldWidth; ++col) for (size_t col = 0;col < fieldSize; ++col)
{ {
fillMovesList(field, moves, row, col, &ownCount, &enemyCount); fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
} }
@ -95,7 +95,7 @@ findHorizontalForwardMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
int int
findHorizontalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) { findHorizontalBackwardMoves(cell (*field)[fieldSize], movesList *moves) {
for (int row = 7; row >= 0; --row) for (int row = 7; row >= 0; --row)
{ {
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
@ -109,12 +109,12 @@ findHorizontalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
int int
findVerticalForwardMoves(cell (*field)[fieldWidth], movesList *moves) { findVerticalForwardMoves(cell (*field)[fieldSize], movesList *moves) {
for (size_t col = 0;col < fieldHeight; ++col) for (size_t col = 0;col < fieldSize; ++col)
{ {
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0; unsigned int ownCount = 0, enemyCount = 0;
for (size_t row = 0;row < fieldWidth; ++row) for (size_t row = 0;row < fieldSize; ++row)
{ {
fillMovesList(field, moves, row, col, &ownCount, &enemyCount); fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
} }
@ -123,7 +123,7 @@ findVerticalForwardMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
int int
findVerticalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) { findVerticalBackwardMoves(cell (*field)[fieldSize], movesList *moves) {
for (int col = 7; col >= 0; --col) for (int col = 7; col >= 0; --col)
{ {
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
@ -137,10 +137,10 @@ findVerticalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
int int
iterDiagForwards(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t col) { iterDiagForwards(cell (*field)[fieldSize], movesList *moves, size_t row, size_t col) {
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0; unsigned int ownCount = 0, enemyCount = 0;
for (; row < fieldWidth && col < fieldHeight; ++row, ++col) for (; row < fieldSize && col < fieldSize; ++row, ++col)
{ {
fillMovesList(field, moves, row, col, &ownCount, &enemyCount); fillMovesList(field, moves, row, col, &ownCount, &enemyCount);
#ifdef debugprint #ifdef debugprint
@ -151,7 +151,7 @@ iterDiagForwards(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t
} }
int int
iterDiagBackwards(cell (*field)[fieldWidth], movesList *moves, int row, int col) { iterDiagBackwards(cell (*field)[fieldSize], movesList *moves, int row, int col) {
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
unsigned int ownCount = 0, enemyCount = 0; unsigned int ownCount = 0, enemyCount = 0;
for (; row >= 0 && col >= 0; --row, --col) for (; row >= 0 && col >= 0; --row, --col)
@ -165,8 +165,8 @@ iterDiagBackwards(cell (*field)[fieldWidth], movesList *moves, int row, int col)
} }
int int
findDiagonalBottomRightMoves(cell (*field)[fieldWidth], movesList *moves) { findDiagonalBottomRightMoves(cell (*field)[fieldSize], movesList *moves) {
for (size_t col = 0; col < fieldHeight; ++col) for (size_t col = 0; col < fieldSize; ++col)
{ {
#ifdef debugprint #ifdef debugprint
printf("F%d\n",col); printf("F%d\n",col);
@ -186,35 +186,35 @@ findDiagonalBottomRightMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
int int
findDiagonalTopLeftMoves(cell (*field)[fieldWidth], movesList *moves) { findDiagonalTopLeftMoves(cell (*field)[fieldSize], movesList *moves) {
for (int col = fieldHeight-1; col >= 0; --col) for (int col = fieldSize-1; col >= 0; --col)
{ {
#ifdef debugprint #ifdef debugprint
printf("F%d\n",col); printf("F%d\n",col);
printf("\n---------------------------------\n"); printf("\n---------------------------------\n");
#endif #endif
iterDiagBackwards(field, moves, fieldHeight-1, col); iterDiagBackwards(field, moves, fieldSize-1, col);
if(col != fieldHeight-1) if(col != fieldSize-1)
{ {
#ifdef debugprint #ifdef debugprint
printf("\n---------------------------------\n"); printf("\n---------------------------------\n");
#endif #endif
iterDiagBackwards(field, moves, col, fieldWidth-1); iterDiagBackwards(field, moves, col, fieldSize-1);
} }
} }
return 0; return 0;
} }
int int
findDiagonalBottomLeftMoves(cell (*field)[fieldWidth], movesList *moves) { findDiagonalBottomLeftMoves(cell (*field)[fieldSize], movesList *moves) {
for(unsigned int i=0; i<fieldHeight; ++i) for(int i=0; i<fieldSize; ++i)
{ {
#ifdef debugprint #ifdef debugprint
printf("\n---------------------------------\n"); printf("\n---------------------------------\n");
#endif #endif
//Anzahl eigener/gegnerischer Steine //Anzahl eigener/gegnerischer Steine
unsigned int ownCount=0, enemyCount=0; unsigned int ownCount=0, enemyCount=0;
for(int row=0; row<= i && row < fieldHeight; ++row) for(int row=0; row<= i && row < fieldSize; ++row)
{ {
int col = i-row; int col = i-row;
#ifdef debugprint #ifdef debugprint
@ -224,7 +224,7 @@ findDiagonalBottomLeftMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
} }
for(int i=fieldWidth; i<2*fieldWidth-1; ++i) for(int i=fieldSize; i<2*fieldSize-1; ++i)
{ {
#ifdef debugprint #ifdef debugprint
printf("i=%d\n", i); printf("i=%d\n", i);
@ -233,7 +233,7 @@ findDiagonalBottomLeftMoves(cell (*field)[fieldWidth], movesList *moves) {
unsigned int ownCount=0, enemyCount=0; unsigned int ownCount=0, enemyCount=0;
int col=7; int col=7;
int row= i-col; int row= i-col;
while(row < fieldWidth-1) while(row < fieldSize-1)
{ {
row= i-col; row= i-col;
#ifdef debugprint #ifdef debugprint
@ -248,8 +248,8 @@ findDiagonalBottomLeftMoves(cell (*field)[fieldWidth], movesList *moves) {
} }
int int
findDiagonalTopRightMoves(cell (*field)[fieldWidth], movesList *moves) { findDiagonalTopRightMoves(cell (*field)[fieldSize], movesList *moves) {
for(int i=2*fieldWidth-2; i>=fieldWidth; --i) for(int i=2*fieldSize-2; i>=fieldSize; --i)
{ {
#ifdef debugprint #ifdef debugprint
printf("i=%d\n", i); printf("i=%d\n", i);
@ -258,7 +258,7 @@ findDiagonalTopRightMoves(cell (*field)[fieldWidth], movesList *moves) {
unsigned int ownCount=0, enemyCount=0; unsigned int ownCount=0, enemyCount=0;
int row=7; int row=7;
int col= i-row; int col= i-row;
while(col<fieldWidth-1) while(col<fieldSize-1)
{ {
col= i-row; col= i-row;
#ifdef debugprint #ifdef debugprint
@ -268,7 +268,7 @@ findDiagonalTopRightMoves(cell (*field)[fieldWidth], movesList *moves) {
row--; row--;
} }
} }
for(int i=fieldWidth-1; i>=0; --i) for(int i=fieldSize-1; i>=0; --i)
{ {
#ifdef debugprint #ifdef debugprint
printf("\n---------------------------------\n"); printf("\n---------------------------------\n");

View file

@ -1,8 +1,7 @@
#ifndef PLAYERLIB_H #ifndef PLAYERLIB_H
#define PLAYERLIB_H #define PLAYERLIB_H
const int fieldHeight=8; const int fieldSize=8;
const int fieldWidth=fieldHeight; //spätere Annahme: Feld quadratisch
extern char enemyc; //initially 0, contains char of enemy's stone extern char enemyc; //initially 0, contains char of enemy's stone
extern char ownc; extern char ownc;
@ -23,16 +22,16 @@ struct movesList {
//returns the character used for the enemy's stones //returns the character used for the enemy's stones
extern char getEnemyChar(char); extern char getEnemyChar(char);
//reads the stateBuffer string into a 2d matrix //reads the stateBuffer string into a 2d matrix
extern int readStateBuffer(char*, cell (*)[fieldWidth]); extern int readStateBuffer(char*, cell (*)[fieldSize]);
//adds all hotizontally-forward found moves to movesList //adds all hotizontally-forward found moves to movesList
int findHorizontalForwardMoves(cell (*)[fieldWidth], movesList *); int findHorizontalForwardMoves(cell (*)[fieldSize], movesList *);
int findHorizontalBackwardMoves(cell (*)[fieldWidth], movesList *); int findHorizontalBackwardMoves(cell (*)[fieldSize], movesList *);
int findVerticalForwardMoves(cell (*)[fieldWidth], movesList *); int findVerticalForwardMoves(cell (*)[fieldSize], movesList *);
int findVerticalBackwardMoves(cell (*)[fieldWidth], movesList *); int findVerticalBackwardMoves(cell (*)[fieldSize], movesList *);
int findDiagonalTopLeftMoves(cell (*)[fieldWidth], movesList *); int findDiagonalTopLeftMoves(cell (*)[fieldSize], movesList *);
int findDiagonalBottomRightMoves(cell (*)[fieldWidth], movesList *); int findDiagonalBottomRightMoves(cell (*)[fieldSize], movesList *);
int findDiagonalBottomLeftMoves(cell (*)[fieldWidth], movesList *); int findDiagonalBottomLeftMoves(cell (*)[fieldSize], movesList *);
int findDiagonalTopRightMoves(cell (*)[fieldWidth], movesList *); int findDiagonalTopRightMoves(cell (*)[fieldSize], movesList *);
#endif #endif

View file

@ -54,10 +54,10 @@ int main(void)
} }
//pure C: //pure C:
//struct cell (*field)[fieldWidth] = malloc(fieldHeight*fieldWidth*sizeof(struct cell)); //struct cell (*field)[fieldSize] = malloc(fieldSize*fieldSize*sizeof(struct cell));
//C++ version: //C++ version:
auto field = new cell[fieldHeight][fieldWidth](); auto field = new cell[fieldSize][fieldSize]();
readStateBuffer(state_buffer, field); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[] readStateBuffer(state_buffer, field); //stateBuffer ist Pointer auf char, field ist Pointer auf struct cell[]
movesList *moves = new movesList(); movesList *moves = new movesList();
@ -85,7 +85,7 @@ int main(void)
// 3. Return result // 3. Return result
//send_move(turn_row, turn_col); //send_move(turn_row, turn_col);
//for(int i=0; i<fieldHeight; ++i) //for(int i=0; i<fieldSize; ++i)
// delete [] field[i]; // delete [] field[i];
delete [] field; delete [] field;
delete [] moves; delete [] moves;