1 Commits

Author SHA1 Message Date
AaroSaila
855f646dea Clearer language 2024-12-15 12:58:35 +02:00
12 changed files with 70 additions and 132 deletions

1
.gitignore vendored
View File

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

View File

@@ -17,13 +17,13 @@ Each part of the snake is a node of a dynamically allocated linked list that con
- A pointer to a order node (more on orders below)
- A pointer to the next snake part node
An order keeps information on when a snake part should change to what direction. An order is also a node in a dynamically allocated linked list that contains:
An order keeps information on when the direction of the snake part has to change and to which direction it should change. An order is also a node in a dynamically allocated linked list that contains:
- The direction that this order eventually tells the snake part to go to
- The delay i.e. how many ticks until the snake part has to change direction.
- A pointer to the next order
## Info
Only tested with the combination of
Only tested and designed to work with the combination of
- GCC
- Linux
- x86 CPU

View File

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

View File

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

View File

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

16
src/globals.h Normal file
View File

@@ -0,0 +1,16 @@
#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,36 +6,29 @@
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include "./utils/utils.h"
#include "./snake/snake.h"
#include "./board/board.h"
const char* VERSION = "1.1.9";
#include "globals.h"
const char SNAKE_VIS = '#';
boardInfo brdInfo;
playableBoardInfo plBrdInfo;
void cmd_args(int argc, char** argv);
void emptyStdinBuffer();
void getIntOrMinusOne(int* dst);
int main(int argc, char** argv) {
cmd_args(argc, argv);
int main() {
const clock_t initClock = clock();
// Board Constraints
printf("Set board size (15 - 60, default: 15): ");
getIntOrMinusOne(&brdInfo.y);
if (!(brdInfo.y >= 15 && brdInfo.y <= 60) || brdInfo.y == -1) {
brdInfo.y = 15;
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);*/
printf("brdInfo.x: %d\n", brdInfo.x);
printf("brdInfo.y: %d\n", brdInfo.y);
char board[brdInfo.y][brdInfo.x];
plBrdInfo.xs = 1;
@@ -44,15 +37,6 @@ int main(int argc, char** argv) {
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
struct termios attr;
@@ -70,21 +54,16 @@ int main(int argc, char** argv) {
setBoardBorders(board);
int points = 0;
int gameSpeed = 0;
const int sleepInterval = 200;
// Snake head setup
snakePart* snakeHead = malloc(sizeof(snakePart));
if (!snakeHead) {
mallocError("snakeHead", "main.c", "main()");
}
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart));
snakeHead->x = randomX(initClock);
snakeHead->y = randomY(initClock);
snakeHead->visChar = '&';
snakeHead->dir = 'w';
snakeHead->order = malloc(sizeof(order));
if (!snakeHead->order) {
mallocError("snakeHead->order", "main.c", "main()");
}
snakeHead->order = (order*) malloc(sizeof(order));
snakeHead->order->dir = snakeHead->dir;
snakeHead->order->delay = -1;
snakeHead->order->next = NULL;
@@ -116,16 +95,8 @@ int main(int argc, char** argv) {
if (snakeHead->x == food.x
&& snakeHead->y == food.y) {
points++;
if (gameSpeed >= 100) {
gameSpeed += 5;
} else {
gameSpeed += 10;
}
if (gameSpeed >= 180) {
gameSpeed = 180;
}
if (points % 5 == 0)
gameSpeed += 15;
do {
food.x = randomX(initClock);
food.y = randomY(initClock);
@@ -189,45 +160,3 @@ game_over:
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,10 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "snake.h"
#include "../board/board.h"
#include "../utils/utils.h"
extern char SNAKE_VIS;
extern boardInfo brdInfo;
@@ -76,18 +74,11 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
while (tail->next != NULL)
tail = tail->next;
snakePart* newTail = malloc(sizeof(snakePart));
if (!newTail) {
mallocError("newTail", "snake.c", "addSnakePart()");
}
snakePart* newTail = (snakePart*) malloc(sizeof(snakePart));
newTail->visChar = SNAKE_VIS;
newTail->dir = tail->dir;
// Order head
newTail->order = malloc(sizeof(order));
if (!newTail->order) {
mallocError("newTail", "snake.c", "addSnakePart()");
}
newTail->order = (order*) malloc(sizeof(order));
newTail->order->dir = newTail->dir;
newTail->order->delay = -1;
// First order if exists
@@ -146,10 +137,7 @@ void pushOrder(order* head, char dir, int delay) {
while (current->next != NULL)
current = current->next;
order* newOrder = malloc(sizeof(order));
if (!newOrder) {
mallocError("newOrder", "snake.c", "pushOrder()");
}
order* newOrder = (order*) malloc(sizeof(order));
newOrder->dir = dir;
newOrder->delay = delay;
newOrder->next = NULL;
@@ -191,10 +179,7 @@ void copyOrders(order* srcHead, order* destHead) {
while (srcCurrent->next != NULL) {
srcCurrent = srcCurrent->next;
destPrev = destCurrent;
destCurrent = malloc(sizeof(order));
if (!destCurrent) {
mallocError("destCurrent", "snake.c", "copyOrders()");
}
destCurrent = (order*) malloc(sizeof(order));
destPrev->next = destCurrent;
destCurrent->dir = srcCurrent->dir;
destCurrent->delay = srcCurrent->delay + 1;

View File

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

View File

@@ -1,8 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "utils.h"
extern boardInfo brdInfo;
@@ -40,7 +39,29 @@ void sleep_ms(const int ms) {
usleep(ms * 1000);
}
void mallocError(const char* varName, const char* fileName, const char* functionName) {
printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName);
exit(1);
void setBoardBorders(char board[][brdInfo.x]) {
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 == 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,8 +2,7 @@
#define UTILS_H_
#include <time.h>
#include "../board/board.h"
#include "../globals.h"
extern boardInfo brdInfo;
@@ -11,6 +10,7 @@ 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 mallocError(const char* varName, const char* fileName, const char* functionName);
void setBoardBorders(char board[][brdInfo.x]);
void printBoard(char board[][brdInfo.x]);
#endif // UTILS_H_