Browse Source

added get_Chara

remotes/origin/David
David Moeller 11 months ago
parent
commit
af57eab9d6
  1. 59
      src/main/c/Snake/get_character.c
  2. 6
      src/main/c/Snake/get_character.h
  3. 54
      src/main/c/Snake/snake_start.c
  4. 13
      test/Snake/test_part_of_snake.c

59
src/main/c/Snake/get_character.c

@ -0,0 +1,59 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
#include <stdio.h>
#include <time.h>
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;
}

6
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

54
src/main/c/Snake/snake_start.c

@ -1,11 +1,18 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include "snake_start.h" #include "snake_start.h"
#include "get_character.h"
#define TIME_TURN 0.5;
void main_menu(); void main_menu();
void game(); void game();
Snake initialize_snake(); Snake initialize_snake();
int *get_next_move(double limit);
void move_snake(Snake *snake);
void draw(Snake *snake); void draw(Snake *snake);
int part_of_snake(Snake *snake, unsigned char tile); int part_of_snake(Snake *snake, unsigned char tile);
@ -47,13 +54,23 @@ void main_menu(){
void game(){ void game(){
Snake snake = initialize_snake(); Snake snake = initialize_snake();
bool running = true; bool running = true;
clock_t t = clock();
while (running) while (running)
{ {
system("clear"); system("clear");
t = clock() - t;
int *mv = get_next_move((double)t / CLOCKS_PER_SEC);
t = clock();
move_snake(&snake);
draw(&snake); draw(&snake);
running = false; running = false;
} }
getchar(); getchar();
getchar();
} }
Snake initialize_snake(){ Snake initialize_snake(){
@ -67,6 +84,43 @@ Snake initialize_snake(){
return 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){ void draw(Snake *snake){
printf("+"); printf("+");
for(int i = 0; i < WIDTH; i++){printf("-");} for(int i = 0; i < WIDTH; i++){printf("-");}

13
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 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 #endif // TEST
Loading…
Cancel
Save