ive been reading a book with C++ tutorials and the nested if else's they show are not working in my combat algorithm. Could someone please help me clean it up so it works. thanks
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
/*
These constants define our upper
and our lower bounds. The random numbers
will always be between 1 and 6, inclusive.
*/
int Test = 0;
int MonsterLevel = 1;
int MonsterHitPoints = 15;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;
int PlayerLevel = 1;
int PlayerHitPoints = 25;
int PlayerAttack = 10;
int PlayerDefense = 5;
int PlayerHitRate = 75;
int LowMonster = 2;
int HighMonster = MonsterAttack - PlayerDefense;
int LowPlayer = 2;
int HighPlayer = PlayerAttack - MonsterDefense;
int PlayerHitCheck = 0;
int MonsterHitCheck = 0;
int PlayerHitLow = 1;
int PlayerHitHigh = 100;
int MonsterHitLow = 1;
int MonsterHitHigh = 100;
int main()
{
/*
Variables to hold random values
for the first and the second die on
each roll.
*/
int first_die, sec_die;
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsignedint) seconds);
/*
Get first and second random numbers.
*/
if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
{
cout << "Monsters & Player Can Fight HP ok.\n";
PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;
if (PlayerHitCheck <= 25)
cout << "Player swings his weapon and misses.\n";
cin >> Test;
else (PlayerHitCheck >=26)
{
cout << "Player swings his weapon and lands a blow!\n"
first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer;
sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer;
cout<< "Players attack is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
cin >> Test;
MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow;
if (MonsterHitCheck <= 40)
cout << "Monster swings and misses.\n";
cin >> Test;
else (MonsterHitCheck >=41)
cout << "Monster swings and lands a blow!\n";
first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
cout<< "Monsters attack is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
} cin >> Test;
}
else ( (MonsterHitPoints <= 0) && (PlayerHitPoints <= 0) )
cout << "Unable to fight! Player or Monster HP are 0.\n";
cin >> Test;
return 0;
}
Could you point me in the direction of how to make it continue to do "Rounds" until player or monster HP hits 0 and how to convert dice roll dmg back into - player / monster HP variables
@Disch thanks alot that helped very much im working on cleaning up the code now. Ive added more since then so that copy is outdated.
im doing this on a seperate cpp till i get the bugs worked out of it, that way i dont mess up my main code, this is going to be part of a combat() function
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
usingnamespace std;
// Strings and extra variables
string PlayerName; // Player's name
string PlayerWeapon; // Name of weapon equipped by player.
string MonsterName = "Zombie"; // Monster name or type
string MonsterWeapon; // Name of weapon equipped by monster if it has one.
char NextRound; // For testing only
// MONSTER VARIABLES
int MonsterLevel = 1;
int MonsterHitPoints = 15;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;
// PLAYER VARIABLES
int PlayerLevel = 1;
int PlayerHitPoints = 25;
int PlayerAttack = 10;
int PlayerDefense = 5;
int PlayerHitRate = 75;
// Monster damage variables using rand
// Also subtracts player defense from monsters damage
int LowMonster = 2;
int HighMonster = MonsterAttack - PlayerDefense;
// Player damage variables using rand
// Also subtraces monster defense from players damage
int LowPlayer = 2;
int HighPlayer = PlayerAttack - MonsterDefense;
// Variable to see if player or monster land a blow
int PlayerHitCheck = 0;
int MonsterHitCheck = 0;
// Player % chance to hit high and low
int PlayerHitLow = 1;
int PlayerHitHigh = 100;
// Monster % chance to hit high and low
int MonsterHitLow = 1;
int MonsterHitHigh = 100;
// Player & Monster damage results die 1 + die 2
int MonsterDamageResults = 0;
int PlayerDamageResults = 0;
int main()
{
/*
Variables to hold random values
for the first and the second die on
each roll.
*/
int first_die, sec_die;
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsignedint) seconds);
/*
Get first and second random numbers.
*/
cout << "You encounter a Level 1 Zombie. Prepare to fight!\n";
cout << "What is your name? <a-z>\n";
cin >> PlayerName;
BEGINCOMBAT:
if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
{
cout << "Starting battle between [" << PlayerName <<"] & [" << MonsterName <<"]!\n";
PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;
if (PlayerHitCheck <= 25)
{
cout << "" << PlayerName <<" Swings his weapon and misses.\n";
cout << "Press <c> and enter to continue to next combat round.\n";
cin >> NextRound;
}
else
{
cout << "" << PlayerName <<" swings his weapon and lands a blow!\n";
first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer; // Calculate damage of first dice roll
sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer; // Calculate damage of second dice roll
cout << "" << PlayerName <<"'s attack is (" << first_die << ", "
<< sec_die << "}\n\n";
cout << "Press <c> and enter to continue to next combat round.\n";
cin >> NextRound;
MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow; // Check to see if monster hits player
}
if (MonsterHitCheck <= 40)
{
cout << "Monster swings and misses.\n";
cin >> NextRound;
}
else
{
cout << "Monster swings and lands a blow!\n";
first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
cout<< "Monsters attack is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
MonsterDamageResults = sec_die + first_die;
cout << "Monster hits player for " << MonsterDamageResults << " damage.\n";
PlayerHitPoints = PlayerHitPoints - MonsterDamageResults;
cout << "Players hitpoints are: " << PlayerHitPoints << ".\n";
cin >> NextRound;
}
}
else
{
cout << "Unable to fight! Player or Monster HP are 0.\n";
cin >> NextRound;
}
DEAD:
cout << "" << PlayerName <<" has died! [GAME OVER]\n\n";
cin >> NextRound;
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
/*
These constants define our upper
and our lower bounds. The random numbers
will always be between 1 and 6, inclusive.
*/
int Test = 0;
int MonsterLevel = 1;
int MonsterHitPoints = 15;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;
int PlayerLevel = 1;
int PlayerHitPoints = 25;
int PlayerAttack = 10;
int PlayerDefense = 5;
int PlayerHitRate = 75;
int LowMonster = 2;
int HighMonster = MonsterAttack - PlayerDefense;
int LowPlayer = 2;
int HighPlayer = PlayerAttack - MonsterDefense;
int PlayerHitCheck = 0;
int MonsterHitCheck = 0;
int PlayerHitLow = 1;
int PlayerHitHigh = 100;
int MonsterHitLow = 1;
int MonsterHitHigh = 100;
int main()
{
/*
Variables to hold random values
for the first and the second die on
each roll.
*/
int first_die, sec_die;
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsignedint) seconds);
/*
Get first and second random numbers.
*/
if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
{
cout << "Monsters & Player Can Fight HP ok.\n";
PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;
if (PlayerHitCheck <= 25)
{
cout << "Player swings his weapon and misses.\n";
cin >> Test;
}
else
{
cout << "Player swings his weapon and lands a blow!\n";
first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer;
sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer;
cout<< "Players attack is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
cin >> Test;
MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow;
}
if (MonsterHitCheck <= 40)
{
cout << "Monster swings and misses.\n";
cin >> Test;
}
else
{
cout << "Monster swings and lands a blow!\n";
first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster;
cout<< "Monsters attack is (" << first_die << ", "
<< sec_die << "}" << endl << endl;
cin >> Test;
}
}
else
{
cout << "Unable to fight! Player or Monster HP are 0.\n";
cin >> Test;
}
return 0;
}
So how much cleaner it is? You can clearly see the control blocks. Makes it much easier to at-a-glance follow the overall program flow.