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

所属分类: 软件编程 / C 语言 阅读数: 101
收藏 0 赞 0 分享

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

test.c

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#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<ROWS*COLS - DEFAULT_COUNT)
 {
 for (i = 0; i <= win; i++)
 {
 printf("请输入坐标:>");
 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("pause\n");
 return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game2.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
 
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<string.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
 
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__

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

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多