Advertisement
micha_b

zahlenraten.c

Jan 1st, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | Gaming | 0 0
  1. /*
  2.  * zraten.c
  3.  *
  4.  * Zahlenraten
  5.  * erweiterte C-Konvertierung des BASIC-Beispiels:
  6.  * 10 print"{clear}guess my number (1-99)":print
  7.  * 20 x=int(rnd(1)*99)+1:n=0
  8.  * 30 input"your guess";g:n=n+1
  9.  * 40 if g<x then print"too small":goto 30
  10.  * 50 if g>x then print"too large":goto 30
  11.  * 60 print"got it! in "n"tries"
  12.  *
  13.  * M. Bergmann 01.01.2023
  14.  *
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22.  
  23. /* -- Prototypen -- */
  24. int create_rnumber(void);
  25. int show_screen(int lauf, char *text, BOOL hit);
  26.  
  27. /* -- create_rnumber() --*/
  28. int create_rnumber(void)
  29. {
  30.     int rnumber = 0;
  31.    
  32.     srand((unsigned)time(NULL));    /* SEED für Zufallszahl */
  33.     rnumber = rand() % 99;          /* Bereich 0 - 99        */
  34.    
  35.     return rnumber;
  36. }
  37.  
  38. /* -- show_screen() -- */
  39. int show_screen(int lauf, char *text, BOOL hit)
  40. {
  41.     int tip;
  42.     char meldung[128];
  43.    
  44.     system("CLS");
  45.    
  46.     strcpy(meldung, text);
  47.    
  48.     puts("====================");
  49.     puts("= ZAHLENRATEN V1.0 =");
  50.     puts("====================\n");
  51.     puts("ERRATE ZAHL ZWISCHEN 0 und 99!\n");
  52.     printf("Versuch : \t%d\n", lauf + 1);
  53.     printf("UEBRIG  : \t%d\n", 14 - lauf);
  54.     printf("HINWEIS : \t%s\n", meldung);
  55.    
  56.     /* Zahl eingeben  */
  57.     if (hit == FALSE)
  58.     {
  59.         printf("\nEingabe: \t");
  60.         scanf("%d", &tip);
  61.     }
  62.    
  63.     return (tip);
  64. }
  65.  
  66. /* -- main() -- */
  67. int main(void)
  68. {
  69.     int rzahl = 0;      /* Zufallzahl zwischen 0 und 99 */
  70.     int guess = 0;      /* geratene Zahl                */
  71.     int run   = 0;      /* aktueller Durchlauf          */
  72.     char meldung[128];
  73.    
  74.     /* -- Zufallszahl erzeugen -- */
  75.     rzahl = create_rnumber();
  76.    
  77.     /* -- max. 15 Durchläufe erlauben -- */
  78.     while (run < 15)
  79.     {  
  80.         /* -- Geratene Zahl einlesen -- */
  81.         if (run == 0)
  82.         {
  83.             guess = show_screen(run, "Rate jetzt!", FALSE);
  84.             run++;
  85.         }
  86.                
  87.         /* -- auswerten und Hinweis geben -- */
  88.         if (guess == rzahl )
  89.         {
  90.             strcpy(meldung, "Treffer - VERSENKT!");
  91.             guess = show_screen(run - 1, meldung, TRUE);
  92.             printf("\nDie zu erratende Zahl war : %d\n\n", rzahl);
  93.             return 0;
  94.         }
  95.         if (guess < rzahl )
  96.         {
  97.             strcpy(meldung, "Zahl zu klein.");
  98.             guess = show_screen(run, meldung, FALSE);
  99.             run++;
  100.         }
  101.         if (guess > rzahl )
  102.         {
  103.             strcpy(meldung, "Zahl zu gross.");
  104.             guess = show_screen(run, meldung, FALSE);
  105.             run++;
  106.         }
  107.     }  
  108.    
  109.     printf("Zahl nicht erraten - schade!\nDie zu erratende Zahl war : %d\n", rzahl);
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement