critical hit system, C++

I am trying to create a simple hit and critical hit system. from using a random number from 1-10 that then has a 20% chance for the user to get a critical hit and using the formula (user number 1-10) * 2.5 + 5 = (the end number). Can I make a if else loop to make it work qith normal hit being one and critcal hit as the other or should I use something else? Or using a string to state the characters for each variable

Last edited on
Probably best if you posted some code (what you've tried/what you have so far) with your issue. The system itself of 20% chance for a critical hit is fairly simple, but I don't understand why or how you're using that formula.
Last edited on
Condensed down my code so its not so much. So for the random number loop i am using is here, (player_attack = 1 + rand() % (10 + 1);) in the code at the moment I tried to make a string hit for the hit idicator then i would add to a if else loop for both hit and critical hit but just keep on coming up as 0, i am jsut stuck at the moment

#include "pch.h"
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>

using namespace std;
int main() {

int enemy_health = 100; //initial Enemy Health(based on enemy)
int player_health = 100; //initial Player Health
int enemy_attack = 0; //Enemy Attack Strength(based on enemy)
int player_attack = 0; //Player Attack Strength(based on level and items)
int selection = 0; //Selection Variable for Battle Menue
int health_power = 0; //Healing power Variable(random number)
int eSelection = 0; //Enemies battle menu selection variable
int counter = 0; //Counter to establish whos turn it is
string score;

cout << "___________________________________________" << endl;
cout << "|Welcome to the Gladiator Battle Simulator|" << endl;
cout << "|-----------------------------------------|___________________________-" << endl;
cout << "|Rules are as they follow 1 and 2 are your attack and heal functions,|" << endl;
cout << "|anything else is disallowed and will not work. GOOD LUCK OUT THERE. |" << endl;
cout << "----------------------------------------------------------------------" << endl;
cout << "Please enter anything for the game to begin!" << endl;

cout << endl << "Player HP: " << player_health << endl;
cout << "Enemy HP : " << enemy_health << endl;
std::cout << "Select an action..." << endl;
cout << "1.) Attack" << endl;
cout << "2.) Heal" << endl;
do
{

if (counter == 0)// if the counter variable is 0 it is the players turn
{
cin >> selection;
srand(static_cast<int>(time(0))); //randomize all the random variables

switch (selection)
{
case 1:// player chooses to ATTACK
player_attack = 1 + rand() % (10 + 1); //gives user a number between 1-10

if (player_attack < 10) {

score = "HIT";

}


cout << "You ATTACK and score a " << " on the enemy for " << player_attack << "HP" << endl;
system("pause");
system("cls");
enemy_health = enemy_health - player_attack;
cout << "Player HP: " << player_health << endl;
cout << "Enemy HP : " << enemy_health << endl;
std::cout << "Select an action..." << endl;
cout << "1.) Attack" << endl;
cout << "2.) Heal" << endl;
cout << ":";
break;

}
counter = 0;
}

} while (enemy_health > 1 && player_health > 1);

return 0;
}

 
player_attack = 1 + rand() % (10 + 1); //gives user a number between 1-10 

Actually that gives a number from 1 to 11. It should be

 
player_attack = rand() % 10 + 1;  // 0 to 9 plus one gives 1 to 10. 

So I guess you want something like this:

1
2
3
4
5
6
7
attack = rand() % 10 + 1;
if (rand() % 100 < 20) {
    // critical hit
}
else {
    // normal hit
}

If you notice that your critical hits are happening a little more than 20% of the time, check out how to properly get a random number. (Using C++’s modern facilities.)
The modern facilities are described in:
http://www.cplusplus.com/reference/random/ and https://en.cppreference.com/w/cpp/numeric/random

Particularly,
* get number between 1-10: std::uniform_int_distribution<int> dice( 1, 10 );
http://www.cplusplus.com/reference/random/uniform_int_distribution/

* 20% chance to get something: std::bernoulli_distribution critical( 0.2 );
http://www.cplusplus.com/reference/random/bernoulli_distribution/
Last edited on
1
2
3
4
5
6
7
do 
{

if (counter == 0)// if the counter variable is 0 it is the players turn
{
cin >> selection;
srand(static_cast<int>(time(0))); //randomize all the random variables 


Do not call srand() multiple times (i.e. in a loop). srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/
As others have said, use the modern C++ facilities.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Topic archived. No new replies allowed.