board size change feature

This commit is contained in:
2024-08-28 10:07:35 +03:00
parent 47d72e2df1
commit 9b515ae973
7 changed files with 69 additions and 80 deletions

3
compile.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
gcc -Wall -Werror -pedantic -o sanke main.c snake/**.c utils/**.c

View File

@@ -1,14 +1,16 @@
#ifndef GLOBALS_H_ #ifndef GLOBALS_H_
#define GLOBALS_H_ #define GLOBALS_H_
// Total board size typedef struct {
#define BRD_SIZE_X 30 int x;
#define BRD_SIZE_Y 15 int y;
} boardInfo;
// Playable board coord range values typedef struct {
#define PL_BRD_XS 1 int xs;
#define PL_BRD_XE BRD_SIZE_X - 2 int xe;
#define PL_BRD_YS 1 int ys;
#define PL_BRD_YE BRD_SIZE_Y - 2 int ye;
} playableBoardInfo;
#endif // GLOBALS_H_ #endif // GLOBALS_H_

88
main.c
View File

@@ -6,17 +6,36 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include "./globals.h"
#include "./utils/utils.h" #include "./utils/utils.h"
#include "./snake/snake.h" #include "./snake/snake.h"
#include "globals.h"
char board[BRD_SIZE_Y][BRD_SIZE_X];
const char SNAKE_VIS = '#'; const char SNAKE_VIS = '#';
boardInfo brdInfo;
playableBoardInfo plBrdInfo;
int main() { int main() {
const clock_t initClock = clock(); 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 // Termios setup
struct termios attr; struct termios attr;
@@ -31,11 +50,11 @@ int main() {
tcsetattr(STDIN_FILENO, 0, &attr); tcsetattr(STDIN_FILENO, 0, &attr);
// Game board setup // Game board setup
for (int i = 0; i < BRD_SIZE_Y; i++) { for (int i = 0; i < brdInfo.y; i++) {
for (int j = 0; j < BRD_SIZE_X; j++) { for (int j = 0; j < brdInfo.x; j++) {
if (i == 0 || i == BRD_SIZE_Y - 1) if (i == 0 || i == brdInfo.y - 1)
board[i][j] = '-'; board[i][j] = '-';
else if (j == 0 || j == BRD_SIZE_X - 1) else if (j == 0 || j == brdInfo.x - 1)
board[i][j] = '|'; board[i][j] = '|';
else else
board[i][j] = ' '; board[i][j] = ' ';
@@ -43,9 +62,9 @@ int main() {
} }
board[0][0] = '+'; board[0][0] = '+';
board[BRD_SIZE_Y - 1][0] = '+'; board[brdInfo.y - 1][0] = '+';
board[0][BRD_SIZE_X - 1] = '+'; board[0][brdInfo.x - 1] = '+';
board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+'; board[brdInfo.y - 1][brdInfo.x - 1] = '+';
int points = 0; int points = 0;
int gameSpeed = 0; int gameSpeed = 0;
@@ -95,7 +114,7 @@ int main() {
food.x = randomX(initClock); food.x = randomX(initClock);
food.y = randomY(initClock); food.y = randomY(initClock);
} while (checkCollision(snakeHead, food.x, food.y)); } while (checkCollision(snakeHead, food.x, food.y));
addSnakePart(snakeHead); addSnakePart(board, snakeHead);
} }
// Input handling // Input handling
@@ -116,7 +135,7 @@ int main() {
// Update board // Update board
board[food.y][food.x] = food.visChar; board[food.y][food.x] = food.visChar;
mvSnakeParts(snakeHead); mvSnakeParts(board, snakeHead);
// Snake collision // Snake collision
if (snakeHead->next != NULL) { if (snakeHead->next != NULL) {
@@ -134,51 +153,10 @@ int main() {
} }
system("clear"); system("clear");
printBoard(); printBoard(board);
printf("Points: %d\n", points); printf("Points: %d\n", points);
printf("Game Speed: %d\n", gameSpeed); 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); sleep_ms(sleepInterval - gameSpeed);
} }
@@ -187,7 +165,7 @@ game_over:
// Game over // Game over
system("clear"); system("clear");
printBoard(); printBoard(board);
printf("Final Points: %d\n", points); printf("Final Points: %d\n", points);
printf("Final Game Speed: %d\n", gameSpeed); printf("Final Game Speed: %d\n", gameSpeed);

View File

@@ -3,10 +3,11 @@
#include <stdbool.h> #include <stdbool.h>
#include "snake.h" #include "snake.h"
extern char board[BRD_SIZE_Y][BRD_SIZE_X];
extern char SNAKE_VIS; 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; snakePart* part = head;
while (1) { while (1) {
@@ -37,16 +38,16 @@ void mvSnakeParts(snakePart* head) {
switch (part->dir) { switch (part->dir) {
case 'w': case 'w':
y = y - 1 < PL_BRD_YS ? PL_BRD_YE : y - 1; y = y - 1 < plBrdInfo.ys ? plBrdInfo.ye : y - 1;
break; break;
case 's': case 's':
y = y + 1 > PL_BRD_YE ? PL_BRD_YS : y + 1; y = y + 1 > plBrdInfo.ye ? plBrdInfo.ys : y + 1;
break; break;
case 'a': 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; break;
case 'd': case 'd':
x = x + 2 > PL_BRD_XE ? PL_BRD_XS : x + 2; x = x + 2 > plBrdInfo.xe ? plBrdInfo.xs : x + 2;
break; break;
default: default:
printf("ERROR in func mvSnakeParts\n"); 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; snakePart* tail = head;
while (tail->next != NULL) while (tail->next != NULL)

View File

@@ -3,6 +3,8 @@
#include "../globals.h" #include "../globals.h"
extern boardInfo brdInfo;
typedef struct orderNode { typedef struct orderNode {
char dir; char dir;
int delay; int delay;
@@ -19,8 +21,8 @@ typedef struct snakeNode {
struct snakeNode* next; struct snakeNode* next;
} snakePart; } snakePart;
void mvSnakeParts(snakePart* head); void mvSnakeParts(char board[][brdInfo.x], snakePart* head);
void addSnakePart(snakePart* head); void addSnakePart(char board[][brdInfo.x], snakePart* head);
void pushOrder(order* head, char dir, int delay); void pushOrder(order* head, char dir, int delay);
void removeOrder(order* head); void removeOrder(order* head);
void addOrders(snakePart* head, char dir); void addOrders(snakePart* head, char dir);

View File

@@ -3,9 +3,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "utils.h" #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) { int randomInt(const int start, const int end, const unsigned int seed) {
/* /*
@@ -22,7 +22,7 @@ int randomX(const clock_t initClock) {
int x; int x;
const unsigned int seed = clock() - initClock; const unsigned int seed = clock() - initClock;
for (int i = randomInt(0, 100, seed); ; i++) { 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) if (x % 2 != 0)
break; break;
} }
@@ -32,16 +32,16 @@ int randomX(const clock_t initClock) {
int randomY(const clock_t initClock) { int randomY(const clock_t initClock) {
const unsigned int seed = clock() - 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) { void sleep_ms(const int ms) {
usleep(ms * 1000); usleep(ms * 1000);
} }
void printBoard() { void printBoard(char board[][brdInfo.x]) {
for (int i = 0; i < BRD_SIZE_Y; i++) { for (int i = 0; i < brdInfo.y; i++) {
for (int j = 0; j < BRD_SIZE_X; j++) { for (int j = 0; j < brdInfo.x; j++) {
printf("%c", board[i][j]); printf("%c", board[i][j]);
} }
printf("\n"); printf("\n");

View File

@@ -2,11 +2,14 @@
#define UTILS_H_ #define UTILS_H_
#include <time.h> #include <time.h>
#include "../globals.h"
extern boardInfo brdInfo;
int randomInt(const int start, const int end, const unsigned int seed); int randomInt(const int start, const int end, const unsigned int seed);
int randomX(const clock_t initClock); int randomX(const clock_t initClock);
int randomY(const clock_t initClock); int randomY(const clock_t initClock);
void sleep_ms(const int ms); void sleep_ms(const int ms);
void printBoard(); void printBoard(char board[][brdInfo.x]);
#endif // UTILS_H_ #endif // UTILS_H_