My Game

closed account (iNU7ko23)
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <ctime>
using namespace 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";
}
else if(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.
Last edited on
closed account (iNU7ko23)
Thanks! All silly mistakes... :P
Topic archived. No new replies allowed.