Completed arena generation functions

This commit is contained in:
JackCarterSmith 2018-06-20 14:40:18 +02:00
parent 0b78b84ef1
commit 3ea1f60e95
3 changed files with 23 additions and 44 deletions

View File

@ -14,8 +14,6 @@ ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
tile_h->canBeMined = 0; tile_h->canBeMined = 0;
tile_h->nextRow = NULL; tile_h->nextRow = NULL;
tile_h->nextColumn = NULL; tile_h->nextColumn = NULL;
return tile_h;
} else if (prevHTile != NULL) { } else if (prevHTile != NULL) {
tile_h = calloc(1,sizeof(ARENA_H_TILE)); tile_h = calloc(1,sizeof(ARENA_H_TILE));
prevHTile->nextRow = tile_h; prevHTile->nextRow = tile_h;
@ -26,7 +24,7 @@ ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
tile_h->nextColumn = NULL; tile_h->nextColumn = NULL;
} }
return NULL; return tile_h;
} }
ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) { ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
@ -53,21 +51,21 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
return NULL; return NULL;
} }
ARENA_H_TILE* genNewArena(int h, int w) { ARENA_H_TILE* genNewArena(int size_h, int size_w) {
ARENA_H_TILE* arenaOrigin = NULL; ARENA_H_TILE* arenaOrigin = NULL;
ARENA_H_TILE* TmpCursor_h = NULL; ARENA_H_TILE* TmpCursor_h = NULL;
ARENA_W_TILE* TmpCursor_w = NULL; ARENA_W_TILE* TmpCursor_w = NULL;
int i,j; int i,j;
for(i=0;i<=h;i++){ for(i=0;i<size_h;i++){
if (i==0) { if (i==0) {
arenaOrigin = createHTile(NULL); arenaOrigin = createHTile(NULL);
TmpCursor_h = arenaOrigin; TmpCursor_h = arenaOrigin;
} else { } else {
createHTile(TmpCursor_h); TmpCursor_h = createHTile(TmpCursor_h);
} }
for(j=0;j<w;j++){ for(j=0;j<size_w;j++){
if (j==0) { if (j==0) {
TmpCursor_w = createWTile(TmpCursor_h, NULL); TmpCursor_w = createWTile(TmpCursor_h, NULL);
} else { } else {
@ -76,47 +74,28 @@ ARENA_H_TILE* genNewArena(int h, int w) {
if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn; if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn;
} }
if (i!=0) TmpCursor_h = TmpCursor_h->nextRow;
} }
return arenaOrigin; return arenaOrigin;
} }
void deleteWTile(ARENA_W_TILE* WTile, int* failNbr) { void deleteWTile(ARENA_W_TILE* WTile) {
addLogInfo("st2"); if (WTile->nextColumn != NULL) {
while (WTile->nextColumn != NULL) { deleteWTile(WTile->nextColumn);
addLogInfo("goto end2");
deleteWTile(WTile->nextColumn, failNbr);
} }
addLogInfo("suppress 1 column");
free(WTile); free(WTile);
if (WTile != NULL) (*failNbr)++;
} }
void deleteHTile(ARENA_H_TILE* HTile, int* failNbr) { void deleteHTile(ARENA_H_TILE* HTile) {
//addLogInfo("st1"); if (HTile->nextRow != NULL) {
if(HTile->nextColumn == NULL && HTile->nextRow == NULL) addLogInfo("yup3"); deleteHTile(HTile->nextRow);
while (HTile->nextRow != NULL) {
if(HTile->nextColumn == NULL) addLogInfo("yup");
//addLogInfo("goto end1");
deleteHTile(HTile->nextRow, failNbr);
} }
addLogInfo("suppress 1 column"); deleteWTile(HTile->nextColumn);
if(HTile->nextColumn == NULL) addLogInfo("yup2");
deleteWTile(HTile->nextColumn, failNbr);
free(HTile); free(HTile);
if (HTile != NULL) (*failNbr)++;
} }
int deleteArena(ARENA_H_TILE* arena) { void deleteArena(ARENA_H_TILE* arena) {
int fnbr = 0; deleteHTile(arena);
int* failNbr = &fnbr;
deleteHTile(arena, failNbr);
return *failNbr;
} }

View File

@ -32,7 +32,7 @@ typedef struct arena_w_tile{ //Columns chained list
ARENA_H_TILE* genNewArena(int h, int w); ARENA_H_TILE* genNewArena(int size_h, int size_w);
int deleteArena(ARENA_H_TILE* arena); void deleteArena(ARENA_H_TILE* arena);
#endif #endif

14
main.c
View File

@ -1,12 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "logHelper.h" #include "logHelper.h"
#include "SDL2/SDL.h" #include <SDL2/SDL.h>
#include "SDL2/SDL_thread.h" #include <SDL2/SDL_thread.h>
#include "SDL2/SDL_mutex.h" #include <SDL2/SDL_mutex.h>
#include "arenaEngine.h" #include "arenaEngine.h"
#define A_HEIGHT 100 //Real value is A_X + 1 #define A_HEIGHT 100
#define A_WIDTH 100 #define A_WIDTH 100
void initDisplayLib() { void initDisplayLib() {
@ -25,9 +25,8 @@ int main(int argc, char *argv[]) {
ARENA_H_TILE* arena = NULL; ARENA_H_TILE* arena = NULL;
addLogInfo("Starting game..."); addLogInfo("Starting game...");
//initDisplayLib(); initDisplayLib();
initResources(); initResources();
addLogInfo("Creating new arena..."); addLogInfo("Creating new arena...");
@ -38,7 +37,8 @@ int main(int argc, char *argv[]) {
} }
addLogInfo("Successfully created arena."); addLogInfo("Successfully created arena.");
if (deleteArena(arena) > 0) addLogCritical("An tile from arena asn't been deleted correctly."); else addLogInfo("Arena successfully deleted from memory."); deleteArena(arena);
addLogInfo("Cleared arena.");