Sign in to follow this  

another noob question by yours truly

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys i am stuck yet again with generating a random hit rate in my text game and was wondering if you could help? :D here is what i have so far

#include <iostream>

// different functions
void intro();
void combat();
bool hit_rates(bool hit_rate);
void end();

// global variables
int monster_health = 50;
int player_health = 100;
char choice;
bool hit_rate;
int hit;
int miss;


int main()
{
	using namespace std;
	cout << " Welcome to the text based game Warriors of Troy! \n";
	intro();
	combat();
	end();
	cin.get();
	return 0;
}
void intro()
{	
	
	char name[20];
	using namespace std;
	cout << " to begin please enter your name! \n";
	cin.getline(name , 20);
	cout << " hello and welcome : " << name << "\n";
	cout << " you are walking to the city when you are attack by a monster! ";

	
}
void combat()
{
	using namespace std;

	while (monster_health > 1){

	cout << endl;
	cout << " what do you do? " << endl
		<< "1) crushing blow " << endl
		<< "2) accurate lunge " << endl
		<< "3) quick lunge " << endl;
	cout << " please enter your choice \n";

	cin >> choice;
	switch(choice)
	{
		case '1' :
			cout << " Crushing blow !";
			if(hit_rate = hit)
			{
			monster_health = monster_health - 20;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			}
			else
			{
				cout << " you missed... ";
			}
			break;
		case '2' :
			cout << " Accurate lunge! ";
			if(hit_rate = hit)
			{
			monster_health = monster_health - 15;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			}
			else
			{
				cout << " you missed...";
			}
			break;
		case '3' :
			cout << " quick lunge! ";
			if(hit_rate = hit)
			{

			monster_health = monster_health - 10;
			cout << " the monster now has :" << monster_health
				<< " health! " << endl;
			}
			else
			{
				cout << " you missed ";
			} 
			break;
		default :
			cout << " you have entered an unavailable option! \n";
			break;
	}
	
		
	};
}
void end()
{
	using namespace std;
	char quit_choice;
	cout << " congratulations you have killed the monster! ";
	cout << " would you like to play again? ";
	cin >> quit_choice;

}
int hit_rates(int hit_rate)
{
	if (hit_rate = rand() % 100 < 75)
	{
		hit_rate = hit;
	}
	else
	{
		hit_rate = miss;
	}
return hit_rate;
}

please help me and comment upon my coding style so i can see what you think :D

Share this post


Link to post
Share on other sites
First off, you have hit_rate defined as a bool and are setting it to a value that isn't bool (hit and miss aren't bool).

Second, you need double equals sign to check equality (==). You're if statements inside your case use single equals which is a set statement.

Also, your hit_rates function header is type bool and the defined function below is returning an int and accepting an int. I don't think that lower int version is even being called...

Even if these don't fix your problems they should be changed.

Share this post


Link to post
Share on other sites
i had done what you had said before i posted this. what i posted was my last attempt at getting it right. but it still didn't work.

but thanks for replying :D

Share this post


Link to post
Share on other sites
using namespace std is global and written only once, not in every function. I would recommend just doing std::cout, because thats why namespaces are there in the first place, to distinguish different code.

while (monster_health > 1){ you should only really use one coding style,
its good to be consistent.

while(1){
}

or:
while(1)
{
}

if(hit_rate == hit) not hit_rate = hit, that assigns stuff
monster_health = monster_health - 20; could be written monster_health -= 20;
as a shortcut.

whats }; doing there randomly? doesn't need ;

bool hit_rates(bool hit_rate); signature doesn't match to your function implementation. int hit_rates(int hit_rate)

random number:
first seed it in main: srand ( time(NULL) ); and
#include <stdlib.h>
#include <time.h>

it shouldn't really be in an if statement.
hit_rate should not be a bool but an int.

hit_rate = rand() % 10 + 1;
if(hit_rate == someNumber)I_hit_something = true;
else i_hit_something = false;

My advice is don't rush the book and learn everything thoroughly.

good luck

Share this post


Link to post
Share on other sites
It could be because hit_rate is never set anywhere. It's only defined as a bool, which might possibly be defaulting it to false. You never call the function hit_rates() which looks like the function that you use to set that variable.

Share this post


Link to post
Share on other sites
Quote:
Original post by rgibson11
using namespace std is global and written only once, not in every function


This is wrong. It obeys scope rules as per normal. If it's going to be used, it's perhaps preferable that it's used within the function - although you're right in that placing it in every function rather defeats the purpose.

Share this post


Link to post
Share on other sites
If you mean every hit is the same and not random, I think you need to seed your rands...

#include <ctime>


...

srand(time(0));




I don't know alot, but thats what some guy told me

Share this post


Link to post
Share on other sites
Quote:
Original post by Powell
If you mean every hit is the same and not random, I think you need to seed your rands...
(...)
srand(time(0));

It's more usual to seed with the current time instead, as rgibson11 already demonstrated.

Now, for some theory, the random number generators we're working with here aren't really random. They're pseudo-random. They generate sequences of numbers, using some kind of algorithm, based on a given starting value (the seed). Try seeding with the same number - you'll get the same 'random' sequence (this can be useful if you want to reliably generate the same data again and again - you only need to store the seed).

So, by seeding with the current time, you reduce the chance of getting the same sequence again.


As for emforce's code, much has been said already. I'll just add a few more things:

- in intro(), replace that char array by a std::string, and use std::getline(std::cin, name); to read the given name. Your code will break when you enter a name that's too long. Using C++' string class will prevent that problem.

- you don't want to check if the monsters health is more than 1, but if it's more than 0. Currently, you treat the monster as death when it has exactly 1 hitpoint left.

- instead of duplicating monster hit and display code in your switch-case statement, write a function: hit_monster(int damage) - and another one, display_monster_health(). Then call these from within your switch-case statement. This reduces code repetition, which is good (for example, when fixing bugs, it's a real pain to have to fix it in multiple places - it's easy to overlook one).

Share this post


Link to post
Share on other sites

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this