From 9b515ae973d88eb1d288e5fabe68aebd6ad05a13 Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Wed, 28 Aug 2024 10:07:35 +0300 Subject: [PATCH] board size change feature --- compile.sh | 3 ++ globals.h | 18 ++++++----- main.c | 88 +++++++++++++++++++-------------------------------- snake/snake.c | 15 +++++---- snake/snake.h | 6 ++-- utils/utils.c | 14 ++++---- utils/utils.h | 5 ++- 7 files changed, 69 insertions(+), 80 deletions(-) create mode 100755 compile.sh diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..a41079e --- /dev/null +++ b/compile.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +gcc -Wall -Werror -pedantic -o sanke main.c snake/**.c utils/**.c diff --git a/globals.h b/globals.h index 85aed3d..c02cfaf 100644 --- a/globals.h +++ b/globals.h @@ -1,14 +1,16 @@ #ifndef GLOBALS_H_ #define GLOBALS_H_ -// Total board size -#define BRD_SIZE_X 30 -#define BRD_SIZE_Y 15 +typedef struct { + int x; + int y; +} boardInfo; -// Playable board coord range values -#define PL_BRD_XS 1 -#define PL_BRD_XE BRD_SIZE_X - 2 -#define PL_BRD_YS 1 -#define PL_BRD_YE BRD_SIZE_Y - 2 +typedef struct { + int xs; + int xe; + int ys; + int ye; +} playableBoardInfo; #endif // GLOBALS_H_ diff --git a/main.c b/main.c index 7a94794..cacb85d 100644 --- a/main.c +++ b/main.c @@ -6,17 +6,36 @@ #include #include #include -#include "./globals.h" #include "./utils/utils.h" #include "./snake/snake.h" - -char board[BRD_SIZE_Y][BRD_SIZE_X]; +#include "globals.h" const char SNAKE_VIS = '#'; +boardInfo brdInfo; +playableBoardInfo plBrdInfo; + int main() { const clock_t initClock = clock(); + // Board Constraints + printf("Set board size (15 - 60): "); + scanf("%d", &brdInfo.y); + if (!(brdInfo.y >= 15 && brdInfo.y <= 60)) { + printf("Invalid input. Board size must be greater than 0.\n"); + exit(0); + } + brdInfo.x = brdInfo.y * 2; + printf("brdInfo.x: %d\n", brdInfo.x); + printf("brdInfo.y: %d\n", brdInfo.y); + char board[brdInfo.y][brdInfo.x]; + + plBrdInfo.xs = 1; + plBrdInfo.xe = brdInfo.x - 2; + plBrdInfo.ys = 1; + plBrdInfo.ye = brdInfo.y - 2; + + // Termios setup struct termios attr; @@ -31,11 +50,11 @@ int main() { tcsetattr(STDIN_FILENO, 0, &attr); // Game board setup - for (int i = 0; i < BRD_SIZE_Y; i++) { - for (int j = 0; j < BRD_SIZE_X; j++) { - if (i == 0 || i == BRD_SIZE_Y - 1) + for (int i = 0; i < brdInfo.y; i++) { + for (int j = 0; j < brdInfo.x; j++) { + if (i == 0 || i == brdInfo.y - 1) board[i][j] = '-'; - else if (j == 0 || j == BRD_SIZE_X - 1) + else if (j == 0 || j == brdInfo.x - 1) board[i][j] = '|'; else board[i][j] = ' '; @@ -43,9 +62,9 @@ int main() { } board[0][0] = '+'; - board[BRD_SIZE_Y - 1][0] = '+'; - board[0][BRD_SIZE_X - 1] = '+'; - board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+'; + board[brdInfo.y - 1][0] = '+'; + board[0][brdInfo.x - 1] = '+'; + board[brdInfo.y - 1][brdInfo.x - 1] = '+'; int points = 0; int gameSpeed = 0; @@ -95,7 +114,7 @@ int main() { food.x = randomX(initClock); food.y = randomY(initClock); } while (checkCollision(snakeHead, food.x, food.y)); - addSnakePart(snakeHead); + addSnakePart(board, snakeHead); } // Input handling @@ -116,7 +135,7 @@ int main() { // Update board board[food.y][food.x] = food.visChar; - mvSnakeParts(snakeHead); + mvSnakeParts(board, snakeHead); // Snake collision if (snakeHead->next != NULL) { @@ -134,51 +153,10 @@ int main() { } system("clear"); - printBoard(); + printBoard(board); printf("Points: %d\n", points); printf("Game Speed: %d\n", gameSpeed); - /* - printf("x: %d\n", snakeHead->x); - printf("y: %d\n\n", snakeHead->y); - printf("food x: %d\n", food.x); - printf("food y: %d\n", food.y); - - { - snakePart* current = snakeHead; - int i = 0; - while (1) { - printf("Part %d:\n", i); - printf("dir: %c\n", current->dir); - printf("orders: \n"); - order* order = current->order; - int order_i = 0; - while (1) { - printf("\t\nOrder %d:\n", order_i); - printf("\tdir: %c\n", order->dir); - printf("\tdelay: %d\n", order->delay); - printf("\tnext: "); - if (order->next == NULL) - printf("NULL\n"); - else - printf("exists\n"); - - if (order->next == NULL) - break; - - order = order->next; - - order_i++; - } - if (current->next == NULL) - break; - - current = current->next; - i++; - } - } - */ - sleep_ms(sleepInterval - gameSpeed); } @@ -187,7 +165,7 @@ game_over: // Game over system("clear"); - printBoard(); + printBoard(board); printf("Final Points: %d\n", points); printf("Final Game Speed: %d\n", gameSpeed); diff --git a/snake/snake.c b/snake/snake.c index e78864a..97e04a5 100644 --- a/snake/snake.c +++ b/snake/snake.c @@ -3,10 +3,11 @@ #include #include "snake.h" -extern char board[BRD_SIZE_Y][BRD_SIZE_X]; extern char SNAKE_VIS; +extern boardInfo brdInfo; +extern playableBoardInfo plBrdInfo; -void mvSnakeParts(snakePart* head) { +void mvSnakeParts(char board[][brdInfo.x], snakePart* head) { snakePart* part = head; while (1) { @@ -37,16 +38,16 @@ void mvSnakeParts(snakePart* head) { switch (part->dir) { case 'w': - y = y - 1 < PL_BRD_YS ? PL_BRD_YE : y - 1; + y = y - 1 < plBrdInfo.ys ? plBrdInfo.ye : y - 1; break; case 's': - y = y + 1 > PL_BRD_YE ? PL_BRD_YS : y + 1; + y = y + 1 > plBrdInfo.ye ? plBrdInfo.ys : y + 1; break; case 'a': - x = x - 2 < PL_BRD_XS ? PL_BRD_XE - 1 : x - 2; + x = x - 2 < plBrdInfo.xs ? plBrdInfo.xe - 1 : x - 2; break; case 'd': - x = x + 2 > PL_BRD_XE ? PL_BRD_XS : x + 2; + x = x + 2 > plBrdInfo.xe ? plBrdInfo.xs : x + 2; break; default: printf("ERROR in func mvSnakeParts\n"); @@ -66,7 +67,7 @@ void mvSnakeParts(snakePart* head) { } } -void addSnakePart(snakePart* head) { +void addSnakePart(char board[][brdInfo.x], snakePart* head) { snakePart* tail = head; while (tail->next != NULL) diff --git a/snake/snake.h b/snake/snake.h index ef9bba6..285d7f9 100644 --- a/snake/snake.h +++ b/snake/snake.h @@ -3,6 +3,8 @@ #include "../globals.h" +extern boardInfo brdInfo; + typedef struct orderNode { char dir; int delay; @@ -19,8 +21,8 @@ typedef struct snakeNode { struct snakeNode* next; } snakePart; -void mvSnakeParts(snakePart* head); -void addSnakePart(snakePart* head); +void mvSnakeParts(char board[][brdInfo.x], snakePart* head); +void addSnakePart(char board[][brdInfo.x], snakePart* head); void pushOrder(order* head, char dir, int delay); void removeOrder(order* head); void addOrders(snakePart* head, char dir); diff --git a/utils/utils.c b/utils/utils.c index c20a6b5..afe50ae 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -3,9 +3,9 @@ #include #include #include "utils.h" -#include "../globals.h" -extern char board[BRD_SIZE_Y][BRD_SIZE_X]; +extern boardInfo brdInfo; +extern playableBoardInfo plBrdInfo; int randomInt(const int start, const int end, const unsigned int seed) { /* @@ -22,7 +22,7 @@ int randomX(const clock_t initClock) { int x; const unsigned int seed = clock() - initClock; for (int i = randomInt(0, 100, seed); ; i++) { - x = randomInt(PL_BRD_XS, PL_BRD_XE, i); + x = randomInt(plBrdInfo.xs, plBrdInfo.xe, i); if (x % 2 != 0) break; } @@ -32,16 +32,16 @@ int randomX(const clock_t initClock) { int randomY(const clock_t initClock) { const unsigned int seed = clock() - initClock; - return randomInt(PL_BRD_YS, PL_BRD_YE, seed); + return randomInt(plBrdInfo.ys, plBrdInfo.ye, seed); } void sleep_ms(const int ms) { usleep(ms * 1000); } -void printBoard() { - for (int i = 0; i < BRD_SIZE_Y; i++) { - for (int j = 0; j < BRD_SIZE_X; j++) { +void printBoard(char board[][brdInfo.x]) { + for (int i = 0; i < brdInfo.y; i++) { + for (int j = 0; j < brdInfo.x; j++) { printf("%c", board[i][j]); } printf("\n"); diff --git a/utils/utils.h b/utils/utils.h index 5544db3..6586937 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -2,11 +2,14 @@ #define UTILS_H_ #include +#include "../globals.h" + +extern boardInfo brdInfo; int randomInt(const int start, const int end, const unsigned int seed); int randomX(const clock_t initClock); int randomY(const clock_t initClock); void sleep_ms(const int ms); -void printBoard(); +void printBoard(char board[][brdInfo.x]); #endif // UTILS_H_