Hangman using C++

Hangman in C++

Here's a video of Hangman game that I've created using visual studio 2015 in C++ :


I've  changed the source code a little from what you've seen in the video. Now this game even has a score board feature! have a look at it!





other changes in the update include :
  • At each word you get 6 lives instead of overall 9 lives which you've seen in the video.
  • When you reset the game by pressing R (upper-case), instead of setting lives to zero and going to menu, the game would automatically restart (no going to menu as seen in video)
  • Your score would be 10 multiplied by the number of word you uncovered successfully!


here's the updated source code :

 // Hangman.cpp : to create a hangman game  
   
 #include "stdafx.h"  
 #include "string.h"  
 #include "stdlib.h"  
 #include "conio.h"  
 #include "time.h"  
   
 //-------------------------------------------------------------------------------------------------------  
   
 //functions frequently used  
   
 void clear(void)  
 {  
      system("cls");      //clear the screen  
 }  
   
 void wait(int type)  
 {  
      char consume;  
      if (type == 1)      //when whitespaces from previous entry are present  
      {  
           printf("\nenter any key to continue : ");  
           scanf_s("%c", &consume, 1); //-->consumes whitespaces  
           _getch();       //enter a key to contnue  
      }  
      if (type == 2)      //when whitespaces are NOT present  
      {  
           printf("\nenter any key to continue : ");  
           _getch();       //enter a key to continue  
      }  
 }  
   
 //---------------------------------------------------------------------------------------------------------  
   
 //class declarations with member function prototypes  
   
 class Menu  
 {  
      char selection;      //stores the user selection  
   
 public:  
      void welcomeScreen(void); //starting welcome screen along with credits  
      void selectionList(void); //the menu for user selection  
 };  
   
 class List  
 {  
      char data_word[30];    //data present at each node  
      List *next;        //the member which makes the node self referential and connects it to another node  
      List *head,*tail;     //head (and) tail addresses of the list  
      int size_of_list;     //size of the list constructed from file   
   
 public:  
   
      List();          //constructor for List object  
      void construct(void);   //construct all the words as linked list  
      char* throwWord(void);   //display and delete a random word from the constructed list  
      void destroyOne(int);   //destroy a single node  
      void destroyAll(void);   //destroy all the nodes  
      void displayAll(void);   //display all the words  
 };  
   
 class Game  
 {  
      int word_no;        //the word number  
      int lives;         //number of lives left  
      char guess;        //user's guess letter  
      char prev_guesses[30];   //user's previosly gussed letters  
      char *word;        //stores the word which user must find  
      char hide_word[30];    //user must convert this into the 'word'(the above char[16])   
   
 public:  
   
      Game();  
      void hideWord(void);    //initially masquerade the word with '*'  
      void display(void);     //game screen display  
      int checkForChar(void);   //reveal the letter if present  
      int isRevealed(void);    //check if user has guessed all the letters  
      void mechanism(void);    //the sequence of implementation of above member functions  
      void writeDetails(void);  //write details into score board  
 };  
   
 //-----------------------------------------------------------------------------------------------------------  
   
 //member function definitions of 'Menu' class :  
   
 void Menu::welcomeScreen(void)  
 {  
      clear();  
   
      printf("\t  HANGMAN v.1.0\n\n\n\n-------- created by CHERUBIM :^) --------\n\n\n");  
      wait(2);  
   
      selectionList();  
 }  
   
 void Menu::selectionList(void)  
 {  
      clear();  
   
      printf("\t  Hangman v.1.0\n----------------------------------------------------\n\n");  
        
      printf("1. Play\n\n2. Score-board\n\n3. Exit\n\n\n your selection : ");  
      scanf_s(" %c", &selection, 1);  
        
      if (selection == '1')  
      {  
           Game Obj;  
           Obj.mechanism();  
           selectionList();  
      }  
      else if (selection == '2')  
      {  
           clear();  
           printf("\t  Hangman v.1.0\n----------------------------------------------------\n\n");  
   
           FILE *file_pointer;  
           char read;  
   
           fopen_s(&file_pointer, "score-board.txt", "r");  
           if (file_pointer == NULL)  
           {  
                printf("file opening error!\n\n");  
                wait(1);  
           }  
   
           printf("  name        score\n");  
           printf("----------      ---------\n\n");  
           while ((read = getc(file_pointer)) != EOF)  
           {  
                printf("%c", read);  
           }  
   
           fclose(file_pointer);  
           wait(1);  
      }  
      else if (selection == '3')  
      {  
           exit(0);  
      }  
      else  
      {  
           clear();  
             
           printf("invalid entery. enter either 1 or 2\n\n");  
           wait(1);  
             
           selectionList();  
      }  
   
      selectionList();  
 }  
   
 //member function definitions of 'List' class :  
   
 List::List()  
 {  
      strcpy_s(data_word, "");  
      next = NULL;  
      head = NULL;  
      tail = NULL;  
      size_of_list = 0;  
 }  
   
 void List::construct(void)  
 {  
      FILE *file_pointer;  
      List *temp;  
      char string[30];  
   
      fopen_s(&file_pointer, "words_collection.txt", "r");  
   
      if (file_pointer == NULL)  
      {  
           clear();  
           printf("file opening error!\n\n");  
   
           wait(2);  
   
           exit(1);  
      }  
   
      for (; fscanf_s(file_pointer, "%s", string, 29) != EOF; size_of_list++)  
      {  
           temp = (List*)malloc(sizeof List);  
           strcpy_s(temp->data_word, string);  
           temp->next = NULL;  
   
           if (size_of_list == 0)  
           {  
                head = tail = temp;  
           }  
           else  
           {  
                tail->next = temp;  
                tail = temp;  
           }  
      }  
   
      fclose(file_pointer);  
 }  
   
 char* List::throwWord(void)  
 {  
      char *throw_word = (char*)malloc(30);  
      int position;  
      List *temp = head;  
   
      if (size_of_list == 0)  
      {  
           clear();  
           printf("CONGRATULATIONS! YOU GUESSED ALL THE WORDS RIGHT!! YOU'RE A CHAMPION :)\n\n\n");  
           wait(2);  
           exit(0);  
      }  
      else  
      {  
           position = (rand() % size_of_list) + 1;  
      }  
   
      for (int index = 1; index < position; temp = temp->next, index++);  
        
      for (int index = 0; temp->data_word[index] != '\0'; index++)  
      {  
           throw_word[index] = temp->data_word[index];  
           throw_word[index + 1] = '\0';  
      }  
        
      destroyOne(position);  
   
      return throw_word;  
 }  
   
 void List::destroyOne(int position)  
 {  
      List* temp = head;  
   
      if (position == 1)  
      {  
           temp = head->next;  
           free(head);  
           head = temp;  
      }  
      else  
      {  
           List *prev = NULL;  
   
           for (int index = 1; index < position; index++)  
           {  
                prev = temp;  
                temp = temp->next;  
           }  
   
   
           prev->next = temp->next;  
           free(temp);  
      }  
   
      size_of_list--;  
 }  
   
 void List::destroyAll(void)  
 {  
      List* temp;  
   
      while (head != NULL)  
      {  
           temp = head->next;  
           free(head);  
           head = temp;  
      }  
 }  
   
 void List::displayAll(void) //for debugging purpose  
 {  
      List* temp = head;  
      printf("size of list : %d\n\n", size_of_list);  
   
      while (temp != NULL)  
      {  
           printf("%s\n", temp->data_word);  
           temp = temp->next;  
      }  
 }  
   
   
 //member function definitions of 'Game' class :  
   
 Game::Game()  
 {  
      word_no = 1;  
      lives = 6;  
      guess = 'a';  
      strcpy_s(prev_guesses, "");  
 }  
   
 void Game::display(void)  
 {  
      printf("\t  Hangman v.1.0\n----------------------------------------------------\n   || X -> exit || R -> restart ||\n\n");  
      printf("Word number : %4d\n\n", word_no);  
      printf("lives left : %4d\n\n\n", lives);  
      printf("%s\n\n\n", hide_word);  
      printf("previously guessed letters : [ %s ]\n\n", prev_guesses);  
      printf("your present guess : ");  
 }  
   
 void Game::hideWord(void)  
 {  
      int index;  
   
      for (index = 0; index < (int) strlen(word); index++)  
      {  
           hide_word[index] = '*';  
      }  
   
      hide_word[index] = '\0';  
 }  
   
 int Game::checkForChar(void)  
 {  
      int return_value = 0;  
   
      // return-value   action  
      //--------------  --------  
      //   0      invalid  
      //   1      present  
      //   2      exit  
   //   3      restart  
      //   -1     not-present  
   
      scanf_s(" %c", &guess, 1);  
   
      if (guess == 'X')  
      {  
           return 2;  
      }  
        
      if (guess == 'R')  
      {  
           return 3;  
      }  
   
      if ((guess >= 'a') && (guess <= 'z'))  
      {  
           for (int index = 0; index < (int) strlen(word); index++)  
           {  
                if (word[index] == guess)  
                {  
                     return_value = 1;  
                     hide_word[index] = word[index];  
                }  
           }  
   
           if (return_value != 1)  
           {  
                return_value = -1;  
           }  
      }  
   
      return return_value;  
 }  
   
 int Game::isRevealed(void)  
 {  
      for (int index = 0; index < (int) strlen(word); index++)  
      {  
           if (hide_word[index] == '*')  
           {  
                return 0;  
           }  
      }  
   
      return 1;  
 }  
   
 void Game::mechanism(void)  
 {  
      List list;  
      int catch_flag = 0;  
   
      list.construct();  
   
      while (lives > 0)  
      {  
           word = list.throwWord();  
           hideWord();  
   
           while ((isRevealed() == 0) && (lives > 0))  
           {  
                clear();  
                display();  
   
                catch_flag = checkForChar();  
                if (catch_flag == 1 || catch_flag == -1) //valid character-> present || not present  
                {  
                     int present_size = strlen(prev_guesses);  
   
                     prev_guesses[present_size] = guess;  
                     prev_guesses[present_size + 1] = '\0';  
   
                     if (catch_flag == 1)  
                     {  
                          printf("\n~the letter is present!~\n\n\n");  
                     }  
                     else if (catch_flag == -1)  
                     {  
                          printf("\n~the letter is NOT present~\n\n\n");  
                          lives--;  
                     }  
                }  
                if (catch_flag == 2) //to exit  
                {  
                     clear();  
                     printf("thanks for playing :^)\n\n");  
                     wait(2);  
                     list.destroyAll();  
                     exit(0);  
                }  
                if (catch_flag == 3) //to restart  
                {  
                     clear();  
                     printf("game set to restart... :^)\n\n");  
                     wait(2);  
                     list.destroyAll();  
                     mechanism();  
                }  
                if (catch_flag == 0)  
                {  
                     printf("invalid entry! use only lower case alphabets!!\n\n\n");  
                }  
   
                wait(1);  
           }  
   
           if (lives > 0)  
           {  
                word_no++; //increase the word number  
                strcpy_s(prev_guesses, "");  
                lives = 6;  
   
                clear();  
                printf("\n\n~you guessed the word, it's %s !~", word);  
                wait(2);  
           }  
   
           free(word);  
      }  
   
      clear();  
   
      printf("\n\nout of lives :( ... you guessed %d words rightly\n", word_no - 1);  
      writeDetails();  
      list.destroyAll();  
      wait(2);  
 }  
   
 void Game::writeDetails(void)  
 {  
      FILE *file_pointer;  
      char name[11];  
   
      printf("\n\nenter your name (max 10 characters) : ");  
      scanf_s("%s", name, 10);  
   
      fopen_s(&file_pointer, "score-board.txt", "w");  
      if (file_pointer == NULL)  
      {  
           clear();  
           printf("file creation error!\n\n");  
           wait(1);  
   
           exit(-1);  
      }  
   
      fprintf(file_pointer, "%10s\t\t%4d\n\n", name, (word_no-1)*10);  
      fclose(file_pointer);  
   
      printf("\n\nname saved in score board!\n");  
 }  
   
 //--------------------------------------------------------------------------------------------------------------  
   
 //main function  
   
 int main(void)  
 {  
      srand((unsigned int)(time(NULL)));  
   
      Menu menu;  
      menu.welcomeScreen();  
   
   return 0;  
 }  
   
   
   

If you want to run the program on your PC, here's a way to do it:

  • Install Microsoft visual studio 2015 with C++ language.
  • Copy and Paste source code.
  • Before building the project you need to create a txt file with the name words_collection and enter all the words you want to have in your game in separate lines like this:
  • Now you can compile and run you program :) ENJOY!

Comments

Post a Comment

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

PvP Chain reaction game using Python

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm