Advertisement

Newbie question... don't laugh

Started by March 21, 2002 03:44 PM
5 comments, last by Mage_gr 22 years, 7 months ago
I tried to make a start for a text game and I have a problem.How do I make it not to end when my first question and answer are finished and to continue to the one about right or left.It is my first so many mistakes might be made.Point these mistakes to me please ok? #include <iostream.h> 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"; 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.Enter:"; cin>> first; switch(first) { case 1:cout<<"The door is made of titanium and has a really tuff lock.You 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 0; } 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 camer caught a glimpse of you.That was it the guards caught you.GAME OVER"; case 2:cout<<"Gongratulations.You are really good at this.An idiot must have left this way unguarde.You made your way out of the prison facility of the base."; default:cout<<"Wrong number"; } return 0; } return 0; }
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
In your main function you need to call your functions game() and gamea();

But, you need to do some checking to make sure they selected the ventilation system in game();

at the end of game(), you need to return your variable first instead of 0...then in main do something like this

if (game() == 2)
{
gamea();
}
Advertisement
Also, you should put your functions before your main()


  #include <iostream.h>int intro(){   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";}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.Enter:";   cin>> first;   switch(first)   {      case 1:cout<<"The door is made of titanium and has a really tuff lock.You 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 camer caught a glimpse of you.That was it the guards caught you.GAME OVER";      break;      case 2:cout<<"Gongratulations.You are really good at this.An idiot must have left this way unguarde.You made your way out of the prison facility of the base.";      break;      default:cout<<"Wrong number";   }   return second;}int main(){   // Run Intro   intro();   if(game() == 2)   {      gamea();   }   else   {      // Player didn''t make it past first part...Game Over      // do nothing.   }   return 0;}  
Well, looking at this code, I am wondering if it compiles.
Anyway, think about what you are telling the computer to do.
When it calls the game() function, it is going to return 0 no matter what choice you make.
Apparantly you have another function, gamea(), that should be called when you make the correct choice in game().
If you tell it to return, it will return.
You instead want to put something in the switch statement, like this:
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.Enter:";
cin>> first;
switch(first)
{
case 1:cout<<"The door is made of titanium and has a really tuff lock.You 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.";
gamea();
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 0;
}

Keep in mind that the way you are doing this game is not the best way. If this is your first project, it reminds me of mine. B-)
You'll quickly find that using a separate data file that holds the text and the choices possible and which choice is correct might be the best way to go. With this structure, you can even have a few different choices possible that will not end the game but will instead make different stories open up.
Imagine if every time you have to make a choice, a specific file is opened.
"Begin.dat" is the file that has the first room that looks like this:
You are in a locked room full of stores for the soldiers of the base.\n
What are you going to do?
CHOICES 3
CHOICE Try to force the door.
RESULT END "BeginResult1.dat"
CHOICE Try to go through the ventilation.
RESULT FILE "Next.dat"
CHOICE Pretend to be ill and call for the guard to check.
RESULT END "BeginResult1.dat"



How does the above work?
What if you had a function that basically opens files, reads out the beginning part, but when it got to the CHOICES tag in your .dat file, it then read in the number of choices?
Then you can have it read in the different choices using a for loop like so:
for (i = 0; i < Number_of_Choices; i++)
{
ReadChoice(); // this will read in choice i
}
And ReadChoice() will need to be able to read the rest of the line after the CHOICE tag.
After that, it will see RESULT and if it FILE, choosing this choice will not end the game but instead load the next room/situation from the file whose name is specified after the FILE tag. If it says END, it will also read in the name of the result's data file.
Why have it stored in a separate file? Well, if you choose one of "game over" choices, it can then cout the result of it before giving a game over screen and telling the program that the game is over.
That is one of the reasons your program is not running properly.
You call game() which just returns right away. The idea that you chose the right choice doesn't matter as it will always simply return no matter what choice you make.
With the way I described above, it is more flexible and once you get the program done, the game development will be simply making the .dat files!
You can add more tags if you want so that other things might happen, but that is up to you.

Hope this helps!

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

[edited by - GBGames on March 21, 2002 5:19:11 PM]
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Thaks for the help guys but being a newbie I didn''t understand the way GBGAMES explained it.Can you please make it clearer?
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Ok, let me try to clear up what I mean.
Imagine you make this game huge!
Let's say everytime you have to make a choice, you can call it a situation.

Now look at the gameplay. The game will basically do the same thing from start to end, right?
It prints out the situation you are in
It prints out the choices available.
It waits for the user to input one of the choices
Depending on that choice it does one of the following:
- goes on to the next situation
- ends the game

That means it is completely possible to make one function that handles every situation!

This is how real world programs are made. Have you ever played Mario Bros. and played Super Mario Bros?
Both made use of data driven design, although Super Mario Bros used it a lot more.
Mario Bros. only had different enemies on the same exact level each time. Super Mario Bros had different levels, graphcis, enemies, powerups, etc.
Quake III Arena doesn't have a separate function for every level it loads. In fact, it does everything based on the data that is loaded so that people who can make maps themselves will be able to provide their own maps. They can make their own models as well.
Starcraft has a map editor that is very powerful. Imagine that the original Starcraft game (in case you don't know...If you don't have it, I suggest you get it! It is a great real time strategy game) is just setup to say "If you destroy everyone else's buildings, you win".
With the map editor, it is possible to have games where the rules of winning are completely different. I found maps where the idea was to play a Starcraft version of Space Invaders, where you get a number of units at the bottom and you have to destroy units that fly from the top of the map down towards your base. I also saw one map that was like Starship Troopers, where the object is to destroy the Big Brain. There is even a Football game where the object is for your worker to bring the crystal to the other end of the field.

The point of all of these is to show that data driven games allow you to make more than hard coded games.

With the way you are doing it, imagine if you have 500 situations.
Now imagine if you want to completely change the story ( say for a sequel) where you are actually a student in school and you want to break out of detention, and for this you decide to keep the number of situations low, like 250.

The way you have it designed now, there will be 250 situations deleted, all of which look like the following:
if (gamea()==something)
{
if (gameb() == something)
{
.
.
.
if (gameaabbcc2() == something)
}
}


The way I am suggesting you do this game is as follows.

game();


That's it. Now when you want to make a sequel, you don't even have to change the original code at all.

How does game() work?
Well, while this will be more complicated, I will try to explain it slowly, since I understand you are completely new to this.

Instead of taking each situation and coding it into a function, you will make one function that does everything the other functions do.
So break it down again. What does this one function have to do that is the same that the other functions do?
1)It prints out the situation you are in
2)It prints out the choices available.
3)It waits for the user to input one of the choices
4)Depending on that choice it does one of the following:
- goes on to the next situation
- ends the game

So making game() is easy.
void game()
{
PrintSituation();
PrintChoices();
ProcessInput(GetUserInput());
}

So now we have game(). What is the problem with it currently?
There is so many ways that this could be done, but let's go through the code here for now.
PrintSituation() will have to actually print out the situation, such as:
"You are in a locked room full of stores for the soldiers of the base. What are you going to do?"
Of course, you want it to be able to know which situation to print. Perhaps this is where a nice game variable would come in handy:
int CurrentSituation
It will hold the value of the current situation. This means that 0 might refer to the first situation, 1 might refer to the next, 2 might refer to the next after that, etc.
Now you have two choices for this variable. You can declare it before main, and so make it global to the program (meaning that any function has access to it), or you can make it local to main and simply pass it to the functions. I would go for the second choice as it will make cleaner code and allow you to test things much more easily.

Of course, now all of the functions you declared will have to take an integer as a parameter. You will have to go through your code and fix this yourself.

So now let's look at the game() function again, with the above notes in mind:
So making game() is easy.
void game(int CurrentSituation)
{
PrintSituation(CurrentSituation);
PrintChoices(CurrentSituation);
CurrentSituation = ProcessInput(GetUserInput(CurrentSituation));
}

What does this do? When you call game(), you pass to it the value of the first situation.Normally this might be 0, but when you are testing things, you might want it to start in the middle of the game instead, so you can rewrite main so that when it calls game() it will pass 10 or 250 or whatever situation you want. This will allow you to see the way a new situation you added works with the game.

Anyway, it will first print out the situation of whatever the CurrentSituation is.
It will also print the choices out.
Then, it will change the current situation to whatever ProcessInput tells it to be, which will take whatever the user chooses as its own parameter.

If you don't understand that part, here is the code for that specific section:
CurrentSituation = ProcessInput(GetUserInput(CurrentSituation), CurrentSituation);

Ok, ProcessInput() is defined somewhat like this:

int ProcessInput(int UserChoice, int CurrentSituation)
{
Depending on the current situation, the choice might result
in the game either ending or moving a new situation
}

It will return a value that represents the situation that the choice will result in.
For instance, let's say you have four rooms. Each room has 2 doors, each leading to another room.
_______
| . | . | The dots are there because I can't get it to look
| 1 . 2 | correctly
|_._|_._|
| . | . |
| 3 . 4 |
|_._|_._|

It is possible to go from a room to another room, then go to another room, then go to another room, and then go back to where you started from.
You can see that there are four different situations you can be in, depending on whether you are in one of the four rooms.

So let's say you have 4 choices in each room:
Go up
Go down
Go left
Go right

Clearly, you can't do that in every room. Room 1 only allows you to go right and down while Room 4 allows you to go only up and left.
Of course if you made more rooms, there could conceivably be rooms that let you go in 3 or all 4 directions!
_______ _______
| . | . | . | . |
| 1 . 2 | 3 . 4 | Notice that rooms 3 and 4 do not have any
|_._|_._|_._|_._| way to enter them...perhaps making a choice
| . | . | . | . | in one room might lead to your situation
| 5 . 6 | 7 | 8 | (which at this point refers to which room
|_._|_._|_._|_._| you're in) to change to one of then?
| . | . | . | . | For instance, pulling a lever in room 16
| 9 . 10| 11 12| might result in ProcessInput() returning 3,
|_._|_._|_._|_._| so now the CurrenSituation is 3.
| . | . | . | . |
| 13. 14. 15 .16|
|_._|_._|_._|_._|

Think about this as well:
Choosing to GO RIGHT in Room 1 results in you being in Room 2.
Choosing to Go DOWN in Room 1 results in you being in Room 3.



What I was trying to explain to you above was that you should create a program that reads a data file instead of just what you coded.
Again, imagine a file that looks like this:


You are in a locked room full of stores for the soldiers of the base. What are you going to do?
CHOICES 3
CHOICE Try to force the door.
RESULT -1
CHOICE Try to go through the ventilation.
RESULT 2
CHOICE Pretend to be ill and call for the guard to check.
RESULT -1


You need to have a way of loading in a file depending on the CurrentSituation variable. This file has everything in it.
It tells the program how many choices there are (imagine not being limited to the requirement that there has to be 3 choices).
For each choice, there is the actual text of the choice and then the value that ProcessInput() should pass if that choice is chosen.
In this specific file, I am telling it that if the second choice is chosen (OR, in other words, if UserChoice() returns 2), then the CurrentSituation should change to 2 (or ProcessInput(), when told that CurrentSituation is 2 and that UserChoice() returns 2, will return 2). The -1s I put in to say that the game is over.
If you do it this way, you can set it up to test if CurrenSituation is -1. If it is, then exit the game() function, perhaps after loading in another dat file that has text representing the result of that choice (like GAME OVER or something).


Seriously, you can do so much if you just spend a little time at it.
I hope this helps a bit. Keep in mind that game(), the way I set it up, will return immediately after the first situation. This is because I didn't tell it to keep LOOPing until the game is over.


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

[edited by - GBGames on March 21, 2002 7:44:19 PM]
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Advertisement
Aight, I''m a newbie game programmer with a pretty good knowledge of C++ (basic OOP, functions, refrences, loops etc..) and for my first game I''m trying to make an RPG too, exept I''m making mine differently. I''m making a battle system first where you can choose attacks, run, use items and stuff like that. But, what I found pretty easy to do is use classes and header files. My main() function is about 3-5 lines long. All main() is there for is to call GameMain() and to exit the program. Even though Idon''t have much experience, heres a suggestion: add a little randomness to it. It might take a little more coding, but I think it would make you''re game a little better, so it doesn''t seem like the same thing happens over and over again, and add a little replay value to it. If anyone thinks I''m wrong, it''s a stupid idea, or I''m just a ranting newbie, just make a reply and say what I did wrong.
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>

This topic is closed to new replies.

Advertisement