• Advertisement
Sign in to follow this  

text based fighter so far

This topic is 3310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

it works but i want it to go threw the game slower instead of showing all the stuff at onece
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
    int hp = 100;
    int cpuhp = 100;
    string name;
    bool gamenotdone = true;
    srand(time(0));
    cout << "welcome to the text fighter" << endl;
    cout << "enter you name" << endl;
    cin >> name;
    while (gamenotdone){
        cout << "you hp is " << hp << endl;

        cout << "you attacked cpu " << endl;
        cpuhp -= rand()%20+1;
        if (cpuhp <= 0)
        {
            cout << "you won" << endl;
            gamenotdone = false;
            return 0;
        }

        cout << name << " got attacked" << endl;
        hp -= rand()%20+1;
        if (hp <= 0)
        {
            cout << "you lost" << endl;
            gamenotdone = false;
            return 0;

        }
}



    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
There are a couple of ways to do sleep functions. The easiest is to way is to do a while loop: while not at the target time. The function std::time() returns the number of seconds since January 1, 1970.
    #include <ctime>
void wait( unsigned int seconds )
{
std::time_t target = std::time(0) + seconds;
while( std::time(0) < target )
; // Do nothing.
}
If you want to wait less than a second, or not in second increments, the you want std::clock() which returns a clock_t.
    #include <ctime>
void wait( unsigned int ticks )
{
std::clock_t target = std::clock(0) + ticks;
while( std::clock(0) < target )
;
}
The only problem is you probably don't know how many ticks are in a second, so you could guess, or you could refer to the variable in ctime called CLOCKS_PER_SEC. (Actually, it's a macro, so no std::CLOCKS_PER_SEC.)

But, both of these have a fatal flaw: That while loop tries to execute as fast as possible, even though it can't exit for a full second. Your CPU usage will jump from a hopefully low number (the usage while idle) to max. This makes the rest of my applications suffer, so unless your game is the only one I care about (e.g. it's full screen), this is unacceptable.

That's the portable solution, but the Windows API provides a much better, but non-portable solution.
#include <windows.h>
int main()
{
sleep( 5000 ); // Sleep 5 seconds.
}

sleep() inputs milliseconds, there are 1000 milliseconds in a second, so I had to multiply 5 by 1000 to get the number of seconds to sleep. What's great about this function is that the OS will know not to give any resources to your program for n seconds so no maxed CPU!

There is unfortunately no standard sleep function (confirmed by a quick google search), nor do I see any truly portable AND good methods. Still, whatever platform you're on, there's bound to be some sort of sleep for it.

Lastly, a code critique:
    #include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
The difference between using ctime and time.h is ctime wraps everything in the namespace std. But, by saying using namespace std, you invalidate that purpose. But, it's a good idea not to write using namespace std anyway. It helps avoid name collisions (with the exception of macros), better documents the code (so people aren't asking "where did this function come from?" if they aren't familiar with the standard), and just produces nicer code. Your program could be rewritten as:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

int main()
{
// So we can refer to them as just cout, cin, and endl.
// They're used so often, we don't want std:: every time.
using std::cout;
using std::cin;
using std::endl;

int hp = 100;
int cpuhp = 100;
std::string name;
bool gamenotdone = true;
std::srand(std::time(0));
cout << "welcome to the text fighter" << endl;
cout << "enter you name" << endl;
cin >> name;
while (gamenotdone){ // Having the { here seems inconsistent with the rest of your code.
// This while loop is very large.
// Splitting it into several functions could improve readability.

cout << "you hp is " << hp << endl;

cout << "you attacked cpu " << endl;
cpuhp -= std::rand()%20+1;
if (cpuhp <= 0)
{
cout << "you won" << endl;
gamenotdone = false;
return 0;
}

cout << name << " got attacked" << endl;
hp -= std::rand()%20+1;
if (hp <= 0)
{
cout << "you lost" << endl;
gamenotdone = false;
return 0;

}
} // In your code, this was improperly indented.



return 0;
}





EDIT: Forgot the bibliography.
std::time()
std::clock()
sleep()

EDIT2: If you don't want to delay the text in seconds, you could prompt with cout (<< "hit any key"), then use cin.get() to wait until the user types something then hits enter, or use getch() or better _getch() from conio.h (non-standard, I think, so some compilers don't support it), which waits for ANY key to be pressed, even Esc or F5, but it's non-standard. It's convenient, though.


[Edited by - Splinter of Chaos on January 25, 2009 11:37:36 PM]

Share this post


Link to post
Share on other sites
I have a related question

Is there any way to call a function, and then cut it off after a set amount of time even if it hasn't returned yet?

For now, I just put time checks into the function itself, but I was hoping to be able to allow users to write their own functions while still enforcing a strict time limit. Speaking of which, is there a good way to do that?

Share this post


Link to post
Share on other sites
That question is not related, and even if it was, the proper thing to do would have been to start your own thread linking to this one and relating it to your query. (However, an exception is when the OP's question has been answered and your question is pertaining to the OP's, not your own--at this point, the thread is a free-for-all.)

I'm tired right now (the only reason I'm responding is the page was still up so I saw your post) so I'm not going to go into detail, but it's almost impossible to do what you're trying to do with conventional, sequential programming. You might be able to speak directly with the hardware (inline assembly) to do something like this, but I don't know how or whether it really is possible. The alternative is to run that function in another thread. If a process is a running application, threads are one process acting like two. The fallowing pseudo code demonstrates:
    void f() { /*do stuff*/ }
int main()
{
Thread thread;
thread.run( f ); // Run f in another process (thread).
thread.waitOnThis( 4000 ); // Allow it 4 secs to run.
thread.terminate();
}
Unfortunately, OOP threads are a year or two away (in the upcoming C++ standard), but you can apply this knowledge to threads of your choice. Then, after you understand them, you can write your own Thread class.

You might find all you need with googling threading [your OS].

However, I wonder if the requirement to only allow that function to run a short time is due to poor functional design. If you want to discuss this any further, create your own topic and link to this one. I probably should have done that instead of posting an answer here, but I'm too tired to care. PM me the new thread if you make one to make sure I see it!

[Edited by - Splinter of Chaos on January 26, 2009 2:12:39 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement