Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
855f646dea |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
build/
|
|
||||||
@@ -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 a order node (more on orders below)
|
||||||
- A pointer to the next snake part node
|
- 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 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.
|
- The delay i.e. how many ticks until the snake part has to change direction.
|
||||||
- A pointer to the next order
|
- A pointer to the next order
|
||||||
|
|
||||||
## Info
|
## Info
|
||||||
Only tested with the combination of
|
Only tested and designed to work with the combination of
|
||||||
- GCC
|
- GCC
|
||||||
- Linux
|
- Linux
|
||||||
- x86 CPU
|
- x86 CPU
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/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
|
||||||
|
|||||||
@@ -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;
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "../globals.h"
|
||||||
|
|
||||||
#include "board.h"
|
extern boardInfo brdInfo;
|
||||||
|
|
||||||
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++) {
|
||||||
|
|||||||
@@ -1,17 +1,7 @@
|
|||||||
#ifndef BOARD_H_
|
#ifndef BOARD_H_
|
||||||
#define BOARD_H_
|
#define BOARD_H_
|
||||||
|
|
||||||
typedef struct {
|
#include "../globals.h"
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
} boardInfo;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int xs;
|
|
||||||
int xe;
|
|
||||||
int ys;
|
|
||||||
int ye;
|
|
||||||
} playableBoardInfo;
|
|
||||||
|
|
||||||
extern boardInfo brdInfo;
|
extern boardInfo brdInfo;
|
||||||
|
|
||||||
|
|||||||
16
src/globals.h
Normal file
16
src/globals.h
Normal 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_
|
||||||
99
src/main.c
99
src/main.c
@@ -6,36 +6,29 @@
|
|||||||
#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;
|
||||||
|
|
||||||
void cmd_args(int argc, char** argv);
|
int main() {
|
||||||
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, default: 15): ");
|
printf("Set board size (15 - 60): ");
|
||||||
getIntOrMinusOne(&brdInfo.y);
|
scanf("%d", &brdInfo.y);
|
||||||
if (!(brdInfo.y >= 15 && brdInfo.y <= 60) || brdInfo.y == -1) {
|
if (!(brdInfo.y >= 15 && brdInfo.y <= 60)) {
|
||||||
brdInfo.y = 15;
|
printf("Invalid input. Board size must be greater than 0.\n");
|
||||||
|
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;
|
||||||
@@ -44,15 +37,6 @@ int main(int argc, char** argv) {
|
|||||||
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;
|
||||||
|
|
||||||
@@ -70,21 +54,16 @@ int main(int argc, char** argv) {
|
|||||||
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 = malloc(sizeof(snakePart));
|
snakePart* snakeHead = (snakePart*) 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 = malloc(sizeof(order));
|
snakeHead->order = (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;
|
||||||
@@ -116,16 +95,8 @@ int main(int argc, char** argv) {
|
|||||||
if (snakeHead->x == food.x
|
if (snakeHead->x == food.x
|
||||||
&& snakeHead->y == food.y) {
|
&& snakeHead->y == food.y) {
|
||||||
points++;
|
points++;
|
||||||
if (gameSpeed >= 100) {
|
if (points % 5 == 0)
|
||||||
gameSpeed += 5;
|
gameSpeed += 15;
|
||||||
} 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);
|
||||||
@@ -189,45 +160,3 @@ 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);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
#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;
|
||||||
@@ -76,18 +74,11 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
|
|||||||
while (tail->next != NULL)
|
while (tail->next != NULL)
|
||||||
tail = tail->next;
|
tail = tail->next;
|
||||||
|
|
||||||
snakePart* newTail = malloc(sizeof(snakePart));
|
snakePart* newTail = (snakePart*) 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 = malloc(sizeof(order));
|
newTail->order = (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
|
||||||
@@ -146,10 +137,7 @@ void pushOrder(order* head, char dir, int delay) {
|
|||||||
while (current->next != NULL)
|
while (current->next != NULL)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
|
||||||
order* newOrder = malloc(sizeof(order));
|
order* newOrder = (order*) 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;
|
||||||
@@ -191,10 +179,7 @@ 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 = malloc(sizeof(order));
|
destCurrent = (order*) 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;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef SNAKE_H_
|
#ifndef SNAKE_H_
|
||||||
#define SNAKE_H_
|
#define SNAKE_H_
|
||||||
|
|
||||||
#include "../board/board.h"
|
#include "../globals.h"
|
||||||
|
|
||||||
extern boardInfo brdInfo;
|
extern boardInfo brdInfo;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
|
#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;
|
||||||
@@ -40,7 +39,29 @@ void sleep_ms(const int ms) {
|
|||||||
usleep(ms * 1000);
|
usleep(ms * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mallocError(const char* varName, const char* fileName, const char* functionName) {
|
void setBoardBorders(char board[][brdInfo.x]) {
|
||||||
printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName);
|
for (int i = 0; i < brdInfo.y; i++) {
|
||||||
exit(1);
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
#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;
|
||||||
|
|
||||||
@@ -11,6 +10,7 @@ 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 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_
|
#endif // UTILS_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user