tiny little bug

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace 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((unsigned int) 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;
}
Last edited on
Topic archived. No new replies allowed.