扫雷

chen_zhe 沙雕 2020-10-11 17:27:49 2020-10-11 21:27:14 5

嗯……看这码风就知道这不是我原创的把……

#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#include <algorithm>
#include <iostream> 
#define REP(i,a,b) for (int i=(a);i<=(b);i++)
#define PER(i,a,b) for (int i=(a);i>=(b);i--)
#define max(x,y) ((x)<(y)?(y):(x))
#define min(y,x) ((x)<(y)?(x):(y))
#define MEM(a,b) memset(a,(b),sizeof(a))
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)//判断这个键是否按下
#define KEY_UP(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 0 : 1)//判断这个键是否弹起
#define KEY_EVERY(lpkeyState) GetKeyboardState(lpKeyState)//获得所有的256个键(键盘按键、鼠标按键等等)的状态,lpKeyState是指向一个256bit的数组,存放所有键的状态。
#define KEY_NOW(nVirtKey) GetKeyState(nVirtKey)//用于判断nVirtKey的状态。用返回值的最高位表示,最高位为1表示当前键处于down的状态;最高位为0当前键处于up状态。此函数从消息队列中读取消息进行处理。
#define setcolor(x) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x)//设置颜色
#define getkey(x) GetAsyncKeyState(x)
#define GetWindow() GetForegroundWindow();//得到窗口信息
/*
鼠标左键 : MOUSE_MOVED 
鼠标右键 :MOUSE_EVENT
鼠标滚轮 : MOUSE_WHEELED
MK_CONTROL当CTRL键按下时。
MK_LBUTTON当鼠标左键按下时。
MK_MBUTTON当鼠标中键按下时。
MK_RBUTTON当鼠标右键按下时.
MK_SHIFT当SHIFT按下时。
*/
using std::cin;
using std::cout;
using std::endl;
int brand();
void GOTO(int x,int y);
int brand(){return (rand()<<16)|(rand()<<1)|(rand()&1);}
void bsrand(){srand(GetTickCount());}
void cls(){system("cls");}
void retr(){//退出程序 
    HWND hWnd=GetForegroundWindow();
    ShowWindow(hWnd,SW_HIDE);
    exit(0);
}
void Window_Hide(HWND hWnd){ShowWindow(hWnd,0);}//隐藏窗口
void Window_Show(HWND hWnd){ShowWindow(hWnd,1);}//显示窗口
int getmouse_y(){//获取鼠标在屏幕中x的位置 
    POINT pt; 
    GetCursorPos(&pt);
    return pt.x;
}
int getmouse_x(){//获取鼠标在屏幕中y的位置 
    POINT pt; 
    GetCursorPos(&pt);
    return pt.y;
}
void setmouse(int y,int x){SetCursorPos(x,y);}//设置鼠标在屏幕中的位置 
void click_left(){//鼠标左键点击
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);  
    Sleep(5);//要留给某些应用的反应时间   
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); 
}
void click_right(){//鼠标右键点击
    mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);  
    Sleep(5);   
    mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);  
}
void GOTO(int x,int y){//将光标移动到屏幕中的位置
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    HANDLE hConsoleOut;
    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
    csbiInfo.dwCursorPosition.Y = x;
    csbiInfo.dwCursorPosition.X = y;
    SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
}
#include "all.h"//copy我的函数库,在上面
using namespace std;
const int flg_xzy[9][2]={{0,0},{0,1},{0,-1},{1,0},{1,1},{1,-1},{-1,0},{-1,1},{-1,-1}};
int ground=0,mp[105][105],n=9,m=9,Set_number=10,number,Ending=0;
//string s1="mode con cols=",s2=" lines=";
bool vis_xzy[105][105],vis[105][105],se[105][105];
int ran(int x){return x<=0?0:(rand()<<16|rand()<<1|(rand()&1))%x;}
void mp_add(int x,int y,int s){
	mp[x][y]=-1;
	for(int k=1;k<9;k++)
	if(flg_xzy[k][0]+x>=0&&flg_xzy[k][0]+x<n&&flg_xzy[k][1]+y>=0&&flg_xzy[k][1]+y<m)
	if(mp[flg_xzy[k][0]+x][flg_xzy[k][1]+y]^-1) mp[flg_xzy[k][0]+x][flg_xzy[k][1]+y]+=s;
}
void mak_xzy(int X,int Y,int num){
	memset(mp,0,sizeof(mp));
	memset(vis_xzy,0,sizeof(vis_xzy));
	for(int i=0;i<9;i++)
	if(flg_xzy[i][0]+X>=0&&flg_xzy[i][0]+X<n&&flg_xzy[i][1]+Y>=0&&flg_xzy[i][1]+Y<m)
	vis_xzy[flg_xzy[i][0]+X][flg_xzy[i][1]+Y]=1;
	while(num--){
		int x=rand()%n,y=rand()%m;
		while(mp[x][y]||vis_xzy[x][y]) x=rand()%n,y=rand()%m;
		mp_add(x,y,1);
	}
}
void DFS_xzy(int x,int y,bool C){
	vis_xzy[x][y]=1;se[x][y]=1;
	if(vis[x][y]||mp[x][y]==-1) Ending=1;
	if(mp[x][y]&&C) return;
	for (int i=1;i<9;i++){
		int xx=x+flg_xzy[i][0],yy=y+flg_xzy[i][1];
		if (xx<0||yy<0||xx>=n||yy>=m||vis_xzy[xx][yy]||vis[xx][yy]) continue;
		DFS_xzy(xx,yy,1);
	}
}
void change_mp(){
	setcolor(ground+12);
	for(int i=0;i<n;i++)
	for(int j=0;j<m;j++)
	if(se[i][j]==1){GOTO(i,j*2);if(!mp[i][j]) printf("  ");else printf("%d ",mp[i][j]);}
}
void Lost(){
	cls();
	setcolor(ground+12);
	printf("你没了qwq\n");
	Sleep(1500);
}
void Win(){
	cls();
	setcolor(ground+12);
	printf("你赢了!\n");
	Sleep(1500);
}
int Start(){
	string s;
//	s=s1+ChangeNumToStr(m)+s2+ChangeNumToStr(n);
//	system(s.c_str());
	setcolor(ground+14);cls();
	for(int i=1;i<=n;++i){for(int j(1);j<=m;++j) printf("");printf("\n");}
	setcolor(ground+15);
	GOTO(n,0);printf("   ");
	GOTO(n,0);printf("%d",number);
	int x=0,y=0;GOTO(x,2*y);setcolor(ground+10);printf("");
	bool t=0;int Js=number;
	while(1){
		char ch=getch();
		if(ch<0){
			ch=getch();
			GOTO(x,y*2);
			if(se[x][y]){
				setcolor(ground+12);
				if(mp[x][y]) printf("%d ",mp[x][y]);else printf("  ");
			}else if(vis[x][y]) setcolor(ground+15),printf("");
				  else setcolor(ground+14),printf("");
			if(ch==75&&y>0) y--;else
			if(ch==77&&y<m-1) y++;else
			if(ch==72&&x>0) x--;else
			if(ch==80&&x<n-1) x++;
			GOTO(x,y*2);
			if(se[x][y]){
				setcolor(160+12);
				if(mp[x][y]) printf("%d ",mp[x][y]);else printf("  ");
			}else setcolor(ground+10),printf("");
		}else{
			GOTO(x,y*2);
			if(se[x][y]){
				setcolor(ground+12);
				if(mp[x][y]) printf("%d ",mp[x][y]);else printf("  ");
			}else if(vis[x][y]) setcolor(ground+15),printf("");
				  else setcolor(ground+14),printf("");
			if((ch=='W'||ch=='w')&&x>0) x--;else
			if((ch=='S'||ch=='s')&&x<n-1) x++;else
			if((ch=='A'||ch=='a')&&y>0) y--;else
			if((ch=='D'||ch=='d')&&y<m-1) y++;else
			if(ch==' '){
				if(!t) t=1,mak_xzy(x,y,Set_number);
				if(mp[x][y]==-1){Lost();return 0;}
				else{
					memset(vis_xzy,0,sizeof(vis_xzy));
					DFS_xzy(x,y,0);change_mp();
					if(Ending){Lost();return 0;}
				}
			}
			if(ch=='P'||ch=='p'){
				GOTO(x,y*2);vis[x][y]=1-vis[x][y];
				if(mp[x][y]==-1&&vis[x][y]==1) number--;
				if(mp[x][y]==-1&&vis[x][y]==0) number++;
				if(vis[x][y]==1) Js--;
				if(vis[x][y]==0) Js++;
				if(Js<0) vis[x][y]=1-vis[x][y],Js++;
				setcolor(ground+15);
				GOTO(n,0);printf("   ");
				GOTO(n,0);printf("%d",Js);
				if(number==0){Win();return 0;}
			}
			GOTO(x,y*2);
			if(se[x][y]){
				setcolor(160+12);
				if(mp[x][y]) printf("%d ",mp[x][y]);else printf("  ");
			}else setcolor(ground+10),printf("");
		}
	}
}
void Set(){
	cls();setcolor(ground+15);
	printf("选择难度\n");
	printf("0.返回\n");
	printf("1.初级\n");
	printf("2.中级\n");
	printf("3.高级\n");
	printf("4.自定义\n");
	char ch=getch();
	while(ch<'0'||ch>'4') ch=getch(); 
	if(ch=='0') return;
	if(ch=='1') n=9,m=9,Set_number=10;else
	if(ch=='2') n=16,m=16,Set_number=40;else
	if(ch=='3') n=16,m=30,Set_number=99;
	if(ch^'4') return;
	int nn=0;
	while(nn<5||nn>25){
		printf("行数(范围5~25):");
		cin>>nn;
		if(nn<5||nn>25) printf("\n重复");
	}
	n=nn;
	int mm=0;
	while(mm<5||mm>30){
		printf("列数(范围5~30):");
		cin>>mm;
		if(mm<5||mm>30) printf("\n重复");
	}
	m=mm;
	int tt=0;
	while(tt<=0||tt>25){
		printf("雷的个数(范围1~%d):",n*m);
		cin>>tt;
		if(tt<=0||tt>25) printf("\n重复");
	}
	Set_number=tt;
}
int main(){
	system("title 扫雷游戏");
	char ch;bsrand();
	while(1){
		setcolor(ground+12);
		cls();memset(se,0,sizeof(se));number=Set_number;Ending=0;
		memset(vis,0,sizeof(vis)); 
		printf("     主菜单\n");
		printf("   0.退出\n");
		printf("   1.开始游戏\n");
		printf("   2.选择难度\n");
		printf("   空格点击,P标记\n");
		ch=getch();
		if(ch=='0') retr();else
		if(ch=='1') Start();else
		if(ch=='2') Set();
	}
	return 0;
}
{{ vote && vote.total.up }}

共 4 条回复

haoyuexiaozi 精神小伙

哪儿弄得

Herobrine 303

1159593860

chen_zhe 沙雕

武知道

root 站长
```cpp
代码
```