So, here I am again, with my mass of code, albeit with more progress.
It's still got a long way to go, and I'm still not that good at coding, but man oh man have I learned a lot through this game.
I come with a few more questions than last time.

So to get to my first problem, You have to go to Select Character -> Create Character -> Enter Name -> Use all skill points. The problem is that it doesn't use all of the skill points. Any suggestions? (This is the setStats function, by the way)

And then another problem with that, is I can't really come up with a good way to end that loop - I was thinking maybe a finish selection(but I couldn't figure a way to implement that). Anything on this one either?

Also, I'm curious about how other people think I should go about adding spells/items to Character's class.


Another topic I'm curious of is how would I write/read creatures to/from files(say for saved characters).

The last one is I can't think of a good way other than an if statement per character in order to make them the character the player will use (unless I overload my select function).


The full program:

//RPG Game
#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
#include <vector>
#include <dos.h>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <fstream>
using namespace std;
HANDLE  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
static string selection;
const int LINE_LENGTH = 75;
unsigned int srand();

void slowText(char c[]){
		int len = strlen(c);
		for(int i=0; i < len;i++) {
			putchar(c[i]);
		    Sleep(30);
		}
}
void hL(string s){
	int highLight = 240;
	HANDLE  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, highLight);
	cout << s << setw(10);
	int nonHighLight = 15;
	SetConsoleTextAttribute(hConsole, nonHighLight);
}
void nHL(string s){
	int nonHighLight = 15;
	SetConsoleTextAttribute(hConsole, nonHighLight);
	cout << s << setw(10);
}
void bS(){
	int blankSpace = 0;
	SetConsoleTextAttribute(hConsole, blankSpace);
	cout <<	"		" << setw(3);
	SetConsoleTextAttribute(hConsole, 15);
}
void borderDown() 
{
    const char o = 209;
 
    for (int i = 0; i < LINE_LENGTH; i++)
        cout << o;
    cout << endl;
} 
void borderUp()
{
    const char o = 207;
 
    for (int i = 0; i < LINE_LENGTH - 2; i++)
        cout << o;
    cout << endl;
}
void select(string s){
	hL(s);
	selection = s;
}

class Creature{
private:
	int statArr[6];
	string name;
public:
	Creature(){
		rollStats();
		name = "Ciziten";
	}
	void rollStats()
	{
		int iniStat[4];
		int temp = 6;
		for(int i = 0; i < 6; i++){
		for(int q = 0; q < 4; q++)
		{
			iniStat[q] = rand() % 6;
			if(temp > iniStat[q]){temp = iniStat[q];}
		}
		statArr[i] = iniStat[0] + iniStat[1] + iniStat[2] + iniStat[3] - temp;
		}
	}

	void setStats()
	{
		char a;
		int y = 0;
		stringstream ss;
		int limit = 40;
		vector<int> stat(6, 1);
		vector<string> stats(6);
		stats[0] = ("Strength");
		stats[1] = ("Stamina");
		stats[2] = ("Dexterity");
		stats[3] = ("Willpower");
		stats[4] = ("Intellect");
		stats[5] = ("Luck");
		stats.push_back("Finish");
		for(char a = 'q'; a != ' ';)
		{
			if(a == 's' || a == 'S')	
			{
				y += 1;
				if(y < 0){y = (stats.size() - 1);}
			}
			if(a == 'w' || a == 'W')
			{
				y -= 1;
				if(y > (stats.size() - 1)){y = 0;}
			}
			if(a == 'a' || a == 'A')
			{
				if(0 < (stat[y] - 1))
				{
					limit += 1;	
				stat[y] -= 1;
				}
			}
			if(a == 'd' || a == 'D')
			{
				if(limit > (stat[y] - 1))
				{
					limit -= 1;	
				stat[y] += 1;
				}
			}
			system("cls");
			cout << endl << endl;
			cout << "Name: " << name;
			cout << endl << endl;
			cout << "Now you must pick your attributes." << endl;
			borderDown();	
			for (int j = 0; j < 6; j++)
			{
				bS();
				cout << stats[j];
				if(y == j){
						cout << " : <" << stat[j] << ">" << endl;
				}		
				else
				{
						cout << " :  " << stat[j] << endl;
				}							
			}
				borderUp();	
				cout << "Amount of attribute points left to spend: " << limit;
			a = getch();	

		}
	}

	void setName()
	{
		cin >> name;
		cin.clear();
	}
	void viewName()
	{
		cout << name;
	}
	void viewStats()
	{
		for(int i = 0; i < 6; i++)
		{
			 cout << statArr[i]	<< endl;
		}
	}
};


int main()
{
	vector<Creature> characters;
	Creature player;
	string menuType;	
	int menu = 0;
	char a ='q';
	string hint;
	vector<vector<string>> menOp(2, vector<string> (2));
	int x = 0;
	int y = 0;
	int num = 0;
	string displayTextBefore = "";
	string displayTextAfter = "";
	for(bool endgame = 1; endgame != 0;)
	{
		switch(menu)
		{
		case 0:
			menuType = "	|Main Menu|";
			menOp[0][0] = "Start Game";
			menOp[0][1] = "Game Options";
			menOp[1][0] = "Select Character";
			menOp[1][1] = "Exit";
			hint = "W, A, S, D are to move and space to select";
			break;
		case 1:
			menuType = "	|Options Menu|";
			menOp[0][0] = "Controls";
			menOp[0][1] = "Color Scheme";
			menOp[1][0] = "About";
			menOp[1][1] = "Back";
			hint = "Options are cool.";
			break;
		case 2:
			menuType = "	|Character Menu|";
			menOp[0][0] = "Select Character";
			menOp[0][1] = "Create Character";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		case 3:
			menuType = "	|!!!|";
			menOp[0][0] = "Yes";
			menOp[0][1] = "No";
			hint = "Exit, if you must.";
			break;
		case 4:
			menuType = "	|Character Creation|";
			menOp[0][0] = "Select Character";
			menOp[0][1] = "Create Character";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		case 5:
			menuType = "	|Character|";
			menOp[0][0] = "Enter Name";
			menOp[0][1] = "Choose Stats";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		}
	for(char a = 'q'; a != ' ';)
	{
		system("cls");
		cout << x << "," << y;
		if(a == 'd' || a == 'D')	
		{
			y += 1;
			if(y > (menOp.at(0).size() - 1)){y = 0;}
		} 
		if(a == 's' || a == 'S')	
		{
			x -= 1;
			if(x < 0){x = (menOp.size() - 1);}
		}
		if(a == 'a' || a == 'A')
		{
			y -= 1;
			if(y < 0){y = (menOp.at(0).size() - 1);}
		}
		if(a == 'w' || a == 'W')
		{
			x += 1;
			if(x > (menOp.size() - 1)){x = 0;}
		}
		SetConsoleTextAttribute(hConsole, 15);
		cout << endl << endl;
		cout << displayTextBefore << endl << endl;
		cout << "			" << menuType << endl;
		borderDown();	
			for (int i = 0; i < menOp.size(); i++)
			{
				for (int j = 0; j < menOp.at(0).size(); j++)
				{		bS();
					if(x == i && y == j){
						select(menOp[i][j]);
					}
					
					else{nHL(menOp[i][j]);}
					
						
				}
				bS();
				cout << endl;
				
			}

			borderUp();
			cout << "Hint: " << hint;
			cout << endl;
			cout << displayTextAfter << endl;
			
			a = getch();	
	}
		if(selection == "Start Game"){cout << "Game Started";
			system("pause");
		}
		if(selection == "Game Options"){menu = 1;}
			if(selection == "Back" && menu == 1){menu = 0;}
		if(selection == "Select Character"){menu = 2;}
			if(selection == "Create Character")
			{
				int nameSet = 0;
				for(bool charCreated = 0; charCreated != 1;)
				{
				if(nameSet != 1){
				cout << "Reports show your name isn't in our records.\n Now what might it be? : ";
				player.setName();
				nameSet = 1;
				}
				else
				{
					player.viewName();
					cout << endl;
				}
				player.setStats();

				}
			}
			if(selection == "Back" && menu == 2){menu = 0;}
		if(selection == "Exit")
		{
			menu = 3;
			x = 0;
			y = 0;
			menOp.resize(1);
		}
			if(selection == "Yes" && menu == 3){
				endgame = 0;
			}
			if(selection == "No" && menu == 3){
				menu = 0;
				menOp.resize(2);
				menOp.at(1).push_back("DEFAULT");
				menOp.at(1).push_back("DEFAULT");
			}
	
	}	
	system("pause");
	return 0;
}

Recommended Answers

All 7 Replies

As an aside: I'd strongly recommend breaking this up into multiple header and implementation files, both to make it more modular, and to reduce the amount of clutter in the main program file. Here I've done this for you to show you how to do it, if you haven't done so before; I've made the necessary changes to make it possible in the process, and this compiles and runs correctly under GCC 4.4.1/Code::Blocks 10.5 .

rpg.cpp

//RPG Game
#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
#include <vector>
#include <dos.h>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include "utility.h"
#include "Creature.h"

using namespace std;
HANDLE  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
string selection;
const int LINE_LENGTH = 75;

int main()
{
	vector<Creature> characters;
	Creature player;
	string menuType;
	int menu = 0;
	char a ='q';
	string hint;
	vector<vector<string> > menOp(2, vector<string> (2));
	int x = 0;
	int y = 0;
	int num = 0;
	string displayTextBefore = "";
	string displayTextAfter = "";
	for(bool endgame = 1; endgame != 0;)
	{
		switch(menu)
		{
		case 0:
			menuType = "	|Main Menu|";
			menOp[0][0] = "Start Game";
			menOp[0][1] = "Game Options";
			menOp[1][0] = "Select Character";
			menOp[1][1] = "Exit";
			hint = "W, A, S, D are to move and space to select";
			break;
		case 1:
			menuType = "	|Options Menu|";
			menOp[0][0] = "Controls";
			menOp[0][1] = "Color Scheme";
			menOp[1][0] = "About";
			menOp[1][1] = "Back";
			hint = "Options are cool.";
			break;
		case 2:
			menuType = "	|Character Menu|";
			menOp[0][0] = "Select Character";
			menOp[0][1] = "Create Character";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		case 3:
			menuType = "	|!!!|";
			menOp[0][0] = "Yes";
			menOp[0][1] = "No";
			hint = "Exit, if you must.";
			break;
		case 4:
			menuType = "	|Character Creation|";
			menOp[0][0] = "Select Character";
			menOp[0][1] = "Create Character";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		case 5:
			menuType = "	|Character|";
			menOp[0][0] = "Enter Name";
			menOp[0][1] = "Choose Stats";
			menOp[1][0] = "Character Guide";
			menOp[1][1] = "Back";
			hint = "Dont have time to make a character? Use a prebuilt.";
			break;
		}
	for(char a = 'q'; a != ' ';)
	{
		system("cls");
		cout << x << "," << y;
		if(a == 'd' || a == 'D')
		{
			y += 1;
			if(y > (menOp.at(0).size() - 1)){y = 0;}
		}
		if(a == 's' || a == 'S')
		{
			x -= 1;
			if(x < 0){x = (menOp.size() - 1);}
		}
		if(a == 'a' || a == 'A')
		{
			y -= 1;
			if(y < 0){y = (menOp.at(0).size() - 1);}
		}
		if(a == 'w' || a == 'W')
		{
			x += 1;
			if(x > (menOp.size() - 1)){x = 0;}
		}
		SetConsoleTextAttribute(hConsole, 15);
		cout << endl << endl;
		cout << displayTextBefore << endl << endl;
		cout << "			" << menuType << endl;
		borderDown();
			for (int i = 0; i < menOp.size(); i++)
			{
				for (int j = 0; j < menOp.at(0).size(); j++)
				{		bS();
					if(x == i && y == j){
						select(menOp[i][j]);
					}

					else{nHL(menOp[i][j]);}


				}
				bS();
				cout << endl;

			}

			borderUp();
			cout << "Hint: " << hint;
			cout << endl;
			cout << displayTextAfter << endl;

			a = getch();
	}
		if(selection == "Start Game"){cout << "Game Started";
			system("pause");
		}
		if(selection == "Game Options"){menu = 1;}
			if(selection == "Back" && menu == 1){menu = 0;}
		if(selection == "Select Character"){menu = 2;}
			if(selection == "Create Character")
			{
				int nameSet = 0;
				for(bool charCreated = 0; charCreated != 1;)
				{
				if(nameSet != 1){
				cout << "Reports show your name isn't in our records.\n Now what might it be? : ";
				player.setName();
				nameSet = 1;
				}
				else
				{
					player.viewName();
					cout << endl;
				}
				player.setStats();

				}
			}
			if(selection == "Back" && menu == 2){menu = 0;}
		if(selection == "Exit")
		{
			menu = 3;
			x = 0;
			y = 0;
			menOp.resize(1);
		}
			if(selection == "Yes" && menu == 3){
				endgame = 0;
			}
			if(selection == "No" && menu == 3){
				menu = 0;
				menOp.resize(2);
				menOp.at(1).push_back("DEFAULT");
				menOp.at(1).push_back("DEFAULT");
			}

	}
	system("pause");
	return 0;
}

utility.h

#ifndef UTILITY_H
#define UTILITY_H 1

#include <string>
#include <windows.h>

extern HANDLE hConsole;
extern std::string selection;
extern const int LINE_LENGTH;

void slowText(char c[]);
void hL(std::string s);
void nHL(std::string s);
void bS();
void borderDown();
void borderUp();
void select(std::string s);
#endif

utility.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <windows.h>
#include <conio.h>
#include "utility.h"

using namespace std;

void slowText(char c[]){
		int len = strlen(c);
		for(int i=0; i < len;i++) {
			putchar(c[i]);
		    Sleep(30);
		}
}

void hL(string s){
	int highLight = 240;
	HANDLE  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, highLight);
	cout << s << setw(10);
	int nonHighLight = 15;
	SetConsoleTextAttribute(hConsole, nonHighLight);
}

void nHL(string s){
	int nonHighLight = 15;
	SetConsoleTextAttribute(hConsole, nonHighLight);
	cout << s << setw(10);
}

void bS(){
	int blankSpace = 0;
	SetConsoleTextAttribute(hConsole, blankSpace);
	cout <<	"		" << setw(3);
	SetConsoleTextAttribute(hConsole, 15);
}

void borderDown()
{
    const char o = 209;

    for (int i = 0; i < LINE_LENGTH; i++)
        cout << o;
    cout << endl;
}

void borderUp()
{
    const char o = 207;

    for (int i = 0; i < LINE_LENGTH - 2; i++)
        cout << o;
    cout << endl;
}

void select(string s){
	hL(s);
	selection = s;
}

Creature.h

#include <string>

#ifndef CREATURE_H
#define CREATURE_H 1

class Creature
{
private:
    int statArr[6];
    std::string name;
public:
    Creature();
    void rollStats();

    void setStats();

    void setName();
    void viewName();
    void viewStats();
};

#endif

Creature.cpp

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <conio.h>
#include "utility.h"
#include "Creature.h"


Creature::Creature()
{
    rollStats();
    name = "Citizen";
}


void Creature::rollStats()
{
    int iniStat[4];
    int temp = 6;
    for(int i = 0; i < 6; i++)
    {
        for(int q = 0; q < 4; q++)
        {
            iniStat[q] = rand() % 6;
            if(temp > iniStat[q])
            {
                temp = iniStat[q];
            }
        }
        statArr[i] = iniStat[0] + iniStat[1] + iniStat[2] + iniStat[3] - temp;
    }
}

void Creature::setStats()
{
    char a;
    unsigned y = 0;
    std::stringstream ss;
    int limit = 40;
    std::vector<int> stat(6, 1);
    std::vector<std::string> stats(6);
    stats[0] = ("Strength");
    stats[1] = ("Stamina");
    stats[2] = ("Dexterity");
    stats[3] = ("Willpower");
    stats[4] = ("Intellect");
    stats[5] = ("Luck");
    stats.push_back("Finish");
    for(char a = 'q'; a != ' ';)
    {
        if(a == 's' || a == 'S')
        {
            y += 1;
            if(y < 0)
            {
                y = (stats.size() - 1);
            }
        }
        if(a == 'w' || a == 'W')
        {
            y -= 1;
            if(y > (stats.size() - 1))
            {
                y = 0;
            }
        }
        if(a == 'a' || a == 'A')
        {
            if(0 < (stat[y] - 1))
            {
                limit += 1;
                stat[y] -= 1;
            }
        }
        if(a == 'd' || a == 'D')
        {
            if(limit > (stat[y] - 1))
            {
                limit -= 1;
                stat[y] += 1;
            }
        }
        system("cls");
        std::cout << std::endl << std::endl;
        std::cout << "Name: " << name;
        std::cout << std::endl << std::endl;
        std::cout << "Now you must pick your attributes." << std::endl;
        borderDown();
        for (unsigned j = 0; j < 6; j++)
        {
            bS();
            std::cout << stats[j];
            if(y == j)
            {
                std::cout << " : <" << stat[j] << ">" << std::endl;
            }
            else
            {
                std::cout << " :  " << stat[j] << std::endl;
            }
        }
        borderUp();
        std::cout << "Amount of attribute points left to spend: " << limit;
        a = getch();

    }
}

void Creature::setName()
{
    std::cin >> name;
    std::cin.clear();
}


void Creature::viewName()
{
    std::cout << name;
}

void Creature::viewStats()
{
    for(int i = 0; i < 6; i++)
    {
        std::cout << statArr[i]	<< std::endl;
    }
}

I know it isn't what you asked for, but I hope it helps anyway.

Thanks again! You have been absolutely excellent in helping me with this. That really did clean the program up a lot, too.

How would I go about operator overloading when I have multiple files?

Could you go into more detail about what you're looking to do? Operator overloading should work similarly to any other function or method, regardless of whether it is divided into multiple files or not. Or were you referring to data files?

Could you go into more detail about what you're looking to do? Operator overloading should work similarly to any other function or method, regardless of whether it is divided into multiple files or not. Or were you referring to data files?

I meant as in having multiple files for the program. What I've been taught is to use friend functions to do operator overloading, but we havent gone over how they divide amongst different files

Usually, in the header file you would have a declaration of the function as a friend of the class in the class itself, then outside of the class, you would have the function prototype itself.

#include <string>

#ifndef CREATURE_H
#define CREATURE_H 1

class Creature
{
private:
    int statArr[6];
    std::string name;
public:
    Creature();
    void rollStats();

    void setStats();

    void setName();
    void viewName();
    void viewStats();

    friend std::ostream& operator<<(std::ostream& os, Creature& cr);
};

std::ostream& operator<<(std::ostream& os, Creature& cr);

#endif

Then in the code file you would have the actual function:

std::ostream& operator<<(std::ostream& os, Creature& cr)
{
    os << cr.name;  // just an example

    // any other output you see fit to have here    

    return os;
}

So to read it from a file, would I overload the << operator? How would I go about doing that for a class, just have it get each stat through get functions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.