程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C/C++_掃雷

C/C++_掃雷

編輯:關於C++
#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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved