4 Replies - 4237 Views - Last Post: 03 August 2012 - 03:05 PM Rate Topic: -----

#1 Delacar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-July 12

My Text Based Game Code

Posted 02 August 2012 - 04:05 PM

Hi all! This post is for other programmers opinions on my code. Now before let me say that I have been programming on and off for roughly 6 months so I really don't know much about programming. I also have a question, and it is most likely a stupid one, but first my game code.

#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;

void showintroduction ( void ){
cout << "Text Based Game Programming Second Try" << endl;
cout << "\nThis game is similar to Desolation. \n";}

void showsituation ( void ){
cout << "You can create you own character or use an already Premade character to try the game out first.\n";
}

void showchoice ( void ){
cout << "1. Make your own Character\n";
cout << "2. Use Premade Character\n";}

int getchoice (int min, int max){
int choice;
    do
    {
      cout << "Please enter one of the above numbers.\n";
      cin >> choice;
    }
    while( choice < min || choice > max);

    return( choice );}

void showresults (int choice){
switch(choice){
case 1:
cout << "For now you can not go back, so all of your choices are final, for now anyway.\n";
cout << "You have choosen to create your own Character.\n";
cout << "You must first choose a class for your character befor you can continue.\n";
cout << "Here are your class choices.\n";
cout << "1: Rogue\n";
cout << "2: Fighter\n";
cout << "3: Cleric\n";
cout << "4: Wizard\n\n";
break;

case 2:
cout << "For now you can not go back, so all of your choices are final, for now anyway.\n";
cout << "You have choosen to use a Premade Character.\n";
cout << "Here are your character choices.\n";
cout << "1: Dante The Fighter\n";
cout << "2: Lok The Master Rogue\n";
cout << "3: Katherine The Cold Wizard\n";
cout << "4: Aeris The Healing Light\n\n";
break;
}


}

void showsituation1 (int choice){
switch (choice){
case 1:
cout << "You have choosen to make a Rogue.\n\n";
break;

case 2:
cout << "You have choosen to make a Fighter.\n\n";
break;

case 3:
cout << "You have choosen to make a Cleric.\n\n";
break;

case 4:
cout << "you have choosen to make a Wizard.\n\n";
break;}
}

void showsituation1A (int choice){
switch (choice){
case 1:
cout << "You have choosen to play as Dante The Fighter.\n";
break;

case 2:
cout << "You have choosen to play as Lok The Master Rogue.\n";
break;

case 3:
cout << "You have choosen to play as Katherine The Cold Wizard.\n";
break;

case 4:
cout << "You have choosen to play as Aeris The Healing Light.\n";
break;}
}

void showsituation2 (void){
cout << "Next you must choose a stat line up.\n";
cout << "Sorry you still can't go back so your choice is final.\n\n";

cout << "         Strength Based Stats\n";
cout << "Strength +4\n";
cout << "Constitution +2\n";
cout << "Dexterity -1\n";
cout << "Intelligence 0\n";
cout << "Wisdom +1\n";
cout << "Charisma +1\n\n";

cout << "         Dexterity Based Stats\n";
cout << "Strength +1\n";
cout << "Constitution +1\n";
cout << "Dexterity +4\n";
cout << "Intelligence 0\n";
cout << "Wisdom -1\n";
cout << "Charisma +2\n\n";

cout << "         Intelligence Based Stats\n";
cout << "Strength -1\n";
cout << "Constitution +1\n";
cout << "Dexterity 0\n";
cout << "Intelligence +4\n";
cout << "Wisdom +2\n";
cout << "Charisma +1\n\n";
}

void showchoice1 (void){
cout << "Please choose one of the stat packs to continue.\n";
cout << "1: Strength Based\n";
cout << "2: Dexterity Based\n";
cout << "3: Intelligence Based\n";
}

void showresults1 (int choice){
switch (choice){
case 1:
cout << "\nYou have choosen the Strength Based Stat Package\n";




break;

case 2:
cout << "You have choosen the Dexterity Based Stat Package\n";
break;

case 3:
cout << "You have choosen the Intelligence Based Stat Package\n";
break;}
}

void showname (void){
cout << "\nNow for the final step: Entering your Character's name.\n\n";
}







int main (){

char str[11];
char name[11];

int choice;

showintroduction();

showsituation();

showchoice();

choice = getchoice(1, 2);

showresults( choice );

switch (choice){
case 1:
choice = getchoice(1, 4);

showsituation1(choice);

showsituation2();

showchoice1();

choice = getchoice(1, 3);

showresults1(choice);

showname();

cout << "Enter your character's name here:\n";
cin.getline(name, 11);

strcpy(str, "\nYour character's name is:  ") << endl;
strcat(str, name);

cout << str << endl;

break;

case 2:
choice = getchoice(1, 4);

showsituation1A(choice);
break;}


}



Now my question, why is that in my game code the cin.getline code will not function properly yet in the following code it will (I followed a books code for this and it works wonderfully)


#include <iostream>
#include <cstring>
using namespace std;

int main()
{

char str[11];
char name[11];

cout << "Enter your character's name here:\n";
cin.getline(name, 10);

strcpy(str, "\nYour character's name is:  ");
strcat(str, name);

cout << str << endl;

}



Please copy the codes to your compiler and run them then tell me what you think. Also to any new game programmers this code took me six months work to build and understand, but i think it is a good starting point for beginner game programmers. I don't care if anyone use this code for their own projects. So check out the codes and give your opinions please, I really want to know what you guys think.

Is This A Good Question/Topic? 0
  • +

Replies To: My Text Based Game Code

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 586
  • View blog
  • Posts: 1,300
  • Joined: 24-April 12

Re: My Text Based Game Code

Posted 03 August 2012 - 11:37 AM

Ok. I got the program working. The most simple solution is to not use cin.getline() at all here. Instead you could use

cin >> name;

and I "think" that would work pretty much just as well.

But I wanted to get a better idea of why. I looked up cin at MSDN.

http://msdn.microsof...2(v=VS.80).aspx

The reason that it wasn't working is because the previous answer was left in the buffer. So, when it got to the line of code and said to itself, "I already have an answer for this question. No point in waiting for an answer." essentially.

So, the solution is to clear out the buffer before that line of code, like this:


cin.clear();

I also read that you need to do a

cin.sync();

in order to syncronize any "internal" and "external" buffers.

I haven't used cin and cout much. So, I'm not all that familiar with how they work in situations like this one. I just did a Google search and looked this up.

But, instead of

cin.clear();
cin.sync();
cin.getline(name, 11);

you might be able to instead use this

cin >> name;

and get what you want.

It looked like the first one there allows for spaces in the name, but the second one does not.

But anyway, that's how you get it to work; you just have to clear that buffer every time you do a getline. (I don't know if the sync is necessary or not because I saw things that suggested it was, but others that suggest it's not.)

You're off to a good start!

One suggestion that I would like to make is to use Tab to indent your code. Below, I'm posting your same code along with the fix. But notice how I used Tab to indent the code. Every time I go deeper into the logic of the code I do one more Tab.

For example, look at showresults1. The switch is inside of showresults, so I indent with a tab there. Then the case statements are inside of the switch, so I indent again. Then the cout's and break's are inside of each case, so I indent again.

The reason for this is just to make it easier to read and easier to understand the logic of the code. The more code you have, the harder it is to read. So, while a small amount of code that you've just recently been working on may make a lot of sense to you, it's good to try and write things to make it easier to see what's happening in the code and what level you are at. If your program gets bigger, or if you stop working on it and come back a couple of months later, you may forget what part of the code is doing. Doing things like indenting make it easier for you are anyone else reading your code to understand how things flow.

I also put 2 blank lines between every procedure or function. Any time I have a section of the code that's really seperate from the code next to it, I use 2 blank lines.

If the code is similar and related, but is still seperate (like the case statements) I just put one blank line in between them.

Doing this, makes it a lot easier to read the code and understand it. It also makes it easier to find things in the code.


// TextAdventureTest1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
#include <cstring>
#include <stdlib.h>



using namespace std;


void showintroduction ( void )
{
	cout << "Text Based Game Programming Second Try" << endl;
	cout << "\nThis game is similar to Desolation. \n";
}


void showsituation ( void )
{
	cout << "You can create you own character or use an already Premade character to try the game out first.\n";
}


void showchoice ( void )
{
	cout << "1. Make your own Character\n";
	cout << "2. Use Premade Character\n";
}


int getchoice (int min, int max)
{
	int choice;


    do
    {
      cout << "Please enter one of the above numbers.\n";
      cin >> choice;
    } while( choice < min || choice > max);

    return( choice );
}


void showresults (int choice)
{
	switch(choice)
	{
		case 1:
			cout << "For now you can not go back, so all of your choices are final, for now anyway.\n";
			cout << "You have choosen to create your own Character.\n";
			cout << "You must first choose a class for your character befor you can continue.\n";
			cout << "Here are your class choices.\n";
			cout << "1: Rogue\n";
			cout << "2: Fighter\n";
			cout << "3: Cleric\n";
			cout << "4: Wizard\n\n";
			break;

		case 2:
			cout << "For now you can not go back, so all of your choices are final, for now anyway.\n";
			cout << "You have choosen to use a Premade Character.\n";
			cout << "Here are your character choices.\n";
			cout << "1: Dante The Fighter\n";
			cout << "2: Lok The Master Rogue\n";
			cout << "3: Katherine The Cold Wizard\n";
			cout << "4: Aeris The Healing Light\n\n";
			break;
	}
}


void showsituation1 (int choice)
{
	switch (choice)
	{
		case 1:
			cout << "You have choosen to make a Rogue.\n\n";
			break;

		case 2:
			cout << "You have choosen to make a Fighter.\n\n";
			break;

		case 3:
			cout << "You have choosen to make a Cleric.\n\n";
			break;

		case 4:
			cout << "you have choosen to make a Wizard.\n\n";
			break;
	}
}


void showsituation1A (int choice)
{
	switch (choice)
	{
		case 1:
			cout << "You have choosen to play as Dante The Fighter.\n";
			break;

		case 2:
			cout << "You have choosen to play as Lok The Master Rogue.\n";
			break;

		case 3:
			cout << "You have choosen to play as Katherine The Cold Wizard.\n";
			break;

		case 4:
			cout << "You have choosen to play as Aeris The Healing Light.\n";
			break;
	}
}


void showsituation2 (void)
{
	cout << "Next you must choose a stat line up.\n";
	cout << "Sorry you still can't go back so your choice is final.\n\n";

	cout << "         Strength Based Stats\n";
	cout << "Strength +4\n";
	cout << "Constitution +2\n";
	cout << "Dexterity -1\n";
	cout << "Intelligence 0\n";
	cout << "Wisdom +1\n";
	cout << "Charisma +1\n\n";

	cout << "         Dexterity Based Stats\n";
	cout << "Strength +1\n";
	cout << "Constitution +1\n";
	cout << "Dexterity +4\n";
	cout << "Intelligence 0\n";
	cout << "Wisdom -1\n";
	cout << "Charisma +2\n\n";

	cout << "         Intelligence Based Stats\n";
	cout << "Strength -1\n";
	cout << "Constitution +1\n";
	cout << "Dexterity 0\n";
	cout << "Intelligence +4\n";
	cout << "Wisdom +2\n";
	cout << "Charisma +1\n\n";
}


void showchoice1 (void)
{
	cout << "Please choose one of the stat packs to continue.\n";
	cout << "1: Strength Based\n";
	cout << "2: Dexterity Based\n";
	cout << "3: Intelligence Based\n";
}


void showresults1 (int choice)
{
	switch (choice)
	{
		case 1:
			cout << "\nYou have choosen the Strength Based Stat Package\n";
			break;

		case 2:
			cout << "You have choosen the Dexterity Based Stat Package\n";
			break;

		case 3:
			cout << "You have choosen the Intelligence Based Stat Package\n";
			break;
	}
}


void showname (void)
{
	cout << "\nNow for the final step: Entering your Character's name.\n\n";
}



//int _tmain(int argc, _TCHAR* argv[])
int main()
{
	char str[11];
	char name[11];
	int choice;


	showintroduction();
	showsituation();
	showchoice();
	choice = getchoice(1, 2);
	showresults( choice );

	switch (choice)
	{
		case 1:
			choice = getchoice(1, 4);
			showsituation1(choice);
			showsituation2();
			showchoice1();
			choice = getchoice(1, 3);

			showresults1(choice);
			showname();

			cout << "Enter your character's name here:\n";
			cin.clear();
			cin.sync();
			cin.getline(name, 11);
			//cin >> name;

			strcpy(str, "\nYour character's name is:  ");
			strcat(str, name);
			cout << str << endl;
			break;

		case 2:
			choice = getchoice(1, 4);

			showsituation1A(choice);
			break;
	}

	return 0;
}




Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7948
  • View blog
  • Posts: 13,535
  • Joined: 19-March 11

Re: My Text Based Game Code

Posted 03 August 2012 - 11:55 AM

I'd suggest that you'll next want to generalize your "showSituation" so you don't have to rewrite it for each situation your players encounter. (writing "showSituation34e()" is really going to suck)

What you'd like to do would be to take in a string (the description of the situation and the options available) and return an int (the choice entered by the player). You might then make a struct encapsulating the String and an array of ints to represent a situation: the ints are the jump table for the response. So you might have "You are in a room full of skulls. There is a man with an axe here, who seems quite interested in your skull. Do you \n1) fight \n2) negotiate \n3) run!" and an array {34, 75, 23}. Each of those numbers points to an entry for a similar struct, with the resulting situation and choice array.
This is one simple way to arrange things - it's not the best, but it's a step up from where you're at now. It'd be worth trying it to see how it works.
Was This Post Helpful? 0
  • +
  • -

#4 Delacar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-July 12

Re: My Text Based Game Code

Posted 03 August 2012 - 02:50 PM

You have save the day thank you BBeck! The cin.clear and cin.sync codes was just what i needed to break free of my name entering problem. Now I can proceed to some actual game code, instead of just the set up codes. So once again thank you!
Was This Post Helpful? 1
  • +
  • -

#5 Delacar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 31-July 12

Re: My Text Based Game Code

Posted 03 August 2012 - 03:05 PM

View Postjon.kiparsky, on 03 August 2012 - 01:55 PM, said:

I'd suggest that you'll next want to generalize your "showSituation" so you don't have to rewrite it for each situation your players encounter. (writing "showSituation34e()" is really going to suck)

What you'd like to do would be to take in a string (the description of the situation and the options available) and return an int (the choice entered by the player). You might then make a struct encapsulating the String and an array of ints to represent a situation: the ints are the jump table for the response. So you might have "You are in a room full of skulls. There is a man with an axe here, who seems quite interested in your skull. Do you \n1) fight \n2) negotiate \n3) run!" and an array {34, 75, 23}. Each of those numbers points to an entry for a similar struct, with the resulting situation and choice array.
This is one simple way to arrange things - it's not the best, but it's a step up from where you're at now. It'd be worth trying it to see how it works.


Yeah, from what little I know of string arrays (which I only started like yesterday,, because I needed the text entering code) they will be a better choice to keep the game code neater, and I think it should fix my not being able to go back to a previous choice if you don't like the choice you are on. I will certainly be keeping that in mind for optimizing the program. Thanks for the opinion and the advice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1