I'm trying to learn how to clean up my code, and I think the solution will be to use custom functions. My current project is a text-based adventure game. Right now I'm trying to get the room system working. Here is my movement code - this code is nested in a switch case that is alphabetized. I'd like the switch case to only contain refrences to custom function calls to help keep it smallish in size and easy to manage. This is what the movement code for going in the direction "down" looks like:
Code:
case 'D':
case 'd':
if( strcmp( input, "Down") == 0 ||
strcmp( input, "down") == 0 ||
strcmp( input, "D") == 0 ||
strcmp( input, "d") == 0 ){
if( ableGoD == 1 ){
printf( "You head down.\n");
floor--;
printf("Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
}
else
printf( "The way is blocked.\n");
}
else
printf("I don't understand Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
break;
I'd like to be able to trim it down to something like this:
Code:
case 'D':
case 'd':
if( strcmp( input, "Down") == 0 ||
strcmp( input, "down") == 0 ||
strcmp( input, "D") == 0 ||
strcmp( input, "d") == 0 ){
call movedown function
}
else
printf("I don't understand Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
break;
In otherwords, I'd like to remove all movement code from my switch case, and keep the actual code in another part of the program to improve readability and keep things neat. Now, keep in mind I've only been programming for a few weeks, but I've read up in my book something that sounds near what I need to do, but I can't get it work. I believe I need to define something like this at the beggining of my program:
int movedown( blahblah); /* function prototype */
And then build upon that outside of my main function with it's own code. However, every attempt I've made at this has failed, and the examples given in my book are not parallel enough for me to understand them very clear.
Could someone out there tell me if I'm headed in the right direction, or possibly post to some code that would be of help to me? I'm not asking the answer be handed to me, just some hints as to if I'm even on the right track.
Another of my ideas is this - instead of having the same error messages posted over and over, I'd like to store all of my error messages in one part of the program, in it's own function. I can't figure out how to do this, either, though. I would need the main function to send a value to the error function, and have the error function read this value and send back the correct error, correct? I haven't figured out how to do that, either, but I feel that it could be accomplished similar to my first problem.
Edit: I think an even better idea for the movement code would be this: Instead of having a single function for each direction, simply do a call to a "move" function, which then takes a look at which direction the character is trying to head in, and once it deduces that, send the correct course of action back to the main function. Having the movement code all in a single function would cut down on code used, and improve readability etc.