代码如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*//*
* File: main.cpp
* Author: Administrator
*
* Created on 2019年4月29日, 上午10:06
*/#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<WinDef.h>
#include<conio.h>
#include<queue>
#include<ctype.h>
#define A 17
#define B 17
#define C 30using namespace std
//定义全局变量
DWORD a,b;
char map[A][B],news,spare;
int BoomTotalNums,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;
floatx = A/2;
floaty = B/2;
flagnum = 0;
BoomTotalNums = 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(BoomTotalNums){
x = rand()%A;
y= rand()%B;
if(map[x][y] == ""){
map[x][y] = "@";
BoomTotalNums--;
}
}
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(45,8);
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; //右
}}