SHARE
TWEET

Play with Words

Ghislain_Mike Apr 26th, 2019 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct PLAYER {
  6.     char NAME[12];
  7.     char PASSWORD[12];
  8. };
  9.  
  10. void menu_message() {
  11.     puts("+----------------------------------------------+");
  12.     puts("|              ~  GAME OF WORDS ~              |");
  13.     puts("| Increase your vocabulary playing with words! |");
  14.     puts("| 1. Login to continue                         |");
  15.     puts("| 2. Register new player                       |");
  16.     puts("| Enter any other letter to exit               |");
  17.     puts("+----------------------------------------------+");
  18. }
  19.  
  20. struct PLAYER register_message() {
  21.     puts("+--------------------------+");
  22.     puts("| Enter player information |");
  23.     puts("+--------------------------+");
  24.  
  25.     struct PLAYER NEW_PLAYER;
  26.  
  27.     printf(" Username: ");
  28.     scanf("%s", NEW_PLAYER.NAME);
  29.  
  30.     printf(" Password: ");
  31.     scanf("%s", NEW_PLAYER.PASSWORD);
  32.  
  33.     return NEW_PLAYER;
  34. }
  35.  
  36. void back_to_menu_message() {
  37.     puts("+------------------+");
  38.     puts("| 1. Back to menu  |");
  39.     puts("| 2. Start playing |");
  40.     puts("+------------------+");
  41. }
  42.  
  43. void start_message() {
  44.     puts("+--------------------------------------+");
  45.     puts("|          ~ START PLAYING ~           |");
  46.     puts("| Form english words using the letters |");
  47.     puts("| provided, choose the number of       |");
  48.     puts("| letters the word should have.        |");
  49.     puts("| Enter the word \"quit\" to stop!       |");
  50.     puts("+--------------------------------------+");
  51. }
  52.  
  53. int find_word(char WORD[12]) {
  54.     char STRING[50];
  55.     int COUNT = 0;
  56.  
  57.     FILE *in_file = fopen("words.txt", "r");
  58.  
  59.     if (in_file == NULL) {
  60.         printf("Error file missing\n");
  61.         exit(0);
  62.     }
  63.  
  64.     while(!feof(in_file)) { //this loop searches the for the current word
  65.         fscanf(in_file,"%s",STRING);
  66.         if(!strcmp(STRING,WORD)) COUNT++; //if match found increment num
  67.     }
  68.     return COUNT;
  69. }
  70.  
  71. int find_player(char NAME[12], char PASSWORD[12]) {
  72.     FILE * MY_FILE = fopen("players.txt","r");
  73.     char FILE_NAME[12], FILE_PASSWORD[12];
  74.     int RESULT;
  75.  
  76.     if(MY_FILE != NULL) {
  77.         while(!feof(MY_FILE)) {
  78.             fscanf(MY_FILE,"%s\t%s", FILE_NAME, FILE_PASSWORD);
  79.  
  80.             if(strcmp(NAME, FILE_NAME) == 0 && strcmp(PASSWORD, FILE_PASSWORD) == 0) RESULT++;
  81.         }
  82.     }
  83.     return RESULT;
  84. }
  85.  
  86. int start_scrabble() {
  87.     int CHOICE, NUMBER_OF_LETTERS;
  88.     struct PLAYER MY_PLAYER;
  89.     char MENU[12] = {"Menu"}; // default value
  90.     char WORD[12] = {"Empty"};
  91.     FILE * MY_FILE;
  92.  
  93.     while(strcmp(MENU, "Menu") == 0) {
  94.         menu_message(); // menu
  95.         scanf("%d", &CHOICE);
  96.  
  97.         switch(CHOICE) {
  98.         case 1:
  99.             puts("+--------------------------+");
  100.             puts("| Enter player information |");
  101.             puts("+--------------------------+");
  102.  
  103.             printf(" Username: ");
  104.             scanf("%s", MY_PLAYER.NAME);
  105.  
  106.             printf(" Password: ");
  107.             scanf("%s", MY_PLAYER.PASSWORD);
  108.  
  109.             if (find_player(MY_PLAYER.NAME, MY_PLAYER.PASSWORD) > 0) {
  110.                 start_message();
  111.                 printf(" Number of letters: ");
  112.                 scanf("%d", &NUMBER_OF_LETTERS);
  113.  
  114.                 do {
  115.                     printf("> ");
  116.                     scanf("%s", WORD);
  117.  
  118.                     if (find_word(WORD) == 0) {
  119.                         if (strlen(WORD) == NUMBER_OF_LETTERS) puts(" [Try again!]");
  120.                         else printf("[The word should have %d words!]\n", NUMBER_OF_LETTERS);
  121.  
  122.                     } else puts(" [Good job!]");
  123.                 } while(strcmp(WORD, "quit") != 0);
  124.                 if(strcmp(WORD, "quit") == 0) exit(0);
  125.             }
  126.  
  127.             break;
  128.         case 2:
  129.             MY_PLAYER = register_message(); // registration
  130.  
  131.             MY_FILE = fopen("players.txt","a+");
  132.             fprintf(MY_FILE,"%s\t%s\n",MY_PLAYER.NAME, MY_PLAYER.PASSWORD);
  133.  
  134.             back_to_menu_message();
  135.             scanf("%d", &CHOICE);
  136.  
  137.             switch(CHOICE) {
  138.             case 1:
  139.                 strcpy(MENU, "Menu");
  140.                 break;
  141.             case 2:
  142.                 start_message();
  143.                 printf(" Number of letters: ");
  144.                 scanf("%d", &NUMBER_OF_LETTERS);
  145.  
  146.                 do {
  147.                     printf("> ");
  148.                     scanf("%s", WORD);
  149.  
  150.                     if (find_word(WORD) == 0) {
  151.                         if (strlen(WORD) == NUMBER_OF_LETTERS) puts(" [Try again!]");
  152.                         else printf("[The word should have %d words!]\n", NUMBER_OF_LETTERS);
  153.  
  154.                     } else puts(" [Good job!]");
  155.                 } while(strcmp(WORD, "quit") != 0);
  156.  
  157.                 strcpy(MENU, "Start playing");
  158.                 break;
  159.             }
  160.  
  161.             break;
  162.         default:
  163.             exit(0);
  164.         }
  165.     }
  166.  
  167.     return 0;
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top