私はスキャンでいくつかの数字を入れたときに作ろうとしています。鉱山(*)があれば、ブームを印刷し、鉱山がなければ近くに鉱山の数を表示します。コードに問題は見つかりませんが、エラーがあります。問題が見つかった場合は、確認してください。コードのエラーを見つけることができません。地雷探知機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(minefield [x] [y] ==' * '){'3}' else { \t \t printf( "%d"、count); \t} '??? –
BLUEPIXY
コンパイラの警告を表示します。たとえば、Clangは、上記のコメントの中で、 "main.c:193:25:括弧のない条件として代入の結果を使用する" *(2)を発行します。 – WhozCraig
既存の鉱山を上書きするかどうかをチェックしないので、考えられるよりも少ない数の地雷を配置する可能性が非常に高いことに注意してください。一方、あなたは既に 'minefield'を初期化していないので、予想以上に多くの鉱山を配置することになります。 –