From af57eab9d66875554fdf4459f344d3e653efcdb3 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 12:53:46 +0100 Subject: [PATCH] added get_Chara --- src/main/c/Snake/get_character.c | 59 ++++++++++++++++++++++++++++++++ src/main/c/Snake/get_character.h | 6 ++++ src/main/c/Snake/snake_start.c | 54 +++++++++++++++++++++++++++++ test/Snake/test_part_of_snake.c | 13 +++++++ 4 files changed, 132 insertions(+) create mode 100644 src/main/c/Snake/get_character.c create mode 100644 src/main/c/Snake/get_character.h diff --git a/src/main/c/Snake/get_character.c b/src/main/c/Snake/get_character.c new file mode 100644 index 0000000..a800c01 --- /dev/null +++ b/src/main/c/Snake/get_character.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +struct termios orig_termios; + +void reset_terminal_mode(){ + tcsetattr(0, TCSANOW, &orig_termios); +} + +void set_conio_terminal_mode(){ + struct termios new_termios; + + /* take two copies - one for now, one for later */ + tcgetattr(0, &orig_termios); + memcpy(&new_termios, &orig_termios, sizeof(new_termios)); + + /* register cleanup handler, and set the new terminal mode */ + atexit(reset_terminal_mode); + cfmakeraw(&new_termios); + tcsetattr(0, TCSANOW, &new_termios); +} + +int kbhit(){ + struct timeval tv = { 0L, 0L }; + fd_set fds; + FD_ZERO(&fds); + FD_SET(0, &fds); + return select(1, &fds, NULL, NULL, &tv) > 0; +} + +int getch(){ + int r; + unsigned char c; + if ((r = read(0, &c, sizeof(c))) < 0) { + return r; + } else { + return c; + } +} + +char get_character(double limit){ + set_conio_terminal_mode(); + + clock_t t = clock(); + char c = 0; + + while ((double)(clock() - t) / CLOCKS_PER_SEC < limit){ + if(kbhit()){c = getch();} + } + + reset_terminal_mode(); + + return c; +} \ No newline at end of file diff --git a/src/main/c/Snake/get_character.h b/src/main/c/Snake/get_character.h new file mode 100644 index 0000000..a955266 --- /dev/null +++ b/src/main/c/Snake/get_character.h @@ -0,0 +1,6 @@ +#ifndef GET_CHARACTER_H +#define GET_CHARACTER_H + +char get_character(double limit); + +#endif // GET_CHARACTER_H \ No newline at end of file diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 86f0204..18d92e8 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -1,11 +1,18 @@ #include #include +#include +#include +#include #include "snake_start.h" +#include "get_character.h" +#define TIME_TURN 0.5; void main_menu(); void game(); Snake initialize_snake(); +int *get_next_move(double limit); +void move_snake(Snake *snake); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); @@ -47,13 +54,23 @@ void main_menu(){ void game(){ Snake snake = initialize_snake(); bool running = true; + clock_t t = clock(); + while (running) { system("clear"); + + t = clock() - t; + int *mv = get_next_move((double)t / CLOCKS_PER_SEC); + t = clock(); + + move_snake(&snake); draw(&snake); running = false; + } getchar(); + getchar(); } Snake initialize_snake(){ @@ -67,6 +84,43 @@ Snake initialize_snake(){ return snake; } +int *get_next_move(double limit){ + limit = TIME_TURN - limit; + int *ptr = (int*) malloc(2 * sizeof(int)); + clock_t t = clock(); + char c; + + c = get_character(limit); + + switch (c){ + case 'w': + case 'A': + printf("Hoch\n"); + break; + case 'a': + case 'D': + printf("Links\n"); + break; + case 's': + case 'B': + printf("Runter\n"); + break; + case 'd': + case 'C': + printf("Rechts\n"); + break; + + default: + break; + } + + return ptr; +} + +void move_snake(Snake *snake){ + +} + void draw(Snake *snake){ printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} diff --git a/test/Snake/test_part_of_snake.c b/test/Snake/test_part_of_snake.c index 5dfad8b..fcc45e9 100644 --- a/test/Snake/test_part_of_snake.c +++ b/test/Snake/test_part_of_snake.c @@ -35,4 +35,17 @@ void test_get_correct_index(void){ TEST_ASSERT_EQUAL_INT(2, result);//2. part ist at 6/8 } + +void test_snake_not_on_tile(void){ + /* arrange */ + int result; + Snake snake = initialize_snake(); + + /* act */ + result = part_of_snake(&snake, 6 * 16 + 6); + + /* assert */ + TEST_ASSERT_EQUAL_INT(-1, result);//-1 snake is not on 6/6 +} + #endif // TEST \ No newline at end of file