A C++ game in a day!

Started by
84 comments, last by delta user 15 years, 10 months ago
[rolls eyes]
Please don't start telling cj270608 about classes and suchlike yet, he is still cannibalising the very basics of procedural c++ usage at the moment, and will be far better equipped to deal with that in a few projects time...

At least wait until he has learnt what a function is...
[/harsh]

@cj270608: Does your current source compile successfully? especially when you are starting you should try to write a few lines, then compile, and test. Then repeat until you have a full program, otherwise when you are finished writing and try to compile you will have so many errors that you will never be able to cope with them by yourself! Will still be around here if you get stuck on sthing.

Mathmo
Advertisement
Quote:Original post by Mathmo
[rolls eyes]
Please don't start telling cj270608 about classes and suchlike yet, he is still cannibalising the very basics of procedural c++ usage at the moment, and will be far better equipped to deal with that in a few projects time...

At least wait until he has learnt what a function is...
[/harsh]

@cj270608: Does your current source compile successfully? especially when you are starting you should try to write a few lines, then compile, and test. Then repeat until you have a full program, otherwise when you are finished writing and try to compile you will have so many errors that you will never be able to cope with them by yourself! Will still be around here if you get stuck on sthing.

Mathmo


Sorry, was just trying to help. :(

I went back and reread his 'coding' and well, it doesn't look like any of those examples would compile and he keeps adding more. Strange. It also seems he hasn't created any functions so classes will be way beyond him. Well an example is there for him once he gets a grip on functions and input/output.
http://lempface.blogspot.com/Read about the RogueLike I'm Developing at my blog!
This compiles perfectly well.

#include <cstdlib> #include <ctime> #include <iostream>using namespace std;int main(){	srand((unsigned)time(0)); 	int php = 500;	int pmaxhp = 500;	int pmp = 111;	int pmaxmp = 111;	int patk = 55;	int pdef = 50;	int pdmg = 0;	int pheal = 0;	int ehp = 765;	int emaxhp = 765;	int emp = 99;	int emaxmp = 99;	int eatk = 66;	int edef = 60;	int edmg = 0;	int chance = 0;	cout << "Welcome, adventurer!" "\n";	cout << "A monster appears!" "\n";	bool won = false;	bool lost = false;	while (!won && !lost)	{	cout << "HP " << php << "/" << pmaxhp << " ";	cout << "MP " << pmp << "/" << pmaxmp << "\n";	cout << "Your attack! What will you do? \n";	cout << "1: Use sword (50 POW, 99% ACC) \n";	cout << "2: Attack with a fireball (~100HP, 70% ACC, 17MP) \n";	cout << "3: Summon a blizzard (~60HP, 85% ACC, 10MP) \n";	cout << "4: Summon thunder (~180HP, 55% ACC, 28MP) \n";	cout << "5: Heal with magic (+~100HP, 20MP) \n";	cout << "6: Use psychic ability (2MP) \n";	cout << "7: Attempt to escape \n";	int action = 0;	while (action<1 || action>7) {		cin >> action; switch (action) { case 1:chance = rand() % 100;  if (chance == 99)  {	  cout << "Strangely, the sword missed... \n";	  break;  }  else  {	  cout << "The sword strikes! \n";	  pdmg = ((rand() % 41 + 80)*50*patk/(100*edef));	  cout << "The enemy was hit for " << pdmg << "HP damage! \n";	  ehp = ehp - pdmg;	  cout << "Your turn is over... \n";  }  break; case 2:	 if (pmp >=17)	 {	 pmp = pmp - 17;	 chance = rand() % 100;	 if (chance >= 70)	 {		 cout << "The fireball missed the target... \n";		 cout << "You have " << pmp << "MP left \n";		 break;	 }	 else	 {		 cout << "The fireball hit the enemy! \n";		 pdmg = ((rand() % 51 + 75));		 cout << "The enemy was hit for " << pdmg << "HP damage \n";		 ehp = ehp - pdmg;		 cout << "You now have " << pmp << "MP left. Your turn is over. \n";	 }	 }	 else	 {		 cout << "Not enough MP... \n";	 }  break; case 3:	 if (pmp >= 10) {	pmp = pmp - 10;	chance = rand() % 100;	 if (chance >= 85)	 {		 cout << "The blizzard did not harm the enemy... \n";		 cout << "You have " << pmp << "MP left \n";		 break;	 }	 else	 {		 cout << "The enemy felt the chill of the blizzard! \n";		 pdmg = ((rand() % 31 + 45));		 cout << "The enemy was hit for " << pdmg << "HP damage \n";		 ehp = ehp - pdmg;		 cout << "You now have " << pmp << "MP left. Your turn is over. \n";	 }	 }	 else	 {		 cout << "Not enough MP... \n";	 }	 break; case 4:	 if (pmp >= 28)	 {    pmp = pmp - 28;	 chance = rand() % 100;	 if (chance >= 55)	 {		 cout << "The thunder missed the enemy... \n";		 cout << "You have " << pmp << "MP left \n";		 break;	 }	 else	 {		 cout << "The enemy was hit by thunder! \n";		 pdmg = ((rand() % 91 + 135));		 cout << "The enemy was hit for " << pdmg << "HP damage \n";		 ehp = ehp - pdmg;		 cout << "You now have " << pmp << "MP left. Your turn is over. \n";	 }	 }	 else	 {		 cout << "Not enough MP... \n";	 }	 break; case 5:	 if (pmp >= 20)	 {  pmp = pmp - 20;  pheal = ((rand() % 21 + 90));  php = php + pheal;  cout << "You have healed yourself for " << pheal << "HP \n";  cout << "You now have " << php << "HP \n";  cout << "You now have " << pmp << "MP left. Your turn is over. \n";	 }	 else	 {		 cout << "Not enough MP... \n";	 }  break; case 6:	 if (pmp >=2)	 {  pmp = pmp - 2;  cout << "Using your psychic power, you sense the enemy's stats! \n";  cout << "Enemy has " << ehp << "HP remaining! \n";  cout << "Enemy has " << emp << "MP remaining! \n";  cout << "You now have " << pmp << "MP left. Your turn is over. \n";	 }	 else	 {		 cout << "Not enough MP... \n";	 }	 break; case 7:  cout << "Wimp! ";  cout << "Your turn has ended \n";  break; default:   cout << endl << "Invalid option! Try again: \n";   break;}	}if (php <= 0)		{			lost = true;			cout << "You have lost!\n";		}		else if (ehp <= 0)		{			won = true;			cout << "You have won!\n";}}}


I use Visual C++ 2008

What I need now is the enemy's turn
Get rid of won, or lost. you only need one. If its easier for you to understand, call that variable gameover.

gameover = False
then: while(!gameover)

there is no reason to check to see if they player has lost, once he has won, or won once he has lost.

For the enemy, just get a random number within range of the number of options the enemy has.

i.e. if all he can do is Stomp, Fireball, or Pump, get an integer between 0 and 2.

0 = stomp, 1 = fireball, 2 = pump. Now, if the option 1 comes up, make sure he has enough MP to cast the spell, if he doesn't you could skip his turn and say enemy tried to cast a spell but didn't have enough MP. OR, and this would be better imho, if the option fireball comes up and he doesn't have enough MP to cast, then get a new random number until the option isn't == 1. Then he'll stomp or pump instead of doing nothing.

http://lempface.blogspot.com/Read about the RogueLike I'm Developing at my blog!
All very good, but where abouts should I put it? I want it to loop back to the start of the player's turn once the enemy's turn has finished
Put a larger loop around both your playerturn loop and your monsterturn loop

so
int main(){     //do initialising stuff          bool playerWon = false;     bool gameOver = false;     while ( true != gameOver )     {          bool playerTurnOver = false;          while ( false != playerTurnOver )          {               // do your player turn bit here          }          if ( /*monster hp is zero*/ )          {               gameOver = true;               playerWon = true;               continue;          }          bool monsterTurnOver = false;          while ( false != monsterTurnOver )          {                // do your monster turn bit here          }          if ( /*player hp is zero*/ )          {               gameOver = true;               playerWon = false;               continue;          }     }     if ( playerWon )     {          // print victory text here     }     else     {          // print losing text     }}
heres an idea:


int main()
{
printf("Game Over\n");
return 0;
}
Quote:Original post by dmatter
Quote:Original post by Mercy84
In the standard for the language you've decided to code this game in, C++, main is declared int.

So make sure you put return 0; at the end of your main function to indicate to the calling process that the program exited happily.

It is legal to omit the return 0 in main.


Only if you declare main as returning void (e.g. void main() {...}). The OP declared it as an int.
Quote:Original post by BrianMJC
Quote:Original post by dmatter
Quote:Original post by Mercy84
In the standard for the language you've decided to code this game in, C++, main is declared int.

So make sure you put return 0; at the end of your main function to indicate to the calling process that the program exited happily.

It is legal to omit the return 0 in main.


Only if you declare main as returning void (e.g. void main() {...}). The OP declared it as an int.

No, the declaration void main is not legal. But omitting the return 0 statement is legal; that is explicitly stated in chapter 3.6.1 of the standard.

Quote:Original post by cj270608
This compiles perfectly well.

I find that very hard to believe. Have you actually tried it? EDIT: Nevermind. It should work. My bad.

[Edited by - Hnefi on June 10, 2008 1:22:39 PM]
-------------Please rate this post if it was useful.
Quote:Original post by cj270608
All very good, but where abouts should I put it? I want it to loop back to the start of the player's turn once the enemy's turn has finished


Try doing something similar to what you did for the player's turn, but instead of getting feedback from the player via cin, instead use rand() to determine the enemy's choice each round. Just place the code right after the player's code - it'll loop back by virtue of the while loop already in place.

P.S. I hope you're giving credit to the kind people here who helped you out. :)

This topic is closed to new replies.

Advertisement