From a4772e24416a3925f83cac8185b8fab40be72b01 Mon Sep 17 00:00:00 2001 From: fdai7184 Date: Fri, 10 Feb 2023 01:26:24 +0100 Subject: [PATCH] refactoring:if-else for arrow keys to switch for visibility --- src/input.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/input.c b/src/input.c index 882eda1..46194cc 100644 --- a/src/input.c +++ b/src/input.c @@ -8,12 +8,12 @@ int kbhit(void); char getch(void); -/*int main(int argc, char ** argv){ +int main(int argc, char ** argv){ printf("wasd or arrows to control snake, q to quit, p to pause game."); while(1){ getInput(); } -}*/ +} /** getInput gets the userinput and reacts to input of w,a,s,d, arrow keys, p or q * utilises getch() and kbhit, no parameters, no returns @@ -24,17 +24,20 @@ void getInput(){ if(key == '\033'){ getch(); char key = getch(); - if(key=='A'){ - printf("up"); - } - else if(key=='B'){ - printf("down"); - } - else if(key=='C'){ - printf("right"); - } - else if(key=='D'){ - printf("left"); + + switch(key){ + case 'A': + printf("up"); + break; + case 'B': + printf("down"); + break; + case 'C': + printf("right"); + break; + case 'D': + printf("left"); + break; } } else{