Guest User

Untitled

a guest
Apr 25th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. int main () {
  8.  
  9.   int eingabex=2, eingabey=10, eingabebomben=10;                                //angabe wie groß matrix
  10.   int i=0, j=0, x=0, y=0;                                                   //hilfsvariablen fuer schleifen
  11.   int bombenverteiler=0, xbombe=0, ybombe=0;                                //koordinaten fuer bombenverteilung
  12.   int gamex=0, gamey=0;                                                     //koordinaten die im spiel eingegeben werden
  13.   int feld[8][8];
  14.   char feldgui[8][8];
  15.   int lost=0;
  16.   int summefelder = eingabex * eingabey;                                    // fuers gewinnen
  17.  
  18.   // I. Teil, 8x8 matrix zufaellig mit bomben fuellen + rest mit nullen + bombenzahlen
  19.    
  20.  for(x=0; x < eingabex; x++){
  21.         for(y=0; y < eingabey; y++){
  22.             feld[x][y] = 0;
  23.         }
  24.     }
  25.  
  26.  
  27.   // solange bomben in zufaellige felder des arrays packen, bis es 10 bomben an 10 verschiednen plaetzen gibtprintf("Die zahlen muessen > 0 sein \n");
  28.  
  29.   srand(time(NULL));
  30.   while(bombenverteiler<eingabebomben){
  31.  
  32.     xbombe = rand() %(eingabex);
  33.     ybombe = rand() %(eingabey);
  34.  
  35.     if (feld[xbombe][ybombe] == 0){
  36.         feld[xbombe][ybombe]=-9; // -9 da ich bombenzahlen hochzaehle, und max. bomben um feld herum = 8
  37.         bombenverteiler++;
  38.     }
  39.  }
  40.  
  41.   // zahlen eintragen, um anzahl bombem in umgebung anzugeben
  42.  
  43.   for(x=0; x<eingabex; x++){
  44.     for(y=0; y<eingabey; y++){
  45.       if(feld[x][y] == 0){
  46.         for(i=(eingabex-1); i<(eingabex+1); i++){
  47.             for (j=(eingabey-1); j<(eingabey+1); j++){
  48.                 if(i < 0 || i > (eingabex) || j < 0 || j > (eingabey)){
  49.                     // nix machen, da außerhalb des arrays
  50.                     //printf("auserhalb arrays \n");
  51.                 }
  52.  
  53.                  else {
  54.                     //printf("im array \n");
  55.                     if(feld[i][j] < 0){
  56.                         feld[x][y] += 1;
  57.                        
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.       }
  63.      
  64.     }
  65.   }
  66.    
  67.  
  68.   //___________________________________________
  69.  
  70.   //II. Teil 'spielprogrammanfang', x und y koordinate eingeben, fuer das feld das man 'aufklicken' moechte
  71.      
  72.  // hilfsfeld / spiel'ansicht'
  73.  
  74.  
  75.   for(x=0; x<eingabex; x++){
  76.     for(y=0; y<eingabey; y++){
  77.         feldgui[x][y] = '#';
  78.     }
  79.   }
  80.    
  81.   printf(" \t Minesweaperprogramm \n Bitte geben sie 2 Koordinaten an, zwischen 0 und %d fuer X, und zwischen 0 und %c fuer Y. \n", eingabex, eingabey);
  82.   printf("Erster wert entspricht X-Koordinate, Zweiter wert entspricht Y-Koordinate \n");
  83.      
  84.   // anfangsmatrix ausgeben, unaufgedackte felder -> #
  85.  
  86.  
  87.   printf("FELD: \n");
  88.   for(x=0; x<eingabex; x++){
  89.                     for(y=0; y<eingabey; y++){
  90.                         printf("%c\t", feld[x][y]);
  91.                     }
  92.                     printf("\n \n");
  93.     }
  94.  
  95.  
  96.   printf("FELDGUI: \n");
  97.  
  98.   for(x=0; x<eingabex; x++){
  99.       for(y=0; y<eingabey; y++){
  100.         printf("%c\t", feldgui[x][y]);
  101.       }
  102.     printf("\n");
  103.   }
  104.  
  105.   // anfang 'spiel'
  106.   while (lost == 0){
  107.  
  108.       summefelder--; //hochzaehlan um herauszufinden, ob alle bombenaufgedeckt sind
  109.    
  110.     // eingabe des feldes das man aufdeckt
  111.    printf("X-Koordinate: ");
  112.    scanf("%d", &gamex);
  113.    
  114.    printf("Y-Koordinate: ");
  115.    scanf("%d", &gamey);
  116.    
  117.    //gewonnen falls, zugnr - anzahl der felder = bomben+10
  118.  
  119.    if (0 <= gamex <= (eingabex) && 0 <= gamey <= (eingabey)){
  120.         if (summefelder > eingabebomben){
  121.  
  122.            
  123.              if(feld[gamex][gamey] < 0){ // da bomben = mind. -1
  124.                 printf(" \a YOU LOST THE GAME \a \n");
  125.                 feldgui[gamex][gamey] = 'B';
  126.    
  127.                 //ausgabe endmatrix
  128.                 for(x=0; x<eingabex; x++){
  129.                     for(y=0; y<eingabey; y++){
  130.                         printf("%c\t", feldgui[x][y]);
  131.                     }
  132.                     printf("\n \n");
  133.                 }
  134.    
  135.                 lost = 1;  
  136.                 break;
  137.             }
  138.    
  139.             else{
  140.  
  141.                 if(feld[gamex][gamey] == 0){
  142.                     // feld 'aufdecken'
  143.                     feldgui[gamex][gamey] = '-';
  144.                 }
  145.                 else{
  146.                 // feld 'aufdecken'
  147.                 feldgui[gamex][gamey] = feld[gamex][gamey];
  148.                 }
  149.  
  150.                 // ergebnismatrix ausgeben
  151.                 for(x=0; x<eingabex; x++){
  152.                     for(y=0; y<eingabey; y++){
  153.                         feldgui[x][y] == '-'? printf("%c \t", feldgui[x][y]):printf("%c \t", feldgui[x][y]);
  154.                     }
  155.                     printf("\n \n");
  156.                 }
  157.              }               // else klammer
  158.          }
  159.         else{
  160.         printf("SIE HABEN GEWONNEN");
  161.         }
  162.    }
  163.    
  164.    else {           //else falls Koordinaten nicht im array liegen
  165.      printf("Koordinaten muessen zwischen 0 und %c liegen! \n", eingabey);
  166.    }
  167.  
  168.   }            
  169.  
  170.   return 0;
  171.  
  172. }
Add Comment
Please, Sign In to add comment