Hello everybody. I'm writing a program (actually a game) about fighting with opponents on the arena. The problem is that I get always the same random numbers on each "while" loop. I created a structure in the following header, which contains information about fighters:
1 2 3 4 5 6 7 8 9 10 11
#ifndef COMPETITOR_H_INCLUDED
#define COMPETITOR_H_INCLUDED
struct competitor
{
std::string name;
int health;
int attack;
};
#endif // COMPETITOR_H_INCLUDED
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <competitor.h>
#include <conio2.h>
usingnamespace std;
int new_game();
int arena();
int fight();
int ID = 0;
competitor player;
int main()
{
srand(time(NULL));
cout << endl << "ARENA" << endl;
cout << endl << "[1] New Game";
cout << endl << "[2] Exit" << endl << endl;
cout << "Option: ";
int option;
cin >> option;
if (option == 1) new_game();
elseif (option == 2) break;
else
{
cout << "Option: ";
cin >> option;
}
return 0;
}
int new_game()
{
clrscr();
cout << "Enter your character's name: ";
cin >> player.name;
player.health = 25;
player.attack = (rand()%6)+1;
arena();
}
int arena()
{
clrscr();
cout << "[1] Next fight" << endl;
cout << "[2] Return to menu" << endl;
cout << endl << "Option: ";
int option;
cin >> option;
if (option == 1) fight();
elseif (option == 2) main();
else
{
cout << "Option: ";
cin >> option;
}
}
int fight()
{
clrscr();
int number = 1;
competitor opponent[2];
opponent[0] = {"Rat", 6, (rand()%4)+1};
opponent[1] = {"Drunkard", 10, (rand()%6)+1};
cout << "Your opponent is " << opponent[ID].name;
cout << endl << "Your opponent's health: " << opponent[ID].health;
cout << endl << "Your health: " << player.health << endl;
while (opponent[ID].health > 0 && player.health > 0)
{
cout << endl << endl << "Round " << number << endl;
cout << opponent[ID].name << " inflicts" << opponent[ID].attack << " damage, ";
cout << "you have " << (player.health = player.health - opponent[ID].attack) << " health points" << endl;
if (player.health <= 0) break;
cout << player.name << " inflicts " << player.attack << " damage, ";
cout << "your opponent has " << (opponent[ID].health = opponent[ID].health - player.attack) << " health points" << endl;
number++;
}
if (player.health > opponent[ID].health)
{
cout << endl << "Congratulations! You managed to defeat your opponent. Prepare for the next fight.";
ID++;
getch();
arena();
}
else
{
cout << endl << "Unfortunately, you have been defeated. Start again.";
ID = 0;
getch();
main();
}
}
Basically it works, on each program run I get different attack values, but in every round (on every "while" loop) they are always the same. I have no idea, how to solve this problem. All help is greatly appreciated.
If you randomize this number once at the start of each game then it remains constant and does not change so it is no surprise that you are getting the same attack damage for each round. The best thing to do would be to create a OnNewRound() function and randomize the attack values inside there so it can be called before each attack (or round) thus making the attack values different.
To sum up: you need to randomize the attack values once per round/attack and not one per game
I hope I have understood the question correctly, if not then please feel free to reply and I will do my best to get back to you and leave as much help as possible.
Thank you for help, your reply gave me a point. But I managed to do it in another way though. I've modified the "while" loop and it looks like this now: