progress
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
#ifndef GLOBAL_MACROS_H
|
|
||||||
#define GLOBAL_MACROS_H
|
|
||||||
|
|
||||||
#define BUF_SIZE 256
|
|
||||||
#define BRD_SIZE_X 15
|
|
||||||
#define BRD_SIZE_Y 30
|
|
||||||
|
|
||||||
#endif // GLOBAL_MACROS_H
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef GLOBALS_H
|
#ifndef GLOBALS_H_
|
||||||
#define GLOBALS_H
|
#define GLOBALS_H_
|
||||||
|
|
||||||
#define BUF_SIZE 256
|
#define BUF_SIZE 256
|
||||||
|
|
||||||
@@ -13,4 +13,4 @@
|
|||||||
#define PL_BRD_YS 1
|
#define PL_BRD_YS 1
|
||||||
#define PL_BRD_YE BRD_SIZE_Y - 2
|
#define PL_BRD_YE BRD_SIZE_Y - 2
|
||||||
|
|
||||||
#endif // GLOBALS_H
|
#endif // GLOBALS_H_
|
||||||
|
|||||||
240
main.c
240
main.c
@@ -2,34 +2,22 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include "./globals.h"
|
#include "./globals.h"
|
||||||
|
#include "./utils/utils.h"
|
||||||
|
#include "./snake/snake.h"
|
||||||
|
|
||||||
//TEMP
|
void mvSnakeParts(snakePart* head);
|
||||||
#include "./test_utils/test_utils.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
char visChar;
|
|
||||||
} brdSymbol;
|
|
||||||
|
|
||||||
typedef struct snakeNode {
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
|
|
||||||
char dir;
|
|
||||||
struct snakeNode* prev;
|
|
||||||
} snakePart;
|
|
||||||
|
|
||||||
int randomInt(const int start, const int end);
|
|
||||||
void sleep_ms(const int ms);
|
|
||||||
void mvSymbol(brdSymbol* symbol, const char dir);
|
|
||||||
|
|
||||||
char board[BRD_SIZE_Y][BRD_SIZE_X];
|
char board[BRD_SIZE_Y][BRD_SIZE_X];
|
||||||
|
|
||||||
|
const char SNAKE_VIS = '#';
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
clock_t initClock = clock();
|
||||||
|
|
||||||
// Termios setup
|
// Termios setup
|
||||||
struct termios attr;
|
struct termios attr;
|
||||||
|
|
||||||
@@ -46,19 +34,12 @@ int main() {
|
|||||||
// Game board setup
|
// Game board setup
|
||||||
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
||||||
for (int j = 0; j < BRD_SIZE_X; j++) {
|
for (int j = 0; j < BRD_SIZE_X; j++) {
|
||||||
if (
|
if (i == 0 || i == BRD_SIZE_Y - 1)
|
||||||
(i > 0 && i < BRD_SIZE_Y - 1)
|
|
||||||
&& (j == 0 || j == BRD_SIZE_X - 1)
|
|
||||||
) {
|
|
||||||
board[i][j] = '|';
|
|
||||||
} else if (
|
|
||||||
(i == 0 || i == BRD_SIZE_Y - 1)
|
|
||||||
&& (j > 0 && j < BRD_SIZE_X - 1)
|
|
||||||
) {
|
|
||||||
board[i][j] = '-';
|
board[i][j] = '-';
|
||||||
} else {
|
else if (j == 0 || j == BRD_SIZE_X - 1)
|
||||||
|
board[i][j] = '|';
|
||||||
|
else
|
||||||
board[i][j] = ' ';
|
board[i][j] = ' ';
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,100 +48,155 @@ int main() {
|
|||||||
board[0][BRD_SIZE_X - 1] = '+';
|
board[0][BRD_SIZE_X - 1] = '+';
|
||||||
board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+';
|
board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+';
|
||||||
|
|
||||||
brdSymbol player;
|
char mvPoints[BRD_SIZE_Y][BRD_SIZE_X];
|
||||||
player.x = randomInt(PL_BRD_XS, PL_BRD_XE);
|
|
||||||
player.y = randomInt(PL_BRD_YS, PL_BRD_YE);
|
|
||||||
player.visChar = '#';
|
|
||||||
|
|
||||||
board[player.y][player.x] = player.visChar;
|
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
||||||
|
for (int j = 0; j < BRD_SIZE_X; j++) {
|
||||||
|
mvPoints[i][j] = '0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int points = 0;
|
||||||
|
|
||||||
|
// Snake head setup
|
||||||
|
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart));
|
||||||
|
snakeHead->x = randomX(initClock);
|
||||||
|
snakeHead->y = randomY(initClock);
|
||||||
|
snakeHead->visChar = SNAKE_VIS;
|
||||||
|
snakeHead->dir = 'w';
|
||||||
|
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");
|
system("clear");
|
||||||
printBoard();
|
|
||||||
printf("x: %d\n", player.x);
|
|
||||||
printf("y: %d\n", player.y);
|
|
||||||
|
|
||||||
|
// Game loop
|
||||||
while (1) {
|
while (1) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
char buf[1] = {0};
|
char buf[1] = {0};
|
||||||
if (read(STDIN_FILENO, buf, 1) == 0)
|
if (read(STDIN_FILENO, buf, 1) != 0 && isalpha(buf[0])) {
|
||||||
continue;
|
char input = buf[0];
|
||||||
char input = buf[0];
|
|
||||||
|
|
||||||
if (!isalpha(input))
|
input = tolower(input);
|
||||||
continue;
|
|
||||||
|
|
||||||
input = tolower(input);
|
if (
|
||||||
|
input == 'w'
|
||||||
|
|| input == 's'
|
||||||
|
|| input == 'a'
|
||||||
|
|| input == 'd'
|
||||||
|
) {
|
||||||
|
if (points > 0)
|
||||||
|
mvPoints[snakeHead->y][snakeHead->x] = input;
|
||||||
|
snakeHead->dir = input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch (input) {
|
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
||||||
case 'w':
|
for (int j = 0; j < BRD_SIZE_X; j++) {
|
||||||
mvSymbol(&player, 'u');
|
if (mvPoints[i][j] == '0')
|
||||||
break;
|
continue;
|
||||||
case 's':
|
|
||||||
mvSymbol(&player, 'd');
|
snakePart* part = snakeHead;
|
||||||
break;
|
while (part->next != NULL) {
|
||||||
case 'a':
|
part = part->next;
|
||||||
mvSymbol(&player, 'l');
|
|
||||||
break;
|
if (part->y == i && part->x == j) {
|
||||||
case 'd':
|
part->dir = mvPoints[i][j];
|
||||||
mvSymbol(&player, 'r');
|
if (part->next == NULL)
|
||||||
break;
|
mvPoints[i][j] = '0';
|
||||||
default:
|
break;
|
||||||
NULL;
|
}
|
||||||
|
|
||||||
|
if (part->next == NULL)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mvSnakeParts(snakeHead);
|
||||||
|
|
||||||
|
if (snakeHead->x == food.x
|
||||||
|
&& snakeHead->y == food.y) {
|
||||||
|
points++;
|
||||||
|
food.x = randomX(initClock);
|
||||||
|
food.y = randomY(initClock);
|
||||||
|
board[food.y][food.x] = food.visChar;
|
||||||
|
addSnakePart(snakeHead);
|
||||||
}
|
}
|
||||||
|
|
||||||
system("clear");
|
system("clear");
|
||||||
printBoard();
|
printBoard();
|
||||||
printf("x: %d\n", player.x);
|
printf("Points: %d\n", points);
|
||||||
printf("y: %d\n", player.y);
|
printf("x: %d\n", snakeHead->x);
|
||||||
|
printf("y: %d\n", snakeHead->y);
|
||||||
|
printf("food x: %d\n", food.x);
|
||||||
|
printf("food y: %d\n", food.y);
|
||||||
|
printf("food: %c\n", board[food.y][food.x]);
|
||||||
|
|
||||||
sleep_ms(50);
|
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
||||||
|
for (int j = 0; j < BRD_SIZE_X; j++) {
|
||||||
|
printf("%c ", mvPoints[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep_ms(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Termios reset
|
// Termios reset
|
||||||
tcsetattr(STDIN_FILENO, 0,&ATTR_ORIG);
|
tcsetattr(STDIN_FILENO, 0, &ATTR_ORIG);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int randomInt(const int start, const int end) {
|
void mvSnakeParts(snakePart* head) {
|
||||||
/*
|
snakePart* part = head;
|
||||||
* Gets random int from range
|
while (1) {
|
||||||
* [start, end[
|
int x = part->x;
|
||||||
*/
|
int y = part->y;
|
||||||
srand(time(NULL));
|
|
||||||
int result = rand() % end;
|
|
||||||
result = result >= start ? result : result + start;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sleep_ms(const int ms) {
|
board[y][x] = ' ';
|
||||||
usleep(ms * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mvSymbol(brdSymbol* symbol, const char dir) {
|
switch (part->dir) {
|
||||||
board[symbol->y][symbol->x] = ' ';
|
case 'w':
|
||||||
|
y = y - 1 < PL_BRD_YS ? PL_BRD_YE : y - 1;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
y = y + 1 > PL_BRD_YE ? PL_BRD_YS : y + 1;
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
x = x - 2 < PL_BRD_XS ? PL_BRD_XE : x - 2;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
x = x + 2 > PL_BRD_XE ? PL_BRD_XS : x + 2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("ERROR in func mvSymbol\n");
|
||||||
|
printf("dir: %c\n", part->dir);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
switch (dir) {
|
part->x = x;
|
||||||
case 'u':
|
part->y = y;
|
||||||
symbol->y = symbol->y - 1 < PL_BRD_YS ? PL_BRD_YE : symbol->y - 1;
|
|
||||||
break;
|
board[y][x] = part->visChar;
|
||||||
case 'd':
|
|
||||||
symbol->y = symbol->y + 1 > PL_BRD_YE ? PL_BRD_YS : symbol->y + 1;
|
if (part->next == NULL)
|
||||||
break;
|
return;
|
||||||
case 'l':
|
|
||||||
symbol->x = symbol->x - 2 < PL_BRD_XS ? PL_BRD_XE : symbol->x - 2;
|
part = part->next;
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
symbol->x = symbol->x + 2 > PL_BRD_XE ? PL_BRD_XS : symbol->x + 2;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("ERROR in func mvSymbol\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
board[symbol->y][symbol->x] = symbol->visChar;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addSnakePart(snakePart** tail) {
|
|
||||||
*(tail)->symbolInfo.visChar =
|
|
||||||
}
|
}
|
||||||
|
|||||||
44
snake/snake.c
Normal file
44
snake/snake.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "snake.h"
|
||||||
|
|
||||||
|
extern char board[BRD_SIZE_Y][BRD_SIZE_X];
|
||||||
|
|
||||||
|
void addSnakePart(snakePart* head) {
|
||||||
|
snakePart* tail = head;
|
||||||
|
|
||||||
|
while (tail->next != NULL)
|
||||||
|
tail = tail->next;
|
||||||
|
|
||||||
|
snakePart* newTail = (snakePart*) malloc(sizeof(snakePart));
|
||||||
|
newTail->visChar = '#';
|
||||||
|
newTail->dir = tail->dir;
|
||||||
|
|
||||||
|
switch (newTail->dir) {
|
||||||
|
case 'w':
|
||||||
|
newTail->x = tail->x;
|
||||||
|
newTail->y = tail->y + 1;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
newTail->x = tail->x - 1;
|
||||||
|
newTail->y = tail->y;
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
newTail->x = tail->x - 1;
|
||||||
|
newTail->y = tail->y;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
newTail->x = tail->x;
|
||||||
|
newTail->y = tail->y - 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("Invalid direction in func addSnakePart\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
newTail->next = NULL;
|
||||||
|
|
||||||
|
tail->next = newTail;
|
||||||
|
|
||||||
|
board[newTail->y][newTail->x] = newTail->visChar;
|
||||||
|
}
|
||||||
17
snake/snake.h
Normal file
17
snake/snake.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef SNAKE_H_
|
||||||
|
#define SNAKE_H_
|
||||||
|
|
||||||
|
#include "../globals.h"
|
||||||
|
|
||||||
|
typedef struct snakeNode {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
char visChar;
|
||||||
|
|
||||||
|
char dir;
|
||||||
|
struct snakeNode* next;
|
||||||
|
} snakePart;
|
||||||
|
|
||||||
|
void addSnakePart(snakePart* head);
|
||||||
|
|
||||||
|
#endif // SNAKE_H_
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "test_utils.h"
|
|
||||||
#include "../globals.h"
|
|
||||||
|
|
||||||
extern char board[BRD_SIZE_Y][BRD_SIZE_X];
|
|
||||||
|
|
||||||
void printBoard() {
|
|
||||||
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
|
||||||
for (int j = 0; j < BRD_SIZE_X; j++) {
|
|
||||||
printf("%c", board[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#ifndef TEST_UTILS_H
|
|
||||||
#define TEST_UTILS_H
|
|
||||||
|
|
||||||
#include "../globals.h"
|
|
||||||
|
|
||||||
void printBoard();
|
|
||||||
|
|
||||||
#endif // TEST_UTILS_H
|
|
||||||
49
utils/utils.c
Normal file
49
utils/utils.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "utils.h"
|
||||||
|
#include "../globals.h"
|
||||||
|
|
||||||
|
extern char board[BRD_SIZE_Y][BRD_SIZE_X];
|
||||||
|
|
||||||
|
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(PL_BRD_XS, PL_BRD_XE, i);
|
||||||
|
if (x % 2 == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int randomY(const clock_t initClock) {
|
||||||
|
const unsigned int seed = clock() - initClock;
|
||||||
|
return randomInt(PL_BRD_YS, PL_BRD_YE, seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sleep_ms(const int ms) {
|
||||||
|
usleep(ms * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printBoard() {
|
||||||
|
for (int i = 0; i < BRD_SIZE_Y; i++) {
|
||||||
|
for (int j = 0; j < BRD_SIZE_X; j++) {
|
||||||
|
printf("%c", board[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
12
utils/utils.h
Normal file
12
utils/utils.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef UTILS_H_
|
||||||
|
#define UTILS_H_
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
#endif // UTILS_H_
|
||||||
Reference in New Issue
Block a user