diff --git a/globals.h b/globals.h new file mode 100644 index 0000000..070e52b --- /dev/null +++ b/globals.h @@ -0,0 +1,16 @@ +#ifndef GLOBALS_H +#define GLOBALS_H + +#define BUF_SIZE 256 + +// Total board size +#define BRD_SIZE_X 30 +#define BRD_SIZE_Y 15 + +// 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 + +#endif // GLOBALS_H diff --git a/main.c b/main.c index 5bf9cad..eb49e72 100644 --- a/main.c +++ b/main.c @@ -2,11 +2,25 @@ #include #include #include -#include "./global_macros.h" +#include +#include +#include "./globals.h" //TEMP #include "./test_utils/test_utils.h" +typedef struct { + int x; + int y; + char visChar; +} brdSymbol; + +int randomInt(const int start, const int end); +void sleep_ms(const int ms); +void mvSymbol(brdSymbol* symbol, const char dir); + +char board[BRD_SIZE_Y][BRD_SIZE_X]; + int main() { // Termios setup struct termios attr; @@ -22,18 +36,16 @@ int main() { tcsetattr(STDIN_FILENO, 0,&attr); // Game board setup - char board[BRD_SIZE_X][BRD_SIZE_Y]; - - for (int i = 0; i < BRD_SIZE_X; i++) { - for (int j = 0; j < BRD_SIZE_Y; j++) { + for (int i = 0; i < BRD_SIZE_Y; i++) { + for (int j = 0; j < BRD_SIZE_X; j++) { if ( - (i > 0 && i < BRD_SIZE_X - 1) - && (j == 0 || j == BRD_SIZE_Y - 1) + (i > 0 && i < BRD_SIZE_Y - 1) + && (j == 0 || j == BRD_SIZE_X - 1) ) { board[i][j] = '|'; } else if ( - (i == 0 || i == BRD_SIZE_X - 1) - && (j > 0 && j < BRD_SIZE_Y - 1) + (i == 0 || i == BRD_SIZE_Y - 1) + && (j > 0 && j < BRD_SIZE_X - 1) ) { board[i][j] = '-'; } else { @@ -43,28 +55,96 @@ int main() { } board[0][0] = '+'; - board[BRD_SIZE_X - 1][0] = '+'; - board[0][BRD_SIZE_Y - 1] = '+'; - board[BRD_SIZE_X - 1][BRD_SIZE_Y - 1] = '+'; + board[BRD_SIZE_Y - 1][0] = '+'; + board[0][BRD_SIZE_X - 1] = '+'; + board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+'; - printBoard(board); + brdSymbol player; + player.x = randomInt(PL_BRD_XS, PL_BRD_XE); + player.y = randomInt(PL_BRD_YS, PL_BRD_YE); + player.visChar = '#'; + + board[player.y][player.x] = player.visChar; + + printBoard(); - /* while (1) { fflush(stdout); - char buf[BUF_SIZE] = {0}; - read(STDIN_FILENO, buf, 2); + char buf[1] = {0}; + read(STDIN_FILENO, buf, 1); + char input = buf[0]; - if (buf[0] == 0) + if (input == 0 || !isalpha(input)) continue; - write(STDOUT_FILENO, buf, 2); - //printf("\n"); + input = tolower(input); + + switch (input) { + case 'w': + mvSymbol(&player, 'u'); + break; + case 's': + mvSymbol(&player, 'd'); + break; + case 'a': + mvSymbol(&player, 'l'); + break; + case 'd': + mvSymbol(&player, 'r'); + break; + default: + NULL; + } + + system("clear"); + printBoard(); + printf("x: %d\n", player.x); + printf("y: %d\n", player.y); + + sleep_ms(50); } - */ // Termios reset tcsetattr(STDIN_FILENO, 0,&ATTR_ORIG); return 0; } + +int randomInt(const int start, const int end) { + /* + * Gets random int from range + * [start, end[ + */ + srand(time(NULL)); + int result = rand() % end; + result = result >= start ? result : result + start; + return result; +} + +void sleep_ms(const int ms) { + usleep(ms * 1000); +} + +void mvSymbol(brdSymbol* symbol, const char dir) { + board[symbol->y][symbol->x] = ' '; + + switch (dir) { + case 'u': + symbol->y = symbol->y - 1 < PL_BRD_YS ? PL_BRD_YE : symbol->y - 1; + break; + case 'd': + symbol->y = symbol->y + 1 > PL_BRD_YE ? PL_BRD_YS : symbol->y + 1; + break; + case 'l': + symbol->x = symbol->x - 2 < PL_BRD_XS ? PL_BRD_XE : symbol->x - 2; + break; + case 'r': + symbol->x = symbol->x + 2 > PL_BRD_XE ? PL_BRD_XS : symbol->x + 2; + break; + default: + printf("ERROR in func mvSymbol\n"); + exit(1); + } + + board[symbol->y][symbol->x] = symbol->visChar; +} diff --git a/test_utils/test_utils.c b/test_utils/test_utils.c index 4661b42..e908a55 100644 --- a/test_utils/test_utils.c +++ b/test_utils/test_utils.c @@ -1,10 +1,12 @@ #include #include "test_utils.h" -#include "../global_macros.h" +#include "../globals.h" -void printBoard(char board[BRD_SIZE_X][BRD_SIZE_Y]) { - for (int i = 0; i < BRD_SIZE_X; i++) { - for (int j = 0; j < BRD_SIZE_Y; j++) { +extern char board[BRD_SIZE_Y][BRD_SIZE_X]; + +void printBoard() { + for (int i = 0; i < BRD_SIZE_Y; i++) { + for (int j = 0; j < BRD_SIZE_X; j++) { printf("%c", board[i][j]); } printf("\n"); diff --git a/test_utils/test_utils.h b/test_utils/test_utils.h index ec1f9b5..e282eca 100644 --- a/test_utils/test_utils.h +++ b/test_utils/test_utils.h @@ -1,8 +1,8 @@ #ifndef TEST_UTILS_H #define TEST_UTILS_H -#include "../global_macros.h" +#include "../globals.h" -void printBoard(char board[BRD_SIZE_X][BRD_SIZE_Y]); +void printBoard(); #endif // TEST_UTILS_H