added iterating diagonally
This commit is contained in:
parent
ff7d0ffa7a
commit
8ac7a59a34
|
@ -1,3 +1,4 @@
|
||||||
|
//#define debugprint
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
@ -6,6 +7,10 @@
|
||||||
//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 (*)[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 enemyc = 0, ownc = 0;
|
||||||
|
|
||||||
char
|
char
|
||||||
|
@ -29,7 +34,9 @@ readStateBuffer(char *stateBuffer, struct cell (*gameField)[fieldWidth]) {
|
||||||
{
|
{
|
||||||
int index = row*fieldHeight+col+1;
|
int index = row*fieldHeight+col+1;
|
||||||
gameField[row][col].content = stateBuffer[index];
|
gameField[row][col].content = stateBuffer[index];
|
||||||
|
#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);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -42,25 +49,33 @@ fillMovesList(cell (*field)[fieldWidth], movesList *moves, size_t row, size_t co
|
||||||
{
|
{
|
||||||
++(*ownCount);
|
++(*ownCount);
|
||||||
*enemyCount = 0;
|
*enemyCount = 0;
|
||||||
|
#ifdef debugprint
|
||||||
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
|
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if(field[row][col].content == enemyc && ownCount)
|
if(field[row][col].content == enemyc && ownCount)
|
||||||
{
|
{
|
||||||
++(*enemyCount);
|
++(*enemyCount);
|
||||||
|
#ifdef debugprint
|
||||||
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
|
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if(field[row][col].content == '.')
|
if(field[row][col].content == '.')
|
||||||
{
|
{
|
||||||
if (*ownCount && *enemyCount > 0)
|
if (*ownCount && *enemyCount > 0)
|
||||||
{
|
{
|
||||||
|
#ifdef debugprint
|
||||||
printf("BAM!\n");
|
printf("BAM!\n");
|
||||||
|
#endif
|
||||||
moves->list[moves->movesNumber].turnRow = row+1;
|
moves->list[moves->movesNumber].turnRow = row+1;
|
||||||
moves->list[moves->movesNumber].turnCol = col+1;
|
moves->list[moves->movesNumber].turnCol = col+1;
|
||||||
++(moves->movesNumber);
|
++(moves->movesNumber);
|
||||||
}
|
}
|
||||||
*ownCount = 0;
|
*ownCount = 0;
|
||||||
*enemyCount = 0;
|
*enemyCount = 0;
|
||||||
|
#ifdef debugprint
|
||||||
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
|
printf("%d,%d: ownCount: %d, enemyCount: %d\n", row, col, *ownCount, *enemyCount);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -120,3 +135,154 @@ findVerticalBackwardMoves(cell (*field)[fieldWidth], movesList *moves) {
|
||||||
}
|
}
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
#ifndef PLAYERLIB_H
|
#ifndef PLAYERLIB_H
|
||||||
#define PLAYERLIB_H
|
#define PLAYERLIB_H
|
||||||
|
|
||||||
const unsigned int fieldHeight=8, fieldWidth=8;
|
const int fieldHeight=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;
|
||||||
struct cell {
|
struct cell {
|
||||||
|
@ -15,7 +17,7 @@ struct move {
|
||||||
|
|
||||||
struct movesList {
|
struct movesList {
|
||||||
unsigned int movesNumber = 0;
|
unsigned int movesNumber = 0;
|
||||||
move *list = new move[fieldWidth*fieldHeight-4]();
|
move *list = new move[100](); //eigentlich maximal 8*8-4 Lösungen, aber manche doppelt? -> Reserve
|
||||||
};
|
};
|
||||||
|
|
||||||
//returns the character used for the enemy's stones
|
//returns the character used for the enemy's stones
|
||||||
|
@ -28,5 +30,9 @@ int findHorizontalForwardMoves(cell (*)[fieldWidth], movesList *);
|
||||||
int findHorizontalBackwardMoves(cell (*)[fieldWidth], movesList *);
|
int findHorizontalBackwardMoves(cell (*)[fieldWidth], movesList *);
|
||||||
int findVerticalForwardMoves(cell (*)[fieldWidth], movesList *);
|
int findVerticalForwardMoves(cell (*)[fieldWidth], movesList *);
|
||||||
int findVerticalBackwardMoves(cell (*)[fieldWidth], movesList *);
|
int findVerticalBackwardMoves(cell (*)[fieldWidth], movesList *);
|
||||||
|
int findDiagonalTopLeftMoves(cell (*)[fieldWidth], movesList *);
|
||||||
|
int findDiagonalBottomRightMoves(cell (*)[fieldWidth], movesList *);
|
||||||
|
int findDiagonalBottomLeftMoves(cell (*)[fieldWidth], movesList *);
|
||||||
|
int findDiagonalTopRightMoves(cell (*)[fieldWidth], movesList *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
/* -*- Mode: C -*- */
|
/* -*- Mode: C -*- */
|
||||||
|
#define debugprint
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -20,7 +21,6 @@ send_move(int row, int col)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int done = 0;
|
int done = 0;
|
||||||
|
@ -61,11 +61,23 @@ int main(void)
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
findHorizontalForwardMoves(field, moves);
|
findHorizontalForwardMoves(field, moves);
|
||||||
findHorizontalBackwardMoves(field, moves);
|
findHorizontalBackwardMoves(field, moves);
|
||||||
findVerticalForwardMoves(field, moves);
|
findVerticalForwardMoves(field, moves);
|
||||||
findVerticalBackwardMoves(field, moves);
|
findVerticalBackwardMoves(field, moves);
|
||||||
|
findDiagonalBottomRightMoves(field, moves);
|
||||||
|
findDiagonalTopLeftMoves(field, moves);
|
||||||
|
#ifdef debugprint
|
||||||
|
printf("BottomLeft\n");
|
||||||
|
#endif
|
||||||
|
findDiagonalBottomLeftMoves(field, moves);
|
||||||
|
#ifdef debugprint
|
||||||
|
printf("TopRight\n");
|
||||||
|
#endif
|
||||||
|
findDiagonalTopRightMoves(field, moves);
|
||||||
|
|
||||||
|
printf("\nZüge:\n");
|
||||||
for (unsigned int i=0; i<moves->movesNumber; ++i)
|
for (unsigned int i=0; i<moves->movesNumber; ++i)
|
||||||
{
|
{
|
||||||
printf("%d,%d\n",moves->list[i].turnRow,moves->list[i].turnCol);
|
printf("%d,%d\n",moves->list[i].turnRow,moves->list[i].turnCol);
|
||||||
|
|
Loading…
Reference in a new issue