Browse Source

refactoring: added function checkMove for clarity

remotes/origin/navigation
KRUGSON 2 years ago
parent
commit
247bff96b6
  1. 51
      src/c/main.c

51
src/c/main.c

@ -10,24 +10,23 @@
bool gameRunning; bool gameRunning;
bool acceptedRules; bool acceptedRules;
//declare needed variables
// declare needed variables
Room *map; Room *map;
// content // content
char *gameInstructionsFile = "../../src/content/game_instructions.txt"; char *gameInstructionsFile = "../../src/content/game_instructions.txt";
char *gameMapFile = "../../src/content/game.map"; char *gameMapFile = "../../src/content/game.map";
//navigation
// navigation
int playerPosition = 0; int playerPosition = 0;
int lastPlayerPosition = 0; int lastPlayerPosition = 0;
// function declarations // function declarations
void printInit(); void printInit();
void acceptInstructions(); void acceptInstructions();
void processInput(); void processInput();
int checkExit(); int checkExit();
int checkMove();
int main() int main()
{ {
@ -38,7 +37,7 @@ int main()
// init and instructions // init and instructions
printInit(); printInit();
//get Content
// get Content
map = getMap(gameMapFile); map = getMap(gameMapFile);
if (acceptedRules == 1) if (acceptedRules == 1)
@ -58,7 +57,6 @@ int main()
return 0; return 0;
} }
// init dialogue // init dialogue
void printInit() void printInit()
{ {
@ -117,13 +115,34 @@ void acceptInstructions()
// process user input // process user input
void processInput(char userInput[20]) void processInput(char userInput[20])
{ {
Room r = map[playerPosition];
if (checkExit(userInput) == 1) if (checkExit(userInput) == 1)
{ {
gameRunning = 0; gameRunning = 0;
printf("!GAME EXIT!\n"); printf("!GAME EXIT!\n");
} }
else if (strcmp(userInput, "north") == 0)
else if (checkMove(userInput) == 1)
{
printf("Wrong Input!\n");
}
}
// function for checking user input of exit
int checkExit(char userInput[20])
{
if (strcmp(userInput, "esc") == 0 || strcmp(userInput, "exit") == 0 || strcmp(userInput, "quit") == 0)
{
return 1;
}
return 0;
}
int checkMove(char userInput[20])
{
Room r = map[playerPosition];
if (strcmp(userInput, "north") == 0)
{ {
printf("->N\n"); printf("->N\n");
lastPlayerPosition = playerPosition; lastPlayerPosition = playerPosition;
@ -135,6 +154,8 @@ void processInput(char userInput[20])
{ {
playerPosition = r.successor; playerPosition = r.successor;
} }
return 0;
} }
else if (strcmp(userInput, "south") == 0) else if (strcmp(userInput, "south") == 0)
{ {
@ -148,21 +169,11 @@ void processInput(char userInput[20])
{ {
printf("You have reached the border. You have to go in the other direction!\n"); printf("You have reached the border. You have to go in the other direction!\n");
} }
return 0;
} }
else else
{
printf("Wrong Input!\n");
}
}
//function for checking user input of exit
int checkExit(char userInput[20])
{
if (strcmp(userInput, "esc") == 0 || strcmp(userInput, "exit") == 0 || strcmp(userInput, "quit") == 0)
{ {
return 1; return 1;
} }
return 0;
} }
Loading…
Cancel
Save