- 分享
- 0
- 人气
- 63
- 主题
- 84
- 帖子
- 2923
- UID
- 354660
- 积分
- 2599
- 阅读权限
- 20
- 注册时间
- 2010-9-29
- 最后登录
- 2018-9-16
- 在线时间
- 14445 小时
|
我要做一个minesweeper 的program,我的问题是原本应该显示周围是号码的grid会显示感叹号,#号,''等等怪怪的东西,要怎样解决啊,头要裂开了
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define EX_ROW 16
#define EX_COL 12
#define TRUE 1
#define FALSE 0
#define WHY 'Y'
#define SPACE ' '
#define STAR '*'
char grid[EX_ROW][EX_COL];
char g_grid[EX_ROW][EX_COL];
void build_grid();
void build_g_grid();
void print_usergrid();
void print_fullgrid();
int checkmines(int,int);
void clearmines(int,int);
void create_mines();
void loss();
void game_start();
void win();
int checkforwin();
void playagain();
void build_grid()
{
int i,j;
for (i=0;i<EX_ROW;i++)
for (j=0;j<EX_COL;j++)
grid[i][j]= STAR;
create_mines();
for(i=0;i<EX_ROW;i++)
for (j=0;j<EX_COL;j++)
if (i==0||i==EX_ROW-1)
grid[i][j] =SPACE;
for(j=0;j<EX_COL;j++)
for(i=0;i<EX_ROW;i++)
if(j==0||j==EX_COL-1)
grid[i][j] =SPACE;
}
void create_mines()
{
int i,j,random;
srand(time(NULL));
for(i=1;i<=EX_ROW;i++)
{
random = rand() % (EX_COL-1)+1;
grid[random][i] =WHY ;
}
}
void build_g_grid()
{
int i,j;
for (i=0;i<EX_ROW;i++)
for(j=0;j<EX_COL;j++)
g_grid[i][j] =STAR;
for (i=0;i<EX_ROW;i++)
for (j=0;j<EX_COL;j++)
if(i==0||i==EX_ROW-1)
g_grid[i][j]=SPACE;
for (j=0;j<EX_COL;j++)
for (i=0;i<EX_ROW;i++)
if(j==0||j==EX_COL-1)
g_grid[i][j]=SPACE;
}
void print_usergrid()
{
int i,j;
system("cls");
printf(" ");
for ( i=1;i<EX_COL-1;i++)
{
printf("%d ",i);
}
printf("\n --------------------");
for (i=0;i<EX_ROW;i++)
for (j=0;j<EX_COL;j++)
if(i==0||i==EX_ROW-1)
g_grid[i][j]=SPACE;
for (j=0;j<EX_COL;j++)
for (i=0;i<EX_ROW;i++)
if(j==0||j==EX_COL-1)
g_grid[i][j]=SPACE;
for(i=0;i<EX_ROW;i++)
{
for(j=0;j<EX_COL;j++)
{
printf(" %c",g_grid[i][j]);
}
if(i>0&&i<EX_ROW-1)
printf("|%d",i);
printf("\n");
}
}
void print_fullgrid()
{
int i,j;
system("cls");
printf(" ");
for ( i=1;i<EX_COL-1;i++)
{
printf("%d ",i);
}
printf("\n --------------------");
for(i=0;i<EX_ROW;i++)
{
for(j=0;j<EX_COL;j++)
{
printf(" %c",grid[i][j]);
}
if(i>0&&i<EX_ROW-1)
printf("|%d",i);
printf("\n");
}
}
int checkmines(int row,int col)
{
int mines=0;
if(grid[row - 1][col] == WHY)
mines++;
if(grid[row + 1][col] == WHY)
mines++;
if(grid[row][col - 1] == WHY)
mines++;
if(grid[row][col + 1] == WHY)
mines++;
if(grid[row - 1][col + 1] == WHY)
mines++;
if(grid[row - 1][col - 1] == WHY)
mines++;
if(grid[row + 1][col + 1] == WHY)
mines++;
if(grid[row + 1][col - 1] == WHY)
mines++;
return mines;
}
void clearmines(int row, int col)
{
int nearbymines = 0;
int i = 0;
nearbymines = checkmines(row,col);
g_grid[row][col] = (int)(((int)SPACE) +nearbymines);
nearbymines = 0;
// Checking nearby mines going up
while(nearbymines < 1 && i < row)
{
nearbymines = checkmines(row - i, col);
g_grid[row - i][col] = (char)(((int)SPACE) +nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going down
while(nearbymines < 1 && row + i < EX_ROW - 2)
{
nearbymines = checkmines(row + i, col);
g_grid[row + i][col] = (char)(((int)SPACE) +nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going left
while(nearbymines < 1 && i < col)
{
nearbymines = checkmines(row, col - i);
g_grid[row][col - i] = (char)(((int)SPACE) +nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going right
while(nearbymines < 1 && col + i < EX_COL - 2)
{
nearbymines = checkmines(row, col + i);
g_grid[row][col + i] = (char)(((int)SPACE) +nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going up-right
while(nearbymines < 1 && col + i < EX_COL - 2 && i < row)
{
nearbymines = checkmines(row -i, col + i);
g_grid[row - i][col +i] = (char)(((int)SPACE) +nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going up-left
while(nearbymines < 1 && i < row && i < row && i < col)
{
nearbymines = checkmines(row - i, col - i);
g_grid[row - i][col - i] = (char)(((int)SPACE) + nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going down-right
while(nearbymines < 1 && row + i < EX_ROW - 2 && col + i < EX_COL - 2)
{
nearbymines = checkmines(row +i, col + i);
g_grid[row + i][col + i] = (char)(((int)SPACE) +nearbymines);
i++;
}
i = 0;
nearbymines = 0;
// Checking nearby mines going down-left
while(nearbymines < 1 && row + i <EX_ROW - 2 && i < col)
{
nearbymines = checkmines(row + i, col - i);
g_grid[row + i][col - i] = (char)(((int)SPACE) +nearbymines);
i++;
}
}
void loss()
{
char choice;
printf("\nYou've lost the game!");
printf("\nWould you like to play again?(Y/N)?");
scanf(" %c",&choice);
if(toupper(choice)== WHY)
{
system("cls");
game_start();
}
else
exit (0);
}
void win()
{
char choice;
printf("\nYou've won the game!");
printf("\nWould you like to play again?(Y/N)?");
scanf(" %c",&choice);
if(toupper(choice) ==WHY)
{
system("cls");
game_start();
}
else
exit(0);
}
int checkforwin()
{
int i,j;
for(i=1;i<EX_ROW-1;i++)
for(j=1;j<EX_COL-1;j++){
if(g_grid[i][j]==STAR &&grid[i][j] != WHY)
return FALSE;
}
return TRUE;
}
void game_start()
{
int row=0,col=0;
build_grid();
build_g_grid();
print_usergrid();
for(;;)
{
do
{
printf("Enter selection:\n");
printf("Row-");
scanf("%d",&row);
printf("Col-");
scanf("%d",&col);
if(row<1||row>(EX_ROW-2)||col<1||col>(EX_COL-2))
{
printf("\n*************************************************************\n");
printf("Invalid tile position!Please enter row and column again");
printf("\n*************************************************************\n");
}
}while(row<1||row>(EX_ROW-2)||col<1||col>(EX_COL-2));
if(grid[row][col] == WHY )
{
printf("You hit a MINE!\n");
print_fullgrid();
loss();
}
else
clearmines(row,col);
print_usergrid();
if(checkforwin() ==TRUE)
win();
}
}
int main (void)
{
game_start();
system("pause");
return 0;
}
|
|