Computer Programming Contest Preparation

ToolBox - Source for: 4/489/judged.c



/home/toolbox/public_html/solutions/4/489/judged.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2019-09-11
   18  * Purpose: fun
   19  * Problem: 489
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_LINE 2048
   27 #define MAX_WRONG 6
   28 
   29 int roundNum;
   30 char word[MAX_LINE];
   31 char guess[MAX_LINE];
   32 int guessed[26];
   33 
   34 
   35 void init()
   36 {
   37     /* FUNCTION init */
   38     int i;
   39 
   40     for (i=0; 26>i; i++)
   41         {
   42             guessed[i] = 0;
   43         }
   44 } /* FUNCTION init */
   45 
   46 void dump()
   47 {
   48     /* FUNCTION dump */
   49 } /* FUNCTION dump */
   50 
   51 int getInput()
   52 {
   53     /* FUNCTION getInput */
   54     int dataReadFlag;
   55 
   56     scanf(" %d ", &roundNum);
   57     dataReadFlag = (-1 != roundNum);
   58     if (dataReadFlag)
   59         {
   60             /* read in two lines */
   61             scanf(" %s ", word);
   62             scanf(" %s ", guess);
   63         } /* read in two lines */
   64     return (dataReadFlag);
   65 } /* FUNCTION getInput */
   66 
   67 int checkGuess(char x)
   68 {
   69     /* FUNCTION checkGuess */
   70     int i;
   71     int v;
   72     int toReturn = 1; /* assume guess is wrong */
   73 
   74     v = x - 'a';
   75     if (0 != guessed[v])
   76         {
   77             /* letter already guessed */
   78             toReturn = 0; /* anything already guessed is free */
   79         } /* letter already guessed */
   80     else
   81         {
   82             /* letter not guessed before */
   83             guessed[v] = 1;
   84             for (i=0; strlen(word)>i; i++)
   85                 {
   86                     /* check each letter */
   87                     if (x == word[i])
   88                         {
   89                             /* match found */
   90                             toReturn = 0;   /* match found, so not wrong */
   91                             word[i] = ' ';  /* blank out found letters */
   92                         } /* match found */
   93                 } /* check each letter */
   94         } /* letter not guessed before */
   95     return toReturn;
   96 } /* FUNCTION checkGuess */
   97 
   98 int anyLeft()
   99 {
  100     /* FUNCTION anyLeft */
  101     int i;
  102     int toReturn = FALSE;
  103 
  104     for (i=0; strlen(word) > i; i++)
  105         {
  106             /* make sure every letter has been replaced */
  107             toReturn = toReturn || (' ' != word[i]);
  108         } /* make sure every letter has been replaced */
  109     return toReturn;
  110 } /* FUNCTION anyLeft */
  111 
  112 void process()
  113 {
  114     /* FUNCTION process */
  115     int i;
  116     int wrong=0;
  117 
  118     init();
  119     printf("Round %d\n", roundNum);
  120     for (i=0; (anyLeft()) && (strlen(guess)) > i; i++)
  121         {
  122             /* guess each letter */
  123             wrong = wrong + checkGuess(guess[i]);
  124         } /* guess each letter */
  125     if (MAX_WRONG < wrong)
  126         {
  127             printf("You lose.\n");
  128         }
  129     else if (anyLeft())
  130         {
  131             printf("You chickened out.\n");
  132         }
  133     else
  134         {
  135             printf("You win.\n");
  136         }
  137 } /* FUNCTION process */
  138 
  139 int main()
  140 {
  141     /* main */
  142     int moreToDo;
  143 
  144     moreToDo = getInput();
  145     while (moreToDo)
  146         {
  147             /* while */
  148             process();
  149             moreToDo = getInput();
  150         } /* while */
  151 
  152     return EXIT_SUCCESS;
  153 } /* main */
  154