13 Commits

12 changed files with 131 additions and 69 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build/

View File

@@ -23,7 +23,7 @@ An order keeps information on when a snake part should change to what direction.
- A pointer to the next order - A pointer to the next order
## Info ## Info
Only tested and designed to work with the combination of Only tested with the combination of
- GCC - GCC
- Linux - Linux
- x86 CPU - x86 CPU

View File

@@ -1,3 +1,3 @@
#!/bin/bash #!/bin/bash
gcc -Wall -Werror -pedantic -o sanke src/main.c src/snake/**.c src/utils/**.c gcc -Og -g -Wall -Werror -pedantic -o build/sanke src/main.c src/snake/**.c src/utils/**.c src/board/**.c

3
compile_release.sh Executable file
View File

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

View File

@@ -1,7 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include "../globals.h"
extern boardInfo brdInfo; #include "board.h"
void setBoardBorders(char board[][brdInfo.x]) { void setBoardBorders(char board[][brdInfo.x]) {
for (int i = 0; i < brdInfo.y; i++) { for (int i = 0; i < brdInfo.y; i++) {

View File

@@ -1,7 +1,17 @@
#ifndef BOARD_H_ #ifndef BOARD_H_
#define BOARD_H_ #define BOARD_H_
#include "../globals.h" typedef struct {
int x;
int y;
} boardInfo;
typedef struct {
int xs;
int xe;
int ys;
int ye;
} playableBoardInfo;
extern boardInfo brdInfo; extern boardInfo brdInfo;

View File

@@ -1,16 +0,0 @@
#ifndef GLOBALS_H_
#define GLOBALS_H_
typedef struct {
int x;
int y;
} boardInfo;
typedef struct {
int xs;
int xe;
int ys;
int ye;
} playableBoardInfo;
#endif // GLOBALS_H_

View File

@@ -6,29 +6,36 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include "./utils/utils.h" #include "./utils/utils.h"
#include "./snake/snake.h" #include "./snake/snake.h"
#include "./board/board.h" #include "./board/board.h"
#include "globals.h"
const char* VERSION = "1.1.9";
const char SNAKE_VIS = '#'; const char SNAKE_VIS = '#';
boardInfo brdInfo; boardInfo brdInfo;
playableBoardInfo plBrdInfo; playableBoardInfo plBrdInfo;
int main() { void cmd_args(int argc, char** argv);
void emptyStdinBuffer();
void getIntOrMinusOne(int* dst);
int main(int argc, char** argv) {
cmd_args(argc, argv);
const clock_t initClock = clock(); const clock_t initClock = clock();
// Board Constraints // Board Constraints
printf("Set board size (15 - 60): "); printf("Set board size (15 - 60, default: 15): ");
scanf("%d", &brdInfo.y); getIntOrMinusOne(&brdInfo.y);
if (!(brdInfo.y >= 15 && brdInfo.y <= 60)) { if (!(brdInfo.y >= 15 && brdInfo.y <= 60) || brdInfo.y == -1) {
printf("Invalid input. Board size must be greater than 0.\n"); brdInfo.y = 15;
exit(0);
} }
brdInfo.x = brdInfo.y * 2; brdInfo.x = brdInfo.y * 2;
printf("brdInfo.x: %d\n", brdInfo.x); /*printf("brdInfo.x: %d\n", brdInfo.x);*/
printf("brdInfo.y: %d\n", brdInfo.y); /*printf("brdInfo.y: %d\n", brdInfo.y);*/
char board[brdInfo.y][brdInfo.x]; char board[brdInfo.y][brdInfo.x];
plBrdInfo.xs = 1; plBrdInfo.xs = 1;
@@ -37,6 +44,15 @@ int main() {
plBrdInfo.ye = brdInfo.y - 2; plBrdInfo.ye = brdInfo.y - 2;
// Set gamespeed
int gameSpeed = 0;
printf("Enter gamespeed (default: 0, max: 180): ");
getIntOrMinusOne(&gameSpeed);
if (gameSpeed == -1 || gameSpeed > 180) {
gameSpeed = 0;
}
// Termios setup // Termios setup
struct termios attr; struct termios attr;
@@ -54,16 +70,21 @@ int main() {
setBoardBorders(board); setBoardBorders(board);
int points = 0; int points = 0;
int gameSpeed = 0;
const int sleepInterval = 200; const int sleepInterval = 200;
// Snake head setup // Snake head setup
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart)); snakePart* snakeHead = malloc(sizeof(snakePart));
if (!snakeHead) {
mallocError("snakeHead", "main.c", "main()");
}
snakeHead->x = randomX(initClock); snakeHead->x = randomX(initClock);
snakeHead->y = randomY(initClock); snakeHead->y = randomY(initClock);
snakeHead->visChar = '&'; snakeHead->visChar = '&';
snakeHead->dir = 'w'; snakeHead->dir = 'w';
snakeHead->order = (order*) malloc(sizeof(order)); snakeHead->order = malloc(sizeof(order));
if (!snakeHead->order) {
mallocError("snakeHead->order", "main.c", "main()");
}
snakeHead->order->dir = snakeHead->dir; snakeHead->order->dir = snakeHead->dir;
snakeHead->order->delay = -1; snakeHead->order->delay = -1;
snakeHead->order->next = NULL; snakeHead->order->next = NULL;
@@ -95,8 +116,16 @@ int main() {
if (snakeHead->x == food.x if (snakeHead->x == food.x
&& snakeHead->y == food.y) { && snakeHead->y == food.y) {
points++; points++;
if (points % 5 == 0) if (gameSpeed >= 100) {
gameSpeed += 15; gameSpeed += 5;
} else {
gameSpeed += 10;
}
if (gameSpeed >= 180) {
gameSpeed = 180;
}
do { do {
food.x = randomX(initClock); food.x = randomX(initClock);
food.y = randomY(initClock); food.y = randomY(initClock);
@@ -160,3 +189,45 @@ game_over:
return 0; return 0;
} }
void cmd_args(int argc, char** argv) {
if (argc == 1) {
return;
}
if (
strcmp(argv[1], "--version") == 0
|| strcmp(argv[1], "-v") == 0
)
{
printf("Sanke version %s\n", VERSION);
exit(0);
}
}
void emptyStdinBuffer() {
char ch;
while ((ch = getchar()) != '\n');
}
void getIntOrMinusOne(int* dst) {
char iBuf[100];
char num[100];
int i = 0;
fgets(iBuf, 100, stdin);
while (!isdigit(iBuf[i])) {
if (iBuf[i] == '\0') {
*dst = -1;
return;
}
i++;
}
for (int j = 0; i < strlen(iBuf); i++, j++) {
num[j] = iBuf[i];
}
*dst = atoi(num);
}

View File

@@ -1,8 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include "snake.h" #include "snake.h"
#include "../board/board.h" #include "../board/board.h"
#include "../utils/utils.h"
extern char SNAKE_VIS; extern char SNAKE_VIS;
extern boardInfo brdInfo; extern boardInfo brdInfo;
@@ -74,11 +76,18 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
while (tail->next != NULL) while (tail->next != NULL)
tail = tail->next; tail = tail->next;
snakePart* newTail = (snakePart*) malloc(sizeof(snakePart)); snakePart* newTail = malloc(sizeof(snakePart));
if (!newTail) {
mallocError("newTail", "snake.c", "addSnakePart()");
}
newTail->visChar = SNAKE_VIS; newTail->visChar = SNAKE_VIS;
newTail->dir = tail->dir; newTail->dir = tail->dir;
// Order head // Order head
newTail->order = (order*) malloc(sizeof(order)); newTail->order = malloc(sizeof(order));
if (!newTail->order) {
mallocError("newTail", "snake.c", "addSnakePart()");
}
newTail->order->dir = newTail->dir; newTail->order->dir = newTail->dir;
newTail->order->delay = -1; newTail->order->delay = -1;
// First order if exists // First order if exists
@@ -137,7 +146,10 @@ void pushOrder(order* head, char dir, int delay) {
while (current->next != NULL) while (current->next != NULL)
current = current->next; current = current->next;
order* newOrder = (order*) malloc(sizeof(order)); order* newOrder = malloc(sizeof(order));
if (!newOrder) {
mallocError("newOrder", "snake.c", "pushOrder()");
}
newOrder->dir = dir; newOrder->dir = dir;
newOrder->delay = delay; newOrder->delay = delay;
newOrder->next = NULL; newOrder->next = NULL;
@@ -179,7 +191,10 @@ void copyOrders(order* srcHead, order* destHead) {
while (srcCurrent->next != NULL) { while (srcCurrent->next != NULL) {
srcCurrent = srcCurrent->next; srcCurrent = srcCurrent->next;
destPrev = destCurrent; destPrev = destCurrent;
destCurrent = (order*) malloc(sizeof(order)); destCurrent = malloc(sizeof(order));
if (!destCurrent) {
mallocError("destCurrent", "snake.c", "copyOrders()");
}
destPrev->next = destCurrent; destPrev->next = destCurrent;
destCurrent->dir = srcCurrent->dir; destCurrent->dir = srcCurrent->dir;
destCurrent->delay = srcCurrent->delay + 1; destCurrent->delay = srcCurrent->delay + 1;

View File

@@ -1,7 +1,7 @@
#ifndef SNAKE_H_ #ifndef SNAKE_H_
#define SNAKE_H_ #define SNAKE_H_
#include "../globals.h" #include "../board/board.h"
extern boardInfo brdInfo; extern boardInfo brdInfo;

View File

@@ -1,7 +1,8 @@
#include <stdio.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "utils.h" #include "utils.h"
extern boardInfo brdInfo; extern boardInfo brdInfo;
@@ -39,29 +40,7 @@ void sleep_ms(const int ms) {
usleep(ms * 1000); usleep(ms * 1000);
} }
void setBoardBorders(char board[][brdInfo.x]) { void mallocError(const char* varName, const char* fileName, const char* functionName) {
for (int i = 0; i < brdInfo.y; i++) { printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName);
for (int j = 0; j < brdInfo.x; j++) { exit(1);
if (i == 0 || i == brdInfo.y - 1)
board[i][j] = '-';
else if (j == 0 || j == brdInfo.x - 1)
board[i][j] = '|';
else
board[i][j] = ' ';
}
}
board[0][0] = '+';
board[brdInfo.y - 1][0] = '+';
board[0][brdInfo.x - 1] = '+';
board[brdInfo.y - 1][brdInfo.x - 1] = '+';
}
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");
}
} }

View File

@@ -2,7 +2,8 @@
#define UTILS_H_ #define UTILS_H_
#include <time.h> #include <time.h>
#include "../globals.h"
#include "../board/board.h"
extern boardInfo brdInfo; extern boardInfo brdInfo;
@@ -10,7 +11,6 @@ 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 setBoardBorders(char board[][brdInfo.x]); void mallocError(const char* varName, const char* fileName, const char* functionName);
void printBoard(char board[][brdInfo.x]);
#endif // UTILS_H_ #endif // UTILS_H_