A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

c++ sloppy little console game (not done)

1.1k views · started by HTML ·
#1
c++ sloppy little console game (not done)
Finish it if you want, or not. :D This was for praciticing with classes and function. I'm moving on now.



#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;



#define stop system("PAUSE")
#define clear system("cls")



//Player information against skeleton

class player
{

private:
int playerhp, playerexp, playerlvl;

public:
int Losehp ()
{
playerhp=10;
playerhp-=2;
return playerhp;

}

int Gainhp ()
{
playerhp++;
return playerhp;
}
int Gainexp ()
{
playerexp=+2;
return playerexp;

}

int Gainlvl ()
{
if(playerexp <= 3)
{

playerlvl=+1;

return playerlvl;
}


}

int Playeratacckingskele()

{
int Choice2, Choice3;
cin >> Choice2, Choice3;
int attack = 1;
int run = 2;

if(Choice2 == run)
{
cout << "Merlin: You may not run from this battle!" << endl;
cout << "Merlin: Choose again!" << endl;
return Playeratacckingskele();
}

if(Choice2 == attack)
{
clear;
cout << "*You battle the skeleton and are victorious!*\n\n";
cout << "Merlin: I will now relay your stats, and I will do this after every battle.\n";
cout << "Your hp is" <<" " << Losehp () << " " << "your exp is" <<" " << Gainexp () << " " << "your level is" <<" " << Gainlvl () << endl << endl;

return 0;

}

}

};




// hehe function to choose weapon.
int choose()
{
string Choice;
cin >> Choice;
cout << endl << endl;
string Bow, bow, Staff, staff, Sword, sword;
Bow = "Bow", bow = "bow", Staff = "Staff", staff = "staff", Sword = "Sword", sword = "sword";

// or "||" doesn't seem to work o.o?
if (Choice == Bow)
{
clear;
cout << "Merlin: A bow man EH? Well here is your weapon master Archer \n.";
cout << "*Merlin hands you your weapon.*\n\n";
return 0;
}

if (Choice == bow)
{
clear;
cout << "Merlin: A bowman Eh? Well here is your weapon master Archer.\n";
cout << "*Merlin hands you your weapon.*\n\n";
return 0;
}

if (Choice == Staff)
{
clear;
cout << "Merlin: Oh-ho? A fellow magician? I will help you in your quest to master\nthe dark arts!\n";
cout << "*Merlin hands you your weapon.*\n\n";
return 0;
}

if (Choice == staff)
{
clear;
cout << "Merlin: Oh-ho? A fellow magician? I will help you in your quest to master the\ndark arts!\n";
cout << "*Merlin hands you your weapon.*\n\n";
return 0;
}

if (Choice == Sword)
{ clear;
cout << "Merlin: There is no opponet to the mighty blade!\n";
cout << "*Merlin hands you your weapon.*\n\n";
return 0;
}

if (Choice == sword)
{
clear;
cout << "Merlin: There is no opponet to the mighty blade!\n";
cout << "*Merlin hands you your weapon.* \n\n";
return 0;
}


}


int main ()
{

cout << "Hello adventurer and welcome to the world of Cytonia! Before we begin type in\n" << "the username you wish to have below.\n\n";



//variables for the username
string Playername;
cin >> Playername;
cout << endl << endl;

cout << "So your name is" << " " << Playername << "?" << " " << "Great name! My name is Merlin and I'm here to help you\n" << "throughout your journey. Before we begin, you must select your weapon!\n\n";

cout <<"Choose from: \n" << "Bow" << endl << "Staff" << endl << "Sword\n\n";



choose();

// don't have to recall cout to make a new line O:!!!
cout << "Merlin: Now that you are properly geared, I will teach you about combat in\nCytonia. When faced against one of the many enimes in Cytonia, you will have a\nchoice to either Attack or Run. Attacking will make you lose HP but give you exp. For every 3 exp you get you gain one level, once your HP gets to 0 the game is over. You begin with 10hp and each level you gain, I heal you to full hp.\n\n";
cout << "Merlin: Lets practice this concept, I will summon a skeleton from the gates of\nhell to battle with you.\n\n";
cout << "*Merlin gently taps his staff against the ground and a skeleton runs toward you* what will you do? *SELECT A NUMBER ONLY!*\n\n";

cout << "1.)Attack" << endl << "2.)Run" << endl << endl;

player a;
a.Playeratacckingskele();


cout << "Ah, I see you are very skilled in combat.";


stop;
return 0;
}


#2
Wouldn't LoseHP() always return 8? Since you're assigning the value of 10 to playerhp every time you call the function.
#3
Yerp, but the program is only fighting one enemy and that's the skeleton. In the project assign that's all it asked for, so I went half ass on it. Brand new at function and classes. :P
#4
Why use so many IF's? Use a switch case.
#5
Chad wrote:
Why use so many IF's? Use a switch case.


I've never used it. I'll go practice it now.
#6
Its a great thing.

Chad showed me that.

I remember when I made my Pirate bot, I had loads of if's and stuff.

After a while I fixed it up with a switch/case.

Cut the loading time down a bit.
#7
Having trouble trying to use a lot of chars with switch. Can someone give me an example?
#8
HTML wrote:
Having trouble trying to use a lot of chars with switch. Can someone give me an example?


example in C#




string[] strData = txtCommands.Split('-');

switch(strData[0])
{
case "Hi":
//Do Command Here;
break;

case "Bye":
//Do Command Here;
break;
}


[/0]
#9
The book I'm reading decides to wait until I wrote all of that before telling me about the constructor. Wow.
#10
In C++ it would be:

switch (Variable_to_test)
{
case 'choice_1':
{
cout<<"Hi";
break;
}
case 'choice_2':
{
cout<<"bye";
break;
}
case 'choice_3':
{
cout<<"Hi again";
break;
}


You have to use a break if you want to leave the switch case :P