Posted By

ozkriff on 05/16/09


Tagged


Versions (?)

guess


 / Published in: C
 

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4.  
  5.  
  6. /* --------------------------- GENERATE_ANSWER --------------------------- */
  7.  
  8. int generate_answer ( char* answer )
  9. {
  10. char* dictionary[] = {
  11. "suck",
  12. "porn",
  13. "linux",
  14. "gcc",
  15. "shit",
  16. NULL };
  17.  
  18. srand ( time(NULL) );
  19. strcpy ( answer, dictionary[rand() % 5] );
  20. printf ( "DBG: answer is: %s\n", answer ); /* DEBUG */
  21.  
  22. return 0;
  23. }
  24.  
  25.  
  26. /* ---------------------------- ANALYZE_WORD ---------------------------- */
  27.  
  28. int analyze_word ( const char* user_word, const char* answer )
  29. {
  30. int ii = 0,
  31. right_chars = 0;
  32. for ( ii = 0; user_word[ii] != '\0'; ii++ )
  33. if ( user_word[ii] == answer[ii] ) {
  34. right_chars++;
  35. printf( "DBG: rigth on %i position %c\n",
  36. ii, answer[ii] ); // DEBUG
  37. }
  38. printf( "wrong! %i chars are right.\n", right_chars );
  39.  
  40. return 0;
  41. }
  42.  
  43.  
  44. /* -------------------------------- MAIN -------------------------------- */
  45.  
  46. int main()
  47. {
  48. char answer[32],
  49. user_word[32];
  50.  
  51. generate_answer ( & answer );
  52.  
  53. while (1){
  54. printf ( "enter word: " );
  55. gets ( & user_word );
  56.  
  57. if ( !strcmp(answer, user_word) ) {
  58. puts( "win!" );
  59. break;
  60. }
  61. else
  62. analyze_word (user_word, answer);
  63. }
  64.  
  65. return 0;
  66. }

Report this snippet  

You need to login to post a comment.