Skip to main content
gamedev.net
🔒 Locked

RPG code

Started by gamechampionx 47 replies 2k views
Original Post
gamechampionx
gamechampionx
I''m working on my RPG, and the code looks fine, but it won''t run. Here''s the code:
  
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

inline int Cls()
{
	system("cls");
	return 0;
}

struct Character
{
	int maxmana, maxhp, strength, defence, magic, agility;
	int mana, hp;
	int experience;
	int damage;
	int damageestimate[2];
	float battleinterval;
	char name[20];
	char race[7];
	char characterclass[15];
};	

Character player;
Character enemy;

char CharacterResults()
{
	char yn;
	bool done = 0;
	do
	{
		Cls();
		cout << "Name: " << player.name;
		cout << "\nRace: " << player.race;
		cout << "\nClass: " << player.characterclass;
		cout << "\n\nStrength: " << player.strength;
		cout << "\nDefence: " << player.defence;
		cout << "\nMagic: " << player.magic;
		cout << "\nAgility: " << player.agility;
		cout << "\n\nMaximum Mana: " << player.maxmana;
		cout << "\nMaximum HP: " << player.maxhp;
		cout << "\n\nBattle Interval: " << player.battleinterval;
		cout << "\n\nWould you like to keep these stats and continue?(y/n)";
		cout << flush;
		yn = getch();
		if (yn != ''y'' && yn != ''Y'' && yn != ''n'' && yn != ''N'')
		{
			cout << "\n\nNot a valid choice.\n\n";
			cout << "Press any key to continue...";
			cout << flush;
			getch();
		}
		else
			done = 1;
	} while (done = 0);
	return yn;
}

int CharacterMenu()
{
	bool done;
	bool done2;
	char result;
	char choice;
	do
	{
		done = 1;
		Cls();
		cout << "What is your name?\n";
		cin.getline (player.name,21);
		do
		{
			done2 = 1;
			Cls();
			cout << "Please choose a race for your character:\n\n";
			cout << "1.  Human: A strong physical race, in offence and defence.\n\n";
			cout << "2.  Goblin: Another physical race, with a powerful attack, but little grasp of"
				<< "\n    magic.\n\n";
			cout << "3.  Elf: A less physical race, with its roots in magic.\n\n";
			cout << "4.  Faerie: A very agile race.\n\n";
			cout << "5.  Dragon: The most well-rounded of all races.\n\n";
			cout << flush;
			choice = getch();
			switch(choice)
			{
			case ''1'':
				strcpy(player.race,"Human\0");
				player.strength = 30;
				player.defence = 30;
				player.magic = 10;
				player.agility = 15;
				break;
			case ''2'':
				strcpy(player.race,"Goblin\0");
				player.strength = 40;
				player.defence = 30;
				player.magic = 5;
				player.agility = 10;
				break;
			case ''3'':
				strcpy(player.race,"Elf\0");
				player.strength = 15;
				player.defence = 10;
				player.magic = 45;
				player.agility = 15;
				break;
			case ''4'':
				strcpy(player.race,"Faerie\0");
				player.strength = 5;
				player.defence = 20;
				player.magic = 25;
				player.agility = 35;
				break;
			case ''5'':
				strcpy(player.race,"Dragon\0");
				player.strength = 20;
				player.defence = 25;
				player.magic = 20;
				player.agility = 20;
				break;
			default:
				cout << "Not a valid choice.\n\n";
				cout << "Press any key to continue...";
				cout << flush;
				getch();
				done = 0;
			break;
			}
		Cls();
		cout << "Please choose a class for your character:\n\n";
		cout << "1.  Blood Mage: A sorceror class with powerful but sometimes suicidal spells.\n\n";
		cout << "2.  Thief: A nimble class that attacks with agility, and steals various things.\n\n";
		cout << "3.  Elemental Mage: A sorceror class that can control the elements.\n\n";
		cout << "4.  Ninja: A strong and agile physical class that uses martial arts spells.\n\n";
		cout << "5.  Gladiator: Another strong physical class that uses close-range weapons.\n\n";
		cout << "6.  Warlord: An agile class that uses long-range weapons.\n\n";
		cout << flush;
		choice = getch();
		switch(choice)
		{
		case ''1'':
			strcpy(player.characterclass,"Blood Mage\0");
			player.strength += 20;
			player.defence += 20;
			player.magic += 20;
			player.agility += 20;
			break;
		case ''2'':
			strcpy(player.characterclass,"Thief\0");
			player.strength += 15;
			player.defence += 15;
			player.magic += 20;
			player.agility += 30;
			break;
		case ''3'':
			strcpy(player.characterclass,"Elemental Mage\0");
			player.strength += 10;
			player.defence += 15;
			player.magic += 35;
			player.agility += 20;
			break;
		case ''4'':
			strcpy(player.characterclass,"Ninja\0");
			player.strength += 30;
			player.defence += 10;
			player.magic += 15;
			player.agility += 25;
			break;
		case ''5'':
			strcpy(player.characterclass,"Gladiator\0");
			player.strength += 30;
			player.defence += 30;
			player.magic += 10;
			player.agility += 10;
			break;
		case ''6'':
			strcpy(player.characterclass,"Warlord\0");
			player.strength += 20;
			player.defence += 20;
			player.magic += 10;
			player.agility += 30;
			break;
		default:
			cout << "Not a valid choice.\n\n";
			cout << "Press any key to continue...";
			cout << flush;
			getch();
			done2 = 0;
			break;
		}
		player.maxmana = 2 * player.magic;
		player.mana = player.maxmana;
		player.maxhp = 2 * player.defence;
		player.hp = player.maxhp;
		player.battleinterval = 1000.0f / (float)player.agility;
		result = CharacterResults();
		if (result == ''n'' || result == ''N'')
		{
			Cls();
			cout << "You may now choose again, press any key...";
			cout << flush;
			getch();
			done2 = 0;
		}
	} while (done2 = 0);
	return 0;
}

int Story()
{
	Cls();
	cout << "Story:\n\n"
		<< "Welcome to the land of Relourin, young warrior.  You have been selected by the\n"
		<< "King himself for this mission.\n\n"
		<< "This land is in grave danger.  Something has emerged into the depths of the\n"
		<< "Great Volcano.  Many strange things have been happening.  Strange unfriendly\n"
		<< "creatures have been roaming the countryside, looking for a fight.  The local\n"
		<< "villages have been sabotaged.  There are far too many of them for us to ward\n"
		<< "off without you.  You are our only hope.\n\n"
		<< "Leave with haste, and rid this land of evil before it''s too late.\n\n"
		<< "Press any key to continue...";
	cout << flush;
	getch();
	CharacterMenu();
	Cls();
	cout << "Press any key to exit..." << flush;
	getch();
	return 0;
}

int main()
{
	char choice;
	Start:
	Cls();
	cout << "RPG\n\n" << "What would you like to do?\n\n";
	cout << "1.  New Game\n" << "2.  Resume Game\n" << "3.  Exit\n\n";
	cout << flush;
	choice = getch();
	switch(choice)
	{
	case ''1'':
		Story();
		break;
	case ''2'':
		Cls();
		cout << "Not yet available\n\n\n";
		cout << "Press any key to reset...";
		cout << flush;
		getch();
		goto Start;
	case ''3'':
		Cls();
		cout << "Press any key to exit..." << flush;
		getch();
		return 0;
	default:
		cout << "Not a valid choice.\n\n";
		cout << "Press any key to continue...";
		cout << flush;
		getch();
		goto Start;
	}
	return 0;
}

  
Here are the errors:
  
--------------------Configuration: RPG - Win32 Debug--------------------
Compiling...
RPG.cpp
C:\Windows\Desktop\Web Site\XtremeSoftware RPG\Rpg\RPG.cpp(211) : error C2062: type ''int'' unexpected (Line of Int Story)
C:\Windows\Desktop\Web Site\XtremeSoftware RPG\Rpg\RPG.cpp(212) : error C2143: syntax error : missing '';'' before ''{'' ({ after Int Story)
C:\Windows\Desktop\Web Site\XtremeSoftware RPG\Rpg\RPG.cpp(234) : error C2601: ''main'' : local function definitions are illegal ({ after Int Main)
C:\Windows\Desktop\Web Site\XtremeSoftware RPG\Rpg\RPG.cpp(269) : fatal error C1004: unexpected end of file found
Error executing cl.exe. (End of File)

RPG.exe - 4 error(s), 0 warning(s)
  
What''s going on?
DracosX
DracosX
You have an extra do { that doesn''t "do" anything
look closely at your code...
You''ll see it in function CharacterMenu().





DracosX:

Master of the General Protection Fault
DracosX:Master of the General Protection Fault
Like2Byte
Like2Byte
I''ve noticed not exactly an error; but, with your Character struct I''d ''typedef'' it.

so...

  
typedef struct character_s
{
int maxmana, maxhp, strength, defence, magic, agility;
int mana, hp;
int experience;
int damage;
int damageestimate[2];
float battleinterval;
char name[20];
char race[7];
char characterclass[15];
} character_t;

typedef struct monster_s
{
// obviously this is a cut and paste job, but you get the idea

int maxmana, maxhp, strength, defence, magic, agility;
int mana, hp;
int experience;
int damage;
int damageestimate[2];
float battleinterval;
char name[20];
char race[7];
char characterclass[15];
} monster_t;

character_t PC1, PC2; //...

monster_t NPC1, NPC2; //...




This allows you to place checks on yourself to perhaps keeps yourself from attacking other good PCs or NPCs that you other wise wouldn''t want to attack/interact with. This would just enforce typechecking. That''s all.
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Like2Byte
Like2Byte
on your line with:

      
CharacterResults()
...
...
...
done = 1;
} while (done = 0);



//you've assigned '0' to done. Thusly causing the while statement to end prematurely. I think you wanted 'done == 0'.


//I've used this method before. try this:


// excuse my C++. it's been a while.

bool done = false;
char test;

do
{
cout << "again?";
cin >> test;
if( test == "n" )
done = true;
}while(!done);


[edited by - Like2Byte on June 24, 2002 10:47:09 AM]

[edited by - Like2Byte on June 24, 2002 10:47:58 AM]
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Gee
Gee
quote:
Original post by Like2Byte
I''ve noticed not exactly an error; but, with your Character struct I''d ''typedef'' it.


I disagree completely, I like using the same foundation for pc''s and npc''s. Creating a universal processing of the character is nice (fight between pc/pc, pc/npc, npc/npc - all interactions will be dealt with universally. Not to mention when you have 10 different kinds of these!).
Although I would suggest derived classes in the future, but don''t think about that now...

gamechapionx, if you know want to be able to identify different kinds of characters (like Like2Byte has stated can be good, and he''s right)... Just throw in a TYPE enum or something:
enum CHARTYPE = {PS,NPC,MONSTER,YOMAMA}; 
and so on...

I''m assuming here that your pc''s and npc''s has got the same value (due to your current layout)...

gamechampionx, ignore this post if you found it useless or to over the top. I just wanted you to know that I think your layout is fine as it is.

btw it''s funny, all these console rpg''s that you see (including how mine were) is just create an advanced character code with 10 different classes... hehe =)
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
nyStagmus
nyStagmus
Usually when you get an unexpected end of file you are missing a closing bracket somewhere. Like DracosX pointed out it is the second do in the CharacterMenu function that has an opening bracket but no closing bracket and while expression.
hammerstein_02
hammerstein_02
In your CharacterMenu() function the first

do
{

doesn''t have anything to line up with

bool is supported by msvc.
Dactylos
Dactylos
You have two opening "do {", but only one closing "} while(...)".
The code is badly indented from the lines

Cls();
cout << "Please choose a class for your character:\n\n";

everything from that Cls(); upto (and possibly including)

} while (done2 = 0);

should be indented one 'step' (also change = to ==), and you should add another

} while (something)

after or before the ending while-line you already have.

[edited by - Dactylos on June 26, 2002 6:10:19 PM]
Peon
Peon
Yeah, once you start seeing errors all the time, you learn what they REALLY mean. For example, as someone posted above, "Unexpected end of file" almost always means you forgot a brace, at least in my experience.

The other day, I compiled my program and got 70 errors; nearly all were due to a (ONE) missing brace
Peon
Peon
Peon
Yeah, once you start seeing errors all the time, you learn what they REALLY mean. For example, as someone posted above, "Unexpected end of file" almost always means you forgot a brace, at least in my experience.

The other day, I compiled my program and got 70 errors; nearly all were due to a (ONE) missing brace
Peon
gamechampionx
gamechampionx
Hey guys, I fixed my game. Here''s the code:


  
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

inline int Cls()
{
system("cls");
return 0;
}

struct Character
{
int maxmana, maxhp, strength, defence, magic, agility;
int mana, hp;
int experience;
int damage;
int damageestimate[2];
float battleinterval;
char name[20];
char race[7];
char characterclass[15];
};

Character player;
Character enemy;

char CharacterResults()
{
char yn;
bool done = 0;
do
{
Cls();
cout << "Name: " << player.name;
cout << "\nRace: " << player.race;
cout << "\nClass: " << player.characterclass;
cout << "\n\nStrength: " << player.strength;
cout << "\nDefence: " << player.defence;
cout << "\nMagic: " << player.magic;
cout << "\nAgility: " << player.agility;
cout << "\n\nMaximum Mana: " << player.maxmana;
cout << "\nMaximum HP: " << player.maxhp;
cout << "\n\nBattle Interval: " << player.battleinterval;
cout << "\n\nWould you like to keep these stats and continue?(y/n)";
cout << flush;
yn = getch();
if (yn != ''y'' && yn != ''Y'' && yn != ''n'' && yn != ''N'')
{
cout << "\n\nNot a valid choice.\n\n";
cout << "Press any key to continue...";
cout << flush;
getch();
}
else
done = 1;
} while (done = 0);
return yn;
}

int CharacterMenu()
{
bool done;
bool done2;
char result;
char choice;
do
{
done2 = true;
Cls();
cout << "What is your name?\n";
cin.getline (player.name,21);
do
{
done = true;
Cls();
cout << "Please choose a race for your character:\n\n";
cout << "1. Human: A strong physical race, in offence and defence.\n\n";
cout << "2. Goblin: Another physical race, with a powerful attack, but little grasp of"
<< "\n magic.\n\n";
cout << "3. Elf: A less physical race, with its roots in magic.\n\n";
cout << "4. Faerie: A very agile race.\n\n";
cout << "5. Dragon: The most well-rounded of all races.\n\n";
cout << flush;
choice = getch();
switch(choice)
{
case ''1'':
strcpy(player.race,"Human\0");
player.strength = 30;
player.defence = 30;
player.magic = 10;
player.agility = 15;
break;
case ''2'':
strcpy(player.race,"Goblin\0");
player.strength = 40;
player.defence = 30;
player.magic = 5;
player.agility = 10;
break;
case ''3'':
strcpy(player.race,"Elf\0");
player.strength = 15;
player.defence = 10;
player.magic = 45;
player.agility = 15;
break;
case ''4'':
strcpy(player.race,"Faerie\0");
player.strength = 5;
player.defence = 20;
player.magic = 25;
player.agility = 35;
break;
case ''5'':
strcpy(player.race,"Dragon\0");
player.strength = 20;
player.defence = 25;
player.magic = 20;
player.agility = 20;
break;
default:
cout << "Not a valid choice.\n\n";
cout << "Press any key to continue...";
cout << flush;
getch();
done = false;
break;
}
} while (!done) ;
do
{
done = true;
Cls();
cout << "Please choose a class for your character:\n\n";
cout << "1. Blood Mage: A sorceror class with powerful but sometimes suicidal spells.\n\n";
cout << "2. Thief: A nimble class that attacks with agility, and steals various things.\n\n";
cout << "3. Elemental Mage: A sorceror class that can control the elements.\n\n";
cout << "4. Ninja: A strong and agile physical class that uses martial arts spells.\n\n";
cout << "5. Gladiator: Another strong physical class that uses close-range weapons.\n\n";
cout << "6. Warlord: An agile class that uses long-range weapons.\n\n";
cout << flush;
choice = getch();
switch(choice)
{
case ''1'':
strcpy(player.characterclass,"Blood Mage\0");
player.strength += 20;
player.defence += 20;
player.magic += 20;
player.agility += 20;
break;
case ''2'':
strcpy(player.characterclass,"Thief\0");
player.strength += 15;
player.defence += 15;
player.magic += 20;
player.agility += 30;
break;
case ''3'':
strcpy(player.characterclass,"Elemental Mage\0");
player.strength += 10;
player.defence += 15;
player.magic += 35;
player.agility += 20;
break;
case ''4'':
strcpy(player.characterclass,"Ninja\0");
player.strength += 30;
player.defence += 10;
player.magic += 15;
player.agility += 25;
break;
case ''5'':
strcpy(player.characterclass,"Gladiator\0");
player.strength += 30;
player.defence += 30;
player.magic += 10;
player.agility += 10;
break;
case ''6'':
strcpy(player.characterclass,"Warlord\0");
player.strength += 20;
player.defence += 20;
player.magic += 10;
player.agility += 30;
break;
default:
cout << "Not a valid choice.\n\n";
cout << "Press any key to continue...";
cout << flush;
getch();
done = false;
break;
}
} while (!done);
player.maxmana = 2 * player.magic;
player.mana = player.maxmana;
player.maxhp = 2 * player.defence;
player.hp = player.maxhp;
player.battleinterval = 1000.0f / (float)player.agility;
result = CharacterResults();
if (result == ''n'' || result == ''N'')
{
Cls();
cout << "You may now choose again, press any key...";
cout << flush;
getch();
done2 = false;
}
} while (!done2);
return 0;
}

int Story()
{
Cls();
cout << "Story:\n\n"
<< "Welcome to the land of Relourin, young warrior. You have been selected by the\n"
<< "King himself for this mission.\n\n"
<< "This land is in grave danger. Something has emerged into the depths of the\n"
<< "Great Volcano. Many strange things have been happening. Strange unfriendly\n"
<< "creatures have been roaming the countryside, looking for a fight. The local\n"
<< "villages have been sabotaged. There are far too many of them for us to ward\n"
<< "off without you. You are our only hope.\n\n"
<< "Leave with haste, and rid this land of evil before it''s too late.\n\n"
<< "Press any key to continue...";
cout << flush;
getch();
CharacterMenu();
Cls();
cout << "Press any key to exit..." << flush;
getch();
return 0;
}

int main()
{
char choice;
Start:
Cls();
cout << "RPG\n\n" << "What would you like to do?\n\n";
cout << "1. New Game\n" << "2. Resume Game\n" << "3. Exit\n\n";
cout << flush;
choice = getch();
switch(choice)
{
case ''1'':
Story();
break;
case ''2'':
Cls();
cout << "Not yet available\n\n\n";
cout << "Press any key to reset...";
cout << flush;
getch();
goto Start;
case ''3'':
Cls();
cout << "Press any key to exit..." << flush;
getch();
return 0;
default:
cout << "Not a valid choice.\n\n";
cout << "Press any key to continue...";
cout << flush;
getch();
goto Start;
}
return 0;
}



It works perfectly so far.
Kylotan
Kylotan
Hint #1 - you don''t need to include the \0 inside those string literals. The use of the quotes (eg. "this is a string") tells the compiler that you want a zero-terminated string. so it will add the zero at the end for you. At the moment you''re wasting a byte in each of those strings, and more importantly wasting your time typing \0 each time.

Hint #2 - when you start your next project, use the modern C++ libraries - that is, iostream rather than iostream.h for example - as the old ones are no longer supported and will make it harder for you to get help. Switching to using a string class such as std::string will also make a lot of things easier.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
gamechampionx
gamechampionx
Actually, I need \0, otherwise, the text displays incorrectly in certain sections.

I''m just wondering how I can make the game less choppy. When you press a wrong key when selectiong character in, I use system("cls"). Is there a way to instead locate a certain position on the screen, and output some " "s to clear part of the screen? Also, what is my next step? How do I make levels?
nonnus29
nonnus29
www.gametutorials.com has several tutorial about formatting console output like your asking about. However their tutes are for win32 so if your not using vis c++ or dev c++ they won''t be much help.
Christian Leinen
Christian Leinen
There''s a set of console functions that you can use when working with a Win32 Console application in MSVC. You can easily write a little library with wrappers like these:


  
#include <windows.h>


void ClearScreen()
{
COORD coordPos = {0};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwWritten = 0;
FillConsoleOutputCharacter(hConsole, '' '', 4000, coordPos, &dwWritten);
}


void ClearPos(WORD wRow, WORD wColumn, WORD wRowCount, WORD wColumnCount)
{
COORD coordPos = {wColumn, wRow};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwWritten = 0;
while(wRowCount--)
{
FillConsoleOutputCharacter(hConsole, '' '', wColumnCount, coordPos, &dwWritten);
coordPos.Y++;
}
}


bool GetPos(LPWORD pwRow, LPWORD pwColumn)
{
CONSOLE_SCREEN_BUFFER_INFO csbInfo = {0};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleScreenBufferInfo(hConsole, &csbInfo))
{
*pwRow = csbInfo.dwCursorPosition.Y;
*pwColumn = csbInfo.dwCursorPosition.X;
return true;
}
else
return false;
}


bool SetPos(WORD wRow, WORD wColumn)
{
COORD coordPos = {wColumn, wRow};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
return(SetConsoleCursorPosition(hConsole, coordPos));
}


More info: Platform SDK -> Base Services -> Files and I/O -> Console Reference

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.