Skip to content

Commit

Permalink
Do-while pattern replaced with goto
Browse files Browse the repository at this point in the history
  • Loading branch information
necromyhan committed Nov 10, 2023
1 parent b75c8a2 commit 0ef0a6e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.27)
cmake_minimum_required(VERSION 3.25)
project(test_game)

set(CMAKE_C_STANDART C99)
Expand Down
160 changes: 75 additions & 85 deletions src/snake.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,33 @@ CreateSnake(
{
SNAKE *snake = NULL;

do
{
snake = SDL_malloc(sizeof(*snake));
if (NULL == snake) { break; }
snake = SDL_malloc(sizeof(*snake));
if (NULL == snake) { goto exit; }

snake->Body = SDL_malloc(CellCount * sizeof(*snake->Body));
if (NULL == snake->Body)
{
SDL_free(snake);
snake = NULL;
break;
}
snake->Body = SDL_malloc(CellCount * sizeof(*snake->Body));
if (NULL == snake->Body)
{
SDL_free(snake);
snake = NULL;
goto exit;
}

SDL_memset(snake->Body, 0x00, sizeof(*snake->Body));
SDL_memset(snake->Body, 0x00, sizeof(*snake->Body));

snake->Body[0].h = CellSize;
snake->Body[0].w = CellSize;
snake->Body[0].x = StartCellX * CellSize;
snake->Body[0].y = StartCellY * CellSize;
snake->Body[0].h = CellSize;
snake->Body[0].w = CellSize;
snake->Body[0].x = StartCellX * CellSize;
snake->Body[0].y = StartCellY * CellSize;

snake->Body[1].h = CellSize;
snake->Body[1].w = CellSize;
snake->Body[1].x = (StartCellX - 1) * CellSize;
snake->Body[1].y = StartCellY * CellSize;
snake->Body[1].h = CellSize;
snake->Body[1].w = CellSize;
snake->Body[1].x = (StartCellX - 1) * CellSize;
snake->Body[1].y = StartCellY * CellSize;

snake->Length = 2;
snake->Direction = SnakeDirectionRight;
} while (false);
snake->Length = 2;
snake->Direction = SnakeDirectionRight;

exit:
return snake;
}

Expand All @@ -56,56 +54,53 @@ MoveSnake(
int FieldWidth,
int FieldHeight)
{
int res = 0;
if (NULL == Snake) { goto exit; }

do
int deltaX, deltaY;
switch (Snake->Direction)
{
if (NULL == Snake) { res = 1; break; }

int deltaX, deltaY;
switch (Snake->Direction)
case SnakeDirectionUp:
{
case SnakeDirectionUp:
{
deltaX = 0;
deltaY = 0 - Snake->Body[0].h;
break;
}
case SnakeDirectionDown:
{
deltaX = 0;
deltaY = Snake->Body[0].h;
break;
}
case SnakeDirectionRight:
{
deltaX = Snake->Body[0].w;
deltaY = 0;
break;
}
case SnakeDirectionLeft:
{
deltaX = 0 - Snake->Body[0].w;
deltaY = 0;
break;
}
default:
{
return;
}
deltaX = 0;
deltaY = 0 - Snake->Body[0].h;
break;
}

for (int i = Snake->Length - 1; i > 0; --i)
case SnakeDirectionDown:
{
Snake->Body[i].x = Snake->Body[i - 1].x;
Snake->Body[i].y = Snake->Body[i - 1].y;
deltaX = 0;
deltaY = Snake->Body[0].h;
break;
}
case SnakeDirectionRight:
{
deltaX = Snake->Body[0].w;
deltaY = 0;
break;
}
case SnakeDirectionLeft:
{
deltaX = 0 - Snake->Body[0].w;
deltaY = 0;
break;
}
default:
{
goto exit;
}
}

Snake->Body[0].x += deltaX;
Snake->Body[0].y += deltaY;
Snake->Body[0].x = (int)(Snake->Body[0].x + FieldWidth) % FieldWidth;
Snake->Body[0].y = (int)(Snake->Body[0].y + FieldHeight) % FieldHeight;
} while (false);
for (int i = Snake->Length - 1; i > 0; --i)
{
Snake->Body[i].x = Snake->Body[i - 1].x;
Snake->Body[i].y = Snake->Body[i - 1].y;
}

Snake->Body[0].x += deltaX;
Snake->Body[0].y += deltaY;
Snake->Body[0].x = (int)(Snake->Body[0].x + FieldWidth) % FieldWidth;
Snake->Body[0].y = (int)(Snake->Body[0].y + FieldHeight) % FieldHeight;

exit: ;
}

void
Expand All @@ -126,16 +121,13 @@ IsSnakeIntersection(
{
bool intersec = false;

do
for (int i = 1; i < Snake->Length; ++i)
{
for (int i = 1; i < Snake->Length; ++i)
if ((intersec = SDL_HasRectIntersectionFloat(&Snake->Body[0], &Snake->Body[i])))
{
if ((intersec = SDL_HasRectIntersectionFloat(&Snake->Body[0], &Snake->Body[i])))
{
break;
}
break;
}
} while (false);
}

return intersec;
}
Expand All @@ -147,21 +139,19 @@ RenderSnake(
{
int res = 0;

do
{
if (NULL == Snake) { res = 1; break; }
if (NULL == Snake) { res = -1; goto exit; }

res = SDL_SetRenderDrawColor(Renderer, 100, 100, 100, 0);
if (res) { goto exit; }

res = SDL_SetRenderDrawColor(Renderer, 100, 100, 100, 0);
if (res) { break; }
res = SDL_RenderFillRects(Renderer, &Snake->Body[1], Snake->Length - 1);
if (res) { goto exit; }

res = SDL_RenderFillRects(Renderer, &Snake->Body[1], Snake->Length - 1);
if (res) { break; }
res = SDL_SetRenderDrawColor(Renderer, 60, 60, 60, 0);
if (res) { goto exit; }

res = SDL_SetRenderDrawColor(Renderer, 60, 60, 60, 0);
if (res) { break; }
res = SDL_RenderFillRect(Renderer, &Snake->Body[0]);

res = SDL_RenderFillRect(Renderer, &Snake->Body[0]);
} while (false);

exit:
return res;
}

0 comments on commit 0ef0a6e

Please sign in to comment.