Jump to content


Check out our Community Blogs

notes

Member Since 09 Apr 2011
Offline Last Active Apr 25 2013 02:09 AM
-----

Posts I've Made

In Topic: Math Operators as Characters

08 November 2012 - 02:31 AM

You might want to do something like :
if ( op == '+') {
    int a = 10 + 5;
}

if ( op == '-') {
    int a = 10 -5;
}

In Topic: Comparing Characters in Strings

03 November 2012 - 11:52 AM

Well, standard string library isn't as good as String library in Qt (QString) or C#, or java strings. There is no ( that I know ) method in standard string library to lower or upper case characters. Howewer you might wan't to write your own, since it's not so hard.
All you will need is ASCII table like one here :
http://www.asciitable.com/

function to lower would look like :
string toLower(string str) {
    for (int i=0;i<str.length();i++)
	    if ( (str[i] >= 65) && (str[i] <= 90) )
		    str[i] = str[i] + 32;
    return str;
}
As you see I am comparing ASCII values(decimal ones) of each character in passed string.
then if value is out of lower's character range I'm incrementing it's ASCII value.
Go on check it out and try to make own toUpper function.

In Topic: Getting classes to interact with eachother

29 October 2012 - 01:51 AM

Recently, I wrote simple - command line RPG game using couple design patterns. I woul'd like to show you how my attack and defense methods look like :

This is my heroes defense method, dont pay much attention to members that are not declared in this scope it's just for showing :
void Hero::defence(int recived_attack) {
/*first Im getting my heroes weapon's defense atribute*/
int bonus =
(firstWeapon.defence_power + secondWeapon.defence_power)/2;
bonus+=agility;

HANDLE hConsole; // Win32 console handle
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10); //10 for green
/*Now we want to add some randomization to our fight*/
recived_attack-= (rand() % bonus+1 );

if (recived_attack < 1 ) {
cout << "\nYou deflected opponents attack!";
}

if (recived_attack > 0 ) {
	 cout << "\nYou recived "<<recived_attack<<" hp damage.";
if (recived_attack >= current_hp ){
SetConsoleTextAttribute(hConsole, 192);
cout << "\nYou are dead.";
alive = false; // Set character to dead
}

if (recived_attack < current_hp ) {
current_hp-=recived_attack;
// Show characters hp left.
cout << "\nYou have " << current_hp << " hp left.";
}
}
SetConsoleTextAttribute(hConsole, 7);
}

As you have seen I've added text atributes to make game more readable. It will work only on windows though.
This is my basic defence algorithm, I've figured it out myself and wanted to make it as simple as possible cause project was growing larger and larger.

Now my, attack method :
int Hero::attack() {
/*Get weapons parameters*/
int hit_power = firstWeapon.attack_power + secondWeapon.attack_power;
hit_power+=power;
hit_power/=3;
/*add some randomization*/
hit_power+= (rand () % agility +1);
return hit_power;
}

Then I have similar methods for creatures ( defense / attack ). And how I am calling them in the fight:
while (hero->alive && oponent->alive) {
oponent->defence(hero->attack());
if (oponent->alive) {
hero->defence(oponent->attack());
}
}
This is always hero attacking first, you could do some randomization there to make game more fun.
This whole project has grown very big so I wont post all code ( its 7 header and cpp files ).

Hope this explains basics of interaction between objects in c++ on given kind a rpg-commandline code samples.

In Topic: Classes!

12 October 2012 - 04:04 PM

showing some kind of destructor would be nice. Also you didn't mention about things in destructor that happen when declaring dynamicly / staticly objects ( you need to delete *ptrs and by default 'type objects' are taken care of by compiler). All in all its a good piece of work! I hope you wont stop by writing this tutorial only.

In Topic: C++ classes

12 October 2012 - 01:19 AM

It is common to use small letters for members of class. You use capital letter to declare new class, and underscore for methods. It isn't any error and there are different ways to code but it's good practice to differ methods from members and class declarations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download