Here is my newly improved combat code, however the loop continues its dual round 1x Player Swing & 1x Monster swing, in the event the monsters HP falls to 0 or below on characters swing. It always executes the monsters swing again before finishing the cycle even though its dead! I was just thinkin as i write this would switching the cycles to the monster swings first and the player second fix this?
Just found a second tiny bug!! If the player lands a perfect blow at the start of the fight taking the monsters HP exactly to 0 then the script gets stuck with unable to fight.
#UPDATE#
I believe i fixed the exact blow bug by using the first if else cycle to catch that. new code below. However the first bug is still there, not sure how to fix it maybe a boolean check? Could use alittle help thanks.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
usingnamespace std;
// Strings and extra variables
string PlayerName; // Player's name
string MonsterName = "Zombie"; // Monster name or type
char NextRound; // Continue to next combat cycle
// MONSTER VARIABLES
int MonsterLevel = 1;
int MonsterHitPoints = 10;
int MonsterAttack = 8;
int MonsterDefense = 3;
int MonsterHitRate = 60;
// PLAYER VARIABLES
int PlayerLevel = 1;
int PlayerExperience = 0;
int PlayerGold = 100;
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.
*/
/// COMBAT SCRIPT BELOW
cout << "You encounter a Level 1 Zombie. Prepare to fight!\n";
cout << "What is your name? <a-z>\n";
cin >> PlayerName;
while ( ( PlayerHitPoints >= 0 ) && ( MonsterHitPoints >= 0 ) )
{
if ( (MonsterHitPoints >= 1) && (PlayerHitPoints >= 1) )
{
cout << "Starting combat round between [" << PlayerName <<"] & [" << MonsterName <<"]!\n";
PlayerHitCheck = rand() % (PlayerHitHigh - PlayerHitLow +1) + PlayerHitLow;
if (PlayerHitCheck <= 25)
{
cout << "" << PlayerName <<" swing's at the " << MonsterName << " and misses!\n";
cout << "Press <c> and enter to continue to the next combat round.\n";
cin >> NextRound;
}
else
{
cout << "" << PlayerName <<" swing's at the " << MonsterName <<" and lands a blow!\n";
first_die = rand() % (HighPlayer - LowPlayer + 1) + LowPlayer; // Calculate player damage of first dice roll
sec_die = rand() % (HighPlayer - LowPlayer +1) + LowPlayer; // Calculate player damage of second dice roll
cout << "" << PlayerName <<"'s attack is (" << first_die << ", "
<< sec_die << "}\n\n";
PlayerDamageResults = sec_die + first_die; // Calculate total damage done by player
cout << "" << PlayerName << " hits " << MonsterName << " for " << PlayerDamageResults << " damage.\n";
MonsterHitPoints = MonsterHitPoints - PlayerDamageResults; // Subtract damage from monsters hitpoints
cout << "" << PlayerName <<"'s hitpoints are: " << PlayerHitPoints << ".\n";
cout << "" << MonsterName <<"'s hitpoints are: " << MonsterHitPoints << ".\n";
cout << "Press <c> and enter to continue to the next combat round.\n";
cin >> NextRound;
MonsterHitCheck = rand() % (MonsterHitHigh - MonsterHitLow +1) + MonsterHitLow; // Check to see if monster hits player
}
if (MonsterHitCheck <= 40)
{
cout << "" << MonsterName <<" swing's at " << PlayerName << " and misses!\n";
cout << "Press <c> and enter to continue to the next combat round.\n";
cin >> NextRound;
}
else
{
cout << "" << MonsterName <<" swing's at " << PlayerName <<" and lands a blow!\n";
first_die = rand() % (HighMonster - LowMonster + 1) + LowMonster; // Calculate monster damage of first dice roll
sec_die = rand() % (HighMonster - LowMonster + 1) + LowMonster; // Calculate monster damage of second dice roll
cout << "" << MonsterName <<"'s attack is (" << first_die << ", "
<< sec_die << "}\n\n";
MonsterDamageResults = sec_die + first_die; // Calculate total damage done by monster
cout << "" << MonsterName <<" hits " << PlayerName <<" for " << MonsterDamageResults << " damage.\n";
PlayerHitPoints = PlayerHitPoints - MonsterDamageResults; // Subtract damage from players hitpoints
cout << "" << PlayerName <<"'s hitpoints are: " << PlayerHitPoints << ".\n";
cout << "" << MonsterName <<"'s hitpoints are: " << MonsterHitPoints << ".\n";
cout << "Press <c> and enter to continue to the next combat round.\n";
cin >> NextRound;
}
}
else
{ // If & Else statements in the event a 1 hit kill blow is scored
if ( (PlayerHitPoints <=0) && ( MonsterHitPoints >=1) )
{
cout << "" << PlayerName <<" has died! [GAME OVER]\n\n";
cout << "Do you wish to reload your saved game? (Currently Unsupported)\n\n";
cin >> NextRound;
}
else
{
cout << "" << MonsterName <<" has died! [VICTORY!]\n\n";
cout << "You find 8 gold pieces on the " << MonsterName <<"!\n";
cout << "You gain 10 experience points!\n";
PlayerGold = PlayerGold + 8; // Award player gold
PlayerExperience = PlayerExperience + 10; // Award player experience
cout << "Players Experience:" << PlayerExperience <<"\n";
cin >> NextRound;
}
}
} // If & Else statements end of combat cycle
if ( (PlayerHitPoints <=0) && ( MonsterHitPoints >=1) )
{
cout << "" << PlayerName <<" has died! [GAME OVER]\n\n";
cout << "Do you wish to reload your saved game? (Currently Unsupported)\n\n";
cin >> NextRound;
}
else
{
cout << "" << MonsterName <<" has died! [VICTORY!]\n\n";
cout << "You find 8 gold pieces on the " << MonsterName <<"!\n";
cout << "You gain 10 experience points!\n";
PlayerGold = PlayerGold + 8; // Award player gold
PlayerExperience = PlayerExperience + 10; // Award player experience
cout << "Players Experience:" << PlayerExperience <<"\n";
cin >> NextRound;
}
return 0;
}