C/C++_扫雷
程序员文章站
2022-09-28 11:38:33
#include
#include"MyHead.h"
using namespace std;
//设置窗口的标题
void setWindo...
#include #include"MyHead.h" using namespace std; //设置窗口的标题 void setWindows(){ system("title 扫雷"); system("mode con: cols=98 lines=25"); } //重置窗口 void ReturnWindows(){ HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SMALL_RECT rc = { 0, 0, 98, 25 }; SetConsoleWindowInfo(hOut, true, &rc); } //设置颜色 void Color(int n){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n); } //申请地图 void GetMap(int ***pMap, int mapHeight, int mapWidth){ *pMap = (int **)malloc(sizeof(int *)*mapHeight); if (*pMap == NULL){ return; } for (int i = 0; i < mapHeight; i++){ *(*pMap + i) = (int*)malloc(sizeof(int)*mapWidth); if (*(*pMap + i) == NULL){ return; } for (int j = 0; j < mapWidth; j++){ if (i == 0 || j == 0 || i == mapHeight - 1 || j == mapWidth - 1){ *(*(*pMap + i) + j) = -10; continue; } *(*(*pMap + i) + j) = 10; } } } //获取地图标记 void GetFlag(int ***pFlag, int mapHeight, int mapWidth){ *pFlag = (int **)malloc(sizeof(int *)*mapHeight); if (*pFlag == NULL){ return; } for (int i = 0; i < mapWidth; i++){ *(*pFlag + i) = (int*)malloc(sizeof(int)*mapWidth); if (*(*pFlag + i) == NULL){ return; } for (int j = 0; j < mapWidth; j++){ *(*(*pFlag + i) + j) = 1; } } } //绘制地图 void PrintMap(int ***pMap, int mapHeight, int mapWidth){ if (pMap == NULL){ return; } /*for (int i = 0; i < mapHeight; i++){ for (int j = 0; j < mapWidth; j++){ cout << *(*(*pMap + i) + j) << " "; } cout << endl; } cout << endl;*/ for (int i = 0; i < mapHeight; i++){ for (int j = 0; j < mapWidth; j++){ switch (*(*(*pMap + i) + j)){ Color(15); case 0: cout << " ";//空地 break; case 10: cout << "■"; //初始地图值; break; case -10: Color(14); cout << "■"; //地图边框值; Color(15); break; case 20: cout << "■"; //隐式雷; break; case 30: Color(4); cout << "★"; //显式雷; Color(15); break; case 40: Color(5); cout << "☆"; //标记雷(true); Color(7); break; case 50: Color(5); cout << "☆"; //标记雷I(false); Color(15); break; default: { switch (*(*(*pMap + i) + j)){ case 1: cout << "①"; break; case 2: cout << "②"; break; case 3: cout << "③"; break; case 4: cout << "④"; break; case 5: cout << "⑤"; break; case 6: cout << "⑥"; break; case 7: cout << "⑦"; break; case 8: cout << "⑧"; break; } } break; } } cout << endl; } } //获取雷的数量 int GetCurrentBoomCount(int ***pMap, int begin_x, int begin_y, int mapHeight, int mapWidth){ int currentBoomCount = 0; for (int i = 0; i < 8; i++){ if ((begin_x + Go_x[i]) >= mapHeight - 1 || (begin_y + Go_y[i]) >= mapWidth - 1 || (begin_x + Go_x[i]) < 1 || (begin_y + Go_y[i]) < 1){ continue; } if (*(*(*pMap + (begin_x + Go_x[i])) + (begin_y + Go_y[i])) == 20){ currentBoomCount++; } } return currentBoomCount; } //运行游戏 void RunGame(int ***pMap, int ***pFlag, int begin_x, int begin_y, int mapHeight, int mapWidth){ int currentBoomCount = GetCurrentBoomCount(pMap, begin_x, begin_y, mapHeight, mapWidth); if (currentBoomCount == 0){ *(*(*pMap + begin_x) + begin_y) = 0; *(*(*pFlag + begin_x) + begin_y) = 0; for (int i = 0; i < 8; i++){ if ((begin_x + Go_x[i]) >= mapHeight - 1 || (begin_y + Go_y[i]) >= mapWidth - 1 || (begin_x + Go_x[i]) < 1 || (begin_y + Go_y[i]) < 1){ continue; } if (*(*(*pFlag + (begin_x + Go_x[i])) + (begin_y + Go_y[i])) == 1){ RunGame(pMap, pFlag, begin_x + Go_x[i], begin_y + Go_y[i], mapHeight, mapWidth); } } } else{ *(*(*pFlag + begin_x) + begin_y) = 0; *(*(*pMap + begin_x) + begin_y) = currentBoomCount; } } //开始游戏 int BeginGame(int ***pMap, int ***pFlag, int begin_x, int begin_y, int mapHeight, int mapWidth, int flag){ if (flag == 0){ if (pMap == NULL){ return -1; } if (begin_x >= mapHeight - 1 || begin_y >= mapWidth - 1 || begin_x < 1 || begin_y < 1){ return -2; } if (*(*(*pMap + begin_x) + begin_y) == 20){ for (int i = 0; i < mapHeight; i++){ for (int j = 0; j < mapWidth; j++){ if (*(*(*pMap + i) + j) == 20){ *(*(*pMap + i) + j) = 30; } } } system("CLS"); PrintMap(pMap, mapHeight, mapWidth); return -3; } else if (*(*(*pMap + begin_x) + begin_y) == 10){ RunGame(pMap, pFlag, begin_x, begin_y, mapHeight, mapWidth); } else{ return -2; } } else{ if (pMap == NULL){ return -1; } if (begin_x >= mapHeight - 1 || begin_y >= mapWidth - 1 || begin_x < 1 || begin_y < 1){ return -2; } if (*(*(*pMap + begin_x) + begin_y) == 20){ getBoom++; system("CLS"); *(*(*pMap + begin_x) + begin_y) = 40; PrintMap(pMap, mapHeight, mapWidth); } else if ((*(*(*pMap + begin_x) + begin_y) == 40)){ getBoom--; system("CLS"); *(*(*pMap + begin_x) + begin_y) = 20; PrintMap(pMap, mapHeight, mapWidth); } else if ((*(*(*pMap + begin_x) + begin_y) == 50)){ system("CLS"); *(*(*pMap + begin_x) + begin_y) = 10; PrintMap(pMap, mapHeight, mapWidth); trueBoom--; } else{ if (*(*(*pFlag + begin_x) + begin_y) == 1){ system("CLS"); *(*(*pMap + begin_x) + begin_y) = 50; PrintMap(pMap, mapHeight, mapWidth); trueBoom++; } else{ return -2; } } } return 0; } //设置雷在地图上 void SetBoom(int ***pMap, int mapHeight, int mapWidth, int boomCount){ if (pMap == NULL){ return; } for (int i = 1; i < mapHeight - 1; i++){ for (int j = 1; j < mapWidth - 1; j++){ boomCount--; *(*(*pMap + i) + j) = 20; if (boomCount == 0){ goto flag; } } } flag:; } //重新排布雷 void SwapBoom(int ***pMap, int mapHeight, int mapWidth){ if (pMap == NULL){ return; } srand((unsigned)time(NULL)); for (int i = 1; i < mapHeight - 1; i++){ for (int j = 1; j < mapWidth - 1; j++){ int ra = 50; while (ra--){ int x = rand() % (mapHeight - 2) + 1; int y = rand() % (mapWidth - 2) + 1; if ((*(*(*pMap + i) + j)) != (*(*(*pMap + x) + y))){ *(*(*pMap + i) + j) = (*(*(*pMap + i) + j)) ^ (*(*(*pMap + x) + y)); *(*(*pMap + x) + y) = (*(*(*pMap + i) + j)) ^ (*(*(*pMap + x) + y)); *(*(*pMap + i) + j) = (*(*(*pMap + i) + j)) ^ (*(*(*pMap + x) + y)); } } } } } //释放地图 void FreeMap(int ***pMap, int mapHeight){ if (pMap == NULL){ return; } for (int i = 0; i < mapHeight; i++){ if (*(*pMap + i) != NULL){ free(*(*pMap + i)); *(*pMap + i) = NULL; } } if (*pMap != NULL){ free(*pMap); *pMap = NULL; } } //登录界面 bool LoginWin(){ char username[150]; char password[150]; cout << endl << endl << endl << endl; Color(12); cout << "\t\t\t\t\t" << "-------------------" << endl; cout << "\t\t\t\t\t" << " 欢迎登录 " << endl; cout << "\t\t\t\t\t" << "-------------------" << endl; cout << "\t\t\t\tUserName:"; Color(11); cin >> username; Color(12); cout << "\t\t\t\tPassWord:"; Color(11); cin >> password; Color(12); cout << endl << endl;; if (strcmp(username, "root") == 0 && strcmp(password, "root") == 0){ cout << "__________________________________________________________________________________________________" << endl; cout << endl; Color(11); cout << "\t...登录成功..." << endl; Color(12); cout << "__________________________________________________________________________________________________" << endl; return true; } else{ cout << "__________________________________________________________________________________________________" << endl; cout << endl; Color(11); cout << "\t...登录失败..." << endl; Color(12); cout << "__________________________________________________________________________________________________" << endl; return false; } Color(7); system("pause"); } //登录成功 void LoginSuc(){ Color(10); cout << endl; cout << "\t\t\t\t\t" << " 登录成功" << endl; cout << "玩法详情:" << endl; cout << "\t① " << "首先输入地图的长(mapHeight)、高(mapWidth)、埋雷的数量(boomCount)。" << endl; cout << "\t② " << "然后输入一个坐标point(x,y)、紧接着输入处理类型(Flag)。" << endl; cout<<"\t 如:1-->标雷。2-->挖雷" << endl; cout << "\t③ " << "当踩到雷的时候则其余的雷全部爆炸、游戏失败。" << endl; cout << "\t④ " << "当正确标注所有雷之后、游戏结束、玩家胜利。" << endl; cout << "\t⑤ " << "注意 所有的坐标point(x,y)"; Color(12); cout<<"[ 0="" mapheight="" >>="" mapwidth="" boomcount;="" getmap(&pmap,="" mapheight,="" mapwidth);="" getflag(&pflag,="" if="" (pmap="=" null="" pflag="=" null){="" cout="" <<="" "...loading="" map="" error..."="" endl;="" return="" 0;="" }="" setboom(&pmap,="" mapwidth,="" boomcount);="" swapboom(&pmap,="" system("cls");="" while="" (true){="" (getboom="=" boomcount="" &&="" trueboom="=" 0){="" messagebox(null,="" text("恭喜你、全部猜中啦"),="" text("system"),="" mb_iconinformation="" |="" mb_ok);="" "...you="" win..."="" printmap(&pmap,="" "enter="" point(x,y):";="" cin="" begin_x="" begin_y;="" flag(0="" or="" 1):";="" flag;="" int="" ret="BeginGame(&pMap," &pflag,="" begin_x,="" begin_y,="" flag);="" (ret="=" -1){="" break;="" else="" -2){="" -3){="" text("很遗憾、踩中雷啦"),="" mb_iconstop="" "...game="" over..."="" else{="" freemap(&pmap,="" mapheight);="" freemap(&pflag,="" returnwindows();="" }
#ifndef MYHEAD_H #define MYHEAD_H #include #include #include #include #include #include #include int getBoom = 0; int trueBoom = 0; int Go_x[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int Go_y[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; //设置窗口的标题 void setWindows(); //重置窗口 void ReturnWindows(); //设置颜色 void Color(int n); //登录界面 bool LoginWin(); //登录成功 void LoginSuc(); //申请地图 void GetMap(int ***pMap, int mapHeight, int mapWidth); //获取地图标记 void GetFlag(int ***pFlag, int mapHeight, int mapWidth); //绘制地图 void PrintMap(int ***pMap, int mapHeight, int mapWidth); //设置雷在地图上 void SetBoom(int ***pMap, int mapHeight, int mapWidth, int boomCount); //重新排布雷 void SwapBoom(int ***pMap, int mapHeight, int mapWidth); //获取雷的数量 int GetCurrentBoomCount(int ***pMap, int begin_x, int begin_y, int mapHeight, int mapWidth); //开始游戏 int BeginGame(int ***pMap, int ***pFlag, int begin_x, int begin_y, int mapHeight, int mapWidth, int flag); //运行游戏 void RunGame(int ***pMap, int ***pFlag, int begin_x, int begin_y, int mapHeight, int mapWidth); //释放地图 void FreeMap(int ***pMap, int mapHeight); #endif