这是一个简单控制台的扫雷!
#include <iostream>
#include "ff.h"
#include <Windows.h>
#include <time.h>
void main()
{
int h = 50 ;
Init(h);
while(1)
{
MoveCursor(0, 0);
draw();
run();
//Sleep(100);
if(lose)
{
std::cout<<"YOU LOSE";
system("pause");
return ;
}
if(win)
{
std::cout<<"YOU WIN";
system("pause");
return ;
}
}
system("pause");
}
头文件..h
extern bool lose ;
extern bool win ;
void MoveCursor(int x, int y);
void Init(int a);
void draw();
void run();
#include <iostream>
#include <Windows.h>
#include<time.h>
#include "ff.h"
#define W 20
#define H 20
#define S (W*H)
//设置小方格的结构体数据:位置、有无雷、是否显示
struct Grid
{
int x ;
int y ;
int C_m ;
bool mine ;
bool visual ;
};
Grid grid[S] ;
int map[S]={};
char * tuxing[] = {"■","□","☆","▼","◎",
"①","②","③","④","⑤","⑥","⑦","⑧"};
//开始 、 雷 、标记、问号、坐标
//地雷的数量------------------
int count_mine = 20 ;
bool lose = false ;
bool win = false ;
int px = 5 ;
int py = 5 ;
int dirx[8] = {1,1,1,0,0,-1,-1,-1};
int diry[8] = {0,-1,1,-1,1,0,-1,1};
//地雷随机排布,初始化-------------
void Init(int a)
{
srand((unsigned int)time(0));
for(int i = 0 ; i < S ; ++i)
{
grid[i].x = i % W ;
grid[i].y = i / W ;
grid[i].mine = false ;
grid[i].visual = false ;
}
while(a)
{
int _get = rand()% S ;
if(grid[_get].mine == false)
{
grid[_get].mine = true ;
--a;
}
}
for(int i = 0 ; i < S ; ++ i )
{
int x = grid[i].x ;
int y = grid[i].y ;
int cm = 0 ;
for(int j = 0 ; j < 8 ; ++j)
{
int a = x+dirx[j];
int b = y+diry[j];
if( a> 0 && a < W && b > 0 && b < H)
{
if( grid[a + b*W].mine == true )
cm++ ;
}
}
grid[i].C_m = cm ;
}
}
void MoveCursor(int x, int y)//-----------------------------设置光标位置
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x * 2;
coord.Y = y;
SetConsoleCursorPosition(hOut, coord);
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(hOut, &cursor_info);
}
//绘制-----------------
void draw()
{
int T_map[S];
memcpy(T_map,map,S*4);
T_map[px+py*W] = 2 ;
for(int i = 0 ; i < S ; ++i)
{
std::cout<<tuxing[T_map[i]];
if(i % W == W-1)
std::cout<<std::endl;
}
}
//扩散------------------------
void kuosan(int a,int b)
{
grid[a+b*W].visual = true ;
if(grid[a+b*W].C_m == 0)
{
map[a+b*W] = 1 ;
for(int j = 0 ; j < 8 ; ++j)
{
int x = a+dirx[j] ;
int y = b+diry[j];
if(x>= 0 && x < W && y >= 0 && y < H && grid[x+y*W].visual == false)
kuosan(x,y);
}
}
else
map[a+b*W] = grid[a+b*W].C_m + 4 ;
}
void run()
{
if((GetAsyncKeyState('W') & 1)&& py> 0)py--;
else if((GetAsyncKeyState('S') & 1)&& py+1< H)py++;
else if((GetAsyncKeyState('A') & 1)&& px> 0)px--;
else if((GetAsyncKeyState('D') & 1)&& px+1< W)px++;
else if(GetAsyncKeyState(' ') & 1)
{
if(grid[px + py*W].visual == false && grid[px + py*W].mine == false)
kuosan(px,py);
else if(grid[px + py*W].visual == false && grid[px + py*W].mine == true)
{
lose = true ;
map[px+py*W] = 4 ;
}
}
else if((GetAsyncKeyState('B') & 1) && grid[px+py*W].visual == false)
{
if(map[px+py*W] == 0)map[px+py*W] = 3 ;
else map[px+py*W] = 0 ;
}
win = true ;
for(int i = 0 ; i < S ; ++ i)
{
if(grid[i].mine == false && map[i] == 0)
{
win =false ;
break ;
}
}
}