C++实现扫雷游戏

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

直接上代码

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<queue>
#include<ctype.h>
#define A 17 //地图的高
#define B 17 //地图的宽
#define C 30 //雷的总数
using namespace std;
DWORD a,b;
char map[A][B],news,spare;
int BoomTotalNum,floatx,floaty,flag[A][B],flagnum,mode,slect[A][B],game;
//颜色属性
const WORD FORE_BLUE = FOREGROUND_BLUE; //蓝色文本属性
const WORD FORE_GREEN = FOREGROUND_GREEN; //绿色文本属性
const WORD FORE_RED = FOREGROUND_RED; //红色文本属性

//开垦地图结构体
struct node {
 int x;
 int y;
};

queue <node> dui;

//打印位置
void position(int x,int y) {
 COORD pos={x,y};
 HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);

 SetConsoleCursorPosition(Out,pos);

}

//隐藏光标 

void Hide() {
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_CURSOR_INFO CursorInfo;
 GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
 CursorInfo.bVisible = false; //隐藏控制台光标
 SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}

//初始化
void Beginning() {
 while(!dui.empty()) {
 dui.pop();
 }

 game=1;
 //BoomTotalNum=C;
 floatx=A/2;
 floaty=B/2;
 flagnum=0;
 BoomTotalNum=C;
 mode=0;
 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
 CONSOLE_SCREEN_BUFFER_INFO csbi;      //定义窗口缓冲区信息结构体
 GetConsoleScreenBufferInfo(handle_out, &csbi);   //获得窗口缓冲区信息
 int x,y;
 srand((unsigned)time(0));
 for(int i=0;i<A;i++) for(int j=0;j<B;j++) {
 map[i][j]=' ';
 flag[i][j]=0;
 slect[i][j]=0;

 }

 while(BoomTotalNum) {
 x=rand()%A;
 y=rand()%B;
 if(map[x][y]==' ') {
 map[x][y]='@';
 BoomTotalNum--;

 }

 }

 SetConsoleTextAttribute(handle_out, FORE_GREEN);
 for(int i=0;i<A;i++) {
 for(int j=0;j<B;j++) printf("█");
 printf("\n");

 }

 position(floaty*2,floatx);
 SetConsoleTextAttribute(handle_out, FORE_RED);
 printf(""); //光标位置
 position(44,9);
 printf("扫雷模式");
 position(44,5);
 printf("剩余雷数:%d ",C-flagnum);
 SetConsoleTextAttribute(handle_out, FORE_GREEN);
 position(5,22);
 printf("按“空格”切换模式");
 position(5,23);
 printf("按“Enter”确认");
 position(5,24);
 printf("按“方向键”选择方块");  

} 

//打印地图的一块儿
void Lump(int xx,int yy) {
 switch(map[xx][yy]) {
 case '1' : printf("①");break; //周围雷的数量(下同)
 case '2' : printf("②");break;
 case '3' : printf("③");break;
 case '4' : printf("④");break;
 case '5' : printf("⑤");break;
 case '6' : printf("⑥");break;
 case '7' : printf("⑦");break;
 case '8' : printf("⑧");break;
 case ' ' :

 if(xx==floatx&&yy==floaty) {
 if(flag[xx][yy]==0) {
  if(mode%2==0) printf("");
  else printf("");
 }

 else printf("");

 }

 else {
 if(flag[xx][yy]==0) printf("█");
 else printf("");

 }

 break;

 case '@' :

 if(xx==floatx&&yy==floaty) {
 if(flag[xx][yy]==0) {
  if(mode%2==0) printf("");
  else printf("");
 }
 else printf("");

 }

 else {
 if(flag[xx][yy]==0) printf("█");
 else printf("");

 }

 break;

 case 'x' : if(floatx==xx&&floaty==yy) printf(""); else printf(" ");break; //已经挖开的空白

 }

}

//移动光标
void Move() {
 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
 CONSOLE_SCREEN_BUFFER_INFO csbi;      //定义窗口缓冲区信息结构体
 GetConsoleScreenBufferInfo(handle_out, &csbi);   //获得窗口缓冲区信息
 int xxx,yyy;
 xxx=floatx;
 yyy=floaty;
 switch(news) {

 case 72 : floatx--;break; //上
 case 80 : floatx++;break; //下
 case 75 : floaty--;break; //左
 case 77 : floaty++;break; //右 

 }

 if(floatx==-1) floatx=A-1; floatx%=A; //两端穿模处理
 if(floaty==-1) floaty=B-1; floaty%=B;

 position(yyy*2,xxx);
 SetConsoleTextAttribute(handle_out, FORE_GREEN);
 Lump(xxx,yyy); //删除原位置 

 if(map[floatx][floaty]=='x') {
 position(floaty*2,floatx);
 printf(" ");

 } 

 position(floaty*2,floatx);
 SetConsoleTextAttribute(handle_out, FORE_BLUE);
 Lump(floatx,floaty); //更新新位置 

} 

//插旗和排雷模式切换
void Mode() {
 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
 CONSOLE_SCREEN_BUFFER_INFO csbi;      //定义窗口缓冲区信息结构体
 GetConsoleScreenBufferInfo(handle_out, &csbi);   //获得窗口缓冲区信息
 mode++;
 SetConsoleTextAttribute(handle_out, FORE_BLUE);
 position(floaty*2,floatx);
 if(mode%2==0) printf("");

 else printf("");

 position(44,9);
 if(mode%2==0) {
 SetConsoleTextAttribute(handle_out, FORE_BLUE);
 printf("扫雷模式");

 }

 else {
 SetConsoleTextAttribute(handle_out, FORE_RED);
 printf("插旗模式");

 }

}

//该点周围地雷数
int Boomnum(int xx,int yy) {
 int num=0;
 if((xx-1>=0)&&(yy-1>=0)&&(map[xx-1][yy-1]=='@')) num++;
 if((xx-1>=0)&&(yy+0>=0)&&(map[xx-1][yy]=='@')) num++;
 if((xx-1>=0)&&(yy+1<B) &&(map[xx-1][yy+1]=='@')) num++;
 if((xx+0>=0)&&(yy-1>=0)&&(map[xx][yy-1]=='@')) num++;
 if((xx+0>=0)&&(yy+1<B) &&(map[xx][yy+1]=='@')) num++;
 if((xx+1<A)&&(yy-1>=0) &&(map[xx+1][yy-1]=='@')) num++;
 if((xx+1<A)&&(yy+0>=0) &&(map[xx+1][yy]=='@')) num++;
 if((xx+1<A)&&(yy+1<B) &&(map[xx+1][yy+1]=='@')) num++;

 return num;

} 

//更新地图
void Open() {
 node c;
 node d;
 while(!dui.empty()) {
 dui.pop();

 }

 c.x=floatx;
 c.y=floaty;
 dui.push(c);
 slect[c.x][c.y]=1;
 while(!dui.empty()) {
 c=dui.front();
 dui.pop();
 if(Boomnum(c.x,c.y)!=0) {
 map[c.x][c.y]=(Boomnum(c.x,c.y)+48);
 continue;

 }

 else {
 map[c.x][c.y]='x';
 if((c.x-1>=0)&&(c.y-1>=0)&&(map[c.x-1][c.y-1]==' ')&&(slect[c.x-1][c.y-1]==0)) {

 d.x=c.x-1;
 d.y=c.y-1;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 if((c.x-1>=0)&&(c.y-0>=0)&&(map[c.x-1][c.y]==' ')&&(slect[c.x-1][c.y]==0)) {
 d.x=c.x-1;
 d.y=c.y-0;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 if((c.x-1>=0)&&(c.y+1<B)&&(map[c.x-1][c.y+1]==' ')&&(slect[c.x-1][c.y+1]==0)) {
 d.x=c.x-1;
 d.y=c.y+1;
 dui.push(d);

 slect[d.x][d.y]=1;

 }

 if((c.x-0>=0)&&(c.y-1>=0)&&(map[c.x][c.y-1]==' ')&&(slect[c.x][c.y-1]==0)) {
 d.x=c.x-0;
 d.y=c.y-1;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 if((c.x-0>=0)&&(c.y+1<B)&&(map[c.x][c.y+1]==' ')&&(slect[c.x][c.y+1]==0)) {
 d.x=c.x-0;
 d.y=c.y+1;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 if((c.x+1<A)&&(c.y-1>=0)&&(map[c.x+1][c.y-1]==' ')&&(slect[c.x+1][c.y-1]==0)) {

 d.x=c.x+1;
 d.y=c.y-1;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 if((c.x+1<A)&&(c.y-0>=0)&&(map[c.x+1][c.y]==' ')&&(slect[c.x+1][c.y]==0)) {

 d.x=c.x+1;
 d.y=c.y-0;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 if((c.x+1<A)&&(c.y+1<B)&&(map[c.x+1][c.y+1]==' ')&&(slect[c.x+1][c.y+1]==0)) {
 d.x=c.x+1;
 d.y=c.y+1;
 dui.push(d);
 slect[d.x][d.y]=1;

 }

 }

 }

} 

int main() {
 freopen("排名.txt","r",stdin);
 Relife: //重玩处
 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
 CONSOLE_SCREEN_BUFFER_INFO csbi;      //定义窗口缓冲区信息结构体
 GetConsoleScreenBufferInfo(handle_out, &csbi);
 Hide();
 Beginning();
 a=GetTickCount();
 while(1) {
 if(kbhit()!=0) {
 spare=getch();
 if((spare!=(-32))&&(spare!=13)&&(spare!=' ')) continue;
 if(spare==13) {
 if(mode%2==0) {
  if(map[floatx][floaty]=='@'&&flag[floatx][floaty]==0) {
  break;
  game=0;
  }
  if(flag[floatx][floaty]==1) continue;
  Open();
  position(0,0);
  SetConsoleTextAttribute(handle_out, FORE_GREEN);
  for(int i=0;i<A;i++) {
  for(int j=0;j<B;j++) Lump(i,j);
  printf("\n");
  }
  position(floaty*2,floatx);
  SetConsoleTextAttribute(handle_out, FORE_BLUE);
  Lump(floatx,floaty);
 }
 else {
  if(map[floatx][floaty]=='x'||(map[floatx][floaty]>'0'&&map[floatx][floaty]<'9'))
  continue;
  if(flag[floatx][floaty]==0) {
  flagnum++;
  flag[floatx][floaty]=1;
  position(floaty*2,floatx);
  SetConsoleTextAttribute(handle_out, FORE_BLUE);
  Lump(floatx,floaty);
  }
  else {
  flagnum--;
  flag[floatx][floaty]=0;
  position(floaty*2,floatx);
  SetConsoleTextAttribute(handle_out, FORE_BLUE);
  Lump(floatx,floaty);
  }
 }
 }
 if(spare==' ') Mode();
 if(spare==-32) {
 news=getch();
 Move();
 }
 for(int i=0;i<A;i++) for(int j=0;j<B;j++) if(map[i][j]=='x'||(map[i][j]>'0'&&map[i][j]<'9')) game++;
 if(game==A*B-C+1) break;
 else game=1;
 SetConsoleTextAttribute(handle_out, FORE_RED);
 position(44,5);
 printf("剩余雷数:%d ",C-flagnum);
 }
 else Sleep(10);
 b=GetTickCount();
 SetConsoleTextAttribute(handle_out, FORE_RED);
 position(44,7);
 printf("用时:"); //用时
 if((b-a)/60000<10) printf("0");
 printf("%d:",(b-a)/60000);
 if(((b-a)/1000)%60<10) printf("0");
 printf("%d:",((b-a)/1000)%60);
 if(((b-a)/10)%100<10) printf("0");
 printf("%d",((b-a)/10)%100);
 }
 SetConsoleTextAttribute(handle_out, FORE_RED);
 position(5,5);
 if(game==1) printf("游戏结束!新年快乐");
 else printf("恭喜通关!大吉大利");
 position(5,8);
 printf("任意键重玩");
 scanf("%c%c",&spare,&spare);
 system("cls");
 position(0,0);
 goto Relife;
} 

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

(0)

相关推荐

  • C++实现扫雷小游戏

    本文实例为大家分享了C++实现扫雷游戏的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<windows.h> #include<stdlib.h> #include<time.h> #include<conio.h> #include<queue> #include<ctype.h> #define A 17 //地图的高 #define B 17 //地图的宽 #define C

  • 利用c++和easyx图形库做一个低配版扫雷游戏

    游戏界面 由于这个游戏是我抱着玩一玩的心态做出来的,所以没有过多的去设计界面,也没有去找游戏的资源(图片.游戏音效等).仅使用了不同颜色的方块来表示游戏中方块的状态和种类.(绿色为初始状态(未翻转的状态),黄色为翻转后的背景颜色,蓝色表示已插旗的方块,红色代表地雷) 图1 游戏主菜单界面 图二 模式一的游戏界面(20*20 40个雷) 图三 模式二的游戏界面(10*10 20个雷) 图四 游戏成功界面 图五 游戏失败界面 2.全部代码 #include<graphics.h> #include

  • C++实现简单扫雷游戏

    扫雷是一个经典的电脑小游戏,用C++来编一下,效果自己试一下 #include<stdio.h> #include<Windows.h> #define YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define CYAN FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define ORANGE FOREGROUND_RED |

  • C++基于EasyX实现简单扫雷游戏

    本文实例为大家分享了C++ EasyX实现简单扫雷游戏的具体代码,供大家参考,具体内容如下 [实现代码] #include <cmath> #include <time.h> #include <easyx.h> #include <conio.h> using namespace std; #define Size 500 //定义窗口大小 #define SquareSize 50 //定义格子大小 #define BackGroundColor LIG

  • C++实现简单的扫雷游戏(控制台版)

    C++新手的代码,请各位多包涵. 用C++写的一个简单的控制台版扫雷游戏.玩家通过输入方块的坐标来翻开方块. 只是一个雏形,能够让玩家执行翻开方块的操作并且判断输赢,还未添加标记方块.游戏菜单.记录游戏时间.重新开一局等等的功能. 玩家输入坐标的方式来翻开方块只适用于小型的"雷区",若"雷区"大了,用坐标会变得很不方便. 代码片段扫雷V1.1 #include<stdio.h> #include<Windows.h> #define YELL

  • php实现的简易扫雷游戏实例

    本文实例讲述了php实现的简易扫雷游戏.分享给大家供大家参考.具体如下: <?php $init = $_POST["init"];//game restart $clickvalue = $_POST["clickvalue"];//minesweeping $checkflag = 0;//Victory or defeat $click_count = 0;//clicks count if($init == null && $click

  • 详解从零开始---用C#制作扫雷游戏

    学C#的原因其实挺简单的,因为一直对游戏挺感兴趣,查了下比较流行的游戏引擎Unity的主要开发语言是C#,所以就决定从C#入手,学学面向对象的编程方法. 以前基本都做的是嵌入式开发,做嵌入式久了,基本上只用C语言,C语言面向过程的特性在嵌入式编程这种资源极度受限的情况确实十分有利,但这种方式在面对大型软件的开发的时候就很难胜任了.编程的模式其实是一种思维习惯,习惯久了以后,想改变确实是一个艰难的过程··· 说起C#,其实在大学的时候学过一个学期,说来惭愧那时候倒也没把它当一门面向对象的语言(其实

  • JavaScript版经典游戏之扫雷游戏完整示例【附demo源码下载】

    本文实例讲述了JavaScript扫雷游戏.分享给大家供大家参考,具体如下: 翻出年初写的游戏贴上来,扫雷相信大家都玩过,先上图: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.or

  • 基于C语言实现的扫雷游戏代码

    本文详细讲述了基于C语言实现的扫雷游戏代码,代码中备有比较详细的注释,便于读者阅读和理解.希望对学习游戏开发的朋友能有一点借鉴价值. 完整的实例代码如下: /* 模拟扫雷游戏 */ #include <graphics.h> #include <math.h> #include <stdio.h> #include <dos.h> #include <stdlib.h> #include <conio.h> #include <

  • java swing实现的扫雷游戏及改进版完整示例

    本文实例讲述了java swing实现的扫雷游戏及改进版.分享给大家供大家参考,具体如下: 版本1: package awtDemo; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import jav

  • 易语言Ex_DirectUI自绘扫雷游戏源码

    MineMartix .版本 2 .支持库 spec .程序集 MineMartix, Ex_控件基类, , ExDUI扩展组件_扫雷按钮 .程序集变量 m_颜色, 整数型 .程序集变量 m_透明度, 整数型 .程序集变量 m_Rect, RectF .程序集变量 m_hIcon, 整数型, , , 图像句柄 .程序集变量 m_单击回调指针, 整数型 .程序集变量 m_Mine, 逻辑型 .程序集变量 m_Pos, PointF .程序集变量 m_Num, 整数型, , , 周围雷数量 .程序集

  • C语言实现扫雷游戏及其优化

    本文实例为大家分享了C语言实现扫雷游戏及其优化的具体代码,供大家参考,具体内容如下 关于扫雷优化 1.核心思想:使用两个二维数组进行设计,一个用于显示,一个用于后台雷的布置. 2.使用宏常量,后期可以任意修改游戏难度. 3.关于扫雷拓展模块,目前使用的方法比较low,若周围均没有,则全部显示. 4.剩余位置数使用全局变量count,必须考虑拓展之后count变化. 有待改进之处 1.需设计标记雷的步骤,增加用户体验. 2.拓展方式有待改进. 3.界面布局仍需要进行优化. 扫雷游戏代码 #incl

  • C语言简易扫雷游戏

    本文实例为大家分享了C语言扫雷游戏的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<stdlib.h> #include<time.h> #define MAX_ROW 9 #define MAX_COL 9 #define MINE_C0UNT 10 void menu() { printf("************************\n"); printf("***** 1.play *

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

    本文指的扫雷是简单模拟电脑中的扫雷游戏,但以我目前的水平,也就只能在黑框中实现 test.c #include<stdio.h> #include<stdlib.h> #include<time.h> #include "game2.h" void menu() { printf("********* welcome ********\n"); printf("**********1.play**********\n&q

随机推荐