1

I am making a game program in turbo c++ for my project and I need help on how to add a game timer, I've seen videos on how to create timer using while loop but I don't know how to implement it to my game. My plan for my game is to have it show 6 initialized letters(ex. "N A E B T S") and within 30 secs input as many words as possible which has corresponding points(6=10pts, 5=8pts, 4=6pts, 3=4pts). The correct words are written in a txt file with their corresponding points. Also the whole thing is in loop with clrscr();

Here is some parts of the game code:

void start()
{
    char arr[10][50] = {" B A N S E T ",
                        " L E A Z D Z ",
                        " M B L U E J ",
                        " P R G N I S ",
                        " A C Q U K Y ",
                        " S A H L E S ",
                        " R E D G A E ",
                        " Z E D Z U B "};

    int i = 0;
    int sum = 0;
    int x = 0;
    do
    {
        clrscr();
        cout << "\n\t\t\t\t\t SCORE: " << sum << " pts"
             << "\n                  ******************************\n";
        cout << "                  *       " << arr[i] << "        *\n";
        cout << "                  ******************************\n\n";
        char a[50], b[50];
        int  c;
        if (arr[0])
        {
            ifstream fin;
            fin.open("lvl1.txt");
            if (fin.fail())
            {
                cout << "File doesn't exist!";
                exit(1);
            }
            cout << "\tEnter word: ";
            cin >> a;
            do
            {
                fin >> b >> c;
                if (fin.eof() == 1)
                {
                    cout << "Incorrect! Try Again!";
                    delay(1500);
                    exit(1);
                }
            } while (strcmp(a, b) != 0);
            fin.close();
            if (strcmp(a, b) == 0)
            {
                sum += c;
            }
        }
    } while(s != 0); 
}
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 4
    Just read `time()` and read it again once the user is over and see how many seconds have elapsed between each call. – Havenard Apr 19 '19 at 06:44
  • sorry I'm still new to coding, how do I do that? – SmashMyKeyboard Apr 19 '19 at 06:47
  • 1
    If you want the program to timeout automatically at 30 seconds, well that will be significantly more complicated. – Havenard Apr 19 '19 at 06:47
  • 1
    `time()` returns the Unix Time, which is how many seconds have passed since `1970-01-01 00:00 UTC`. Fetch the current time with `time()` and save it into a variable like `time_t start = time();`. After the user input, check `time() - start` and if the result is more than 30 the user took more than 30 seconds to write the answer. – Havenard Apr 19 '19 at 06:49
  • @SmashMyKeyboard Use an up to date compiler and check what you can do with [`std::chrono`](https://en.cppreference.com/w/cpp/chrono) – πάντα ῥεῖ Apr 19 '19 at 06:54
  • we're only allowed to create the program in a turbo c++ – SmashMyKeyboard Apr 19 '19 at 06:56
  • 1
    @SmashMyKeyboard You should be aware that turbo-c++ was already outdated in the last century. You'll not learn anything useful for modern c++ programming using it. – πάντα ῥεῖ Apr 19 '19 at 07:03
  • I know but my professor, insisted that we should learn from the beginning which is kind of dumb cause most of the stuff here is already using the newer programming language – SmashMyKeyboard Apr 19 '19 at 07:26
  • is it possible to make a timeout with turbo c++? – SmashMyKeyboard Apr 19 '19 at 07:27
  • @SmashMyKeyboard: There's nothing bad with learning from the beginnings, but that would be assembly / machine language. And unlike Turbo C++, assembly does have some relevance today, because it's what CPU's execute. As for the "newer programming language", that would be C++17 (with C++20 in the works). Turbo C++ fails to implement C++98; we're really not exaggerating when we say it was outdated last century. – MSalters Apr 19 '19 at 07:33
  • yea, I'll tell that to my professor after the project. Is it possible to add that timeout? If not, I guess I'll be removing the timer and find something to make it interesting. – SmashMyKeyboard Apr 19 '19 at 07:40
  • [`time()`](https://en.cppreference.com/w/c/chrono/time) is a really ancient function of the C standard library (adopted in the C++ standard library). Why don't you try what was hinted above? This should even work in the outdated Turbo C++... – Scheff's Cat Apr 19 '19 at 07:56
  • I cannot understand why your Prof. insists in usage of Turbo C++. (Changing the Prof. is not an option, is it?) State of the art compilers like `g++` and `clang` are open source and for free. Hence, costs cannot be the issue. Beside of this, there are online compilers which are always good for small tests. (I even use them in daily work to check things out because there is little to no overhead beside of just write, compile, and run on button press.) Turbo C++ came with a nice little IDE. But the compiler is that old and C++ has evolved much in the last decade... – Scheff's Cat Apr 19 '19 at 08:03
  • @Scheff It seems to be usual in india, I also can't grasp their reasons doing so. I am just a bit frightened, since they do all kind of rocket science there as well. – πάντα ῥεῖ Apr 19 '19 at 08:29
  • @Havenard ive tried using the hint you gave me, I got an error called "Too few parameters in call to 'time(long*)' – SmashMyKeyboard Apr 19 '19 at 10:42
  • @Scheff yea i've been watching indians in yt when I don't understand something. Our professor rarely teach so we have to self study. – SmashMyKeyboard Apr 19 '19 at 10:44
  • The link I provided exposes some sample code also. The `long*` parameter is used to provide storage but you don't need to use it. I've always seen it like that: `time_t t = time(nullptr);` (in the link as well). – Scheff's Cat Apr 19 '19 at 10:47
  • _i've been watching indians in yt_ :-) I still don't understand how programming (which is textual communication by nature) can be learned in television (or YouTube which I consider as comparable). What about good old books? Or a google research for (written) articles? It's surely a question of age... ;-) – Scheff's Cat Apr 19 '19 at 10:53
  • 1
    @Scheff following the link you provided and with the help of Havenard. I was able to make it a sort of speed run, time starts when the void start(); opens and ends once you have completed all levels. It prints out how fast you finished the game which is also good alternative to what I want it to be from the start. Btw this is also my first post, I'm liking the community. Cheers – SmashMyKeyboard Apr 19 '19 at 12:07

1 Answers1

0

You can use PIT as a timer I used it in here:

its a mines game in old Turbo C++ and MS-DOS. For more info about PIT see:

there are links to PIT reference and examples I recommend you to see the PCGPE.

Now back to your question. You should register PIT ISR routine doing your timing/timeouting in the background ... Here example I just busted in DOSBOX:

#include <dos.h>
#include <conio.h>
#include <iostream.h>

int stop=0;
int timeout_cnt=0;

const int int_PIT=0x08;
void interrupt (*isr_PIT0)(...)=NULL; // original ISR handler
void interrupt isr_PIT(...) // new ISR handler
    {
    isr_PIT0(); // call original handler
    // here do your stuff
    if (timeout_cnt) timeout_cnt--;
    else stop=1;
    }

void main()
    {
    clrscr();
    isr_PIT0=getvect(int_PIT);  // store original ISR
    setvect(int_PIT,isr_PIT);   // set new ISR
    cout << "start counting" << endl;
    stop=0;
    timeout_cnt=(3*182)/10;     // init timeout 18.2Hz -> 3 sec
    for (;!stop;)
        {
        // here do your stuff
        }
    cout << "timeouted" << endl;
    setvect(int_PIT,isr_PIT0);  // restore original ISR
    getch(); // this is duplicated just to avoid DOSBOX glitches
    getch();
    getch();
    }

You basically need just dos.h all the other stuff is just for printing and handling keyboard.

So I created ISR that hooks up to PIT which is called with 18.2 Hz frequency. The timeout is initiated by setting the timeout_cnt to timeout time value and reseting the stop:

stop = 0;
timeout_cnt = time[sec] * 18.2;

ported to integer... once counter underflows it sets the stop value to true. I also call the original ISR handler as MS-DOS relays on it. Do not forget to restore original ISR before apps exit.

btw the timeout_cnt and stop variables should be volatile but IIRC it does not matter in old Turbo C++ as there are no optimizations that could optimize them out to speak of.

In case you change the PIT frequency you should call the original handler with 18.2 Hz and restore original PIT frequency before apps exit.

This can be also used as a sort of multitasking as you can do stuff in the ISR handler too (regardless of the main code) but you need to be careful as the main code can be paused at any time like in middle of writing string to screen and if your background stuff is printing too you can have distorted output etc ... so similar rules like in multi-threading applies.

Spektre
  • 49,595
  • 11
  • 110
  • 380