created a src folder
This commit is contained in:
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_
|
||||
175
src/main.c
Normal file
175
src/main.c
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
#include "./utils/utils.h"
|
||||
#include "./snake/snake.h"
|
||||
#include "globals.h"
|
||||
|
||||
const char SNAKE_VIS = '#';
|
||||
|
||||
boardInfo brdInfo;
|
||||
playableBoardInfo plBrdInfo;
|
||||
|
||||
int main() {
|
||||
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
|
||||
struct termios attr;
|
||||
|
||||
tcgetattr(STDIN_FILENO, &attr);
|
||||
|
||||
const struct termios ATTR_ORIG = attr;
|
||||
|
||||
attr.c_lflag &= ~(ECHO | ICANON);
|
||||
attr.c_cc[VMIN] = 0;
|
||||
attr.c_cc[VTIME] = 0;
|
||||
|
||||
tcsetattr(STDIN_FILENO, 0, &attr);
|
||||
|
||||
// Game board setup
|
||||
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] = '+';
|
||||
|
||||
int points = 0;
|
||||
int gameSpeed = 0;
|
||||
const int sleepInterval = 200;
|
||||
|
||||
// Snake head setup
|
||||
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart));
|
||||
snakeHead->x = randomX(initClock);
|
||||
snakeHead->y = randomY(initClock);
|
||||
snakeHead->visChar = '&';
|
||||
snakeHead->dir = 'w';
|
||||
snakeHead->order = (order*) malloc(sizeof(order));
|
||||
snakeHead->order->dir = snakeHead->dir;
|
||||
snakeHead->order->delay = -1;
|
||||
snakeHead->order->next = NULL;
|
||||
snakeHead->next = NULL;
|
||||
|
||||
board[snakeHead->y][snakeHead->x] = snakeHead->visChar;
|
||||
|
||||
// Food setup
|
||||
struct {
|
||||
int x;
|
||||
int y;
|
||||
char visChar;
|
||||
} food;
|
||||
food.x = randomX(initClock);
|
||||
food.y = randomY(initClock);
|
||||
food.visChar = '$';
|
||||
|
||||
board[food.y][food.x] = food.visChar;
|
||||
|
||||
// Screen init
|
||||
system("clear");
|
||||
|
||||
// Game loop
|
||||
while (1) {
|
||||
fflush(stdout);
|
||||
char buf[1] = {0};
|
||||
|
||||
// Food collision
|
||||
if (snakeHead->x == food.x
|
||||
&& snakeHead->y == food.y) {
|
||||
points++;
|
||||
if (points % 5 == 0)
|
||||
gameSpeed += 15;
|
||||
do {
|
||||
food.x = randomX(initClock);
|
||||
food.y = randomY(initClock);
|
||||
} while (checkCollision(snakeHead, food.x, food.y));
|
||||
addSnakePart(board, snakeHead);
|
||||
}
|
||||
|
||||
// Input handling
|
||||
if (read(STDIN_FILENO, buf, 1) != 0 && isalpha(buf[0])) {
|
||||
char input = buf[0];
|
||||
input = tolower(input);
|
||||
|
||||
if (
|
||||
(input == 'w' && snakeHead->dir != 's')
|
||||
|| (input == 's' && snakeHead->dir != 'w')
|
||||
|| (input == 'a' && snakeHead->dir != 'd')
|
||||
|| (input == 'd' && snakeHead->dir != 'a')
|
||||
) {
|
||||
snakeHead->dir = input;
|
||||
addOrders(snakeHead, input);
|
||||
}
|
||||
}
|
||||
|
||||
// Update board
|
||||
board[food.y][food.x] = food.visChar;
|
||||
mvSnakeParts(board, snakeHead);
|
||||
|
||||
// Snake collision
|
||||
if (snakeHead->next != NULL) {
|
||||
snakePart* current = snakeHead->next;
|
||||
while (1) {
|
||||
if (current->x == snakeHead->x
|
||||
&& current->y == snakeHead->y)
|
||||
goto game_over;
|
||||
|
||||
if (current->next == NULL)
|
||||
break;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
system("clear");
|
||||
printBoard(board);
|
||||
printf("Points: %d\n", points);
|
||||
printf("Game Speed: %d\n", gameSpeed);
|
||||
|
||||
sleep_ms(sleepInterval - gameSpeed);
|
||||
}
|
||||
|
||||
game_over:
|
||||
|
||||
// Game over
|
||||
system("clear");
|
||||
|
||||
printBoard(board);
|
||||
printf("Final Points: %d\n", points);
|
||||
printf("Final Game Speed: %d\n", gameSpeed);
|
||||
|
||||
tcsetattr(STDIN_FILENO, 0, &ATTR_ORIG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
185
src/snake/snake.c
Normal file
185
src/snake/snake.c
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "snake.h"
|
||||
|
||||
extern char SNAKE_VIS;
|
||||
extern boardInfo brdInfo;
|
||||
extern playableBoardInfo plBrdInfo;
|
||||
|
||||
void mvSnakeParts(char board[][brdInfo.x], snakePart* head) {
|
||||
snakePart* part = head;
|
||||
while (1) {
|
||||
|
||||
order* orderHead = part->order;
|
||||
|
||||
if (orderHead->next != NULL) {
|
||||
if (orderHead->next->delay == 0) {
|
||||
part->dir = orderHead->next->dir;
|
||||
removeOrder(orderHead);
|
||||
}
|
||||
}
|
||||
|
||||
if (orderHead->next != NULL) {
|
||||
order* current = orderHead->next;
|
||||
while (1) {
|
||||
if (current->delay > 0)
|
||||
current->delay--;
|
||||
if (current->next == NULL)
|
||||
break;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
int x = part->x;
|
||||
int y = part->y;
|
||||
|
||||
board[y][x] = ' ';
|
||||
|
||||
switch (part->dir) {
|
||||
case 'w':
|
||||
y = y - 1 < plBrdInfo.ys ? plBrdInfo.ye : y - 1;
|
||||
break;
|
||||
case 's':
|
||||
y = y + 1 > plBrdInfo.ye ? plBrdInfo.ys : y + 1;
|
||||
break;
|
||||
case 'a':
|
||||
x = x - 2 < plBrdInfo.xs ? plBrdInfo.xe - 1 : x - 2;
|
||||
break;
|
||||
case 'd':
|
||||
x = x + 2 > plBrdInfo.xe ? plBrdInfo.xs : x + 2;
|
||||
break;
|
||||
default:
|
||||
printf("ERROR in func mvSnakeParts\n");
|
||||
printf("dir: %c\n", part->dir);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
part->x = x;
|
||||
part->y = y;
|
||||
|
||||
board[y][x] = part->visChar;
|
||||
|
||||
if (part->next == NULL)
|
||||
return;
|
||||
|
||||
part = part->next;
|
||||
}
|
||||
}
|
||||
|
||||
void addSnakePart(char board[][brdInfo.x], snakePart* head) {
|
||||
snakePart* tail = head;
|
||||
|
||||
while (tail->next != NULL)
|
||||
tail = tail->next;
|
||||
|
||||
snakePart* newTail = (snakePart*) malloc(sizeof(snakePart));
|
||||
newTail->visChar = SNAKE_VIS;
|
||||
newTail->dir = tail->dir;
|
||||
// Order head
|
||||
newTail->order = (order*) malloc(sizeof(order));
|
||||
newTail->order->dir = newTail->dir;
|
||||
newTail->order->delay = -1;
|
||||
// First order if exists
|
||||
if (tail->order->next != NULL)
|
||||
copyOrders(tail->order, newTail->order);
|
||||
|
||||
|
||||
switch (newTail->dir) {
|
||||
case 'w':
|
||||
newTail->x = tail->x;
|
||||
newTail->y = tail->y + 1;
|
||||
break;
|
||||
case 's':
|
||||
newTail->x = tail->x;
|
||||
newTail->y = tail->y - 1;
|
||||
break;
|
||||
case 'a':
|
||||
newTail->x = tail->x + 2;
|
||||
newTail->y = tail->y;
|
||||
break;
|
||||
case 'd':
|
||||
newTail->x = tail->x - 2;
|
||||
newTail->y = tail->y;
|
||||
break;
|
||||
default:
|
||||
printf("Invalid direction in func addSnakePart\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
newTail->next = NULL;
|
||||
|
||||
tail->next = newTail;
|
||||
|
||||
board[newTail->y][newTail->x] = newTail->visChar;
|
||||
}
|
||||
|
||||
void pushOrder(order* head, char dir, int delay) {
|
||||
order* current = head;
|
||||
while (current->next != NULL)
|
||||
current = current->next;
|
||||
|
||||
order* newOrder = (order*) malloc(sizeof(order));
|
||||
newOrder->dir = dir;
|
||||
newOrder->delay = delay;
|
||||
newOrder->next = NULL;
|
||||
|
||||
current->next = newOrder;
|
||||
}
|
||||
|
||||
void removeOrder(order* head) {
|
||||
order* newFirstOrder = head->next->next;
|
||||
free(head->next);
|
||||
head->next = newFirstOrder;
|
||||
}
|
||||
|
||||
void addOrders(snakePart* head, char dir) {
|
||||
snakePart* current = head;
|
||||
int i = 1;
|
||||
while (1) {
|
||||
if (current->next == NULL)
|
||||
break;
|
||||
|
||||
current = current->next;
|
||||
|
||||
pushOrder(current->order, dir, i);
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void copyOrders(order* srcHead, order* destHead) {
|
||||
if (srcHead->next == NULL) {
|
||||
printf("ERROR in copyOrders: no orders to copy\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
order* srcCurrent = srcHead;
|
||||
order* destCurrent = destHead;
|
||||
order* destPrev = destHead;
|
||||
|
||||
while (srcCurrent->next != NULL) {
|
||||
srcCurrent = srcCurrent->next;
|
||||
destPrev = destCurrent;
|
||||
destCurrent = (order*) malloc(sizeof(order));
|
||||
destPrev->next = destCurrent;
|
||||
destCurrent->dir = srcCurrent->dir;
|
||||
destCurrent->delay = srcCurrent->delay + 1;
|
||||
destCurrent->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool checkCollision(snakePart* head, int x, int y) {
|
||||
snakePart* current = head;
|
||||
while (1) {
|
||||
if (current->x == x && current->y == y)
|
||||
return true;
|
||||
|
||||
if (current->next == NULL)
|
||||
break;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
32
src/snake/snake.h
Normal file
32
src/snake/snake.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef SNAKE_H_
|
||||
#define SNAKE_H_
|
||||
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
|
||||
typedef struct orderNode {
|
||||
char dir;
|
||||
int delay;
|
||||
struct orderNode* next;
|
||||
} order;
|
||||
|
||||
typedef struct snakeNode {
|
||||
int x;
|
||||
int y;
|
||||
char visChar;
|
||||
|
||||
char dir;
|
||||
order* order;
|
||||
struct snakeNode* next;
|
||||
} snakePart;
|
||||
|
||||
void mvSnakeParts(char board[][brdInfo.x], snakePart* head);
|
||||
void addSnakePart(char board[][brdInfo.x], snakePart* head);
|
||||
void pushOrder(order* head, char dir, int delay);
|
||||
void removeOrder(order* head);
|
||||
void addOrders(snakePart* head, char dir);
|
||||
void copyOrders(order* srcHead, order* destHead);
|
||||
bool checkCollision(snakePart* head, int x, int y);
|
||||
|
||||
#endif // SNAKE_H_
|
||||
49
src/utils/utils.c
Normal file
49
src/utils/utils.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "utils.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
extern playableBoardInfo plBrdInfo;
|
||||
|
||||
int randomInt(const int start, const int end, const unsigned int seed) {
|
||||
/*
|
||||
* Gets random int from range
|
||||
* [start, end[
|
||||
*/
|
||||
srand(seed);
|
||||
int result = rand() % end;
|
||||
result = result >= start ? result : result + start;
|
||||
return result;
|
||||
}
|
||||
|
||||
int randomX(const clock_t initClock) {
|
||||
int x;
|
||||
const unsigned int seed = clock() - initClock;
|
||||
for (int i = randomInt(0, 100, seed); ; i++) {
|
||||
x = randomInt(plBrdInfo.xs, plBrdInfo.xe, i);
|
||||
if (x % 2 != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
int randomY(const clock_t initClock) {
|
||||
const unsigned int seed = clock() - initClock;
|
||||
return randomInt(plBrdInfo.ys, plBrdInfo.ye, seed);
|
||||
}
|
||||
|
||||
void sleep_ms(const int ms) {
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
15
src/utils/utils.h
Normal file
15
src/utils/utils.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef UTILS_H_
|
||||
#define UTILS_H_
|
||||
|
||||
#include <time.h>
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
|
||||
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 printBoard(char board[][brdInfo.x]);
|
||||
|
||||
#endif // UTILS_H_
|
||||
Reference in New Issue
Block a user