# Seach Function to scan a area

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

## Recommended Posts

Hello everyone new here to the forum's. I have some question's. Well i have two
bool's they both are stated as an area in game.What i want to do is make
a search function, so i can search what's inside of differnt area's.
can check it out an see where im trying to
go with it.

sorry for not using the code tags, but it's not working right or im not getting it right.

 #include <iostream> #include <string> #include <cstdlib> #include <time.h> #include <vector> using namespace std; bool A1(); bool A2(); void search(); string direction; int main() { A1(); A2(); system("pause"); } bool A1() { while (NULL != 4) { cout << "Your in a cold dungeon\n"; cout << "> "; cin >> direction; cin.clear(); cin.ignore(INT_MAX,'\n'); cout << endl; if (direction == ("north")) { cout << "you follow the path north of you\n\n"; return 0; } if (direction == ("south")) { cout << "theres a rock blocking the path\n\n"; search(); } if (direction == ("back")) { return false; } } } bool A2() { while (NULL != 4) { cout << "you come to a intersection\n"; cout << "> "; cin >> direction; cin.clear(); cout << endl; if (direction == ("north")) { cout << "Wall\n"; search(); } if (direction == ("south")) { cout << "you seel a small hole, that you could crawl throu\n"; A1(); } if (direction == ("back")) { return false; } } } void search() { srand((unsigned)time(0)); vector<string> areas; areas.push_back("dark\n"); areas.push_back("light\n"); int areas_number = rand()%areas.size(); if(&A1) { cout << areas[areas_number]; } else if(&A2) { cout << areas[areas_number]; } } 

##### Share on other sites
The Preview Post button is your friend!

To use code tags, click the <> button. Between the tags, insert your code. When copying and pasting from MSVC, the forum will automatically insert a bunch of html tags (for color and font), so I just open my source file in notepad then copy and paste what is relevent.

First game? "Firsts" of anything are always confusing, and are always good learning experiences. What I've learned from my numerous attempts at creating games is to have a clear direction of what you want. Once you have that in mind, think about a way to implement it. It does NOT have to be the best way (you will probably waste a lot of time staring at your monitor contemplating the "best way"), just use what makes sense to you.

It looks like you are making a game where the player walks around the cells of a map, and in each cell there are objects the player can interact with. So think about that, ask yourself questions... go into detail for everything. How large is the map? Are there special characteristics about this map? Is this a combat zone? What sort of enemies are likely to show up here? What can a map cell contain? A description of the cell, perhaps. And objects? What sort of objects? Chests, switches, set pieces, locked doors... Can I move into this cell? Is this cell an exit for the current map? If so, what map will it take me to, and what cell will I start in? And so on...

So how could you possibly store all of this? Well you could manually program it by hand, having functions for every one of your map cells, and having a function for every possibility of interaction. Well... you could do that, but I think most of us would agree that keeping all of that data in a file would be a better idea: http://www.cplusplus...tutorial/files/
"How would I do that?" I don't know, what seems like a good idea to you?

"OK," you say, "but how would I keep track of this data in my program?"
Take a look here:
http://www.cplusplus...torial/classes/
You should read everything under "Object Oriented Programming" on that site. Classes are the basis of C++, after all.

If you can't see how you could use the above tools in your games, remember you are the programmer/designer/everything. Anything is possible.

Some tips on what you have:
1. Using NULL != 4 as a condition for anything is extremely error prone and is considered bad style. Just make a temporary bool and set it to false when you need to exit your loop or whatever.
2.
if(&A1) ... else if(&A2) 
All bad things. These if statements will always be true because you are retrieving the memory address of functions!
Check this out: http://www.cplusplus...rial/functions/ and this http://www.cplusplus...orial/pointers/ (especially the reference operator)

3. Your main() function is confusing. A good idea that the vast majority of games use is creating a "game loop", which usually consists of
while (playingGame == true) { getInput(); processInput(); showOutput(); }

I'm not the most social animal on Earth (and not even close to being the best programmer), so if I'm not clear on something let me know.

Good luck.

##### Share on other sites

The Preview Post button is your friend!

To use code tags, click the <> button. Between the tags, insert your code. When copying and pasting from MSVC, the forum will automatically insert a bunch of html tags (for color and font), so I just open my source file in notepad then copy and paste what is relevent.

First game? "Firsts" of anything are always confusing, and are always good learning experiences. What I've learned from my numerous attempts at creating games is to have a clear direction of what you want. Once you have that in mind, think about a way to implement it. It does NOT have to be the best way (you will probably waste a lot of time staring at your monitor contemplating the "best way"), just use what makes sense to you.

It looks like you are making a game where the player walks around the cells of a map, and in each cell there are objects the player can interact with. So think about that, ask yourself questions... go into detail for everything. How large is the map? Are there special characteristics about this map? Is this a combat zone? What sort of enemies are likely to show up here? What can a map cell contain? A description of the cell, perhaps. And objects? What sort of objects? Chests, switches, set pieces, locked doors... Can I move into this cell? Is this cell an exit for the current map? If so, what map will it take me to, and what cell will I start in? And so on...

So how could you possibly store all of this? Well you could manually program it by hand, having functions for every one of your map cells, and having a function for every possibility of interaction. Well... you could do that, but I think most of us would agree that keeping all of that data in a file would be a better idea: http://www.cplusplus...tutorial/files/
"How would I do that?" I don't know, what seems like a good idea to you?

"OK," you say, "but how would I keep track of this data in my program?"
Take a look here:
http://www.cplusplus...torial/classes/
You should read everything under "Object Oriented Programming" on that site. Classes are the basis of C++, after all.

If you can't see how you could use the above tools in your games, remember you are the programmer/designer/everything. Anything is possible.

Some tips on what you have:
1. Using NULL != 4 as a condition for anything is extremely error prone and is considered bad style. Just make a temporary bool and set it to false when you need to exit your loop or whatever.
2.
if(&A1) ... else if(&A2) 
All bad things. These if statements will always be true because you are retrieving the memory address of functions!
Check this out: http://www.cplusplus...rial/functions/ and this http://www.cplusplus...orial/pointers/ (especially the reference operator)

3. Your main() function is confusing. A good idea that the vast majority of games use is creating a "game loop", which usually consists of
while (playingGame == true) { getInput(); processInput(); showOutput(); }

I'm not the most social animal on Earth (and not even close to being the best programmer), so if I'm not clear on something let me know.

Good luck.

Well thank you monkey for the help. Right now im checking out those link's. Also i know the code is kinda sloppy.
I was just trying to make something quick just to do some testing.

Also how could i make a proper text parser? As you can see i have it set up so you can type in the direction.
Would that work like that or should i put them in some type of struct or maybe a 2d array?

##### Share on other sites
I was hoping that wasn't your actual program...

Storing a list of possible commands would be fine. Once you get to the meat and potatos of it usually a better way will bite you on the ass. For instance, are you going to force the player to type 'north' everytime? You could have a struct for each action that holds strings of aliases for that command.

##### Share on other sites

I was hoping that wasn't your actual program...

Storing a list of possible commands would be fine. Once you get to the meat and potatos of it usually a better way will bite you on the ass. For instance, are you going to force the player to type 'north' everytime? You could have a struct for each action that holds strings of aliases for that command.

Yes i would like it for the player to type in 'north' everytime. Right now in my game its based of picking a choice like 1,2,3.
So should i make a struct like this

 enum room{ NONE, A1, A2, A3 }; enum { NORTH, SOUTH, WEST, EAST }; struct room { char *name; int next[4]; } rooms[] ={ { "None"}, { "A1", {NONE, A2,A3,NONE}}, { "A2", {A1,A3,NONE,A1}}, { "A3", {A2,NONE,NONE,NONE}}, };

• 45
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631372
• Total Posts
2999627
×