Jump to content


MY C++ Text based game :P


14 replies to this topic

#1 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 06 April 2008 - 10:11

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.


#2 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 April 2008 - 10:55

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.

Posted Image

#3 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 06 April 2008 - 12:57

The download is the already compiled .exe :)

#4 Waris

    Endless Sip

  • Gold Member
  • 7458 posts
  • Projects: The End of Days, DTU Donutin Council Co-Chairman

Posted 06 April 2008 - 12:59

I don't understand shit :)

#5 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 April 2008 - 13:04

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.


Posted Image

#6 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 06 April 2008 - 13:05

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

#7 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 April 2008 - 13:09

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.


Posted Image

#8 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 06 April 2008 - 13:17

#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)



#9 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 April 2008 - 13:57

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.

Posted Image

#10 Sgt. Rho

    Kerbal Rocket Scientist

  • Project Leader
  • 6870 posts
  • Projects: Scaring Jebediah.

Posted 06 April 2008 - 14:00

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

EDIT: Fix0rt

Attached File(s)


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


#11 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 April 2008 - 14:26

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

Posted Image

#12 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 06 April 2008 - 21:02

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

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#13 Reaper94

    rawr!!

  • Member
  • 1178 posts
  • Projects: Being more loved and less loathed by community

Posted 06 April 2008 - 21:05

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?

 RaiDK, on 3 Jun 2009, 10:09, said:

MY BEAK IS ONE WHICH WILL PIERCE THE HEAVENS.

Posted Image

#14 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 06 April 2008 - 21:07

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

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#15 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 April 2008 - 21:20

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.

Posted Image



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users