Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <conio.h>
- #include <ctype.h>
- #include <time.h>
- // `getch` is not available on linux. look for an alternative
- #ifndef getch
- #define getch _getch
- #endif
- // Maximum guesses a player can take
- #define MAXTRY 10
- // Maximum letters a word may have
- #define MAXLTR 128
- const char * g_Words[] = {
- "house",
- "car",
- "phone",
- "brick",
- "moon",
- "elephant"
- };
- void ClearScreen()
- {
- #if defined(WIN32) || defined(_WIN32)
- system("cls");
- #else
- system("clear");
- #endif
- }
- const char * PickWord()
- {
- // Generate a random index within the range of the words array
- const int i = rand() % (sizeof(g_Words) / sizeof(g_Words[0]));
- // Return the word located at the resulted index
- return g_Words[i];
- }
- void Render(const char * w, const char * l, int tries)
- {
- printf("%3i tries left to guess the word: ", (MAXTRY - tries));
- // Keep an unaltered letter pointer
- const char * lb = l;
- // Output the guessed and hidden word letters
- while (*w != '\0' && (l - lb) < MAXLTR)
- {
- // Print the letter with a space to make it more distinguishable
- putchar(*l);
- putchar(' ');
- // Advance to the next character
- ++l;
- ++w;
- }
- // End the line
- puts("");
- }
- int main()
- {
- // Feed the random generator with a seed
- // Use the generic current time-stamp
- srand(time(NULL));
- // Pick a random word to start with
- const char * word = PickWord();
- // Memory for the guessed words
- // Assuming they're bellow 128 characters
- char letters[MAXLTR];
- // Initialize all guessed characters to _
- memset(letters, '_', sizeof(letters));
- // Try counter and whether to pick a new word
- int tries = 0, renew = 0;
- // Perform the initial render
- Render(word, letters, tries);
- // Enter the application loop
- for(;;)
- {
- const char ch = tolower(getch());
- // Are we trying the game again?
- if (ch == '\r' /* <- ASCII for ENTER */ && renew > 0)
- {
- // Pick a new random word to start with
- // Note: doesn't check if the same word was picked!
- word = PickWord();
- // Initialize all guessed characters to underscore
- memset(letters, '_', sizeof(letters));
- // Reset the `try` counter
- tries = 0;
- // Game was renewed
- renew = 0;
- // Clear the screen
- ClearScreen();
- // Perform the initial render
- Render(word, letters, tries);
- // And start the game
- continue;
- }
- // Are we leaving the game?
- if (ch == 27 /* <- ASCII for ESC */)
- {
- // Clear the screen
- ClearScreen();
- // Show the answer only if the word wasn't guessed
- if (strncmp(word, letters, strlen(word)) == 0)
- {
- puts("Sad to see you go. And FYI, I let you win!");
- }
- else
- {
- printf("Sad to see you go. The word was: %s\n", word);
- }
- // Stop the game
- break;
- }
- // Is this an accepted letter? (isalnum(ch) alpha-numeric?)
- else if (isalpha(ch) == 0)
- {
- continue; // Invalid input
- }
- // Get pointers to the word and list of letters to compare
- char * l = letters;
- const char * w = word;
- // See if the given character is correct
- while (*w != '\0' && (l - letters) < MAXLTR)
- {
- // Was this letter already guessed?
- // And is this letter a match?
- if (*l == '_' && *w == ch)
- {
- // Unlock this letter
- *l = ch;
- // We're done here
- break;
- }
- // Advance to the next character
- ++l;
- ++w;
- }
- // Was there a match?
- if (*w == '\0' || (l - letters) >= MAXLTR)
- {
- ++tries; // Increment the number of tries
- }
- else
- {
- // tries = 0; // Reset the counter whenever the player guesses a letter?
- }
- // Did we exhaust the number of tries?
- if (tries >= MAXTRY)
- {
- // Clear the screen
- ClearScreen();
- // Ask the player if he wants to try again
- printf("GAME OVER! The word you were looking for was actually '%s'\n", word);
- puts("Press ENTER to try again or ESC to leave.");
- // Renew the game on next loop
- renew = 1;
- // Wait for input
- continue;
- }
- // Did the user completed guessing the word?
- else if (strncmp(word, letters, strlen(word)) == 0)
- {
- // Clear the screen
- ClearScreen();
- // Ask the player if he wants to try again
- printf("YOU WON! The word you were looking for was indeed '%s'\n", word);
- puts("Press ENTER to try again or ESC to leave.");
- // Renew the game on next loop
- renew = 1;
- // Wait for next input
- continue;
- }
- // Render everything to the screen again
- Render(word, letters, tries);
- };
- }
Add Comment
Please, Sign In to add comment