patterncppMinor
Battle system in C++
Viewed 0 times
battlesystemstackoverflow
Problem
I am not really having any problems with this. I'm just wondering if anybody has any ideas for a path for me to look down for bettering this little FF turn-based style of game in my free time.
I know somebody is going to complain about my global variables and the fact that I am not using object-oriented programming, but it will eventually get there. This is a boredom project that I'm plunking through.
Any advice or insight on any issues or ideas would be cool. And if somebody is looking into making a turn-based style of game, they can feel free to use up my code. Sorry about any bad formatting as well; I haven't really learned that yet.
```
// random.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int main()
{
int eHealth=100; //initial Enemy Health(based on enemy)
int pHealth=100; //initial Player Health
int eAttack=0; //Enemy Attack Strength(based on enemy)
int pAttack=0; //Player Attack Strength(based on level and items)
int selection=0; //Selection Variable for Battle Menue
int hPower=0; //Healing power Variable(random number)
int eSelection=0; //Enemies battle menu selection variable
int counter=0; //Counter to establish whos turn it is
int itemSelect=0; //Item inventory selection
int eMagic=50; //Enemies magic meter
int pMagic=50; //Players magic meter
cout>selection;
srand(static_cast(time(0)));//randomize all the random variables
switch(selection)
{
case 1:// player chooses to ATTACK
pAttack=1+rand()%(35-1+1);//attack power can be between 1-35
cout9)
{
hPower=1+rand()%(35-1+1);//healing power can be any number between 1-35
cout>itemSelect;
switch(itemSelect)
I know somebody is going to complain about my global variables and the fact that I am not using object-oriented programming, but it will eventually get there. This is a boredom project that I'm plunking through.
Any advice or insight on any issues or ideas would be cool. And if somebody is looking into making a turn-based style of game, they can feel free to use up my code. Sorry about any bad formatting as well; I haven't really learned that yet.
```
// random.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int main()
{
int eHealth=100; //initial Enemy Health(based on enemy)
int pHealth=100; //initial Player Health
int eAttack=0; //Enemy Attack Strength(based on enemy)
int pAttack=0; //Player Attack Strength(based on level and items)
int selection=0; //Selection Variable for Battle Menue
int hPower=0; //Healing power Variable(random number)
int eSelection=0; //Enemies battle menu selection variable
int counter=0; //Counter to establish whos turn it is
int itemSelect=0; //Item inventory selection
int eMagic=50; //Enemies magic meter
int pMagic=50; //Players magic meter
cout>selection;
srand(static_cast(time(0)));//randomize all the random variables
switch(selection)
{
case 1:// player chooses to ATTACK
pAttack=1+rand()%(35-1+1);//attack power can be between 1-35
cout9)
{
hPower=1+rand()%(35-1+1);//healing power can be any number between 1-35
cout>itemSelect;
switch(itemSelect)
Solution
-
-
You should generally call
-
You are doing an awful lot of similar I/O over and over -- particularly, outputting a menu and getting a response. You should look at creating a function to do that, and then just call it whenever you want the user to choose something.
-
-
You have a bunch of magic numbers. You'd do better to make those constants; then you could use them by name, and wouldn't need comments telling people what they mean. :)
system("PAUSE") is entirely unnecessary here. You could do the same thing by simply clearing out cin and then reading a char.-
You should generally call
srand once, at the beginning of the program. Once you've seeded the PRNG, it'll be good for a while -- probably much longer than this program will run. :) The way you're doing it, if the loop runs twice in the same second, and the user makes the same choices, the same outcome will occur.-
You are doing an awful lot of similar I/O over and over -- particularly, outputting a menu and getting a response. You should look at creating a function to do that, and then just call it whenever you want the user to choose something.
-
using namespace std; is laziness. :) If you don't want to type std:: before that stuff, i'd recommend at least only using the names you actually use. Otherwise, you could run into interesting issues later when you decide to use the same names that the standard library used (which is not at all uncommon).-
You have a bunch of magic numbers. You'd do better to make those constants; then you could use them by name, and wouldn't need comments telling people what they mean. :)
Context
StackExchange Code Review Q#38544, answer score: 7
Revisions (0)
No revisions yet.