#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define framex 10
#define framey 3
#define R 10
#define C 20
#define W 20
using namespace std;
int flag=40, bom=40;
int pass=1;
int Currx = framex;
int Curry = framey;
struct point
{
string str = "□";
string num = "■";
int judge = 0;
int x = 0;
int sta=0;
}Point[W][W];
void gotoxy(HANDLE fd, int x, int y){
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(fd, pos);
};
void HideTheCursor(HANDLE fd)
{
CONSOLE_CURSOR_INFO cciCursor;
if(GetConsoleCursorInfo(fd, &cciCursor))
{
cciCursor.bVisible = FALSE;
SetConsoleCursorInfo(fd, &cciCursor);
}
}
void ShowTheCursor(HANDLE fd)
{
CONSOLE_CURSOR_INFO cciCursor;
if(GetConsoleCursorInfo(fd, &cciCursor))
{
cciCursor.bVisible = TRUE;
SetConsoleCursorInfo(fd, &cciCursor);
}
}
void Game_Menu(HANDLE fd){
system("cls");
gotoxy(fd, framex, 5);
cout << "*******************";
gotoxy(fd, framex, 7);
cout << " 扫雷 ";
gotoxy(fd, framex, 9);
cout << " 任意键开始 ";
gotoxy(fd, framex, 11);
cout << " 按q或Q退出 ";
gotoxy(fd, framex, 13);
cout << " 按w,a,s,d移动 ";
gotoxy(fd, framex, 15);
cout << " 按j插旗,k确认 ";
gotoxy(fd, framex, 17);
cout << " 按p翻开 ";
gotoxy(fd, framex, 19);
cout << "*******************";
};
void color(int m){
HANDLE consolehend;
consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehend, m);
};
int DrawMap(HANDLE fd){
system("cls");
gotoxy(fd, framex + C, framey - 3);
color(15);
cout << "第" << pass << "关";
for(int i = 0; i < W; i++){
gotoxy(fd, framex, framey + i);
for(int j = 0; j < W; j++){
cout<<Point[i][j].str;
}
}
for(int i=1;i<=40;){
int n=rand()%19+0;
int m=rand()%19+0;
if (Point[n][m].x==0){
Point[n][m].x=1;
if(n!=0) Point[n-1][m].sta++;
if(n!=0&&m!=0) Point[n-1][m-1].sta++;
if(n!=0&&m!=19) Point[n-1][m+1].sta++;
if(m!=0) Point[n][m-1].sta++;
if(m!=19) Point[n][m+1].sta++;
if(n!=19&&m!=0) Point[n+1][m-1].sta++;
if(n!=19) Point[n+1][m].sta++;
if(n!=19&&m!=19) Point[n+1][m+1].sta++;
i++;
}
}
return 0;
};
int DrawPoint(HANDLE fd,int x,int y)
{
gotoxy(fd, x, y);
cout<<Point[x-framex][y-framey].str;
gotoxy(fd, x, y);
}
void Move(HANDLE fd,int x,int y){
gotoxy(fd, x, y);
ShowTheCursor(fd);
}
int c=0;
int InputPro(HANDLE fd,char ch){
switch(ch)
{
case 'w':
case 'W':
{
if(Curry>framey) Curry --;
Move(fd, Currx, Curry);
break;
}
case 'a':
case 'A':
{
if(Currx>framex+1) Currx -=2;
Move(fd, Currx, Curry);
break;
}
case 's':
case 'S':
{
if(Curry<framey+W-1) Curry ++;
Move(fd, Currx, Curry);
break;
}
case 'd':
case 'D':
{
if(Currx<framex+2*W-2) Currx +=2;
Move(fd, Currx, Curry);
break;
}
case 'j':
case 'J':
{
Point[(Currx-framex)/2][Curry-framey].str="★";
DrawPoint(fd,Currx,Curry);
break;
}
case 'k':
case 'K':
{
Point[(Currx-framex)/2][Curry-framey].str="■";
DrawPoint(fd,Currx,Curry);
flag--;
break;
}
case'p':
case'P':
{
if(Point[(Currx-framex)/2][Curry-framey].x==1) {
Point[(Currx-framex)/2][Curry-framey].str="●";
cout<<"●";
Move(fd, 10, 46);
cout<<"dead!";
c=1;
}
else{
cout<< Point[(Currx-framex)/2][Curry-framey].sta<<" ";
}
Move(fd, Currx, Curry);
}
default:;
return c;
}
}
int main()
{
HANDLE fd = GetStdHandle(STD_OUTPUT_HANDLE);
Game_Menu(fd);
char ch = getch();
if(ch == 'q' || ch == 'Q') return 0;
DrawMap(fd);
gotoxy(fd, Currx, Curry);
while(1)
{
ch = getch();
if(ch == 'q' || ch == 'Q')
{
gotoxy(fd, framex+W, framey+W);
return 0;
}
InputPro(fd,ch);
if(c==1) {
Move(fd, 10, 24);
cout<<"正确答案:"<<endl;
for(int i=0;i<=19;i++){ Move(fd,10,25+i);
for(int j=0;j<=19;j++){
if(Point[j][i].x==0) cout<<Point[j][i].sta<<" ";
else cout<<"●";
}
}
Move(fd, 10, 47);
return 0;
}
if (flag==0){
int co=0;
for(int i=0;i<=19;i++)
for(int j=0;j<=19;j++){
if(Point[i][j].x==1&&Point[i][j].str=="■")
co++;
}
cout<<"正确判断:"<<co;
return 0;
}
}
}