Hi I just made my first game without help however I'm not sure I did it very efficiently. If you want to you can compile it and please give me some feedback on how I could improve next time.
#include <iostream>
#include <ctime>
usingnamespace std;
int playerlife = 15;
int trolllife = 50;
void f(int choice);
int main()
{
int answer;
cout << "You are on a adventure to save a princess when you come across a troll!\n";
cout <<" Let's battle!\n";
do {
cout << "Playerlife: " << playerlife << "\n";
cout << "Trolllife: " << trolllife << "\n";
cout << "Do you want to...?\n";
cout << "1) Strike the troll!\n";
cout << "2) Defend yourself!\n";
cin >> answer;
if(answer == 1 || answer == 2)
{
f(answer);
}
}
while(playerlife > 0 && trolllife > 0);
if(playerlife == 0)
cout << "You died and so will the princess.";
else
cout << "Congrats you won and save the princess!";
int exit;
do
{
cout << "Press 1 to exit";
cin >> exit;
}
while( exit != 1);
return 0;
}
void f(int choice)
{
srand ( time(NULL) );
int secret = rand() % 8 + 1;
switch(choice) {
case 1: cout << "You attack the troll.\n";
if(secret > 2)
{
cout << "You strike ten points damage to the troll.\n";
trolllife= trolllife - 10;
break;
}
else
{
cout << "Your attack recoils and you lose five damage.\n";
playerlife = playerlife - 5;
break;
}
return;
case 2: cout << "You defend yourself.\n";
if(secret > 4)
{
cout << "You don't lose any damage and neither does the Troll.\n";
}
elseif(secret > 2)
{
cout << "Your defence recoils and you do five damage to the troll.\n";
trolllife= trolllife - 5;
}
else
{
cout << "The troll avades your defence and you lose ten damage.\n";
playerlife = playerlife - 10;
}
return;
}
return;
}
Move srand() on Line 59 up into your main function, you should only call this function once per execution of the program. The 'break' commands in Line 68 and Line 74 don't do anything. The princess should always be in another castle :P.