| | |
Problems With Simple C++ Battle System; Need Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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(); } }
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 5
0
#2 22 Days Ago
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; 22 Days Ago at 5:32 pm.
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 22 Days Ago
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 22 Days Ago
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; 22 Days Ago at 6:24 pm.
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 0
0
#7 21 Days Ago
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++
| Thread Tools | Search this Thread |
.dll antivirus api array arrays background bitmap borland c++ calculator char char* class code commentinghelp compression computer console decide delayload desktop download ebook email embedded encryption engine error exam file fpx fstream function functions game graph guessing gui hash health insert int introduction java jni library linker linux lnk2019 math microsoft modal multiple numbers objects operating output parallel performance php pong practice primenumbersinrange pro problem professor program programming python qt read recursion recursive regqueryvalueex rpg samples search space sticky stop store string struct subclass system temperature template templates text tree tutorial upgrade url vista visual visualstudio web win32 windows xp






