Archived

This topic is now archived and is closed to further replies.

Newbie Question....Help

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You must have been bored of me and my posys by now but please give it another try.You see in the following program which is a text game(if it can be said as one)I don''t know why but it loads the gamea() function twice when I execute it.Please help me.

Share this post


Link to post
Share on other sites
I had it in the previous forum but I now made some alterations look please and help me.

#include <iostream.h>

int game()
{
int first;
cout<<"You are in a locked room full of stores for the soldiers of the base.\nWhat are you going to do?Enter a number.Enter 1 in order to try to force the door.\nEnter 2 in order to try to go through the ventilation.\nEnter 3 in order to pretend to be ill and call for the guard to check.\nEnter:";
cin>> first;
switch(first)
{
case 1:cout<<"The door is made of titanium and has a really tuff lock.\nYou don''t think you can open it.GAME OVER";
break;
case 2:cout<<"Yeah that''s it you made it through the ventilation.Thank god you were on a diet before you were captured.";
break;
case 3:cout<<"Nope.Didn''t work.The guard understood your trick and started laughing.You must try something else.GAME OVER";
break;
default:cout<<"Wrong number";
}
return first;
}

int gamea()
{
int second;
cout<<"Good you made it through the ventilations.Now you are in a hall.\nYou can either go right or left.Enter 1 for right or 2 for left.Enter:";
cin>> second;
switch(second)
{
case 1:cout<<"Right didn''t appear to be the right choice, a surveilance \ncamer caught a glimpse of you.That was it the guards caught\n you.GAME OVER";
break;
case 2:cout<<"Gongratulations.You are really good at this.An idiot must\n have left this way unguarded.You made your way\n out of the prison facility of the base.";
break;
default:cout<<"Wrong number";
}
return second;
}

int gameb()
{
int third;
cout<<"You are now in the Prison yard.You approach the exit door\n but see that it requires a password.\nFortunately it gives you a hint.\nThe password is (2*2*8)-30.\nEnter password:";
cin>> third;
switch(third)
{
case 2:cout<<"Oh boy you are good.You found the password.You made it\n completely out of the prison and into the\nmain base.";
break;
default:cout<<"Wrong password.GAME OVER.";
}
return third;
}
int main()
{
cout<<"eeeeeee\n";
cout<<"eeeeeee\n";
cout<<"ee\n";
cout<<"ee\n";
cout<<"ee\n";
cout<<"ee\n";
cout<<"eeeeeee 11111\n";
cout<<"eeeeeee 11 11\n";
cout<<"ee 11 11\n";
cout<<"ee 11\n";
cout<<"ee 11\n";
cout<<"ee 11\n";
cout<<"eeeeeee 11\n";
cout<<"eeeeeee 11\n";

if(game()==2)
{
gamea();
}

if(gamea()==2)
{
gameb();
}

return 0;
}



Share this post


Link to post
Share on other sites
if(game()==2)    // game() is called, and if it returns 2
{ // ...
gamea(); // then gamea() is called.
}

if(gamea()==2) // now, gamea() is called again, if it returns 2
{ //...
gameb(); // then gameb() is called.
}

what you want to do is this:
if (game()==2)
{
if (gamea()==2)
{
gameb();
};
};

of course, that is a really bad set-up for a game of any size at all.
i do not mean this in a rude way, but i think you ought to brush up on your basic c/c++ before you keep on at this one...

Share this post


Link to post
Share on other sites
Here''s why.
You call it twice.

if(game()==2)
{
gamea();
}
This is the first time you call it.
When you get 2 returned from game(), you call gamea().
However you then do the following.

if(gamea()==2)
{
gameb();
}

gamea() is then called again in the if statement.
You then call gameb() if gamea returns 2.


To be honest, simply looking at your code and trying to figure out line by line what the computer will do is the best thing to do at this point. Simply looking at that code for two seconds, I was able to see the problem.
I figure that telling you to figure it out yourself isn''t the nicest thing, and I remember when I was in your shoes.
Here is how you should implement it.

if (game()==2)
{
if (gamea() == 2)
{
gameb()
}
}

What this does is call game(), and check if it returns 2.
If it doesn''t, then do whatever you want after the if statement.
If it does, go ahead and call gamea(), and check if it returns 2.
If it doesn''t, then do whatever you want after the if statement.
If it does, go ahead and call gameb().


If you notice, I just showed you not a bug so much but just bad design. Imagine trying to make this game bigger in the future.
You will have to keep nesting if statements each time.
Imagine if you want more than one choice possible, and if you pick one choice over the other, you get different branches in the storyline. Then you will have if statements like the following:

if (game()==2)
{
if (gamea() == 2)
{
gameb1()
}
else if (gamea() == 3)
{
gameb2()
}
else if (gamea() == 4)
{
gameb3()
}
else if (gamea() == 5)
{
gameb4()
}
else if (gamea() == 6)
{
gameb5()
}

}

This gets really hard to read and code really fast.
I suggested a way of loading everything from dat files. I suggest you look at it if you haven''t.

-------------------------
(Gorgeous graphics)+(beautiful sound effects)+(symphonic music)+(no gameplay) != Good game

Share this post


Link to post
Share on other sites
Change

    
if(game()==2)
{
gamea();
}
if(gamea()==2)
{
gameb();
}

for this:

  
if(game()==2)
{
if(gamea()==2)
{
gameb();
}
}


when you call the function gamea(), it returns the number, but also execute what is inside of it, so it will print the couts in the screen. So, when you call gamea(); the computer will do it once, and when you call if(gamea()==2){}, it will do again, it will not reutilize the result from the first call.

EDIT - Sorry, it seems I was a little slow.

[edited by - algumacoisaqualquer on March 21, 2002 6:55:53 PM]

Share this post


Link to post
Share on other sites
Forget about .dat files at the moment. You need to walk before you can run. What I suggest you do is take a look at the following code, which you might be able to use as a framework (note: there are gaps in the code, but you should get the idea)...


    
#include <iostream>

#include <string>

#include <stdlib.h>

using namespace std;

enum rooms { START, HALL, YARD };
enum acions { QUIT, INVALID, NORTH, EAST, SOUTH, WEST };

int current_room = START;
bool run_game = true;

void display_description()
{
// This function just displays the description

// for the current room.

switch(current_room)
{
case START:
cout << "You are in a locked room full of stores...\n";
break;
case HALL:
cout << "Now you are in a hall...\n";
break;
case YARD:
cout << "You are now in the Prison yard...\n";
break;
default:
break;
}
}

int get_input()
{
// This function gets input from the player

// and sets a variable to indicate what action

// to take.

cout << "What do you want to do now?\n";

std::string action;
cin >> action;

if(tolower(action[0]) == 'n')
return NORTH;

if(tolower(action[0]) == 'e')
return EAST;

if(tolower(action[0]) == 's')
return SOUTH;

if(tolower(action[0]) == 'w')
return WEST;

if(tolower(action[0]) == 'q')
return QUIT;

return INVALID;
}

void move_player( int direction )
{
// This function allows the player to move

// between rooms, and includes the rules for

// what the valid moves are.

switch(current_room)
{
case START:
if(direction == NORTH)
{
cout << "You move north\n";
current_room = HALL;
}
else
{
cout << "You can't go in that direction.\n";
}
break;
case HALL:
if(direction == SOUTH)
{
cout << "You move south\n";
current_room = START;
}
else
{
cout << "You can't go in that direction.\n";
}
break;
case YARD:
break;
default:
break;
}
}

int main()
{
// The main game loop.

// Keep looping until the player quits.

while(run_game)
{
display_description();
int input = get_input();
if(input != QUIT)
move_player(input);
else
run_game = false;
}
}


This is a simple implementation of a state machine which I think is appropriate to your level of experience, and should help get the task done. There are various ways the code can be improved, but you can worry about that later. Your priority at the moment is to get something working, right?

Note that there is a main game loop, which displays a room description, awaits input from the user, and then carries out the user action. That's the basis of what you need to do. The move_player() function is the most interesting. It's the basis of a simple State Transition Machine, where it transitions from one state (or room) to the next, depending on the input from the user. So, if you go north from the start point, you end up in the hall. If you go east from the start point, you are told that you can't do that and the state stays the same.

Try compiling and running this code. Step through it in the debugger if it helps. Do you understanding what is happening?

[edited by - SabreMan on March 21, 2002 7:14:29 PM]

Share this post


Link to post
Share on other sites