Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c69b5ec8a2 | |||
| 7f5394353d | |||
| 12c328d4c5 | |||
| f96a344573 | |||
| fcbeb38ea5 | |||
| 1dc0040fb3 | |||
| 0e992c3be5 | |||
| a762a8673d | |||
| 30c0acbbfe | |||
| 5bbc653b7e | |||
| 7b04c22d83 | |||
| 53135f50ad | |||
|
|
a9863b3441 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
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 the next snake part node
|
||||
|
||||
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:
|
||||
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:
|
||||
- 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 and designed to work with the combination of
|
||||
Only tested with the combination of
|
||||
- GCC
|
||||
- Linux
|
||||
- x86 CPU
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/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
3
compile_release.sh
Executable 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;
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
#include "board.h"
|
||||
|
||||
void setBoardBorders(char board[][brdInfo.x]) {
|
||||
for (int i = 0; i < brdInfo.y; i++) {
|
||||
|
||||
@@ -1,7 +1,17 @@
|
||||
#ifndef 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;
|
||||
|
||||
|
||||
@@ -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_
|
||||
99
src/main.c
99
src/main.c
@@ -6,29 +6,36 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "./utils/utils.h"
|
||||
#include "./snake/snake.h"
|
||||
#include "./board/board.h"
|
||||
#include "globals.h"
|
||||
|
||||
const char* VERSION = "1.1.9";
|
||||
|
||||
const char SNAKE_VIS = '#';
|
||||
|
||||
boardInfo brdInfo;
|
||||
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();
|
||||
|
||||
// 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);
|
||||
printf("Set board size (15 - 60, default: 15): ");
|
||||
getIntOrMinusOne(&brdInfo.y);
|
||||
if (!(brdInfo.y >= 15 && brdInfo.y <= 60) || brdInfo.y == -1) {
|
||||
brdInfo.y = 15;
|
||||
}
|
||||
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;
|
||||
@@ -37,6 +44,15 @@ int main() {
|
||||
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;
|
||||
|
||||
@@ -54,16 +70,21 @@ int main() {
|
||||
setBoardBorders(board);
|
||||
|
||||
int points = 0;
|
||||
int gameSpeed = 0;
|
||||
const int sleepInterval = 200;
|
||||
|
||||
// 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->y = randomY(initClock);
|
||||
snakeHead->visChar = '&';
|
||||
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->delay = -1;
|
||||
snakeHead->order->next = NULL;
|
||||
@@ -95,8 +116,16 @@ int main() {
|
||||
if (snakeHead->x == food.x
|
||||
&& snakeHead->y == food.y) {
|
||||
points++;
|
||||
if (points % 5 == 0)
|
||||
gameSpeed += 15;
|
||||
if (gameSpeed >= 100) {
|
||||
gameSpeed += 5;
|
||||
} else {
|
||||
gameSpeed += 10;
|
||||
}
|
||||
|
||||
if (gameSpeed >= 180) {
|
||||
gameSpeed = 180;
|
||||
}
|
||||
|
||||
do {
|
||||
food.x = randomX(initClock);
|
||||
food.y = randomY(initClock);
|
||||
@@ -160,3 +189,45 @@ 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);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#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;
|
||||
@@ -74,11 +76,18 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
|
||||
while (tail->next != NULL)
|
||||
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->dir = tail->dir;
|
||||
// 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->delay = -1;
|
||||
// First order if exists
|
||||
@@ -137,7 +146,10 @@ void pushOrder(order* head, char dir, int delay) {
|
||||
while (current->next != NULL)
|
||||
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->delay = delay;
|
||||
newOrder->next = NULL;
|
||||
@@ -179,7 +191,10 @@ void copyOrders(order* srcHead, order* destHead) {
|
||||
while (srcCurrent->next != NULL) {
|
||||
srcCurrent = srcCurrent->next;
|
||||
destPrev = destCurrent;
|
||||
destCurrent = (order*) malloc(sizeof(order));
|
||||
destCurrent = malloc(sizeof(order));
|
||||
if (!destCurrent) {
|
||||
mallocError("destCurrent", "snake.c", "copyOrders()");
|
||||
}
|
||||
destPrev->next = destCurrent;
|
||||
destCurrent->dir = srcCurrent->dir;
|
||||
destCurrent->delay = srcCurrent->delay + 1;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef SNAKE_H_
|
||||
#define SNAKE_H_
|
||||
|
||||
#include "../globals.h"
|
||||
#include "../board/board.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
@@ -39,29 +40,7 @@ void sleep_ms(const int ms) {
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
#define UTILS_H_
|
||||
|
||||
#include <time.h>
|
||||
#include "../globals.h"
|
||||
|
||||
#include "../board/board.h"
|
||||
|
||||
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 randomY(const clock_t initClock);
|
||||
void sleep_ms(const int ms);
|
||||
void setBoardBorders(char board[][brdInfo.x]);
|
||||
void printBoard(char board[][brdInfo.x]);
|
||||
void mallocError(const char* varName, const char* fileName, const char* functionName);
|
||||
|
||||
#endif // UTILS_H_
|
||||
|
||||
Reference in New Issue
Block a user