←  Code Block

Fallout Studios Forums

»

MY C++ Text based game :P

Sgt. Rho's Photo Sgt. Rho 06 Apr 2008

First, a thanks goes to codecat and warbz for helping indirectly :)

Well, you can choose between 3 difficulty levels ,and have to kill a monster which, depending oon the difficulty level, has 100, 200 or 300 HP.

Works perfectly fine^^

#include <iostream>
 #include <conio.h>
 #include <windows.h>
 #include <ctime>
 #include <string>
 
 using namespace std;
 
 
 int UserChoice;
 int BeastDamage;
 string Player;
 int Difficulty;
 
 int main &#40;&#41;
 {
	 cout << &#34;Hallo. \n Wie heisst du? \n&#34;;
	 cin >> Player;
	 cout << &#34;\n Waehle bitte einen schwierigkeits grad \n&#34;;
	 cout << &#34;1 fuer Leicht &#40;Gegner hat 100 Lebenspunkte&#41; \n 2 fuer Mittel &#40;Gegner hat 200 Lebenspunkte&#41; \n 3 fuer Schwer &#40;Gegner hat 300 Lebenspunkte&#41; \n&#34;;
	 cin >> Difficulty;
	 getch&#40;&#41;;
 
		if &#40; !&#40;cin >> Difficulty&#41; || &#40;Difficulty != 1 && Difficulty != 2 && Difficulty != 3&#41;&#41;
		 {
			 cin.clear&#40;&#41;;
			 cin.ignore&#40;numeric_limits<streamsize>&#58;&#58;max&#40;&#41;, &#39;\n&#39;&#41;;
 
			 cout << &#34;Bitte 1 fuer Leicht, 2 fuer Mittel, oder 3 fuer Schwer eingeben! \n&#34;;
		 }
 
 int InitHealth = 100;
 int BeastHealth = 100 * Difficulty;
 
   while &#40;BeastHealth > 0 && &#40;Difficulty == 1 || Difficulty == 2 || Difficulty == 3 &#41;&#41; {
 
	 cout << &#34;Du wirst von einem Monster angegriffen! \n&#34;;
	 cout << &#34;Was wirst du tun? Gib 1 ein um anzugreifen oder 2 um dich zu heilen \n&#34;;
	 cin >> UserChoice;
	 cout << &#34;Du hast &#34; << InitHealth << &#34; Lebenspunkte \n&#34;;
	 getch&#40;&#41;;
 
 
 
	 switch&#40;UserChoice&#41;
	 {
		 case 1&#58;
			 {
 
	 int DamageDone = rand&#40;&#41;%30;
 
 
 
		 cout << &#34;Du hast dem Monster &#34; << DamageDone << &#34; Lebenspunkte Abgezogen \n&#34;;
		 BeastHealth = BeastHealth - DamageDone;
 
		  if &#40;BeastHealth < 30&#41; {
			 int BeastHealed = rand&#40;&#41;%30;
			 BeastHealth = BeastHealth + BeastHealed;
 
			 cout << &#34;Das monster hat &#34; << BeastHealed << &#34; Regeneriert! \n&#34;; } else if &#40;BeastHealth > 30&#41;  {
				 BeastDamage = rand&#40;&#41;%25;
				 InitHealth = InitHealth - BeastDamage;
				 cout << &#34;Das monster hat dir &#34; << BeastDamage << &#34; Lebenspunkte abgezogen! \n Du hast noch &#34; << InitHealth << &#34; Lebenspunkte \n&#34;; }
 
		 cout << &#34;Das Monster hat noch &#34; << BeastHealth << &#34; Lebenspunkte \n&#34;;
 
 
 
 if &#40;BeastHealth < 1&#41; { cout << &#34;Gute arbeit, du hast Gewonnen!&#34;; } else if &#40;InitHealth < 1&#41; { cout << &#34;Das Monster hat dich getötet!&#34;; }
			 }
			 break;
 
 
		 case 2&#58;
			 {
	 int Healed = rand&#40;&#41;%30;
 
			 if &#40;BeastHealth < 30&#41; {
			 int BeastHealed = rand&#40;&#41;%30;
			 BeastHealth = BeastHealth + BeastHealed;
 
			 cout << &#34;Das monster hat &#34; << BeastHealed << &#34; Regeneriert! \n&#34;; } else if &#40;BeastHealth > 30&#41;  {
				 BeastDamage = rand&#40;&#41;%25;
				 InitHealth = InitHealth - BeastDamage;
				 cout << &#34;Das monster hat dir &#34; << BeastDamage << &#34; Lebenspunkte abgezogen! \n Du hast noch &#34; << InitHealth << &#34; Lebenspunkte \n&#34;; }
 
		 InitHealth = InitHealth + Healed;
		 cout << &#34;Du hast &#34; << Healed << &#34; Lebenspunkter regeneriert. Du hast nun &#34; << InitHealth << &#34; Lebenspunkte \n \n&#34;;
			 }
			 if &#40;BeastHealth < 1&#41; { cout << &#34;Gute arbeit, du hast Gewonnen!&#34;; } else if &#40;InitHealth < 1&#41; { cout << &#34;Das Monster hat dich getötet!&#34;; }
 
		 if &#40; !&#40;cin >> UserChoice&#41; || &#40;UserChoice != 1 && UserChoice != 2&#41;&#41;
		 {
			 cin.clear&#40;&#41;;
			 cin.ignore&#40;numeric_limits<streamsize>&#58;&#58;max&#40;&#41;, &#39;\n&#39;&#41;;
 
			 cout << &#34;Bitte 1 zum Angreifen oder 2 zum Heilen eingeben! \n&#34;;
		 }
 }
 
 
 }
 getch&#40;&#41;;
 return 0;
 }


Whoops...that was the old code, updated.

Attached File(s)


Edited by Master_Chief, 06 April 2008 - 10:36.
Quote

Warbz's Photo Warbz 06 Apr 2008

Looks alright but I can't read what I assume is German?
Theres no point having it available for download as its easier just to copy and paste the code if anyone is going to try it.
Quote

Sgt. Rho's Photo Sgt. Rho 06 Apr 2008

The download is the already compiled .exe :)
Quote

Waris's Photo Waris 06 Apr 2008

I don't understand shit :)
Quote

Warbz's Photo Warbz 06 Apr 2008

Yeh, It don't help that all the outputs are in German.

EDIT: And I just noticed all the variables are in English. Master_Chief you trying to see how many languages you can use at once or something? lol
Edited by Warbz, 06 April 2008 - 13:05.
Quote

Sgt. Rho's Photo Sgt. Rho 06 Apr 2008

I will post it in english too, just lemme translate it :)
Quote

Warbz's Photo Warbz 06 Apr 2008

Nah, Try and work some Spanish words in there too.
You'll then have 4 languages in it.

German
English
Spanish
and of course C++.

lmao

EDIT: Maybe even put some morse code in it!
Edited by Warbz, 06 April 2008 - 13:09.
Quote

Sgt. Rho's Photo Sgt. Rho 06 Apr 2008

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <string>

using namespace std;


int UserChoice;
int BeastDamage;
string Player;
int Difficulty;

int main &#40;&#41;
{
	cout << &#34;Hello. \n Who are you? \n&#34;;
	cin >> Player&#59;
	cout << &#34;\n Please select a difficulty level \n&#34;;
	cout << &#34;Enter 1 for easy, 2 for medium or 3 for hard difficulty level \n&#34;;
	cin >> Difficulty&#59;
	getch&#40;&#41;;

	   if &#40; !&#40;cin >> Difficulty&#41; || &#40;Difficulty != 1 && Difficulty != 2 && Difficulty != 3&#41;&#41;
		{
			cin.clear&#40;&#41;;
			cin.ignore&#40;numeric_limits<streamsize>&#58;&#58;max&#40;&#41;, &#39;\n&#39;&#41;;

			cout << &#34;Enter 1 for easy, 2 for medium or 3 for hard difficulty level \n&#34;;
		}

int InitHealth = 100;
int BeastHealth = 100 * Difficulty;

  while &#40;BeastHealth > 0 && &#40;Difficulty == 1 || Difficulty == 2 || Difficulty == 3 &#41;&#41; {

	cout << &#34;A Beast is attacking you! \n&#34;;
	cout << &#34;What are you gonna do? Press 1 to attack or 2 to heal! \n&#34;;
	cin >> UserChoice;
	cout << &#34;You have &#34; << InitHealth << &#34; Hit Points. \n&#34;;
	getch&#40;&#41;;



	switch&#40;UserChoice&#41;
	{
		case 1&#58;
			{

	int DamageDone = rand&#40;&#41;%30;



		cout << &#34;You have damaged the enemy for &#34; << DamageDone << &#34; Hit Points \n&#34;;
		BeastHealth = BeastHealth - DamageDone;

		 if &#40;BeastHealth < 30&#41; {
			int BeastHealed = rand&#40;&#41;%30;
			BeastHealth = BeastHealth + BeastHealed;

			cout << &#34;The Beast has recovered &#34; << BeastHealed << &#34; Hit Points! \n&#34;; } else if &#40;BeastHealth > 30&#41;  {
				BeastDamage = rand&#40;&#41;%25;
				InitHealth = InitHealth - BeastDamage;
				cout << &#34;The Hostile beeing has damaged you for &#34; << BeastDamage << &#34; HP! \n You still have &#34; << InitHealth << &#34; Hit Points \n&#34;; }

		cout << &#34;The Monster has &#34; << BeastHealth << &#34; HP Remaining \n&#34;;



if &#40;BeastHealth < 1&#41; { cout << &#34;Great job, you have defeated the beast! &#34;; } else if &#40;InitHealth < 1&#41; { cout << &#34;You are Defeated&#34;; }
			}
			break;


		case 2&#58;
			{
	int Healed = rand&#40;&#41;%30;

			if &#40;BeastHealth < 30&#41; {
			int BeastHealed = rand&#40;&#41;%30;
			BeastHealth = BeastHealth + BeastHealed;

			cout << &#34;The Beast has recovered &#34; << BeastHealed << &#34; Hit Points! \n&#34;; } else if &#40;BeastHealth > 30&#41;  {
				BeastDamage = rand&#40;&#41;%25;
				InitHealth = InitHealth - BeastDamage;
				cout << &#34;The Hostile beeing has damaged you for &#34; << BeastDamage << &#34; HP! \n  You still have &#34; << InitHealth << &#34; Hit Points \n&#34;; }

		InitHealth = InitHealth + Healed;
		cout << &#34;You did recover &#34; << Healed << &#34; Health Points. \n You have now &#34; << InitHealth << &#34; Health Points \n \n&#34;;
			}
			if &#40;BeastHealth < 1&#41; { cout << &#34;Great job, you have defeated the beast!&#34;; } else if &#40;InitHealth < 1&#41; { cout << &#34;You are Defeated!&#34;; }

		if &#40; !&#40;cin >> UserChoice&#41; || &#40;UserChoice != 1 && UserChoice != 2&#41;&#41;
		{
			cin.clear&#40;&#41;;
			cin.ignore&#40;numeric_limits<streamsize>&#58;&#58;max&#40;&#41;, &#39;\n&#39;&#41;;

			cout << &#34;Enter either 1 to attack or 2 to heal! \n&#34;;
		}
}


}
getch&#40;&#41;;
return 0;
}

Attached File(s)

Quote

Warbz's Photo Warbz 06 Apr 2008

Right,
1) I think the way the information is displayed to the player is pretty bad.
2)I have -11 health and am still attacking the enemy.
Quote

Sgt. Rho's Photo Sgt. Rho 06 Apr 2008

what? Hmm...I guess I made an error there >.<

EDIT: Fix0rt

Attached File(s)


Edited by Master_Chief, 06 April 2008 - 14:10.
Quote

Warbz's Photo Warbz 06 Apr 2008

Also when you use a cin (User input) dont use a getch(), its annoying.
Quote

CodeCat's Photo CodeCat 06 Apr 2008

getch() shouldn't really be used in general anyway.
Quote

Reaper94's Photo Reaper94 06 Apr 2008

View PostWarbz, on 6 Apr 2008, 11:55, said:

Looks alright but I can't read what I assume is German?
Theres no point having it available for download as its easier just to copy and paste the code if anyone is going to try it.


how wld u do that?

copy and paste into notepad and save or somming?
Quote

CodeCat's Photo CodeCat 06 Apr 2008

No, copy and paste into a C++ source file and then compile and run it.
Quote

Warbz's Photo Warbz 06 Apr 2008

IF you don't know that, then unless you actually want to properly learn the C++ language, theres no point explaining it to you, just use the .exe provided.
Quote