是你的甜甜呀 2016-11-22 14:02 采纳率: 0%
浏览 1266

C语言编写的扫雷代码,但是在就算第一行和第一列的周围雷数时是有问题的数目错误

test.c
#include
#include "game.h"
#include
#include

void menu()
{
printf("******** 欢迎进入本游戏 ********\n");
printf("******** 1.开始游戏 ********\n");
printf("******** 0.退出游戏 ********\n");
}
enum Option
{
EXIT,
PLAY
};

void menu2()
{
printf("------请选择游戏等级------\n");
printf("------ 1.一般难度 ------\n");
printf("------ 2.中等难度 ------\n");
printf("------ 3.高级难度 ------\n");
}

void general_game()
{
int x = 0;
int y = 0;
int win = 0;
int n = 0;
char real[rows+2][cols+2] = { 0 };
char board[rows+2][cols+2] = { 0 };
srand((unsigned)time(NULL));
memset(real, '0', sizeof(char)*(rows+2)*(cols+2));
memset(board, '*', sizeof(char)*(rows+2)*(cols+2));
//display(board, rows , cols );
set_real1(real, rows , cols );
display(real, rows , cols );
display(board, rows , cols );

while (win < rows *cols  - DEFAULT_COUNT1)
{
    printf("请输入坐标: ");
    scanf("%d%d", &x, &y);
    n++;
    if ((n == 1) && (real[x][y] = '1'))
    {
        real[x][y] = '0';
        real[x - 1][y - 1] = '1';
    }
  if (real[x][y] == '1')
    {
        printf("很抱歉,您被炸死了!\n");
        break;
    }
    else
    {  
        win++;
        int count = get_real_count(real, x, y);
        board[x][y] = count + '0';

    }
  display(board, rows, cols);
}
if (win >= (cols )*(rows ) - DEFAULT_COUNT1)
{
    printf("恭喜您,排雷成功 \n");
}

}
void medium_game()
{
int x = 0;
int y = 0;
int win = 0;
int n = 0;
char real[rows+2][cols+2] = { 0 };
char board[rows+2][cols+2] = { 0 };
srand((unsigned)time(NULL));
memset(real, '0', sizeof(char)*(rows+2)*(cols+2));
memset(board, '*', sizeof(char)*(rows+2)*(cols+2));
//display(board, rows , cols );
set_real1(real, rows , cols );
display(real, rows , cols );
display(board, rows , cols );

while (win < (cols )*(rows ) - DEFAULT_COUNT2)
{
    printf("请输入坐标: ");
    scanf("%d%d", &x, &y);
    n++;
    if ((n == 1) && (real[x][y] = '1'))
    {
        real[x][y] = '0';
        real[x - 1][y - 1] = '1';
    }
    if (real[x][y] == '1')
    {
        printf("很抱歉,您被炸死了!\n");
        break;
    }
    else
    {

        int count = get_real_count(real, x, y);
        board[x][y] = count + '0';
        win++;
    }
    display(board, rows , cols );
}
if (win >= (cols )*(rows ) - DEFAULT_COUNT2)
{
    printf("恭喜您,排雷成功 \n");
}

}
void advanced_game()
{
int x = 0;
int y = 0;
int win = 0;
int n = 0;
char real[rows+2][cols+2] = { 0 };
char board[rows+2][cols+2] = { 0 };
srand((unsigned)time(NULL));
memset(real, '0', sizeof(char)*(rows+2)*(cols+2));
memset(board, '*', sizeof(char)*(rows+2)*(cols+2));
//display(board, rows , cols );
set_real1(real, rows , cols );
display(real, rows, cols );
display(board, rows , cols);

while (win < (cols)*(rows) - DEFAULT_COUNT3)
{
    printf("请输入坐标: ");
    scanf("%d%d", &x, &y);
    n++;
    if ((n == 1) && (real[x][y] = '1'))
    {
        real[x][y] = '0';
        real[x - 1][y - 1] = '1';
    }
    if (real[x][y] == '1')
    {
        printf("很抱歉,您被炸死了!\n");
        break;
    }
    else
    {
        win++;
        int count = get_real_count(real, x, y);
        board[x][y] = count + '0';

    }
    display(board, rows , cols );
}
if (win >= (cols )*(rows ) - DEFAULT_COUNT3)
{
    printf("恭喜您,排雷成功 \n");
}

}
int main()
{
int input = 0;
int n = 0;
do
{
menu();
printf("请选择: ");
scanf("%d", &input);
switch (input)
{
case PLAY:
menu2();
printf("请选择难度等级: ");
scanf("%d", &n);
switch (n)
{
case 1: general_game();
break;
case 2: medium_game();
break;
case 3: advanced_game();
break;
default:
printf("选择错误,请重新输入\n");
break;
}
case EXIT: break;
default:
printf("选择错误,请重新输入\n");
break;
}
} while (input);
return 0;
}

game .c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include
#include
#include

void display(char arr[rows +2][cols+2 ], int ROW, int COL)
{
int i = 0;
int j = 0;
printf("\n");
printf(" 0 1 2 3 4 5 6 7 8 9\n");
printf(" _________________________________________\n");
for (i = 0; i < rows ; i++)
{
printf(" %d | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n",i, arr[i][0], arr[i][1], arr[i][2], arr[i][3], arr[i][4], arr[i][5], arr[i][6], arr[i][7], arr[i][8], arr[i][9]);
if (i != rows - 1)
{
printf(" |---|---|---|---|---|---|---|---|---|---|");
}
printf("\n");
}
}
int get_rand_num()
{
return rand() % 10+ 1 ;
}
void set_real1(char arr[rows+2 ][cols+2 ], int ROW, int COL)
{
int count = DEFAULT_COUNT1;
while (count)
{
int x = get_rand_num();
int y = get_rand_num();
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}

void set_real2(char arr[rows +2][cols+2 ], int ROW, int COL)
{
int count = DEFAULT_COUNT2;
while (count)
{
int x = get_rand_num();
int y = get_rand_num();
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}

void set_real3(char arr[rows +2][cols+2], int ROW, int COL)
{
int count = DEFAULT_COUNT3;
while (count)
{
int x = get_rand_num();
int y = get_rand_num();
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}

int get_real_count(char arr[rows+2 ][cols+2 ], int x, int y)
{

int count = 0;
for (x = rows - 2; x<= rows; x++)//计算选择的位置周围雷的个数

{
for (y = cols - 2; y <= cols; y++)
{
if ((x >= 0) && (y >= 0) && arr[x][y] == '1')
count++;
}

    {
        return count;
    }
}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-11-22 15:19
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?
  • ¥50 复现论文;matlab代码编写
  • ¥30 echarts 3d地图怎么实现一进来页面散点数据和卡片一起轮播
  • ¥15 数字图像的降噪滤波增强