| | |
Problems With Simple C++ Battle System; Need Help
![]() |
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
Okay, what I'm trying to do here is set up a some-what simple battle system for a text based game... I'm having some trouble getting it to work correctly though. I want the Player's attack and the Monster's defense to be random each time the program loops (which is also not working), but it keeps giving the same numbers.
I don't have much experience, so any help you can offer would be appreciated, thanks.
I don't have much experience, so any help you can offer would be appreciated, thanks.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; // Variables int charLevel, Floor; int Hp, Atk, Def; int monLevel, monHp, monAtk, monDef; string action, charName, monName; int damageDealt; // Prototypes void battlePhase(); int main() { // Defining the character cout << "What is your name? "; cin >> charName; cout << "\nWhat level are you? "; cin >> charLevel; cout << "\nWhat floor are you on? "; cin >> Floor; cout << "\nWhat monster do you wish to fight? "; cin >> monName; // Player Stats // charLevel defined earlier srand((unsigned)time(0)); Hp = (15 + ( charLevel * 5 )); Atk = (((rand() % 6) + 0) + (charLevel - 1)); Def = (((rand() % 4) + 0) + (charLevel - 1)); // Monster Stats monLevel = (1/2) * Floor + (charLevel - 2); if ( monLevel < 0 ) { monLevel == 1; } monHp = (10 + (monLevel * 2)); monAtk = (((rand() % 4) + 1) + (monLevel - 1)); monDef = (((rand() % 3) + 1) + (monLevel - 1)); while (Hp > 0 || monHp > 0) { battlePhase(); } return 0; } void battlePhase() { // Actual Battle cout << endl << "\nWhat do you want to do? "; cin >> action; if ( action == "Attack" || action == "attack" ) { cout << endl << charName << " attacked " << monName << " for " << Atk << endl; cout << monName << " defended " << charName << " for " << monDef << endl; damageDealt = Atk - monDef; if ( damageDealt < 0 ) { damageDealt = 0; } if ( damageDealt == 0 ) { cout << "\nNo damage!"; } if ( damageDealt > 0 ) { cout << monName << " took " << damageDealt << " damage!"; monHp = monHp - damageDealt; } } else { cout << "\nThat isn't an action! Try typing 'attack'."; battlePhase(); } }
0
#2 Nov 2nd, 2009
If you want the Atk, Def, monAtk, and monDef variables to randomize for each iteration of the loop, you will have to calculate them inside the loop. Currently, you calculate them before the loop starts and you have no way to get back to them to re-calculate them.
Last edited by Fbody; Nov 2nd, 2009 at 4:32 pm.
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 Nov 2nd, 2009
Thanks for the feedback... Hm, I tried what you said and moved:
Into the if statement to make:
It still doesn't seem to work however, and when I tried this the first result for the monDef was -1... Which shouldn't be able to happen.
I'm rather stumped.
C++ Syntax (Toggle Plain Text)
Atk = (((rand() % 6) + 0) + (charLevel - 1)); Def = (((rand() % 4) + 0) + (charLevel - 1)); monAtk = (((rand() % 4) + 1) + (monLevel - 1)); monDef = (((rand() % 3) + 1) + (monLevel - 1));
Into the if statement to make:
C++ Syntax (Toggle Plain Text)
void battlePhase() { // Actual Battle cout << endl << "\nWhat do you want to do? "; cin >> action; if ( action == "Attack" || action == "attack" ) { // Randomizing the attack/defend values for player and monster Atk = (((rand() % 6) + 0) + (charLevel - 1)); Def = (((rand() % 4) + 0) + (charLevel - 1)); monAtk = (((rand() % 4) + 1) + (monLevel - 1)); monDef = (((rand() % 3) + 1) + (monLevel - 1)); cout << endl << charName << " attacked " << monName << " for " << Atk << endl; cout << monName << " defended " << charName << " for " << monDef << endl; damageDealt = Atk - monDef; if ( damageDealt < 0 ) { damageDealt = 0; } if ( damageDealt == 0 ) { cout << "\nNo damage!"; } if ( damageDealt > 0 ) { cout << monName << " took " << damageDealt << " damage!"; monHp = monHp - damageDealt; } } else { cout << "\nThat isn't an action! Try typing 'attack'."; battlePhase(); } }
It still doesn't seem to work however, and when I tried this the first result for the monDef was -1... Which shouldn't be able to happen.
I'm rather stumped.
0
#4 Nov 2nd, 2009
You need to use srand, with integrated time, so the random integer will change(seed).
Example:
Edit: Also include the header: #include<time.h> if you do not already have it.
Also, if you don't mind, please send me the source code once you are done so I can check it out. I love checking out others' RPGs that they make.
Example:
C++ Syntax (Toggle Plain Text)
srand ((unsigned)time(0)); random3 = (rand() % 4) + 1;
Edit: Also include the header: #include<time.h> if you do not already have it.
Also, if you don't mind, please send me the source code once you are done so I can check it out. I love checking out others' RPGs that they make.
Last edited by restrictment; Nov 2nd, 2009 at 5:24 pm.
•
•
Join Date: Nov 2008
Posts: 55
Reputation:
Solved Threads: 4
0
#7 Nov 3rd, 2009
Apparently using "goto x;" is some kind of ungodly error that i seem to have picked up. But I have been able to create some really successful programs with it.
You can create different type of modules within your program using goto.
For instance:
In which after each attack against you, it makes sure your health is correct and checks to see if you are dead. In which the game would end, otherwise if you are not dead, it goes to the checkpoint module that continues the battle.
You can create different type of modules within your program using goto.
For instance:
C++ Syntax (Toggle Plain Text)
{ HPSTASIS: hptot = Health - EnDamage; Health = hptot; if (Health <= 0) {cout<<"You have died.";system("PAUSE");goto endgame;} else if (Health > 0){ goto CHECKPOINT; } }
![]() |
Similar Threads
- Simple Operating System (Assembly)
- Simple backup system... (C#)
- A simple grading system (C++)
- Does anyone know how to write a simple grading system in C++ programming??? (C++)
- Simple Inventory System (VB.NET)
- Control Simple Electric System (C#)
- power supply replaced,problems remain:usb,speakers,shutdown (Troubleshooting Dead Machines)
- how to run simple c++ prog from another system? (C++)
Other Threads in the C++ Forum
- Previous Thread: Function trouble (beginner)
- Next Thread: Beginner C++
Views: 771 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for c++, system
administrator algorithm api array arrays assignment basic beginner binary bitmap buttons c++ c++borland calculator char char* class client code compile compiler console constructor convert cout delete desktop development embedded encryption error file filestream fstream function functions game graph gui homework input int java job library link linker linux loop math matrix microsoft multidimensional newbie null numbers objects output performance pointer pointers prime problem problems program programing programming project projects python qt read recursion recursive rpg selection sql stream string strings struct studio subclass switch system template templates text tree url variables vector visual web win32 window windows winsock world xml






