Is there a simpler way for me to make this?

it just takes a long time to write all of the if statements that my probalam.

#include <string>
#include <iostream>
#include <ostream>
#include <cstdlib>
#include <iomanip>


using namespace std;

string space=" ";
string name;


int main(){
char name [16]; //START OF DIAOLOGE
cout << "Warning you are changing your name... please enter your name wisely here:"+ space;
cin >> name;
cout << "your name is now:"+ space << name << endl;
system("pause");
cout << "Thank you for changing your name at changeroo 5000 if you ever want it back it\nwill cost 5000 coins"+ space << name << endl;
system ("pause");
cout << "'some time later, lets say 5 years'" << endl;
system("pause");
cout << "lets begin our journey shall we"+ space + name + "?" << endl;
system ("pause");
cout << "since you've got this far i think you might be up to the task of helping me out a bit will yah'?\nwhat i need you to do is find the changeroo 5000 and report to me where it is." << endl;
system ("pause");
cout << "well since you didn't say no lets go,you walk north into the woods." << endl;
system ("pause");
cout << " What do you do?" << endl;
system ("pause"); //END OF DIAOLOGE
cout << " go 1=north, 2=south, 3=east, or 4=west?" << endl; //START OF CHOICES
int n;
cin >> n;
if(n == 1){
cout<<"Going north led you into woods." << endl; //NORTH CHOICE
system("pause");
cout<<" after going north you encounter a giant bear, what do you do?" << endl;
system("pause");
cout<<" you can 1=Attck with fists, 2=try and run away, or 3=wait and hope it won't see you." << endl;
int Bear;
cin>>Bear;
if(Bear == 1){ //PART 1 OF BEAR CHOICE (NORTH CHOICE)
cout<<" sorry you died, GAME OVER"<<endl;
system("pause");
}
if(Bear == 2){ //PART 2 OF BEAR CHOICE (NORTH CHOICE)
cout<<"you ran away and the Giant bear didn't see you."<<endl;
system("pause");
cout<<"after the run you feel very exhausted."<<endl;
system("pause");
cout<<"you can either 1=try and find water or food, or 2=stay and help comes soon."<<endl;
int help;
cin>>help;
if(help == 1){ //PART 2.1 OF BEAR CHOICE (NORTH CHOICE)
cout<<"you are unsuccessful and pass out."<<endl;
system("pause");
cout<<"sometime later you wake up to find youre in a giant pit with no way out.\nyou slowly starve to death, GAME OVER."<<endl;
if(help == 2){ //PART 2.2 OF BEAR CHOICE (NORTH CHOICE)
cout<<"sometime late a hunter finds you resting on the ground.\nhe carries you to his little hut in the woods."<<endl;
}
}
if(Bear == 3){ //PART 3 OF BEAR CHOICE (NORTH CHOICE)
cout<<"you got lucky the bear didn't see you and it ran away."<<endl;
system("pause");
}
}
if(n == 2){ //SOUTH CHOICE
cout <<"Going south led you to a frozen lake." << endl;
system("pause");
}
if(n == 3){ //EAST CHOICE
cout <<"hello you went east" << endl;
system("pause");
}
if(n == 4){ //WEST CHOICE
cout <<"hello you went west" <<endl;
system("pause");
}





return 0;
}


Looks like you are doing "code-driven" development. What it basically means is that you are hard-coding out all the paths by use of if statements and letting the current point of execution determine where you are in the game.

This is generally a bad way to go.

A better way to go is "data-driven" development, which means you write very generic code that works in all situations and use variables/data to determine where you are in the game.

I went over this in more detail in another thread. Here's a link:

http://www.cplusplus.com/forum/beginner/71141/#msg379639
Ok thank you, im a very very big noob in c++, also im only 13.

well i dont really understand your link there sorry im only a little bit through my c++ book.My uncle hasnt taught me that much either yet.
Last edited on
I am working on a game which, while very different from your game, at least uses some sort of 'map structure' with randomly generated rooms and unique x- and y-coordinates for every room.

While the game is in the alpha and the code for even the generator is not yet completed (I can generate the coordinates, but since every generated 'map' has to comply with certain rules (like that every open room should be reachable from start- and end-rooms A and B), it is far from done) the idea might be useful to you.

I am most certainly still a beginner, like yourself, but perhaps explaining my idea and what I programmed would serve to teach both of us.
Last edited on
That helped a little bit i kind of get what you mean by the randomly generated rooms and cords, but my problem is i don't what the words are to type out this stuff, like i know what things do if i can see them in action know what im saying?
It might be a good idea to take some more time to type out your questions. I hope that I understood correctly that you would hope to learn more from looking at some sort of code and going from there. So here you go; This is my code to generate a grid of coordinates, easily modified to add information to each coordinate by modified the 'room' structure.

There is no actual randomness in this map yet! Blame it on the fact that I am a beginning programmer and that I have been programming other things first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// This is a work-in-process procedural map generator for an idea of mine.
// For now, I am generating the co-ordinates of the map and then display them.
// With the 'room' structure in place I should be able to create larger and more detailed packages for the rooms which form the 'building blocks' of the map.

#include <iostream>
#include <conio.h>
#include <fstream>
#include <windows.h>
#include <stdlib.h>

#define PAUSE _getch() // This line of code will ask the user for input, and upon getting a character (which is then technically entered into no existing variable) continues running. Useful for debugging.

using namespace std;

struct room
{
    double id;
    int x;
    int y;
	char displaysymbol;
};

int mapxsize;
int mapysize;
room * map; // We designate a dynamic array of type 'room'.

room roomgenerator (int roomx, int roomy);
void mapdisplay (room *map);
int randomnumber (int range_min, int range_max);

int main()
{
    cout << "NP map generator\n\n";
    cout << "Please enter the intended x- and y-sizes for the map.\nThe maximum size is 20x20 tiles.\n\n";

    do //The program checks whether the suggested x-size is within the allowed limits.
    {
        cout << "Size (x): ";
        cin >> mapxsize;
        if (mapxsize > 20 || mapxsize < 1)
        {
            cout << "Please type a valid value between 0 and 21!\n";
        }
    } while (mapxsize > 20 || mapxsize < 1);

    do //The program checks whether the suggested y-size is within the allowed limits.
    {
        cout << "Size (y): ";
        cin >> mapysize;
        if (mapysize > 20 || mapysize < 1)
        {
            cout << "Please type a valid value between 0 and 21!\n";
        }
    } while (mapysize > 20 || mapysize < 1);
    map = new (nothrow) room [mapxsize*100+mapysize]; // I create a dynamic memory unit here. Note that it is not completely efficient with space. This is because of the way I allocate the rooms to their own numbers.
    if (map == 0)
    {
    cout << "Errorcode MG01: Failure assigning memory.";
    exit (EXIT_FAILURE);
    PAUSE;
    }
    cout << "\n";
    int roomx;
    int roomy;
    for (roomx=1; roomx<=mapxsize; roomx++) // I run the roomgenerator once for every room to be generated.
    {   for (roomy=1; roomy<=mapysize; roomy++)
        {
            roomgenerator (roomx,roomy);
        }
    }
    cout << "\n\nMapdisplay will now run.";
    PAUSE;
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; // Sloppy code to 'clear' the console. I will later replace it, but for practice and debugging purposes, it is alright.
    mapdisplay (map);
    PAUSE;
	delete map;
    return 0;
}

room roomgenerator (int roomx, int roomy)
{
    map [(roomx-1)*100+(roomy-1)].id = roomx*100+roomy; // In the 'map' array, we generate a room with a number that is 100*x + Y. For the map with coordinates x = 10 and y = 15, for instance, we would get number '1015'. This is also its slot in the array.
    map [(roomx-1)*100+(roomy-1)].x = roomx;
    map [(roomx-1)*100+(roomy-1)].y = roomy;
	map [(roomx-1)*100+(roomy-1)].displaysymbol = '+';
    cout << "Room number " << map [(roomx-1)*100+(roomy-1)].id << "\n";
    cout << "x = " << map [(roomx-1)*100+(roomy-1)].x << " y = " << map [(roomx-1)*100+(roomy-1)].y << "\n\n";
    return map [(roomx-1)*100+(roomy-1)];
}

void mapdisplay (room *map) // This code will be expanded along with the variables contained in the 'room' structure and then ported into a different part of the final program.
/* The mapdisplay displays the cahracter assigned to every room. For now, that's just plusses. */
{
	cout << "This is the grid of coordinates:\n\n\n\n";
    for (int roomx = 1; roomx<=mapxsize; roomx++)
	{	
		cout << "\n";
        for (int roomy = 1; roomy<=mapysize; roomy++)
        {
            cout << map [(roomx-1)*100+(roomy-1)].displaysymbol;
        }
    }
}

int randomnumber (int range_min, int range_max)
{
	int result = 0;
	cout << "Hello! I am a function that is not called yet because I do absolutely nothing. I serve as a placeholder until my beginning programmer learns how to actually generate a random number! Then I will be used for all sorts of things!";
	cout << "\nI am so excited! What if I will be used to generate random types of rooms and contents and all other kinds of information?! And what if the number I return can be modified for all sorts of different functions?! Wow!";
	return result;
}


It's a little too big for the forum, I guess, but whatever- You should be able to read it.
I like the way you did all that but most of that im lost, ME NOOB.
Have you tried following the C++ language tutorial?

http://www.cplusplus.com/doc/tutorial/

I'm a 'noob', like yourself. That you are trying this at your age says something about your will to learn, at least. It is a good idea to follow the tutorial, step by step, and code little things every time you see something new.

"So the chapter I just read is about variables.. Let's see how I can use those!"

Only when you fully understand a chapter, continue to the next one. Take it easy, give yourself time to think, and read the examples in the tutorial.

If you have any questions, then follow the format as stated on this forum to ask good and coherent questions.

(All the above makes up for a total of 666 characters. I knew that I was demonic, somehow!)

If, at any moment, you think "hey, this works!", then don't rest there. Only continue when you can say; "This works because of.. [INSERT REASON HERE]."

Programming is not 'writing some lines, some magic happens, and then the program works'. Never pause until you know the magic, and never blindly copy anything until you understand it.

You can cannibalize my code for the part which generates the coordinate grid, if you wish, and use that. But don't blindly copy. Try to understand it.
Last edited on
Topic archived. No new replies allowed.