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;
}
1) '0' k 'if (Minenfeld [x] [y] == '*') {' 3) 'else { \t \t printf ("% d ", Anzahl); \t} '??? –
BLUEPIXY
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
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. –