Improvements on a game

Started by
6 comments, last by truestory 11 years, 8 months ago
Hello Everyone i am just wondering if i can get some tips on how i could improve on my code that i have done, i know that some parts could be redone but i just want to know what parts i could make better


#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello everyone and welcome to this game.\n";
cout << "In this game the aim is to attack the wolf with what you think is the best attack, goodluck!\n";
int healthme=500;
int healthit;
for(healthit=500 ;healthme>0 && healthit>0; ) {
cout << "Your Health is " << healthme << " and the wolfs health is " << healthit << "\n";
cout << "So, what attack do you want to hit the wolf with a weapon or with magic?";
string attack;
cin >> attack;

if (attack.find("weapon") != std::string::npos) {
cout << "What part of the wolf do you want to attack, the body or the head?\n";
string bodypart;
cin >> bodypart;
if(bodypart.find("body") != std::string::npos) {
healthit=healthit-50;
cout << "You hit the wolf!\n";
}

else if(bodypart.find("head") != std::string::npos) {
healthit = healthit-100;
healthme=healthme-20;
cout << "It was a good hit, but it bites back!\n";
}

else cout << "you missed! (spelt it wrong) \n";
}
else if (attack.find("magic") != std::string::npos){
cout << "what element would you like to attack with? fire lightning, ice, water, or earth?\n";
string magic;
cin >> magic;
if(magic == "fire"){
healthit=healthit-50;
cout << "It does good damage!\n";
}
else if(magic == "lightning"){
healthit=healthit-100;
cout << "It does amazing damage!\n";
}

else if(magic == "earth"){
healthit= healthit-70;
cout << "It hits hard!\n";
}
else if(magic=="ice") {
healthit = healthit-30;
cout << "It doesn't seem to hurt much!\n";
}
else if(magic=="water") {
healthit = healthit - 10;
cout << "It seems unaffected!\n";
}
else cout << "You missed! (spelt it wrong!)\n";
}
else cout << "you took to long! (spelt wrong!)";
if(healthit>=0){
if(healthit<=100){

cout << "He is Angry now and doing more damage!\n";
healthme = healthme-75;
}
else {
cout << "The wolf goes out to attack!\n";
healthme= healthme-50;
}
}
}
if(healthme<=0) cout << "Your Dead... Game over i guess?";
else cout << "The wolf is dead! You Win!";

return 0;
}




if you can bother please take the time and tell me what i could make better biggrin.png thank you
Advertisement
From what I assume you set out to do, this is quite fine. It's not really a complex design that can have many solutions, so I guess it's not much to comment on.

How ever, if you would expand the attacks and magic to include more different types you should probably go with a strategy pattern instead of all those if-else statements. You basically replace the if-else statements with an object structure with virtual calls.

There's no point in doing that to the existing code, since its so small, but any larger switches should use something similar to the strategy pattern.
that's the type of comments that i want biggrin.png thank you
Two things I would suggest:
#1, instead of using a for loop, a while loop at the start would be better. Like this:

int healtme = 500;
int healthit = 500;

while (healthme > 0 && healthit > 0) {
...


Also, I would suggest making the damages a bit more random. So, a typical hit to the body would be 50 damage exactly. Lets make it so it's 50 +/- 10 (so anywhere from 40 to 60 damage):

// healthit -= X is the same as healthit = healthit - X
healthit -= GetRandomDamage(50, 10);

Make another function to perform this act

// Returns a random value within DamageRange of Damagebase
int GetRandomDamage(int DamageBase, int DamageRange)
{
// if DamageBase is 50, and DamageRange is 10, DamageBase will be 40 here
int DamageDealt = DamageBase - DamageRange;

// if DamageRange is 10, then this will add anywhere from 0 to 20 damage*
DamageDealt += (rand() % (DamageRange*2));

return DamageDealt;
{


Good Luck!

* (Actually, it will add 0 to 19 damage, since anything mod 20 will be 0 to 19)

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

not a huge issue but can save errors from user input, you should use getline(cin, attack) instead of cin >> attack since there are issues with spaces (and use cin.ignore(10000, '\n') before calling cin >> variable because getline puts everything upto a newline into a string but doesnt remove the newline)


as for improving maybe you can also put magic points (MP), which decrease anytime you use magic but increase at the end of every turn

also adding to BeerNutts idea, why not randomize the accuracy of the attacks? like 10% chance of failing, and similar possibility for the wolf.

maybe add an additional magic, healing to increase your health?
Always improve, never quit.
Beer Nuts and arkane7 thank you the the tips and hints that i can use, i will be adding most of those things into my game little by little biggrin.png but once again thank you for the idea smile.png
Not really crucial, but you may consider refactoring out the substring search predicate, e.g., as in here: http://ideone.com/6eDwg
// A decade or so ago it would also be a good idea to put "inline" keyword in the "substring_found" function declaration, but nowadays non-retarded compilers inline what's necessary anyway -- so you won't pay for a function call and the generated machine code will be identical to one generated from your current code.

This way you express your intent (testing whether a substring was found in a string) a little clearer (in a sense, comparing the result of the string::find method with std::string::npos is an internal implementation detail and both are idiosyncratic ways of std::string) and you're also prepared (just add another overload of "substring_found") in case you ever need to use another string (even C-style string, not that you necessarily should).

I would also move the "using namespace std;" using-directive (if you have to use it at all, it's mostly for porting legacy/pre-1998 code) inside your "main" function in order to avoid polluting global namespace.
Its not such a big deal but I would consider using enums for bodyparts, weapons and magic. It tends to make code easier to maintain

This topic is closed to new replies.

Advertisement