diff options
author | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2020-01-09 16:41:57 +0100 |
---|---|---|
committer | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2020-01-09 16:41:57 +0100 |
commit | 7891fda413ff314ff5cd977d96392988c16216e3 (patch) | |
tree | de7497fb57b567ac57c3f6f0451f09a219b69d14 | |
download | csnake-7891fda413ff314ff5cd977d96392988c16216e3.tar.gz csnake-7891fda413ff314ff5cd977d96392988c16216e3.tar.bz2 csnake-7891fda413ff314ff5cd977d96392988c16216e3.zip |
initial commit
-rwxr-xr-x | a.out | bin | 0 -> 17912 bytes | |||
-rw-r--r-- | linked_list.c | 76 | ||||
-rw-r--r-- | linked_list.h | 25 | ||||
-rw-r--r-- | linked_list.o | bin | 0 -> 2512 bytes | |||
-rw-r--r-- | snake.c | 236 |
5 files changed, 337 insertions, 0 deletions
diff --git a/a.out b/a.out new file mode 100755 index 0000000..e502c3e --- /dev/null +++ b/a.out Binary files differdiff --git a/linked_list.c b/linked_list.c new file mode 100644 index 0000000..98b672b --- /dev/null +++ b/linked_list.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "linked_list.h" + +Node *initialise_node() +{ + Node* newNode = malloc(sizeof(Node)); + + newNode->next = NULL; + newNode->prev = NULL; + + return newNode; +} + +void free_node(Node* node) +{ + if(node) + free(node); + return; +} + +LinkedList *initialise_linked_list() +{ + LinkedList* newList = malloc(sizeof(LinkedList)); + + newList->head = NULL; + newList->tail = NULL; + + return newList; +} + +void free_linked_list(LinkedList* list) +{ + Node* element; + + while(list->head) + { + element = list->head->next; + free_node(list->head); + list->head = element; + } + free(list); +} + +void append_linked_list(LinkedList* list, int x, int y) +{ + Node* newNode = initialise_node(); + + newNode->x = x; + newNode->y = y; + + newNode->prev = list->tail; + + if(list->tail) + list->tail->next = newNode; + list->tail = newNode; + if(!list->head) + list->head = newNode; +} + +void remove_tail_linked_list(LinkedList* list) +{ + Node* element; + + if(!list->tail) + return; + element = list->tail->prev; + free_node(list->tail); + list->tail = element; + if(list->tail) + list->tail->next = NULL; + else + list->head = NULL; +} + diff --git a/linked_list.h b/linked_list.h new file mode 100644 index 0000000..fcea7a0 --- /dev/null +++ b/linked_list.h @@ -0,0 +1,25 @@ +#ifndef _LINKED_LIST_H +#define _LINKED_LIST_H + +typedef struct Node +{ + int x, y; + struct Node *next, *prev; +} Node; + +typedef struct LinkedList +{ + Node* head, *tail; +} LinkedList; + +Node *initialise_node(void); +void free_node(Node *); +LinkedList *initialise_linked_list(void); +void free_linked_list(LinkedList *); +void append_linked_list(LinkedList *, int, int); +void remove_tail_linked_list(LinkedList *); +void print_linked_list(LinkedList *, void (*)(void *)); + + +#endif + diff --git a/linked_list.o b/linked_list.o new file mode 100644 index 0000000..04e25e6 --- /dev/null +++ b/linked_list.o Binary files differdiff --git a/snake.c b/snake.c new file mode 100644 index 0000000..cb95844 --- /dev/null +++ b/snake.c @@ -0,0 +1,236 @@ +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <time.h> + +#include <ncurses.h> +#include "linked_list.h" + +void playGame(LinkedList* snake, int* apple, int* stats); +void drawSnake(LinkedList* snake, int* apple); +void init_snake(LinkedList* snake); +int check_collision(LinkedList* snake, int* apple); +void move_left(LinkedList* snake); +void move_right(LinkedList* snake); +void move_up(LinkedList* snake); +void move_down(LinkedList* snake); +void follow(int* tmp, int* prev, LinkedList* snake); + +int fps = 60; + +int main() +{ + WINDOW* windw; + LinkedList* snake; + int stats[3], apple[2]; + int time, score, win; + + windw = initscr(); + clear(); + noecho(); + cbreak(); + + snake = initialise_linked_list(); + apple[0] = 20; + apple[1] = 10; + //= { 1, 2}; + + /* add initial nodes */ + init_snake(snake); + + playGame(snake, apple, stats); + + time = stats[0]; + score = stats[1]; + win = stats[2]; + + getch(); /* Wait for user input */ + endwin(); + printf("Time: %d\n", time); + printf("Score: %d\n", score); + + return 0; +} + +void playGame(LinkedList* snake, int* apple, int* stats) +{ + int start = (int)time(NULL); + int score = 0; + int win = 0; + int i = 0; + while(1) + { + clear(); + printw("Update %d", i); + if(i < 7) + move_up(snake); + else if(i < 12) + move_left(snake); + else if(i < 17) + move_down(snake); + else + move_right(snake); + + drawSnake(snake, apple); + refresh(); + switch(check_collision(snake, apple)) + { + //apple + case 1: + score++; + //new apple + //new node + append_linked_list(snake, snake->tail->x - 1, snake->tail->y); + break; + //self + case 2: + win = 0; + return; + break; + //wall + case 3: + win = 0; + return; + break; + default: + break; + } + usleep(50000); + //usleep(1000000 / fps); + i++; + } + + // time + *(stats + 0) = (int)time(NULL) - start; + // score + *(stats + 1) = score; + // win + *(stats + 2) = win; +} + +void drawSnake(LinkedList* snake, int* apple) +{ + Node* element; + + element = snake->head; + while(element) + { + mvaddstr(element->y, element->x, "*"); + element = element->next; + } + + mvaddstr(apple[0], apple[1], "="); +} + +void init_snake(LinkedList* snake) +{ + int midx = COLS / 2; + int midy = LINES / 2; + + append_linked_list(snake, midx - 2, midy); + append_linked_list(snake, midx - 1, midy); + append_linked_list(snake, midx, midy); + append_linked_list(snake, midx + 1, midy); + append_linked_list(snake, midx - 2, midy); +} + +int check_collision(LinkedList* snake, int* apple) +{ + //0 no collision + //1 collision with apple + //2 collision with self + //3 collision with wall + + Node* element; + + /* check collision with apple */ + if(snake->head->x == apple[0] && snake->head->y == apple[1]) + return 1; + + /* check collision with self */ + element = snake->head->next; + while(element) + { + if(snake->head->x == element->x && snake->head->y == element->y) + return 2; + element = element->next; + } + + /* check collision with wall */ + if((snake->head->y == 0 || snake->head->y == LINES) || (snake->head->x == 0 || snake->head->x == COLS)) + return 3; + + /* no collision */ + return 0; +} + +void move_left(LinkedList* snake) +{ + int tmp[2]; + int prev[2]; + + *(prev + 0) = snake->head->x; + *(prev + 1) = snake->head->y; + snake->head->x = snake->head->x - 1; + + follow(tmp, prev, snake); +} + +void move_right(LinkedList* snake) +{ + int tmp[2]; + int prev[2]; + + *(prev + 0) = snake->head->x; + *(prev + 1) = snake->head->y; + snake->head->x = snake->head->x + 1; + + follow(tmp, prev, snake); +} + +void move_up(LinkedList* snake) +{ + int tmp[2]; + int prev[2]; + + *(prev + 0) = snake->head->x; + *(prev + 1) = snake->head->y; + snake->head->y = snake->head->y - 1; + + follow(tmp, prev, snake); +} + +void move_down(LinkedList* snake) +{ + int tmp[2]; + int prev[2]; + + *(prev + 0) = snake->head->x; + *(prev + 1) = snake->head->y; + snake->head->y = snake->head->y + 1; + + follow(tmp, prev, snake); +} + +void follow(int* tmp, int* prev, LinkedList* snake) +{ + Node* element; + element = snake->head->next; + while(element) + { + *(tmp + 0) = element->x; + *(tmp + 1) = element->y; + + element->x = *(prev + 0); + element->y = *(prev + 1); + + *(prev + 0) = *(tmp + 0); + *(prev + 1) = *(tmp + 1); + + element = element->next; + } +} + + + + |