栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言模拟实现简单扫雷游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言模拟实现简单扫雷游戏

本文指的扫雷是简单模拟电脑中的扫雷游戏,但以我目前的水平,也就只能在黑框中实现

test.c

#include
#include
#include
#include "game2.h"
 
 
void menu()
{
 printf("********* welcome ********n");
 printf("**********1.play**********n");
 printf("**********0.exit**********n");
}
enum Option
{
 EXIT,
 PLAY
};
 
void game()
{
 int x = 0;
 int y = 0;
 int i = 0;
 int win = 0;
 char mine[ROWS + 2][COLS + 2] = { 0 };
 char show[ROWS + 2][ROWS + 2] = { 0 };
 init_board(mine, ROWS + 2, COLS + 2, '0');
 init_board(show, ROWS + 2, COLS + 2, '*');
 //display(mine, ROWS + 2, COLS + 2);#define _CRT_SECURE_NO_WARNINGS
 //display(show, ROWS + 2, COLS + 2);
 mine_set(mine, ROWS + 2, COLS + 2);
 display(mine, ROWS + 2, COLS + 2);
 while (win");
 scanf("%d%d", &x, &y);
 //合法性判断
  if ((x>0) && (x <= ROWS) && (y > 0) && (y <= COLS))
  {
  if ((i == 0) && (mine[x][y] == '1'))
  {
   (mine[x][y] = '0') ;
  }
  if (mine[x][y] == '1')
  {
   printf("很遗憾,你被炸死了n");
   break;
  }
  else
  {
   int count = 0;
   win++;
   count = get_mine_num(mine, x, y);
   show[x][y] = count + '0';
   display(show, ROWS + 2, COLS + 2);
  }
  }
  else
  {
  printf("输入错误请重新输入n");
  }
 }
 if (win >= ROWS*COLS - DEFAULT_COUNT)
 {
  printf("恭喜你,扫雷成功n");
 }
 }
}
int main()
{
 int input = 0;
 srand((uint_t)time(NULL));
 do
 {
 menu();
 printf("请选择:>");
 scanf("%d", &input);
 switch (input)
 {
 case PLAY:
   game();
  break;
 case EXIT:
  break;
 default:
  printf("输入错误,请重新输入n");
  break;
 }
 } while (input);
 system("pausen");
 return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game2.h"
#include
#include
#include
 
void init_board(char arr[ROWS + 2][COLS + 2], int row, int col,char ch)
{
 memset(arr, ch, sizeof(char) * row * col);
}
void display(char arr[ROWS + 2][COLS + 2], int row, int col)
{
 int i = 0;
 int j = 0;
 printf("  ");
 for (i = 0; i < col - 2; i++)
 {
 printf("%d ", i + 1);
 }
 printf("n");
 for (i = 0; i < row - 2; i++)
 {
 printf("%2d ", i + 1);
 for (j = 0; j < col - 2; j++)
 {
  printf("%c ", arr[i + 1][j + 1]);
 }
 printf("n");
 }
}
void mine_set(char arr[ROWS + 2][COLS + 2], int row, int col)
{
 int x = 0;
 int y = 0;
 int count = DEFAULT_COUNT;
 while (count)
 {
 x = rand() % 10 + 1;
 y = rand() % 10 + 1;
 if (arr[x][y] != '1')
 {
  arr[x][y] = '1';
  count--;
 }
 }
}
int get_mine_num(char arr[ROWS + 2][COLS + 2], int x, int y)
{
 return (arr[x][y - 1] - '0') +
   (arr[x - 1][y - 1]-'0')- +
   (arr[x - 1][y]-'0') +
   (arr[x - 1][y + 1]-'0') +
   (arr[x][y + 1]-'0') +
   (arr[x + 1][y + 1]-'0') +
   (arr[x + 1][y]-'0') +
   (arr[x + 1][y - 1]-'0');//返回周围雷的个数
}

game.h

#ifndef __GAME_H__
#define __GAME_H__
 
#define ROWS 10
#define COLS 10
#define DEFAULT_COUNT 20
typedef unsigned int uint_t;//类型重命名
 
#include
#include
#include
#include
 
void init_board(char arr[ROWS + 2][COLS + 2], int row, int col,char ch);//初始化
void display(char arr[ROWS + 2][COLS + 2], int row, int col);
void mine_set(char arr[ROWS + 2][COLS + 2], int row, int col);//放雷
int get_mine_num(char arr[ROWS + 2][COLS + 2], int row, int col);//统计坐标周围雷的个数
 
 
#endif //__GAME_H__

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号