Page 3 of 4

Re: Where to start...I'm the new guy btw

Posted: Mon Apr 27, 2009 3:10 pm
by MarauderIIC
Which part,

Code: Select all

class Room {
    Room* exits[NUM_EXITS];
};
?

Re: Where to start...I'm the new guy btw

Posted: Mon Apr 27, 2009 3:57 pm
by Terranova3y2
I made a mistake...

My Rooms function is in its own file, not its own class I mixed them up.

I had a look at some material for classes and I wonder if it would be overcomplicated for a text game like this? Maybe I'm not seeing the importance of it yet.

My new book should be here tomorrow so I'll be able to read about it in better detail.

A random fact about the game now: I compiled it to check memory usage of the exe - 3mb of memory on it...I guess I should really read up on pointers.

Re: Where to start...I'm the new guy btw

Posted: Mon Apr 27, 2009 6:24 pm
by MarauderIIC
Terranova3y2 wrote:I had a look at some material for classes and I wonder if it would be overcomplicated for a text game like this?
No.

Re: Where to start...I'm the new guy btw

Posted: Tue Apr 28, 2009 7:47 pm
by eatcomics
Martyj wrote:I went the college route and learned java first. I'll be doing c++ this summer. Tbh learning java has helped me with more than just understanding java. I can read some c++ (can't write it though) can do php. Also it helps you understand the computer more. I'd recommend starting on java because it's a good language. It's not platform dependent, if you program on a mac you can use a java program on windows. I would not recommend using tutorials that just go through the language by showing you programs. Id recommend a book that goes over all of java.

What's nice about java is you get an api to use. The sun corp provides you with everything you would ever need and more.

Another thing that helps is a good IDE.

Here is the book I used for java
http://www.amazon.com/Building-Java-Pro ... 565&sr=8-1

That's my suggestion though, start with java.
I myself have been giving sideways glances at java, I might check out said book...

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 5:06 pm
by Terranova3y2
Well, I think have the majority of everything I'll need for my game done.

I have my health system working now, its very basic

Code: Select all

int playerHP = 5; // Declares starting health
I set a function to check your HP each time you make a wrong move

Code: Select all

int checkHP()
{
	if (playerHP <= 0)
	{
		cout << "After taking too big a beating,\n";
		cout << "you drop dead on the floor...\n";
		cout << "GAME OVER!";
		cin.ignore();
		cin.get();
		exit(0);
	}
The reason it's only being added in now is because I couldn't figure out a way to go back to the screen before if you made the wrong choice.

Now I'm using recursion, I didn't think you could call a function inside itself, but now I do :P

Code: Select all

SecondScreen()
{
...

if (playerInput == menuArray[1])
	{
		--playerHP;
		cout << "\nAfter searching for hours you find nothing.\n";
		cout << "During the search you fell on your face and\n";
		cout << "lose 1 point of health.\n";
		cout << "\nPress any key to continue.\n";
		cin.ignore();
		cin.get();
		checkHP();
		SecondScreen();
	}
I've read a bit more on classes and I'm trying to figure out a way to use it in this game so thats the next step.

I'll eventually make the game longer, I'm more worried about the code rather than storyline for the time being.

http://www.megaupload.com/?d=W8P57DYV
Heres an exe of the game (Don't forget to check the About section on the main menu)

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 6:09 pm
by MarauderIIC
If your recursion occurs only at the end of the function, use a loop instead. It's called tail-end recursion and is fully removable. For instance, your function, assuming that the ...'s are nothing (I'm sure they're not and are totally relevant to this loop's condition, but I have no way of telling what they are), might be more like

Code: Select all

SecondScreen() {
while (playerInput == menuArray[1])
   {
      --playerHP;
      cout << "\nAfter searching for hours you find nothing.\n";
      cout << "During the search you fell on your face and\n";
      cout << "lose 1 point of health.\n";
      cout << "\nPress any key to continue.\n";
      cin.ignore();
      cin.get();
      checkHP();
   }
}
Of course you'd have to get the player's input again somewhere in there, but...

Also, use cin.sync() instead of cin.ignore() if you want to remove everything on the input buffer.

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 6:33 pm
by Terranova3y2
I'm not sure if that would work with the way I've set it up

I'll show the complete function for you to see.

Code: Select all

int SecondScreen()
{
	cout << "##########################################\n";
	cout << "#                                        #\n";
	cout << "# You open your eyes, very groggy        #\n";
	cout << "# and you have no idea where you are.    #\n";
	cout << "# It is very dark, the only light source #\n";
	cout << "# is coming from a window.               #\n";
	cout << "#                                        #\n";
	cout << "##########################################\n";
	cout << "#                                        #\n";
	cout << "# Your HP: " << playerHP << "                             #\n";
	cout << "#                                        #\n";
	cout << "##########################################\n";
	cout << "\n";
	cout << "1.) Look out the window?\n";
	cout << "2.) Look around some more?\n\n";
	cin >> playerInput;
	if (playerInput == menuArray[0]) // Tests for players input
	{
		ThirdScreen();
	}
	else
		if (playerInput == menuArray[1])
	{
		--playerHP;
		cout << "\nAfter searching for hours you find nothing.\n";
		cout << "During the search you fell on your face and\n";
		cout << "lose 1 point of health.\n";
		cin.ignore();
		cin.get();
		checkHP();
		SecondScreen();
	}
I'd still have to call the function inside itself to go back to that screen even with the while loop right?

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 6:42 pm
by MarauderIIC
I did a replace all (seven spaces) with (tab). That might have screwed up the room description.
This function looks like it should be void, not int.

Code: Select all

    void SecondScreen()
	{
	do { //while playerInput == menuArray[1]
		cout << "##########################################\n";
		cout << "#					     #\n";
		cout << "# You open your eyes, very groggy	 #\n";
		cout << "# and you have no idea where you are.    #\n";
		cout << "# It is very dark, the only light source #\n";
		cout << "# is coming from a window.		 #\n";
		cout << "#					     #\n";
		cout << "##########################################\n";
		cout << "#					     #\n";
		cout << "# Your HP: " << playerHP << "				 #\n";
		cout << "#					     #\n";
		cout << "##########################################\n";
		cout << "\n";
		cout << "1.) Look out the window?\n";
		cout << "2.) Look around some more?\n\n";
		cin >> playerInput;
		if (playerInput == menuArray[0]) // Tests for players input
		{
			ThirdScreen();
		}
		else if (playerInput == menuArray[1])
		{
			--playerHP;
			cout << "\nAfter searching for hours you find nothing.\n";
			cout << "During the search you fell on your face and\n";
			cout << "lose 1 point of health.\n";
			cin.ignore();
			cin.get();
			checkHP();
		}
	} while (playerInput == menuArray[1]); //end do-while loop.
}
That's the quick-fix, anyway. I'm not sure I care for the structure, but iterative solutions are always more efficient than recursive solutions, although they're usually harder to understand. Not in this case, though.

Edit: Oh yeah, you'll find that you'll have a small alignment problem when you output the health in the screen above. Check out printf or setw.
Edit2: By "fully removable" I meant "fully" as in "always." And "always" means always :)

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 7:13 pm
by Terranova3y2
You're right thats much better, I just tested it there and it works perfectly.

I haven't gotten around to loops yet properly, recursion just came up in the book I'm reading and it fit what I needed.

I agree that the structure isn't great either. I'm probably going to make a function specifically for the text alone

so it would be

Code: Select all

void SecondScreen()
   {
   do { //while playerInput == menuArray[1]
      Screen2text();
      if (playerInput == menuArray[0]) // Tests for players input
      {
         ThirdScreen();
      }
      else if (playerInput == menuArray[1])
      {
         --playerHP;
         lossHP();
         checkHP();
      }
   } while (playerInput == menuArray[1]); //end do-while loop.
}
Which would make it a bit better I think.

There was something I forgot to ask earlier.

Is there any easy way to clear the screen in C++?

I googled it and it came up with system("cls"); but thats a windows prompt and I've also read that its a bad command to call

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 7:31 pm
by MarauderIIC
No easy way to clear the screen in C++ as far as I'm aware.
What I don't like about our structure there is the if that matches the while condition.

Code: Select all

    Screen2text();
    while (playerInput == menuArray[0]) {
        --playerHP;
        lossHP();
        checkHP();
        Screen2text();
    }
    ThirdScreen();
}
Does that still do the right stuff? It assumes there are only the two options in menuArray. If that's not true, add a "if (playerInput == menuArray[1])" above ThirdScreen().

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 7:48 pm
by Terranova3y2
MarauderIIC wrote:No easy way to clear the screen in C++ as far as I'm aware.
ah crap :(
MarauderIIC wrote: What I don't like about our structure there is the if that matches the while condition.

Code: Select all

    Screen2text();
    while (playerInput == menuArray[0]) {
        --playerHP;
        lossHP();
        checkHP();
        Screen2text();
    }
    ThirdScreen();
}
Does that still do the right stuff? It assumes there are only the two options in menuArray. If that's not true, add a "if (playerInput == menuArray[1])" above ThirdScreen().
Yeah that works great too. I can't say that I really understand all of it though. I get that its looping while the condition is true but isn't it the same thing I was doing with if statement by calling the function inside itself?

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 7:52 pm
by MarauderIIC
No, if you call the function inside itself, it adds all the function's data to the stack. Then when you return, you go from one copy to the previous copy (to the previous copy to the previous copy as you return, return, return...)
Recusrion is always slower than an iterative solution and has a bit of a fatal flaw. Put this code in your program and run it in debugging (so crashing doesn't screw it up):

Code: Select all

#include <iostream>
using namespace std;
void recurse() {
recurse();
cout << "pants" << endl;
recurse();
}
int main() {
recurse();
return 0;
}
It'll "stack overflow" because the stack, which holds all the function info, is finite :)
Don't get me wrong. Recursion is great. Just don't use it for everything.

Re: Where to start...I'm the new guy btw

Posted: Wed Apr 29, 2009 8:15 pm
by Terranova3y2
I see what you mean lol

You made it clearer to me now. Thanks for explaining.

I'm going to go back over the code again and implement everything.

Re: Where to start...I'm the new guy btw

Posted: Fri May 01, 2009 5:28 pm
by Terranova3y2
Well I've implimented everything that Maurader helped me with and the game is looking good now.

Anything thats output to the screen is in its own function

Code: Select all

void AboutText()
{
	cout << "I created this game to learn C++\n";
	cout << "It is very simple and uses basic \n";
	cout << "if statements and functions\n";
	cout << endl;
	cout << "Thanks to the Elysian Shadows\n";
	cout << "forum members for all their help\n";
	cout << "and motivation.\n";
	cout << endl;
	cout << "Press any key to return to the Main Menu";
	cin.sync();
	cin.get();
}
And then the flow of each screen is handled with while loops and if statements

Code: Select all

void SecondScreen()
{
	Screen2text();
	while (playerInput == menuArray[1]) 
	{
		--playerHP;
		lossHP();
		checkHP();
		Screen2text();
	}
	if (playerInput == menuArray[0])
	{
    ThirdScreen();
	}
	else 
	{
		cout << "Invalid Selection\n";
		Screen2text();
	}
}
Unfortunately I did find 2 big bugs, one I was able to fix.

The other is if someone enters a character instead of a number anywhere in the game, it goes into an infinite loop and crashes. I think it has to do with it trying to put a character into an integer but I've no idea how to fix it as of yet.

Apart from that the game runs as well as I could hope for my first "game"

Re: Where to start...I'm the new guy btw

Posted: Fri May 01, 2009 6:29 pm
by MarauderIIC
Yes. When cin tries to put something into the wrong variable type, it silently gets put into a fail state and all subsequent cin calls are skipped.
To check for this, do something like

Code: Select all

while (!cin) {
    cout << "Bad selection!" << endl;
    cin.clear(); //unset the "bad" flag
    cin.sync(); //eat all unread characters. this is a no-op if the bad bit is set, so you must .clear first
    cin >> input;
}
You could even put this into a function.

Code: Select all

char /* or whatever */ input;
input = getUserInput();

char /* or whatever */ getUserInput() {
    char /* or whatever */ retval;
    cin >> retval;
    while (!cin) {
        /* all that stuff up there... */
    }
    return retval;
}
... or you could just use getline instead. Either cin.getline(cStringName, sizeof(cStringName)) or getline(cin, cppStringName) *cough* the latter *cough*