[SOURCE] Basic C++ Hangman in the Command Prompt

Discussion in 'Programming General' started by Nullware, Feb 27, 2008.

[SOURCE] Basic C++ Hangman in the Command Prompt
  1. Unread #1 - Feb 27, 2008 at 10:01 AM
  2. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [SOURCE] Basic C++ Hangman in the Command Prompt

    I made this to see what I was capable of in a little bit of spare time and I thought it turned out all right so I've decided to share it in hopes that it might help some people. (It's commented almost throughout) :)

    I also wouldn't mind if any of the better programmers want to point out flaws in my code or my style of programming. Points to improve on and criticism would actually be appreciated. ;)

    I use Dev-CPP which uses the MinGW compiler so you might have to change the return statement at the end or make other minor changes to get it to work.

    Code:
    #include <cstdlib> // Standard C++ library
    #include <iostream> // Basic input/output library
    #include <conio.h> // Required for getch()
    
    using namespace std;
    
    int toupper(int c);
    
    // Match the user's guess up against the puzzle word and update any characters guessed right
    int updateWord(string& tempWord, string realWord, string guessedChar) {
           string previousWord = tempWord; // Hold the previous value of the puzzle with unknowns
    
           for(int i=0;i<=realWord.length()-1;i++)  // Loop through the length of the puzzle string
           {
             if (realWord.substr(i,1)==guessedChar) { // If the current character being looped through matches the user's guess
             tempWord = tempWord.substr(0,i)+ guessedChar + tempWord.substr(i+1,tempWord.length()); // Replace that character in the progress string
              
             }
           }
           
           if(previousWord == tempWord) { // If the puzzle hasn't changed
               cout << "Sorry, there are none of that letter in the puzzle.\n";
           }
           else{ // The puzzle was updated due to some letters being found
               cout << "Some letters were found and now your puzzle has been updated.\n";
           }
    }
    
    int main(int argc, char *argv[])
    {
        string dictionary[6] = { "hockey", "revolution", "extortion", "football", "alias", "detention" }; // Initialize the dictionary of words to use
        string guess, playagain; // Strings to hold the user input
        
        cout << "------------------ Welcome to Hangman! ------------------\n";
        
      do
      {
        
        srand(time(0)); // Initialize our reference to the time variable for randomizations
        int wordNumber = rand() % 6; // Generate a random number to choose a word from the dictionary
        
        string word = dictionary[wordNumber]; // Save the value of the word we're using in an independant variable
        string tempWord (word.length(),'_'); // Setup a variable with underscores to represent letters not known of the length of the chosen word
        
        while(tempWord != word) // While the word hasn't been guessed loop through
        {
        cout << "\nThe puzzle is " << tempWord << " (" << tempWord.length() << " characters), guess a letter.\n"; // Ask the user to guess a letter and output his current status in guessing the word
    
        guess = getch(); // Get the user's guess by keyboard input
    
        updateWord(tempWord, word, guess); // Check the user's guess up against the puzzle word characters            
        }
        
        cout << "You win! The word was: " << word << "\n"; // Output this message if the user wins
        
        do {
        cout << "\nWould you like to play again? (Y/N).\n";
        playagain = getch();
        }while(playagain!="y" && playagain !="n"  && playagain !="Y"  && playagain !="N"); // Keep asking if the user wants to play again until he enters yes (y) or no (n)
    
      }while(playagain=="y" || playagain=="Y"); // Loop through the game again if he presses y (yes)
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
     
  3. Unread #2 - Feb 27, 2008 at 1:03 PM
  4. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    [SOURCE] Basic C++ Hangman in the Command Prompt

    system("pause") is wildly inefficient, try getchar.
     
  5. Unread #3 - Feb 27, 2008 at 1:38 PM
  6. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [SOURCE] Basic C++ Hangman in the Command Prompt

    Oh, Dev-CPP automatically puts that at the end of your code so you can view any output before it closes. I can remove it anyway now because it will always ask you whether you want to quit before it does. ;)
     
  7. Unread #4 - Feb 27, 2008 at 7:11 PM
  8. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    [SOURCE] Basic C++ Hangman in the Command Prompt

    Lol, system("PAUSE") only doesn't work in Unix or Cygwin environments. What to do is write an app which does
    printf("Press any key to continue . . . ");
    getch();
    and put that in /usr/bin/
     
  9. Unread #5 - Feb 29, 2008 at 8:51 AM
  10. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [SOURCE] Basic C++ Hangman in the Command Prompt

    I was hoping for some feedback/criticism on my methods of programming and such..
     
  11. Unread #6 - Feb 29, 2008 at 2:33 PM
  12. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    [SOURCE] Basic C++ Hangman in the Command Prompt

    Not a bad idea, actually. Still, that's a shitty way of doing things, when you could just use the cross-platform getch.
     
  13. Unread #7 - Feb 29, 2008 at 7:12 PM
  14. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [SOURCE] Basic C++ Hangman in the Command Prompt

    Contradictory statement is contradictory.

    Good job for terminal ;)
     
< [Poll][TuT] Anyone wants to make an autotyper in C#??? | Hex Editing Functions >

Users viewing this thread
1 guest


 
 
Adblock breaks this site