Feb 29, 2012 at 8:47pm UTC
HI tell me what you think about this program. Also how can i put a class in it or is it too small to put a class
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int comphealth = 175;
int yourhealth = 175;
int whatattack;
int attack;
int shoot;
int heal;
char yourname [20];
int restart;
void yourchoice()
{
srand(time(0));
attack = rand() %30;
shoot = rand() %25;
heal = rand() %11;
cout << "Its your turn: " ;
cin >> whatattack;
switch (whatattack)
{
case 1:
comphealth = comphealth - attack;
break ;
case 2:
comphealth = comphealth - shoot;
break ;
case 3:
yourhealth = yourhealth + heal;
break ;
}
cout << "Your health is " << yourhealth << endl;
cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}
void compattack()
{
srand(time(0));
attack = rand() %40;
cout << "Now its computers turn" << endl;
cout << "He strikes you in the chest!" << endl;
yourhealth = yourhealth - attack;
cout << "Your health is " << yourhealth << endl;
cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}
void yourchoiceagain()
{
srand(time(0));
attack = rand() %30;
shoot = rand() %25;
heal = rand() %11;
cout << "Its your turn: " ;
cin >> whatattack;
switch (whatattack)
{
case 1:
comphealth = comphealth - 20;
break ;
case 2:
comphealth = comphealth - 15;
break ;
case 3:
yourhealth = yourhealth + 5;
break ;
}
cout << "Your health is " << yourhealth << endl;
cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}
int main()
{
do {
system("CLS" );
if (yourhealth <= 0 || comphealth <= 0){
yourhealth = 175;
comphealth = 175;
}
cout << "Enter your name: " ;
cin >> yourname;
cout << yourname << " VS " << "COMPUTER" << endl << endl;
cout << "Here are your options:" << endl;
cout << "Press 1 and enter to strike your opponent" << endl;
cout << "Press 2 and enter to shoot your opponent" << endl;
cout << "Press 3 and enter to heal yourself" << endl << endl;
cout << "Time to fight! " << endl;
yourchoice();
compattack();
while (comphealth > 0 && yourhealth > 0)
{
yourchoiceagain();
compattack();
if (comphealth <= 0 || yourhealth <= 0){
break ;
}
}
if (comphealth <= 0){
cout << "You win!" << endl;
}
else if (yourhealth <= 0){
cout << "You lose" << endl;
}
cout << "Enter 1 to quit or enter 2 to restart: " ;
cin >> restart;
}while (restart == 2);
cin.get();
return 0;
}
Last edited on Feb 29, 2012 at 8:47pm UTC
Feb 29, 2012 at 9:33pm UTC
First of all, you need to use function prototypes. It can cause all kinds of "undeclared function" errors if you call a function somewhere that is declared further down in the program.
Second... nothing is too small for a class, but judging by your program's look and simplicity, I'd say you're probably not ready for classes. Learn the basics, first.
Last edited on Feb 29, 2012 at 9:33pm UTC
Feb 29, 2012 at 9:34pm UTC
what are function prototypes
Feb 29, 2012 at 9:38pm UTC
and if i did know enough for classes...how would i use them in this program
Feb 29, 2012 at 9:40pm UTC
function prototypes is the same as function declarations. You don't really need it here because you define the function before you call it.
You should only call srand once at the start of your program. Make the call at the start of main and remove all other calls to srand.
Feb 29, 2012 at 11:07pm UTC
Classes go far, far beyond that little tiny simplistic view of it. You can actually make your player and the computer each into a subclass of a character, one controllable by the computer, and the other controllable by the user. Then you make functions that interact with their data to repetitively enter a function that can access both of the subclasses and perform the combat functions.
Mar 1, 2012 at 1:32am UTC
That's what packetpirate was implying. If you don't know how to use methods and interact between classes, then you need to study up a bit more.
Mar 1, 2012 at 1:53am UTC
Well, that's simple. You're only using the random once, and then passing the same value to both attack functions.