C++实现扫雷游戏(控制台版)

 更新时间:2022年05月07日 11:43:27   作者:yangshuolll  
这篇文章主要为大家详细介绍了C++实现扫雷游戏,控制台版的扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现扫雷游戏的具体代码,供大家参考,具体内容如下

需要开一个map.txt  写入地图

地图中 *表示空地   ; x表示地雷

**********
**********
**x*******
**********
**********
**********
**********
**********
**********

然后就是扫雷的控制台代码了,只简单的检测了一下

#include <stdio.h>
#include <string.h>
#define SIZE 10
char img_map[SIZE + 2][SIZE + 2]; // the image of a map
int num_map[SIZE + 2][SIZE + 2]; // calculate the total number of mine in one block.
int open_map[SIZE + 2][SIZE + 2]; // which img shoud user open.
int sumMine = 0;
int sumBlock = 0;
int dir[8][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
int beyond_board (int x,int y){ // judge whether the step is out of board;
 if( x < 0 || x >= SIZE || y < 0 || y >= SIZE ){
 return 1; // beyond board return 1;
 }
 return 0;
}
void read_img_map(){  // get data from map.txt
 FILE *p_file = fopen("map.txt","r");
 int i = 0,j;
 for (i = 0;i < SIZE;i++){
 fread(img_map[i],sizeof(char),SIZE+1,p_file);
 }
/* for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if(img_map[i][j] == '*'){
 img_map[i][j] = ' ';
 }
 }
 }
*/
}
 
void write_num_map(){  // transfer img_map to num_map
 int i = 0,j = 0,k = 0;
 for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if (img_map[i][j] == 'x'){
 sumMine++;  // the total number of mine in the map
 num_map[i][j] = 9; // 9 represent a mine here
 continue;
 }
 for (k = 0;k < 8;k++){
 int stepx = i + dir[k][0],stepy = j + dir[k][1];
 if ( !beyond_board (stepx,stepy) ){
 if (img_map[stepx][stepy] == 'x'){
  num_map[i][j] += 1;
 }
 }
 } 
 }
 }
/* for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
  printf("%d",num_map[i][j]);
 }
 printf("\n");
 } 
 */
}
void show_all_mine(){ // 在地图中显示所有的地雷的位置
 int i,j;
 for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if (num_map[i][j] == 9) { 
 open_map[i][j] = 1; // 找到地雷后在 openmap 中标记 
 }
 }
 }
}
void show_all_map(){
 int i,j;
 for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if(open_map[i][j]){
 if(num_map[i][j] == 9){ 
 printf("X"); // x represetn mine
 }
 else{
  printf("%d",num_map[i][j]); // show the number has been opened
 }
 }
 else{
  printf("*");  // the block is coverd;
 }
 }
 printf("\n");
 }
}
void find_empty(int x,int y){  //搜索算法
 // printf("x = %d y = %d\n",x,y);
 // show_all_map();
 if (beyond_board(x,y)){
 return ;
 }
 if (open_map[x][y]){
 return ; 
 }
 if (!num_map[x][y]){ // 遇到零时还要继续翻上下左右
 open_map[x][y] = 1;
 }else if(num_map[x][y] != 9){ // 遇到数字了即搜索停止
 open_map[x][y] = 1;
 return ;
 }
 //else {  //****遇到雷时搜索停止
 // return ;
 // }
 int i;
 for (i = 0 ;i < 8;i++){
 find_empty(x + dir[i][0],y + dir[i][1]);
 }
 
}
int sum_one_open_map(){
 int i,j;
 int s = 0;
 for (i = 0;i < SIZE;i++){
 for ( j = 0;j < SIZE;j++)
 if (open_map[i][j]){
 s++;
 }
 } 
 return s;
}
int main()
{
 read_img_map();
 write_num_map();
 show_all_map();
 memset(open_map,0,sizeof(open_map)); // reset the open_map.
 int x,y; // empty = 0 , mine = 9, number = others
 sumBlock = SIZE * SIZE - sumMine;
 int sum = 0;
 while(sumBlock != sum){
 printf("please input the postion x,y: ");
 scanf("%d %d",&x,&y);
 scanf("%*[^\n]"); //clean the buffer
 scanf("%*c");
 x--;
 y--;
 if(beyond_board(x,y)){ // the position is beyond the board
 printf("beyond the board and please input the position again:");
 continue;
 }
 if(!num_map[x][y]){ //is empty
  find_empty(x,y); 
  show_all_map(x,y); 
 }else if(num_map[x][y] == 9){ // is mine
 show_all_mine();
 show_all_map();
 break;
 }else{    // is number
 open_map[x][y] = 1;
 show_all_map();
 }
 sum = sum_one_open_map();
 }
 if (sum==sumBlock) printf("YOU WIN! \n");
 else {
 printf("YOU LOSE!\n");
 }
 
 return 0;
}

 

运行截图:

更多精彩游戏小代码,请点击《游戏专题》阅读

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

相关文章

  • Qt实现手动切换多种布局的完美方案

    Qt实现手动切换多种布局的完美方案

    通过点击程序界面上不同的布局按钮,使主工作区呈现出不同的页面布局,多个布局之间可以通过点击不同布局按钮切换,支持的最多的窗口为9个,不同布局下窗口数随之变化,这篇文章主要介绍了Qt实现手动切换多种布局的完美方案,需要的朋友可以参考下
    2024-07-07
  • C++小游戏教程之猜数游戏的实现

    C++小游戏教程之猜数游戏的实现

    这篇文章主要和大家详细介绍如何利用C++做一个简易的猜数游戏,分为用户猜数和系统猜数。文中的示例代码讲解详细 ,感兴趣的小伙伴可以尝试一下
    2022-11-11
  • C++实现迷宫算法实例解析

    C++实现迷宫算法实例解析

    这篇文章主要介绍了C++实现迷宫算法实例解析,是一个比较经典的C++算法,有一定的学习与借鉴价值,需要的朋友可以参考下
    2014-07-07
  • C++11 nullptr实现初始化空指针

    C++11 nullptr实现初始化空指针

    避免产生“野指针”最有效的方法,就是在定义指针的同时完成初始化操作,本文主要介绍了C++11 nullptr初始化空指针,感兴趣的可以了解一下
    2022-01-01
  • C++设计模式之适配器模式(Adapter)

    C++设计模式之适配器模式(Adapter)

    这篇文章主要为大家详细介绍了C++设计模式之适配器模式Adapter,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • C语言线性表顺序存储结构实例详解

    C语言线性表顺序存储结构实例详解

    这篇文章主要介绍了C语言线性表顺序存储结构实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • C语言实现的猴子吃桃问题算法解决方案

    C语言实现的猴子吃桃问题算法解决方案

    这篇文章主要介绍了C语言实现的猴子吃桃问题解决方案,较为详细的分析了猴子吃桃问题并给出了C语言算法的实现方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • C语言超详细讲解猜数字游戏的实现

    C语言超详细讲解猜数字游戏的实现

    现在很多游戏都有抽奖抽卡的功能,其实这个就类似于猜数字,生成一个随机数,然后你去猜,猜对了就得奖。猜到一定次数就会保底。要实现猜数字的小游戏,首先是要让程序生成随机数,这就要用到rand、srand和time这三个函数,其次要了解时间戳
    2022-07-07
  • 用C语言实现三子棋游戏

    用C语言实现三子棋游戏

    这篇文章主要为大家详细介绍了用C语言实现三子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • C++中友元类和嵌套类使用详解

    C++中友元类和嵌套类使用详解

    友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元,所谓嵌套类,就是在类中声明的类。如下代码中,类Inner就是一个嵌套类,类Outer是外围类
    2022-08-08

最新评论