hey guys, I have been all over the net trying to find why my game over fuction is not being called. here is the main bulk of the code, and under it is the game over function which is a seprate source file, but they are both compiled and linked together ok without any errors. When the int health reaches 0, it should call gameover(), but it just carrys on regardless. please take a look and let me know of anyway i can fix it: the code is pretty long, the gameover() call is in main(). thanks in advance!

/*
* Set the new color information
*/
SetConsoleTextAttribute ( h, FOREGROUND_GREEN|FOREGROUND_INTENSITY );
system("cls");
char sentmp[]="The land of Wiza, which is basicly a land of child fantasy\n"
"has always been a peaceful land, where young trainee wizards\n"
"and witches roam, and practice their magic in the hope that\n"
"one day, they can use it to help bring peace to the world...\n"
"\n\n";

char sentmpa[]="But one day, the evil witch DRUCILA cast an evil spell\n"
"on all the people who live in Wiza.\n"
"Her plan is to make all the people her slaves, so she can\n"
"have enough power to conquer the WHITE WILLOW, which is the\n"
"source of all our powers, and turn it evil...\n"
"\n\n";

char sentmpb[]="But hope is not lost....\n"
"\n\n"
"I have been instructed by the WHITE WILLOW to challenge\n"
"DRUCIA and break her spell that she has over us.\n"
"I am all but an amatuer witch at the moment, that is why\n"
"I need your help to battle the monsters that DRUCILA has\n"
"crafted into Wiza...\n"
"I know if we work as a team, we can defeat DRUCILA and restore\n"
"peice to Wiza!\n"
"\n\n";

void wizavillage()
{
char sente[]="\tWIZA VILLAGE\n"
"\n\n"
"Welcome to my village.... Hmm, I guess it would be a good\n"
"Idea to stock up on magic or supplies before we venture into\n"
"The woods ahead, Ok then, were to first ";

else if (items == 1)
{
items=items-1; // lose key from stats
system("cls");
char sentmso[]="\t\tM A G I C S H O P\n"
"\n\n"
"A cheery looking woman looks at you from behind the counter.\n"
"She seems a little young to be running a magic shop,\n"
"but you do not let this bother you as you aproach her.\n"
"\n\n";

char sentta[]="\n\n"
"Erm, hello there....\n"
"I am traveling on an adventure to save the kingdom from Drucila's\n"
"evil spell, and Wizadora and myself would like to purchase some\n"
"magical supplies if that is ok....\n"
"\n\n";

else if ((choice == 'y') && (Exp <=0))
{
health=health-20;
system("cls");
printf("OLD WOMAN\n"
"\n\n");
char sentth[]="I can see it in your eyes, you are lying to me!\n"
"For this punishment.... Back you go, and I will take\n"
"Some of your life force too, and use it to create my own!\n"
"NEVER LIE TO A WISE ONE!\n"
"\n\n";

printf("OLD WOMAN:");
char senttj[]="\n\n"
"I like people who tell the truth, for a reward, I will give you a\n"
"clue...\n"
"\n\n"
"Go to the OLD CHURCH. The minester should be able to help you\n"
"gain some experience in battle...\n"
"\n\n"
"Now, I shall return you to the signpost you past, it is the nearest I can\n"
"get you to the church....\n"
"\n\n";

cout<<""<< pname <<""<<endl;
char sentl[]="\n\n"
"I do not know who she was Wizadora, but we need that Experience.\n"
"We'd better head to the old church. Maybe we can find some answera there.\n"
"\n\n";

/*
* Set the new color information
*/
SetConsoleTextAttribute ( h, FOREGROUND_GREEN|FOREGROUND_INTENSITY );
system("cls");
char senteog[]="Drucila Falls to the floor and trys to grab you\n"
"as you take a step back from her...\n"
"\n\n"
"The chapel begins to tumble to the floor, you manage to\n"
"escape through the large window on your left hand side.\n"
"\n\n"
"You run and run as the cathedral tumbles down behind you.\n"
"\n\n"
"The sun breaks through the clouds as they turn from dark\n"
"to white\n"
"In the faint rays of the sun, the monsters that littered the\n"
"ground turn back to the once happy people of Wiza...\n"
"\n\n"
"You then arrive back in the town square, and recive three\n"
"huge cheers as the entire town turns out to give their thanks!\n"
"\n\n"
"The WHITE WILLOW begins to glow and sprout with joy once more...\n"
"\n\n"
"You can see the light flowing from it's glistening bark...\n"
"\n\n"
"\n\n"
"With Drucila now defeated, and the land of Wiza finaly free, you can\n"
"now take a well earned break....\n"
"\n\n"
"\n\n"
"\n\n"
"\n\n"
"CONGATULATIONS!!!!!\n"
"\n\n"
"\n\n"
"\n\n"
"\n\n"
"\n\n"
"\n\n";

Since you are obviously using a C++ compiler, why not use classes and do away with all those global variables? A little thought now will make maintenance and expansion orders of magnitude easier. You should always strive to minimize the scope of all variables and you should also consider naming conventions, particularlly for module wide and global variables. You may also want to consider extracting the prose from your code into a file as it will enable you to make changes easier (won't have to recompile), not to mention with some clever setup of state variables, enable code reuse.

As for your particular complaint, since gameover is only called once, in main, at the end, how sure are you that the program ever gets to that point? Either run the program in a debugger or put debug output statements so you know the state of your program at critical steps.

06-24-2005

swgh

ok thanks alot for the advise.. it's odd that my code will not compile on your compiler. I just ran the debug and will serously concider using classes, might cut the functions down a little too, I think I will deal with the game over error after I re-organised it a little better.

ONE MORE THING:

If I put the gameover function into the main source file and call it from main and not an external source, would it work better or worse?

06-24-2005

elad

One possible approach would be to create a player class and a game class. In main() you'd declare a game object. In setting up the attributes of the game object you can declare a player object. During a given game, if the player's health decreases to zero you can call the gameOver() function. In this scenario I think you could have the gameOver() function be a member of either the player or the game class.

As somewhat of an aside, you could declare one or more player objects for any given game. In this scenario I'd have a removePlayer() function if a given player's health decreases to zero, and a gameOver() if all players have been removed. However, setting up a single player game is probably preferable to setting up a multi-player game.

06-24-2005

mitakeet

It seems to me that your gameover function should be called whenever the number of points is <= 0, yet in your main() you only call it once, yet exit the game (at least I presume you exit after spells()) anyway.

Regarding the compilation problems, what compiler are you using? It seems to have very serious standards issues if it doesn't flag that at least as a warning (perhaps you have warnings turned off? (always a bad idea, most warnings are actually errors)).

Think of the game as a state machine. Each action causes reactions that may cascade (if the player gets shot and looses all their points, the game is over) requiring that the state be updated (perhaps recursively). Though many programs appear to be linear, they need not be implemented that way (yours is very linear, which gives the user very little freedom to explore). So called 'event driven' programs (Windows programs are the archetype) allows the user to choose to do whatever they want to do (subject to some contraints) and the programmer must always maintain the state of the program so it can be examined from any part in the program. It may sound tedious, like you have to have logic/error checking code everywhere, but with a little thought you can encapsulate much of that into function/methods and just call them with various flags.

06-24-2005

Salem

Some general comments.

1. All your functions return void, and take no parameters.
2. A consequence of 1 is that you have lots of global variables instread.
The whole thing is basically 'structured goto' programming.

You can spot so many problems from well indented code, simply because you can see how the code is going to execute from the way the code is indented.
Also, if(powers=+1) isn't going to be doing what you want it to.

If for no other reason, consider that most people who could answer your questions will simply take one look at that mass of poorly indented code, say "ugh" and move on to another question.

06-24-2005

MathFan

Quote:

it's odd that my code will not compile on your compiler

Hmmm.... I agree there, it seems to work for me, and I'm using GNU C++ Compiler. Though your code looks kind of odd, too:

Quote:

return signpost(); // return to this point after system("pause")

I'm not very clear about what you want this code to do; and your comment is strange. Why the return anyway? If you want recursion, you could just do it like:

Code:

signpost();

Quote:

If I put the gameover function into the main source file and call it from main and not an external source, would it work better or worse?

That has really nothing to say. In both cases your gameover function will be present in the game and called from the main(). That is, as long as it is correct and compiles without errors.

The location of it hasn't any significance. Though it's a good idea to split your code in several files so that it becomes more readable. That will help you later when you are going to debug and for other people who are going to look at your code.