我正在研究minesweeper的项目,但问题是当我使用此代码填充数组时,但是当我使用它时,它在检查(如果)的行上给了我错误
我一直在尝试修复它,但是由于某种原因它无法正常工作 (问题出在随机函数中,现在正在尝试对其进行修复)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void boardsfill(char **board,int size)
{
int i,j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)board[i][j]='0';
}
int countsquares(char **board,int N, int x, int y)
{
char count='0';
if(y==0&&x==0)
{
if(board[0][1]=='X')count++;
if(board[1][1]=='X')count++;
if(board[1][0]=='X')count++;
}
else if(y==0&&x==N-1)
{
if(board[0][N-2]=='X')count++;
if(board[1][N-2]=='X')count++;
if(board[1][N-1]=='X')count++;
}
else if(y==N-1&&x==0)
{
if(board[N-2][0]=='X')count++;
if(board[N-2][1]=='X')count++;
if(board[N-1][1]=='X')count++;
}
else if(y==N-1&&x==N-1)
{
if(board[N-2][N-1]=='X')count++;
if(board[N-2][N-2]=='X')count++;
if(board[N-1][N-2]=='X')count++;
}
else if(y==0)
{
if(board[0][x-1]=='X')count++;
if(board[1][x-1]=='X')count++;
if(board[1][x]=='X')count++;
if(board[1][x+1]=='X')count++;
if(board[0][x+1]=='X')count++;
}
else if(y==N-1)
{
if(board[N-1][x-1]=='X')count++;
if(board[N-2][x-1]=='X')count++;
if(board[N-2][x]=='X')count++;
if(board[N-2][x+1]=='X')count++;
if(board[N-1][x+1]=='X')count++;
}
else if(x==0)
{
if(board[y-1][x]=='X')count++;
if(board[y-1][x+1]=='X')count++;
if(board[y][x+1]=='X')count++;
if(board[y+1][x+1]=='X')count++;
if(board[y+1][x]=='X')count++;
}
else if(x==N-1)
{
if(board[y-1][x]=='X')count++;
if(board[y-1][x-1]=='X')count++;
if(board[y][x-1]=='X')count++;
if(board[y+1][x-1]=='X')count++;
if(board[y+1][x]=='X')count++;
}
else
{
if(board[y-1][x-1]=='X')count++;
if(board[y-1][x]=='X')count++;
if(board[y-1][x+1]=='X')count++;
if(board[y][x+1]=='X')count++;
if(board[y+1][x+1]=='X')count++;
if(board[y+1][x]=='X')count++;
if(board[y+1][x-1]=='X')count++;
if(board[y][x-1]=='X')count++;
}
return count;
}
void fill_flags(char** flags,int size)
{
int i, j;
printf("not me... ughh...\n");
printf("not me... ughh...\n");
printf("not me... ughh...");
for(i = 0; i < size; i++)
for(j = 0; j < size; j++)
flags[i][j] = 178;
}
void random_fill(char** board,int size,int minenum)
{
int i, random,random2;
srand(time(NULL));
for (i = 0; i < minenum; i++)
{
random = rand() % (size);
printf(" %d \n",random);
random2=rand()%(size);
printf(" %d \n",random2);
if(board[random][random2]=='X') i--;
printf("qwewqewqe");
board[((int)random)][((int)random2)] = 'X';
printf("%d \n",i);
}
}
void numbers_InSlots(char** board,int size)
{
int i,j;
for(i=0;i<size;i++)
for(j=0;j<size;j++){
if(board[i][j]!='X')board[i][j]=countsquares(board,size,i,j);
}
}
void player_fill(char** board,int size,int minenum)
{
int i,x,y;
printf("pls enter x y of every mine!");
for (i = 0; i < minenum; i++)
{
while(scanf("%d",&x)==0)printf("error pls try again!");
while(scanf("%d",&y)==0)printf("error pls try again!");
if(board[x][y]!='X')
board[x][y] = 'X';
else
{i--;
/**/printf("there is a bomb here try again!");
}
}
}
void InitBoards(char** board,char** flags,int M)
{
}
void play_game(char **board,char **flags,int size,int *lost)
{
int i,j,x;
printf("Please insert row and column numbers: \n>");
while(scanf("%d%d",&i,&j)==0)
printf("Coordinates out of range! Please try again");
while()
}
void print_board(char **board,char **flags,int size)
{
int row, col;
printf("\n\n");
for(row = 0; row < size; row++){
for(col = 0; col < size; col++) printf("%c ", flags[row][col]);
printf("\n");
}
}
void startgame(char **board,char **flags,int size)
{
int lost=0;
do
{
play_game(board,flags,size,&lost);
print_board(board,flags,size);
} while(lost != 1);
}
int main()
{
int size,m,type,flag=1,i,q,j;
char **board,**flags;
printf("*****************************************************************\n");
printf("\nWelcome to MineSweeper!\n");
printf("\n*****************************************************************\n\n");
printf("Please enter the size of board and the number of bombs: ");
scanf("%d %d",&size,&m);
if((board=(char **)malloc(size*size*sizeof(char)))== NULL)
{
printf("malloc failed \n");
return -1;
}
if((flags=(char **)malloc(size*size*sizeof(char)))== NULL)
{
printf("malloc failed \n");
return -1;
}
/**********************************************************************************************/
for(i=0;q<size;q++)
for(j=0;j<size;j++)board[i][j]='0';
printf("Would you like to play the random or the deterministic version?\n1.random 2.deterministic\n");
for(i=0;i<1;i++)
{
if((scanf("%d",&type)==0)||type>2||type<1)
{
printf("please enter 1 or 2!");
i--;
}
}
if(type==1)
{
random_fill(board,size,m);
}/************************the random code^^**********/
if(type==2)/*************************the fill code^^**********/
{
player_fill(board,size,m);
}
printf("atleast we are here!");
fill_flags(flags,size);
printf("\n*****************************************************************\n");
printf("\n The boards were initialized successfully! lets start the game\n");
printf("\n**************************(flags[r][c]***************************************\n\n");
startgame(board,flags,size);
return 0;
}
我希望它能正常运行而不会崩溃!