Advertisement
Sayukoo

[PROJEKT] WISIELEC?

Nov 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.12 KB | None | 0 0
  1. /// https://gieseanw.wordpress.com/2010/03/29/ascii-hangman/ <<< JEDEN PROJEKT
  2. /// https://github.com/gieseanw/Hangman
  3. /// WISIELECE WZÓR
  4.  
  5. #include<stdio.h>    /*Glowna deklaracja pliku*/
  6.  
  7. #include<string.h>   /*<string.h> dla strcmp();,strlen(); <<  Uzycie tych funkcji przez ta biblioteke*/
  8.  
  9. #include<stdlib.h>
  10.  
  11. void showHangman(int);
  12.  
  13. int main(void)
  14. {
  15.     char hangmanWord[100], tempWord[100];       /**hangmanWord[] array for the original word and tempWord[] array to get the alphabet from user and compare it with original word**/
  16.     char hangmanOutput[100];                    /**This array will show the remaining blanks and correct inputs**/
  17.     int wrongTry = 6 , matchFound = 0;          /**player will get 5 chance, so we use wrongTry as chance counter**/
  18.                                                 /**matchFound to search the alphabet, if the alphabet from user does not exist
  19.                                                 in the original word it will remain 0, upon finding the word, matchFound will
  20.                                                 be set as 1**/
  21.     int counter = 0 , position = 0, winner, length , i;
  22.     char alphabetFromUser;
  23.  
  24.     system("cls");                              /**for clearing the screen**/
  25.     printf("\n\n Enter any word in small case and hit >>ENTER<<");
  26.     printf("\n\n\t Enter HERE ==>  ");
  27.     scanf("%s",hangmanWord);                    /**get the string from opponent**/
  28.     printf("\n\n Now give the COMPUTER to your friend and see if he/she can CRACK it!!!");
  29.     printf("\n\n\tHIT >>ENTER<<");
  30.     getchar();                                  /**hold the computer screen**/
  31.     length = strlen(hangmanWord);               /**get the length of the word**/
  32.  
  33.     system("cls");
  34.  
  35.     printf("\n\n !!!!!!!!!!!!!!!!!!!Welcome to the HANGMAN GAME!!!!!!!!!!!!!!!!!\n\n\n");   /**Brief description of the game**/
  36.     printf("\n\n You will get 5 chances to guess the right word");
  37.     printf("\n\n So help the Man and get...set...GO..!!");
  38.  
  39.     getchar();
  40.  
  41.     printf("\n\n\tHIT >>ENTER<< ");
  42.  
  43.     getchar();
  44.  
  45.     system("cls");
  46.  
  47.         printf("\n\t||===== ");                 /**show the HANGMAN**/
  48.     printf("\n\t||    | ");
  49.         printf("\n\t||      ");
  50.         printf("\n\t||      ");
  51.         printf("\n\t||      ");
  52.         printf("\n\t||      ");
  53.  
  54.     printf("\n\n     The word has %d alphabets \n\n",length);  /**tell the user how many alphabets the word has**/
  55.     for( i = 0; i < length ; i++)
  56.     {
  57.         hangmanOutput[i] = '_';
  58.         hangmanOutput[length] = '\0';
  59.     }
  60.  
  61.     for(i = 0 ; i < length ; i++)
  62.     {
  63.         printf(" ");
  64.         printf("%c",hangmanOutput[i]);        /**Show the Word With n(length of the original word) number of underscores (_)**/
  65.  
  66.     }
  67.     while(wrongTry != 0)                        /**while loop for exiting the program when no try left**/
  68.     {
  69.         matchFound = 0;
  70.         printf("\n\n   enter any alphabet from a to z and please use small case!!");
  71.         printf("\n\n\t Enter HERE ==> ");
  72.  
  73.         fflush(stdin);
  74.  
  75.         scanf("%c",&alphabetFromUser);            /**get alphabet from user**/
  76.     if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/
  77.     {
  78.         system("cls");
  79.         printf("\n\n\t Wrong input TRY AGAIN ");
  80.         matchFound = 2;
  81.     }
  82.     fflush(stdin);
  83.     if (matchFound != 2)
  84.     {
  85.         for(counter=0;counter<length;counter++)    /**for loop to check whether player input alphabet exists or not in the word**/
  86.         {
  87.             if(alphabetFromUser==hangmanWord[counter])
  88.              {
  89.                matchFound = 1;
  90.                      }//end of if()
  91.             }//end of for()
  92.  
  93.        if(matchFound == 0)                      /**in case of wrong guess**/
  94.         {
  95.               printf("\n\t :( You have %d tries left ",--wrongTry);
  96.               getchar();
  97.               showHangman(wrongTry);
  98.               getchar();
  99.         }//end of if()
  100.  
  101.        else
  102.        {
  103.          for(counter = 0; counter < length; counter++)
  104.              {
  105.                  matchFound = 0;
  106.                  if(alphabetFromUser == hangmanWord[counter])
  107.               {
  108.                  position = counter ;
  109.                  matchFound = 1;
  110.               }//end of if
  111.               if(matchFound == 1)
  112.               {
  113.                  for(i = 0 ; i < length ; i++)
  114.                  {
  115.                       if( i == position)
  116.                       {
  117.                           hangmanOutput[i] = alphabetFromUser; /**Put the alphabet at right position**/
  118.                       }
  119.                       else if( hangmanOutput[i] >= 'a' && hangmanOutput[i] <= 'z' ) /** If the position already occupied
  120.                                                                                   by same alphabet then no need to
  121.                                                                                   fill again EASY!! and continue */
  122.                       {
  123.                           continue;
  124.                       }
  125.  
  126.                       else
  127.                       {
  128.                           hangmanOutput[i] = '_';            /** Put a blank at not guessed alphabet position **/
  129.                       }
  130.                 }
  131.                 tempWord[position] = alphabetFromUser;      /**put the alphabet in another char array to check with the original word**/
  132.                 tempWord[length] = '\0';                    /**put the NULL character at the end of the temp string**/
  133.                 winner = strcmp(tempWord,hangmanWord);      /**upon True comparison it will return 0**/
  134.  
  135.                 if(winner == 0)                             /**if the player guessed the whole word right then he/she is the WINNER**/
  136.                 {
  137.                     printf("\n\n\t \t YAHOO!!!!! You are the WINNER !!!!!");
  138.                     printf("\n\n\t The Word was %s ",hangmanWord);
  139.                     printf("\n\n\n\n\t\tEASY HUH???\n\n");
  140.                     getchar();
  141.                     return 0;
  142.                 }//end of inner if
  143.            }//end of outer if
  144.         }//end of for loop
  145.       }//end of else
  146.      }// end of if(matchFound != 2) condition
  147.  
  148.     printf("\n\n\t");
  149.     for(i = 0 ; i < length ; i++)
  150.       {
  151.           printf(" ");
  152.           printf("%c",hangmanOutput[i]);                /**Show the original Word With blanks and right Input alphabet**/
  153.       }
  154.  
  155.     getchar();
  156.     }//end of while loop
  157.  
  158.       if(wrongTry <= 0)                                 /**if the player can not guess the whole word in 5 chaces**/
  159.       {
  160.           printf("\n\n\t The Word was %s ",hangmanWord);
  161.           printf("\n\n\t The man is dead you IDIOT!!!!!");
  162.           printf("\n\n\t Better luck next!!!");
  163.  
  164.       }
  165. getchar();
  166. return 0;
  167. }//end of main();
  168.  
  169.  
  170.  
  171. void showHangman(int choice)                            /**This function show the hangman after each wrong try**/
  172.  {
  173.  
  174.      switch(choice)
  175.      {
  176.  
  177.      case 0:
  178.          system("cls");
  179.     printf("\n\t||===== ");
  180.     printf("\n\t||    | ");
  181.     printf("\n\t||   %cO/",'\\');
  182.     printf("\n\t||    | ");
  183.     printf("\n\t||   / %c",'\\');
  184.     printf("\n\t||      ");
  185.     break;
  186.      case 1:
  187.          system("cls");
  188.     printf("\n\t||===== ");
  189.     printf("\n\t||    | ");
  190.     printf("\n\t||   %cO/",'\\');
  191.     printf("\n\t||    | ");
  192.     printf("\n\t||     %c",'\\');
  193.     printf("\n\t||      ");
  194.     break;
  195.      case 2:
  196.          system("cls");
  197.     printf("\n\t||===== ");
  198.     printf("\n\t||    | ");
  199.     printf("\n\t||   %cO/",'\\');
  200.     printf("\n\t||    | ");
  201.     printf("\n\t||      ");
  202.     printf("\n\t||      ");
  203.     break;
  204.      case 3:
  205.          system("cls");
  206.     printf("\n\t||===== ");
  207.     printf("\n\t||    | ");
  208.     printf("\n\t||   %cO/",'\\');
  209.     printf("\n\t||      ");
  210.     printf("\n\t||      ");
  211.     printf("\n\t||      ");
  212.     break;
  213.      case 4:
  214.          system("cls");
  215.     printf("\n\t||===== ");
  216.     printf("\n\t||    | ");
  217.     printf("\n\t||   %cO ",'\\');
  218.     printf("\n\t||      ");
  219.     printf("\n\t||      ");
  220.     printf("\n\t||      ");
  221.     break;
  222.      case 5:
  223.          system("cls");
  224.     printf("\n\t||===== ");
  225.     printf("\n\t||    | ");
  226.     printf("\n\t||    O ");
  227.     printf("\n\t||      ");
  228.     printf("\n\t||      ");
  229.     printf("\n\t||      ");
  230.     break;
  231.       }//end of switch-case
  232.       return;
  233.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement