19_1_18.c

  • 扫雷
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
#define COUNT 10
char Mine[ROWS][COLS];
char Show[ROWS][COLS];

需要两个棋盘,一个是雷图,一个展示给玩家

  1. 初始化棋盘
void InitBoard(char board[ROWS][COLS], char set)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < ROWS; ++i)
 {
  for (j = 0; j < COLS; ++j)
  {
   board[i][j] = set;
  }
 }
}
  1. 打印棋盘
void DisplayBoard(char board[ROWS][COLS])
{
 int i = 0;
 int j = 0;
 printf("0  ");
 for (i = 1; i <= ROW; i++)
 {
  printf("%d ", i);//打印横标(0--10)
 }
 printf("\n");
 for (i = 1; i <= ROW; i++)//打印竖标(1--10)
 {
  printf("%d  ", i);
  for (j = 1; j <= COL; j++)
  {
   printf("%c ", board[i][j]);//玩家棋盘数组
  }
  printf("\n");
 }
 printf("\n");
}
  1. 设置地雷
void SetMine(char board[ROWS][COLS])
{
 int count = COUNT;
 while (count)
 {
  int x = rand() % ROW + 1;
  int y = rand() % COL + 1;
  if (board[x][y] == '0')
  {
   board[x][y] = '1';
   count--;
  }
 }
}
  1. 计算周围的雷数
int GetMineCount(char Mine[ROWS][COLS], int x, int y)
{
 return Mine[x - 1][y - 1]
 1. Mine[x - 1][y]
 2. Mine[x - 1][y + 1]
 3. Mine[x][y - 1]
 4. Mine[x][y + 1]
 5. Mine[x + 1][y - 1]
 6. Mine[x + 1][y]
 7. Mine[x + 1][y + 1]
 8. 8 * '0';
}
  1. 翻格子
void extend_board(char Mine[ROWS][COLS], char Show[ROWS][COLS], int x, int y)
{
 Show[x][y] = GetMineCount(Mine, x, y) + '0';
 if (GetMineCount(Mine, x, y) == 0)
 {
  Show[x - 1][y - 1] = GetMineCount(Mine, x - 1, y - 1) + '0';
  Show[x - 1][y] = GetMineCount(Mine, x - 1, y) + '0';
  Show[x - 1][y + 1] = GetMineCount(Mine, x - 1, y + 1) + '0';
  Show[x][y - 1] = GetMineCount(Mine, x, y - 1) + '0';
  Show[x][y + 1] = GetMineCount(Mine, x, y + 1) + '0';
  Show[x + 1][y - 1] = GetMineCount(Mine, x + 1, y - 1) + '0';
  Show[x + 1][y] = GetMineCount(Mine, x + 1, y) + '0';
  Show[x + 1][y + 1] = GetMineCount(Mine, x + 1, y + 1) + '0';
 }
}
  1. 计算剩余未翻开格数
int countnum()
{
 int count = 0;
 int i = 0;
 int j = 0;
 for (i = 1; i <= ROWS - 2; i++)
 {
  for (j = 1; j <= COLS - 2; j++)
  {
   if (Show[i][j] == '*')
   {
    count++;
   }
  }
 }
 return count;
}
  1. 输入坐标扫雷
void FindMine(char Mine[ROWS][COLS], char  Show[ROWS][COLS])
{
 int x = 0;
 int y = 0;
 while (1)
 {
  printf("请输入坐标\n");
  scanf("%d%d", &x, &y);
  if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
  {
   if (Mine[x][y] == '1')
   {
    printf("很遗憾,你被炸死了\n");
    DisplayBoard(Mine);
    break;
   }
   else
   {
    extend_board(Mine, Show, x, y);
    DisplayBoard(Show);
   }
  }
  else
  {
   printf("坐标错误\n");
  }
  if (countnum() == COUNT)
  {
   printf("你赢了");
   break;
  }
 }
}
  1. 菜单及流程
int main()
{
 srand((unsigned int)time(NULL));
 printf("------------------\n");
 printf("----  1.play  ----\n");
 printf("----  0.exit  ----\n");
 printf("------------------\n");
 if (getchar() == '1')
 {
  InitBoard(Mine, '0');
  InitBoard(Show, '*');
  SetMine(Mine);
  DisplayBoard(Show);
  FindMine(Mine, Show);
 }
 system("pause");
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值