简单的扫雷

game.h
# ifndef __GAME_H__
# define __GAME_H_
# include<stdio.h>
# include<stdlib.h>
# include<time.h>
# define ROW 10
# define COL 10
# define ROWS ROW+2
# define COLS COL+2
# define EASYCOUNT 10
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
void InitBoard(char arr[ROWS][COLS], int row, int col, char set);
void Setmine(char arr[ROWS][COLS],int count, int row, int col);
int GetMineCount(char arr[ROWS][COLS], int row, int col);
# endif

game.c
# include"game.h"

void InitBoard(char arr[ROWS][COLS], int row, int col, char set)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < col; j++)
  {
   arr[i][j] = set;
  }
 }
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf("   ");
 for (i = 1; i <= col; i++)
 {
  printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
   printf("%2d ", i);
  for (j = 1; j <= col; j++)
  {
   printf("%c ", arr[i][j]);
  }
  printf("\n");
 }
}
void Setmine(char arr[ROWS][COLS],int count, int row, int col)
{
 while (count)
 {
  int x = rand() % row + 1;
  int y = rand() % col + 1;
  if (arr[x][y] == '0')
  {
   arr[x][y] = '1';
   count--;
  }
   
 }
}
int GetMineCount(char arr[ROWS][COLS], int x, int y)
{
 return arr[x - 1][y] +
  arr[x - 1][y - 1] +
  arr[x][y - 1] +
  arr[x + 1][y - 1] +
  arr[x + 1][y] +
  arr[x + 1][y + 1] +
  arr[x][y + 1] +
  arr[x - 1][y + 1] - 8 * '0';
}

test.c
# define _CRT_SECURE_NO_WARNINGS 1
# include<stdio.h>
# include<stdlib.h>
# include"game.h"
# include<time.h>
void menu()
{
 printf("*************************************\n");
 printf("****1.play                 0.exit****\n");
 printf("*************************************\n");
}
void game()
{
 int win = 0;
 int x = 0;
 int y = 0;
 char mine[ROWS][COLS] = { 0 };
 char show[ROWS][COLS] = { 0 };
 DisplayBoard(show, ROW, COL);
 InitBoard(mine, ROWS, COLS,'0');
 //DisplayBoard(mine, ROWS, COLS);
 InitBoard(show, ROWS, COLS, '*');
 DisplayBoard(show, ROW, COL);
 Setmine(mine, EASYCOUNT, ROWS, COLS);
 //DisplayBoard(mine, ROWS, COLS);
 while (win<ROW*COL-EASYCOUNT)
 {
  printf("请输入坐标\n");
  scanf("%d %d", &x, &y);
  if (x >= 1 && x<=ROW&&y>=1 && y <= COL)
  {
   printf("你被炸死了\n");
   DisplayBoard(mine, ROWS, COLS);
   break;
  }
  else
  {
   int count = GetMineCount(mine, x, y);
   win++;
   show[x][y] = count + '0';
   DisplayBoard(show, ROW, COL);
  }
 }
 if (win >= ROW*COL - EASYCOUNT)
 {
  printf("恭喜你,排雷成功\n");
 }
}
void test()
{
 srand((unsigned int)time(NULL));
 int input = 0;
 do{
  menu();
  printf("请选择\n");
  scanf("%d", &input);
  switch (input)
  {
  case 1:
   game();
   break;
  case 0:
   break;
  default:
   printf("选择错误\n");
   break;
  }
 } while (input);
}
int main()
{
 test();
 system("pause");
 return 0;
}
结果:

猜你喜欢

转载自blog.csdn.net/xuruhua/article/details/80021255