2017-05-01 4 views
0

Ich versuche es zu machen, wenn ich ein paar Zahlen scanne, und wenn es meins (*) gibt, drucke Boom und wenn es keine Mine gibt, drucke die Anzahl der Minen in der Nähe. Ich kann kein Problem mit dem Code finden, aber es liegt ein Fehler vor. Bitte überprüfen Sie es, wenn Sie das Problem finden.Ich kann keinen Fehler des Codes finden. Minesweeper C

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#define N 10 

int main (void) 
{ 
    char minefield [N][N]; 
    int i, j; 
    int k; 
    int x, y; 
    int count; 
    int mine_number; 

    count = 0; 
    mine_number = N*N/10; 

    srand((long)time(NULL)); 


    for (k=1; 0< k < mine_number; k=k+1) { 
     i = rand() % N; 
     j = rand() % N; 
     minefield [i][j] = '*'; 
    } 

    for (i=0; i < N; i=i+1) { 
     for (j=0; j < N; j=j+1) { 
      count = 0; 
      if (minefield[i][j] != '*') { 
       if (i == 0) { 
        if (j == 0) { 
         if (minefield [i][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j] == '*') { 
          count = count + 1; 
         } 
        } 
        else if (j == N-1) { 
         if (minefield [i+1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j-1] == '*') { 
          count = count + 1; 
         } 
        } 

        else { 
         if (minefield [i][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j-1] == '*') { 
          count = count + 1; 
         } 
        } 
       } 

       else if (i == N-1) { 
        if (j == 0) { 
         if (minefield [i-1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j+1] == '*') { 
          count = count + 1; 
         } 
        } 

        else if (j == N-1) { 
         if (minefield [i-1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j-1] == '*') { 
          count = count + 1; 
         } 
        } 

        else { 
         if (minefield [i][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j-1] == '*') { 
          count = count + 1; 
         } 
        } 
       } 

       else { 
        if (j == 0) { 
         if (minefield [i-1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j] == '*') { 
          count = count + 1; 
         } 
        } 

        else if (j == N-1) { 
         if (minefield [i-1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j] == '*') { 
          count = count + 1; 

         } 
        } 

        else { 
         if (minefield [i-1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i-1][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j+1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j-1] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j] == '*') { 
          count = count + 1; 
         } 
         if (minefield [i+1][j+1] == '*') { 
          count = count + 1; 
         } 
        } 
       } 
      } 
     } 
    } 

    scanf("%d %d", &x, &y); 

    if (minefield[x][y] = '*') { 
     printf("boom"); 
    } 

    else { 
     printf("%d", count); 
    } 

    return 0; 
} 
+0

1) '0 ' k 'if (Minenfeld [x] [y] == '*') {' 3) 'else { \t \t printf ("% d ", Anzahl); \t} '??? – BLUEPIXY

+2

Schalten Sie Ihre Compilerwarnungen ein. Clang beispielsweise gibt Folgendes aus: * "main.c: 193: 25: Verwenden des Ergebnisses einer Zuweisung als Bedingung ohne Klammern" * on (2) im obigen Kommentar. – WhozCraig

+1

Beachten Sie, dass Sie sehr wahrscheinlich weniger Minen platzieren, als Sie denken, weil Sie nicht überprüfen, ob Sie eine bestehende Mine überschreiben. Auf der anderen Seite könnte es sein, dass du mehr Minen platzierst als erwartet, weil du dein "Minenfeld" nicht initialisierst, welches schon Müll-Sterne haben könnte. –

Antwort

0

Ihr grundlegendes Problem ist, dass Sie zählen vor Sie den Eingang zu lesen, das heißt, bevor Sie wissen, was x und y ist. Mit anderen Worten, momentan zählst du "etwas" auf dem gesamten Minenfeld, was nicht das ist, was du willst.

Also das erste, was zu tun ist, lesen Sie x und y vor dem Zählen.

Weiter scheint es mir, dass Ihre große if Aussage schwer zu lesen ist. Sie könnten reorganisieren es mögen:

if (scanf("%d %d", &x, &y) != 2 || x < 0 || x >= N || y < 0 || y >= N) 
{ 
    // Illegal input 
    exit(1); 
} 

if (minefield[x][y] == '*') { 
    printf("boom"); 
} 
else { 
    count = 0; 

    if (x-1 >= 0 && y-1 >= 0) count += (minefield[x-1][y-1] == '*'); 
    if (x-1 >= 0) count += (minefield[x-1][y] == '*'); 
    .... 
    .... Add code to cover all 8 combinations (i.e. add the 5 missing combinations) 
    .... 
    if (x+1 < N && y+1 < N) count += (minefield[x+1][y+1] == '*'); 

    printf("%d", count); 
} 
+0

danke für deine antwort. ich werde es versuchen. –

Verwandte Themen