This commit is contained in:
2024-07-30 12:29:12 +03:00
parent 69ca98ffb5
commit aa0a2c5fb9
4 changed files with 124 additions and 26 deletions

16
globals.h Normal file
View File

@@ -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

120
main.c
View File

@@ -2,11 +2,25 @@
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <stdlib.h> #include <stdlib.h>
#include "./global_macros.h" #include <time.h>
#include <ctype.h>
#include "./globals.h"
//TEMP //TEMP
#include "./test_utils/test_utils.h" #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() { int main() {
// Termios setup // Termios setup
struct termios attr; struct termios attr;
@@ -22,18 +36,16 @@ int main() {
tcsetattr(STDIN_FILENO, 0,&attr); tcsetattr(STDIN_FILENO, 0,&attr);
// Game board setup // Game board setup
char board[BRD_SIZE_X][BRD_SIZE_Y]; for (int i = 0; i < BRD_SIZE_Y; i++) {
for (int j = 0; j < BRD_SIZE_X; j++) {
for (int i = 0; i < BRD_SIZE_X; i++) {
for (int j = 0; j < BRD_SIZE_Y; j++) {
if ( if (
(i > 0 && i < BRD_SIZE_X - 1) (i > 0 && i < BRD_SIZE_Y - 1)
&& (j == 0 || j == BRD_SIZE_Y - 1) && (j == 0 || j == BRD_SIZE_X - 1)
) { ) {
board[i][j] = '|'; board[i][j] = '|';
} else if ( } else if (
(i == 0 || i == BRD_SIZE_X - 1) (i == 0 || i == BRD_SIZE_Y - 1)
&& (j > 0 && j < BRD_SIZE_Y - 1) && (j > 0 && j < BRD_SIZE_X - 1)
) { ) {
board[i][j] = '-'; board[i][j] = '-';
} else { } else {
@@ -43,28 +55,96 @@ int main() {
} }
board[0][0] = '+'; board[0][0] = '+';
board[BRD_SIZE_X - 1][0] = '+'; board[BRD_SIZE_Y - 1][0] = '+';
board[0][BRD_SIZE_Y - 1] = '+'; board[0][BRD_SIZE_X - 1] = '+';
board[BRD_SIZE_X - 1][BRD_SIZE_Y - 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) { while (1) {
fflush(stdout); fflush(stdout);
char buf[BUF_SIZE] = {0}; char buf[1] = {0};
read(STDIN_FILENO, buf, 2); read(STDIN_FILENO, buf, 1);
char input = buf[0];
if (buf[0] == 0) if (input == 0 || !isalpha(input))
continue; continue;
write(STDOUT_FILENO, buf, 2); input = tolower(input);
//printf("\n");
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 // Termios reset
tcsetattr(STDIN_FILENO, 0,&ATTR_ORIG); tcsetattr(STDIN_FILENO, 0,&ATTR_ORIG);
return 0; 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;
}

View File

@@ -1,10 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include "test_utils.h" #include "test_utils.h"
#include "../global_macros.h" #include "../globals.h"
void printBoard(char board[BRD_SIZE_X][BRD_SIZE_Y]) { extern char board[BRD_SIZE_Y][BRD_SIZE_X];
for (int i = 0; i < BRD_SIZE_X; i++) {
for (int j = 0; j < BRD_SIZE_Y; j++) { 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("%c", board[i][j]);
} }
printf("\n"); printf("\n");

View File

@@ -1,8 +1,8 @@
#ifndef TEST_UTILS_H #ifndef TEST_UTILS_H
#define 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 #endif // TEST_UTILS_H