免責事項-これは宿題なので、答えは欲しくありませんが、ガイダンスを得るために行く人がいません(1週間待ちたくない場合は、通信で勉強します).
とにかく、マインスイーパ関数を少し書いています。ユーザーは、グリッドのサイズと地雷の数を入力します。次に、ボードと結果を印刷します。たとえば、地雷が 2 つある 3 * 3 のボードでは、次のように出力します。
...
*..
..*
110
*11
10*
* は明らかに地雷です。結果を除いて、すべてが順調です。私の比較は機能していないようです。== 記号を使用して文字を比較していますが、これはこれを行うための完全に合法的な方法のように見えますが、結果は意味がありません。
私は答えが欲しいのではなく、正しい方向へのポイントだけです。設定した元の配列の範囲外で配列を比較している可能性があります。この場合、この問題を解決する方法がわかりません。
私のコードは以下です。ありがとう!
#include <stdio.h>
#include <stdlib.h>
void number1(int *stats);
void number2(int *stats);
int main(void)
{
int n,m,i,j,k,x, rand1, rand2, counter, numMines;
char choice;
do
{
printf("------------\n");
printf("lines (m) = : ");
scanf("%d", &m);
printf("\ncolumns (n) = : ");
scanf("%d", &n);
printf("\nMines = :");
scanf("%d", &numMines);
// check to ensure that numMines is not > n * m
if (numMines > m * n )
{
printf("Error. You cann't have more mines than available spaces ");
break;
}
counter = 0;
//create minefield array
char mineGrid[n][m];
//set all fields in minesweeper to safe (.)
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
{
mineGrid[j][i] = '.';
}
}
// Need to randomally make some mines for the array
//bounds of the random numbers is defined by the values of n and m
for (i = 0; i < numMines; i++)
{
//generate random number
rand1 = ((double)rand() / ((double)RAND_MAX + 1) * n);
rand2 = ((double)rand() / ((double)RAND_MAX + 1) * m);
//set as a mine:
mineGrid[rand1][rand2] = '*';
//Note : at the moment, mines can overlap. need to come back to fix this up.
}
//print out the msweeper grid
x = 0;
for (j = 0; j < n; j++)
{
printf("\n");
x++;
for (i = 0; i < m; i++)
{
printf("%c", mineGrid[j][i]);
}
printf(" ");
}
// here is where I will print the results:
printf("\n");
for (j = 0; j < n; j++)
{
for (k = 0; k < m; k++)
{
if (mineGrid[x+0][k+1]=='*')
counter++;
if (mineGrid[x+0][k-1]=='*')
counter++;
if (mineGrid[x+1][k+0]=='*')
counter++;
if (mineGrid[x+1][k-1]=='*')
counter++;
if(mineGrid[x+1][k+1]=='*')
counter++;
if (mineGrid[x-1][k+0]=='*')
counter++;
if (mineGrid[x-1][k-1]=='*')
counter++;
if(mineGrid[x-1][k+1]=='*')
counter++;
printf("%d", counter);
counter = 0;
}
printf("\n");
}
printf("\n\nTry again? (y/n:) ");
scanf("\n%s", &choice);
}while(choice == 'y' || choice == 'Y');
}