i want to make a text based rpg game, but ive run into a problem, im fairly "new" to c++, know a few concepts but still learning.
So what i want to know is; how do i open a file and edit the values in it?
say i have a file called health, for my characters health and its set to 100. I know you can just subtract the variable by the damage you set the monsters to do, but i dont know how i store the health and edit it, while my program goes along.
ie. this is the farthest i got:
ifstream objectFile("health.txt");
string health;
double NoH;
cout << "welcome" << endl;
objectFile >> health >> NoH;
cout << health << ' ' << NoH - 15;
NoH = Number of health. So far all my program does, is read the file, and subtract 15 from 100 because thats the damage the monster do. But how do i make it "take out" the value and subtract 15 from it? then store it back so it's 85, and next time i get hit its 65 for example etc, so it makes it the objective of the game is about surviving, ie avoiding getting hit and stuff. i mean avoiding your hp to reach 0, but also i want to learn how to implement simple text based creatures you can kill, by letting their hp reach 0.
thanks
what about using ini /properties file?
or using json?
if you are making rpg, player save the values (hp,mp,item, map(x,y) etc) to file when saving the game? and the rest of the time should store/calculate those value in memory I guess.
about json
https://code.google.com/p/vjson/
about ini file
http://www.hyperrealm.com/libconfig/
Related
Alright, I'm a little bit (honestly way too) confused about how the heck I can make a program interact with another program.
For example, let's say a game, a shooter, when you run an external program and you make your character not able to die, or immediately shoot when detects an enemy, etc...
I was reading a little bit about it, and they say you have to know how the "target" is composed. But I still don't get it.
For example, let's say we've got a simple code like this:
#include <iostream>
#include <windows.h>
int main() {
for(int h = 0; ; h++) {
std::cout << "The H's value is: " << h << std::endl;
Sleep(1000);
}
return 0;
}
Then, how do I create another program where I can change the H's value to zero everytime I press any key?
Don't get me wrong, I ain't trying to hack anyone or anything, I'm just curious about how those programs work.
(Sorry if I've got some grammar issues, English isn't my native language).
Specific to your program in the exapmle if we take that the program is already compiled and you are not allowed to make any changes to the source code the solution would be to build a program which will run with high enough privileges to examine this process's memory and directly change the in-memory value of h, which should be on the top of the stack(or almost).
Speaking of some more "legal" ways to do so you should check you should read about inter process communication which can be done with multiple methods. Read this.
However most "Bots" and programs which help cheaters in games are in many cases graphics based and are able to analyse the image and thus help aim. On the other hand some "recoil reducers" simply move your mouse in the opposite direction of the recoil of the gun in game. So there is a ton of approaches to your question and for every particular case the answer might be different.
I'm working on a traveling sales person program in C++. I'm very new to C++ and its so much different from Java that the simple things really get confusing.
How do I use standard input to accept commands from a file (apparently I don't open the file, my professor just uses it to feed in commands or something) and then use those commands to run my program accordingly?
An example would be
city a
city b
city c
city d
a c 1300
a d 1400
a b 900
d c 1500
So basically an unknown amount of information is going to be passed into my program and then my program needs to declare a specific number of cities and then attach travel costs between each of them as specified. I think I can do the latter part, but my problem is knowing how to take an unknown number of inputs and then attach those inputs to variables.
I guess in Java I would do something like this:
While(nextLine = true){
if (nextLine.contains ("city"){
String nextLine = nextLine;
...and so on
}
}
Start with waiting a filename by ifstream, then you can get inputs by char or line in which using a char pointer and determine it with text size i believe somethig like this
std::ifstream::pos_type filesize(const char* filename)
{
Now you buffered, go on for what you know from java and combine it. Besides, like Sam's sugesstion, u you should read
I have a program that is a game tournament that battles recursively and deletes players from a player container until there is only 1 player left. It is the card game War, and for every battle I need to output the winner of each battle in the game to a file. So for example it would look something like this
John vs James
Battle 1: James wins
Battle 2: John wins
...
Battle 5: James wins, John is out of cards. James wins the game
but then since this is a tournament, John gets removed from the container and the next two people battle so we go from 16 to 8 to 4 to 2 players and then lastly the winner. I already have the code for this all worked out I need need to know how I can do this part recursively:
ofstream outFile;
outFile.open(argv[2]);
outFile << player1Name << " vs. " << player2Name << endl;
outFile << "Battle" << //number battle & winner etc, etc
I want to do this so that every time the PROGRAM is ran, it overrides everything in the file, but everytime a GAME is played within the tournament it just adds to the end of the previous battle. I have a few ideas but I'm not sure what's the best way to go about this. I was thinking about putting
ofstream outFile;
outFile.open(argv[2]);
In the driver.cpp file, then calling the recursive function game.tournament() but passing in game.tournament(outFile), into void Game::tournament(ofstream& outFile) then using the outFile << statements and calling doing the recursive call tournament(outFile) and once the recursive function ends, .close() the file back in the driver.cpp. Would this work the way I think it would? As in it opens and closes in the driver, but I have access to print out to it in the recursive function
Also to be clear the base case is when the queue of players has a .size() < 2, meaning it has only 1 player left in it and thus that player is the winner, that's why my recursive function has a void return value. This is not the part I need help with but I figured I'd include it in case people were wondering
At the beginning of your program, before calling the recursion open your file like this
outFile.open(argv[2], std::ofstream::trunc);
This will truncate everything from the file, then close it.
Next in your recursive function, open the file for appending like this
outFile.open(filename, std::ofstream::app);
See the reference for ofstream here
I am making a text based game experiment thing for fun because I just started learning about a week ago, when I use the value of health that the player has instead of 65 it comes up as 6.500000e+01 which, doesn't read very well how do I prevent this? This is my first time here should I post my whole code?
if 'eat' in food:
print "Then starts the feast, you eat everything you can reach. After a while you become to tired and need to rest."
print "You wake up with no sense of how much time has passed. You ate too much but feel healthier. You continue on into through the only doors you can see."
player["health"] += 15
print "You are now at %(health) health." % player
"Try %d" % (player ["health"])
Yeah. So, I'm trying to make a code for a guessing game. In this game, there's a hard mode. In hard mode, you have 15 guesses, and have to guess between 1 and 500. But my problem is this:
I'm trying to have hard mode save & display your wins/losses, but when it outputs the contents of wins.txt it outputs something like this:
Wins: 0x7fffee26df78
Losses: 0x7fffee26e178
It's really confusing me. Here's the part of the code I have for that:
ifstream losses_var("losses.txt");
ifstream wins_var("wins.txt");
losses_var>> loss;
wins_var>> win;
wins_var.close();
losses_var.close();
Then it gets called with:
cout<<"Wins: "<< wins <<"\nLosses: "<< losses <<"\n"
If you would like to see the full source code, it's here: http://pastebin.com/gPT37uBJ
My second problem:
Hard mode won't display when you win. That's pretty much the whole problem. In my code, the loop for asking users for input uses
while (guess != randNum)
So at the end bracket I have what I want the code to display when a user wins, but it just doesn't run. It just stops. I would like it if someone could help me with this. The line that has the bug is line 97 through 105. Again, source code is here: http://pastebin.com/gPT37uBJ
You've got your variable names confused
cout<<"Wins: "<< wins <<"\nLosses: "<< losses <<"\n";
should be
cout<<"Wins: "<< win <<"\nLosses: "<< loss <<"\n";
It's important to pick good variable names. One reason is so that you don't confuse yourself about what your variables mean (if you confuse yourself think how it's going to be for someone else looking at your code).
Others have already answered the output problem (win vs. wins). The other problem is probably in your logic of while loop nesting. The outer loop (while (guess != randNum)) starts, but its body contains the entire inner loop (while (guesses_left != 0)). This means that the outer condition is not checked again until the inner loop terminates, which means you've run out of guesses. Also note that if you guess correctly, inner loop will never terminate. You probably want something like this:
while (guesses_left > 0) {
// input user's guess
if (guess < randNum) {
// process it
} else if (guess > randNum) {
// process it
} else {
// it's equal, user won
// do what's necessary for a win
return 0;
}
}
// ran out of guesses
// do what's necessary for a loss
return 0;
You are not writing your variables win and loss to cout. From your pasted code, I can see that wins and losses are ofstream objects, which means you are probably seeing addresses there. I would advise you to choose more informative variable names to avoid hard to spot mistakes like this.