Page 1 of 2

Development Topic: Text Game

Posted: Sun Jun 07, 2009 6:52 am
by Martijn
Hi thanks for reading.
"Development Topic: Text Game" it sounds fancy, but it's not.
I'm kinda stuck atm, because my program doesn't do what I want it to do.

Here is what I got so far;

Code: Select all

#include <iostream>

using namespace std;
//character
string nickname;  
string skills;
string ShowSkills;
string ShowItems;

string items;
int gold;

//game
string input; 
string direction; 
static string place_name = "Place Name"; // so it doesnt resets
string description;
string exits;

// pos
int x = 0;
int y = 0;

// static


//bool running = true;

void position();
void cityhall();

int main()
{
  
    cout << "What's your name adventurer? ";
    cin >> nickname;
    
    system("CLS");
    cout << "Welcome to the world of err... pizza....." << nickname << endl;
    cout << "start your journey now, type start to continue." << endl;
    
    cout << "\n\n" << endl; // spacer
    cin >> input;
        
    if (input == "start"){
       system("CLS");    
    }
    else
    {
        cout << "Error, please type start. " << endl;
        system("pause");
    }
    while(input != "exit") {
          
        position(); 
          
        cin >> input;
        if (input == "n" || input == "north"){ y += 1; }
        if (input == "e" || input == "east") { x += 1; }
        if (input == "s" || input == "south"){ y -= 1; }
        if (input == "w" || input == "west") { x -= 1; }

        if (input == "!skills") { ShowSkills; }
        if (input == "!items") { ShowItems; }                
                        }
                         }
void position()
{
     cout << "[[" << place_name << "]] <" << x << ", " << y << ">" << endl;
     cout << description << endl;
         cout << "\n\n" << endl; // spacer   
}


// City Hall
void cityhall()
{
     x = 0;
     y = 0;
     exits = "n";
     place_name = "City Hall";
     cout << "This is the central gathering room. The room is fairly " << endl;
     cout << "empty, except for a large table in center of the room." << endl;
     cout << "There is an exit to the north that leads out into town." << endl;
}
// Main Street
void mainstreet()
{ 
     x = 0;
     y = 1;
     exits = "w" "e";
     place_name =  "Main Street";
     cout << "This is the main street, there is a big statue of the king " << endl;
     cout << "in the middle of the street. The cobbeld pavement continues " << endl;
     cout << "to the east and west. " << endl;
}         

/*
// Stats and Skills
int Health = 100;
int Mana = 20;

int level = 1;
void ShowSkills()
{
     cout << "You've currently got:" << endl;
     cout << "You got: " << Health << " HP points." << endl;
     cout << "You got: " << Mana   << " MP points."<< endl;
     
     cout << "\n\n" << endl;
     cout << "You are currently level " << level << "." << endl;

}

void ShowItems()
{
     cout << "You got " << gold << " gold pieces." << endl; 
     cout << "You are carrying:" << endl;
     cout << items << endl;
}
*/

Please ignore the complete stats and skills part!
Well, I want the program to start and output

"[[City Hall]] <0, 0>"

but it outputs

"[[Place name]] <0, 0>"

when I "update" it (I press N) it does work, but not at the start!


It's hard to explain, but basically the just displays "position" while it should display "city hall" with the template of "position" (the [[name]] etc).

Re: Development Topic: Text Game

Posted: Sun Jun 07, 2009 9:06 am
by programmerinprogress
either i'm going blind or you haven't called the CityHall() function, and just the position one, therefore it looks to me like you're not actually "telling" the program to assign the value "City Hall" to the string place_name

also, to simplify things, you could write the CityHall() and MainStreet() functions into one function like this:

Code: Select all

 SetLocation(int x, int y, string name, string message);
//and you could follow this by changing position to something like 
GetPosition() 
//to make things clearer


But honestly, I don't see how the place_name would change at all, because I don't see an instance where you call it in the program, but correct me if i'm wrong.

Re: Development Topic: Text Game

Posted: Sun Jun 07, 2009 9:06 am
by Lucas
Edit: beaten to it xD

The problem is that you aren't giving place_name a value until you call the cityHall() function, so you could either call the function before position() to set the place_name (But it will cout all of the other stuff in the function aswell) or you could always just try passing your position function an argument something like this.

Code: Select all

void position(string place_name);

int main() {
    //stuff
     position("City Hall");
    //stuff
}

void position(string place_name)
{
     cout << "[[" << place_name << "]] <" << x << ", " << y << ">" << endl;
     cout << description << endl;
     cout << "\n\n" << endl; // spacer
}
It's not the best way by far but it would solve your problem.

Re: Development Topic: Text Game

Posted: Sun Jun 07, 2009 10:17 am
by programmerinprogress
static string place_name = "Place Name"; // so it doesnt resets
Just out of curiosity, why do you think you need to declare this static? I don't see any purpose for it...

Re: Development Topic: Text Game

Posted: Sun Jun 07, 2009 12:20 pm
by Martijn
Wow thanks guys, that's a great idea! :O
I would have never thought of it, I want to test it out right away but my compiler is doing weird.
This the current code:

Code: Select all

#include <iostream>

using namespace std;

//character
int Health = 100;
int Mana = 20;

string nickname;  
string skills;
string ShowSkills;
string ShowItems;

string items;
int gold;

//game
string input; 
string direction; 
static string place_name = "Place Name"; // so it doesnt resets
string description;
string exits;

// pos
int x = 0;
int y = 0;

// static


//bool running = true;

void position(string place_name);
void cityhall();

int main()
{
  
    cout << "What's your name adventurer? ";
    cin >> nickname;
    
    system("CLS");
    cout << "Welcome to the world of err... pizza....." << nickname << endl;
    cout << "start your journey now, type start to continue." << endl;
    
    cout << "\n" << endl; // spacer
    cin >> input;
        
    if (input == "start"){
       system("CLS");    
    }
    else
    {
        cout << "Error, please type start. " << endl;
        system("pause");
    }
    while(input != "exit") {
          
        position("City Hall"); 
          
        cin >> input;
        if (input == "n" || input == "north"){ y += 1; }
        if (input == "e" || input == "east") { x += 1; }
        if (input == "s" || input == "south"){ y -= 1; }
        if (input == "w" || input == "west") { x -= 1; }

        if (input == "!skills") { ShowSkills; }
        if (input == "!items") { ShowItems; }                
                        }
                         }
void position(string place_name);
{
     cout << "[[" << place_name << "]] <" << x << ", " << y << ">" << endl;
     cout << description << endl;
         cout << "\n\n" << endl; // spacer 
     cout << "<" << Health << "hp " << Mana << "mp>" << endl;  
}


// City Hall
void cityhall()
{
     x = 0;
     y = 0;
     exits = "n";
     place_name = "City Hall";
     cout << "This is the central gathering room. The room is fairly " << endl;
     cout << "empty, except for a large table in center of the room." << endl;
     cout << "There is an exit to the north that leads out into town." << endl;
}
// Main Street
void mainstreet()
{ 
     x = 0;
     y = 1;
     exits = "w" "e";
     place_name =  "Main Street";
     cout << "This is the main street, there is a big statue of the king " << endl;
     cout << "in the middle of the street. The cobbeld pavement continues " << endl;
     cout << "to the east and west. " << endl;
}         


// Stats and Skills
/*
int level = 1;
void ShowSkills()
{
     cout << "You've currently got:" << endl;
     cout << "You got: " << Health << " HP points." << endl;
     cout << "You got: " << Mana   << " MP points."<< endl;
     
     cout << "\n\n" << endl;
     cout << "You are currently level " << level << "." << endl;

}

void ShowItems()
{
     cout << "You got " << gold << " gold pieces." << endl; 
     cout << "You are carrying:" << endl;
     cout << items << endl;
}
*/
And this, is the error:
C:\Documents and Settings\Admin\Bureaublad\text game\main.cpp:72: error: expected unqualified-id before '{' token
C:\Documents and Settings\Admin\Bureaublad\text game\main.cpp:72: error: expected `,' or `;' before '{' token
No matter how many ; I place (semi-colon, right?) it still doesn't compile.

Oh and I have been told by a friend to try and make it static, as he thought it would reset itself somehow.
Let me... just find the MSN log... there we go...
try to make the place_name static
so other funcs can reach it and the data doesn't reset
when you say its = "City hall" in the function. it will only be like that inside the function, and not outside

Re: Development Topic: Text Game

Posted: Sun Jun 07, 2009 12:50 pm
by programmerinprogress
You put a semicolon after the start of the definition of "position(string)" it will compile if you remove that.

Code: Select all

void position(string place_name);
{
     cout << "[[" << place_name << "]] <" << x << ", " << y << ">" << endl;
     cout << description << endl;
         cout << "\n\n" << endl; // spacer
     cout << "<" << Health << "hp " << Mana << "mp>" << endl;
}

needs to become 

void position(string place_name) // semicolon gone! compiles now :-)
{
     cout << "[[" << place_name << "]] <" << x << ", " << y << ">" << endl;
     cout << description << endl;
         cout << "\n\n" << endl; // spacer
     cout << "<" << Health << "hp " << Mana << "mp>" << endl;
}
Also, i'm going to say that the keyword static doesn't need to be there at all, anyone can correct me if i'm wrong, but I though the term static used with a non-class variable just stopped the variables from being accessed from code in other files, it controls scope as far as i'm aware.

*grabs big old C++ reference*
from book "Global Static variables: applying the specifier static to a global variable instructs the compiler to create a global variable only to the file in which you declared it"

as far as i'm aware, what you said only concerns local static variables, because it persits each time the function is called. (But global variables do that anyway!)

Re: Development Topic: Text Game

Posted: Sun Jun 07, 2009 1:37 pm
by Martijn
Oh right! :D.
Sweet it works :).

Been messing with that for days really! Now, I'm going to try to make the walking work :p.
Wish me luck xD

Re: Development Topic: Text Game

Posted: Wed Jun 10, 2009 2:01 am
by Martijn
Still can't fix the map loading etc, can someone push me into the right direction... again?

Re: Development Topic: Text Game

Posted: Wed Jun 10, 2009 3:42 am
by Lucas
Martijn wrote:Still can't fix the map loading etc, can someone push me into the right direction... again?
What exactly is your problem with "map loading etc"?

Re: Development Topic: Text Game

Posted: Wed Jun 10, 2009 2:49 pm
by Martijn
Oh sorry, well I want to type "n" and then it should show the info of "Main Street" and have 2 exits to E and W, so you can "walk" also i would like to know how I can put the display 4 lines of code as "description" (eq:)

describtion = "
cout << "This is the main street, there is a big statue of the king " << endl;
cout << "in the middle of the street. The cobbeld pavement continues " << endl;
cout << "to the east and west. " << endl; "

Re: Development Topic: Text Game

Posted: Wed Jun 10, 2009 5:08 pm
by dandymcgee
Martijn wrote:Oh sorry, well I want to type "n" and then it should show the info of "Main Street" and have 2 exits to E and W, so you can "walk" also i would like to know how I can put the display 4 lines of code as "description" (eq:)

describtion = "
cout << "This is the main street, there is a big statue of the king " << endl;
cout << "in the middle of the street. The cobbeld pavement continues " << endl;
cout << "to the east and west. " << endl; "
This code would be the single string equivalent:

Code: Select all

string description = "This is the main street, there is a big statue of the king \nin the middle of the street. The cobbeld pavement continues \nto the east and west. \n";
cout << description;
Where "\n" means newline character (does the same thing as " << endl << ").

Re: Development Topic: Text Game

Posted: Thu Jun 11, 2009 12:27 pm
by MarauderIIC
dandymcgee wrote:Where "\n" means newline character (does the same thing as " << endl << ").
Technically no. endl forces the output buffer to be written to the screen, \n does not. So if you use endl at the end of every line and your CPU is at like 100%, you'll see one line printed at a time. If you use \n and then endl or cout.flush() on the last line, you'll see all the lines written to the screen at once. In both of these scenarios, you might get part of a line printed as the system flushes the output buffer on its own. As a side effect, endl is "slower" -- this might be most noticeable during file I/O (that's something I just now thought of :P)

Re: Development Topic: Text Game

Posted: Thu Jun 11, 2009 4:01 pm
by dandymcgee
MarauderIIC wrote:
dandymcgee wrote:Where "\n" means newline character (does the same thing as " << endl << ").
Technically no. endl forces the output buffer to be written to the screen, \n does not. So if you use endl at the end of every line and your CPU is at like 100%, you'll see one line printed at a time. If you use \n and then endl or cout.flush() on the last line, you'll see all the lines written to the screen at once. In both of these scenarios, you might get part of a line printed as the system flushes the output buffer on its own. As a side effect, endl is "slower" -- this might be most noticeable during file I/O (that's something I just now thought of :P)
Thanks for that correction. In a beginner's eyes they still cause the same end result (wrapping text), but you're absolutely right in saying using \n does not cause anything to be written at the moment it is read.

Re: Development Topic: Text Game

Posted: Fri Jun 12, 2009 4:44 am
by Martijn
Argh this is driving me nuts :(.

Code: Select all

#include <iostream>

using namespace std;

//character
int Health = 100;
int Mana = 20;

string nickname;  
string skills;
string ShowSkills;
string ShowItems;

string items;
int gold;

//game
string input; 
string direction; 
static string place_name = "Place Name"; // so it doesnt resets
string description;
string exits;

// pos
int x = 0;
int y = 0;

// static


//bool running = true;

void position(string place_name);
void cityhall();

int main()
{
  
    cout << "What's your name adventurer? ";
    cin >> nickname;
    
    system("CLS");
    cout << "Welcome to the world of err... pizza....." << nickname << endl;
    cout << "start your journey now, type start to continue." << endl;
    
    cout << "\n" << endl; // spacer
    cin >> input;
        
    if (input == "start"){
       system("CLS");    
    }
    else
    {
        cout << "Error, please type start. " << endl;
        system("pause");
    }
    while(input != "exit") {
          
        position("City Hall"); 
          
        cin >> input;
        if (input == "n" || input == "north"){ y += 1; }
        if (input == "e" || input == "east") { x += 1; }
        if (input == "s" || input == "south"){ y -= 1; }
        if (input == "w" || input == "west") { x -= 1; }

        if (input == "!skills") { ShowSkills; }
        if (input == "!items") { ShowItems; }                
                        }
                         }
void position(string place_name)
{
     cout << "[[" << place_name << "]] <" << x << ", " << y << ">" << endl;
     cout << description << endl;
         cout << "\n\n" << endl; // spacer 
     cout << "<" << Health << "hp " << Mana << "mp>" << endl; 
}


// City Hall
void cityhall()
{
     x = 0;
     y = 0;
     exits = "n";
     place_name = "City Hall";
     description = "This is the central gathering room. The room is fairly \n";
     "empty, except for a large table in center of the room.\n";
     "There is an exit to the north that leads out into town.\n";
}
// Main Street
void mainstreet()
{ 
     x = 0;
     y = 1;
     exits = "w" "e";
     place_name  =  "Main Street";
     description = "This is the main street, there is a big statue of the king \n" ;
     "in the middle of the street. The cobbeld pavement continues \n";
     "to the east and west. \n";
}         


// Stats and Skills
/* comment for now
int level = 1;
void ShowSkills()
{
     cout << "You've currently got:" << endl;
     cout << "You got: " << Health << " HP points." << endl;
     cout << "You got: " << Mana   << " MP points."<< endl;
     
     cout << "\n\n" << endl;
     cout << "You are currently level " << level << "." << endl;

}

void ShowItems()
{
     cout << "You got " << gold << " gold pieces." << endl; 
     cout << "You are carrying:" << endl;
     cout << items << endl;
}
*/
I don't understand why it just doesn't output description, and if I type north output mainstreet :/.

Re: Development Topic: Text Game

Posted: Fri Jun 12, 2009 6:55 am
by Lucas
I'm not quite sure which part of your code calls the functions to display this information anyway.
The only thing I can think of is that you are expecting your function to check the x, y variables when you change them by typing N or North, but it just doesn't work like that.

You need to do some additional checks after you have updated your x, y variables.

Again this isn't by far the best way to do things but it would once again solve your problem and hopefully you will see where you are going wrong.

Code: Select all

position("City Hall");  //We're putting this function before the game loop otherwise every time you loop back to the beginning it will display the original message
while(input != "exit") {
	cin >> input;
	
	//Check player input
	if (input == "n" || input == "north") { y += 1; }
	if (input == "e" || input == "east") { x += 1; }
	if (input == "s" || input == "south") { y -= 1; }
	if (input == "w" || input == "west") { x -= 1; }
	if (input == "!skills") { ShowSkills; }
	if (input == "!items") { ShowItems; }
	
	//Check our new co-ordinates
	if(x == 0 && y == 0) {
		cityhall();
	}
	else if(x == 0 && y == 1) {
		mainstreet();
	}
	else {
		cout << "You new co-ordinates are: " << x << ", " << y << endl;
	}
}
This also makes the x, y variables in the functions completely redundant so you may as well get rid of them.

Basically within a game loop you need to check all of the logic otherwise nothing will happen.
You checked for the players input but after that you didn't do anything to check the position or do anything with it, so the function mainstreet() was never called.

I didn't test the above code since I am at work so hopefully I haven't made any syntax mistakes and things.