1

I'm making the game Boggle in C for a project. If you're not familiar with Boggle, that's okay. Long story short, there's a time limit on each round. I'm making the time limit 1 minute.

I have a loop that displays the game board and asks the user to enter a word, then calls a function that checks to see if the word is accepted, and then it loops back again.

    while (board == 1)
{

    if (board == 1)
    {
        printf(display gameboard here);
        printf("Points: %d                  Time left: \n", player1[Counter1].score);

        printf("Enter word: ");
        scanf("%15s", wordGuess);

        pts = checkWord(board, wordGuess);

The while (board == 1) needs to be changed so that it loops only for 1 minute.

I want the user to only be able to do this for 1 minute. I also would like for the time to be displayed where I have Time left: in the printf statement. How would I achieve that? I've seen some examples online of others using a timer in C and the only way I'm thinking this is possible is if I let the user go past the time limit but when the user tries to enter a word past the time limit, it will notify them that time is up. Is there any other way?

EDIT: I'm coding this on a Windows 10 PC.

brann22
  • 13
  • 3
  • 1
    getitimer()/setitimer() API can help you. – gzh Apr 27 '18 at 02:29
  • 1
    what platform are you using? – John Stone Apr 27 '18 at 02:34
  • 2
    There isn’t a good way to do it in standard C; you are obliged to use platform-specific code. So, identifying the platform is crucial. – Jonathan Leffler Apr 27 '18 at 03:01
  • Sorry. I forgot to mention I'm using Windows 10. – brann22 Apr 27 '18 at 03:15
  • 1
    There is nothing in ANSI C concerning timers, you need to call an API. There are several ways to do this, you could setup a Timer Event, but that's probably too much for a beginner, and you want to show the time left, so look at https://msdn.microsoft.com/en-us/library/windows/desktop/ms724338(v=vs.85).aspx – cdarke Apr 27 '18 at 06:58
  • @brann22: If you want to update the timer simultaneously while the user types, and control the entire terminal window contents, you can use [Curses](https://en.wikipedia.org/wiki/Curses_(programming_library)). For Windows 10, you can use either ncurses via WSL, or for example [PDCurses](https://sourceforge.net/projects/pdcurses/files/pdcurses/3.6/). – Nominal Animal Apr 28 '18 at 09:56

1 Answers1

0

Use standard C time() to obtain the number of seconds (real-world time) since Epoch (1970-01-01 00:00:00 +0000 UTC), and difftime() to count the number of seconds between two time_t values.

For the number of seconds in a game, use a constant:

#define  MAX_SECONDS  60

Then,

char    word[100];
time_t  started;
double  seconds;
int     conversions;

started = time(NULL);
while (1) {

    seconds = difftime(time(NULL), started);
    if (seconds >= MAX_SECONDS)
        break;

    /* Print the game board */

    printf("You have about %.0f seconds left. Word:", MAX_SECONDS - seconds);
    fflush(stdout);

    /* Scan one token, at most 99 characters long. */
    conversions = scanf("%99s", word);
    if (conversions == EOF)
        break;    /* End of input or read error. */
    if (conversions < 1)
        continue; /* No word scanned. */

    /* Check elapsed time */
    seconds = difftime(time(NULL), started);
    if (seconds >= MAX_SECONDS) {
        printf("Too late!\n");
        break;
    }

    /* Process the word */
}
Nominal Animal
  • 38,216
  • 5
  • 59
  • 86