1. 程式人生 > 其它 >C/C++實現掃雷小遊戲

C/C++實現掃雷小遊戲

技術標籤:c++

C/C++實現掃雷

  • 對於掃雷小遊戲來說,相信很多C/C++的初學者都希望能會這一款遊戲

  • 本人是用C++寫的,如果想用C語言寫的小夥伴對應修改即可

  • 我們先梳理思路

  • 1.初始化棋盤,這裡需要兩個棋盤,玩過掃雷遊戲的玩家應該知道,給你的介面是*****這樣的,需要你自己去掃點,但對於設計這款遊戲的人來說,是已經隨機佈置了多少個雷的。

  • 2.所以先初始化設計者棋盤 和 玩家棋盤

  • 設計者棋盤如下:

    在這裡插入圖片描述

  • 玩家棋盤如下:在這裡插入圖片描述

***初始化棋盤***
void Myboard::InitBoard(char board[ROWS][COLS],int row,int col,char ret)
{ int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ret; // 初始化放* 0 } } }
注意紅框外面的是行列座標
  • 3.看到設計者棋盤跟玩家棋盤,我們應該知曉 棋盤是10*10的格子。外面還有行列座標,所以我們的行和列應該設定為ROWS:11,COLS:11。
***列印棋盤***
void Myboard::DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0; int j = 0; for (i = 0; i < row; i++) { cout << i << " "; } cout << endl; for (i = 1; i < row-1; i++) { cout << i << " "; for (j = 1; j < col; j++) { cout << board[i][j] << " "; } cout <<
endl; } cout << 10 << " "; for (i = 1; i < row; i++) { cout << board[10][i] << " "; } cout << endl; }
  • 4.初始化之後設計者棋盤佈雷,1表示雷。
***隨機在設計者棋盤上佈置雷,這裡我佈雷的10個雷***
void Myboard::Set_mine(char board[ROWS][COLS])
{
	int x = 0, y = 0;
	srand((unsigned)time(NULL)); //隨機數種子
	int count = Count;
	while (count)
	{
		x = rand() % 10 + 1;  // 產生1-10的隨機數填入棋盤中
		y = rand() % 10 + 1;
		if (board[x][y] =='0')
		{
			board[x][y] = '1';
		}
		count--;
	}
}
  • 5.掃雷的時候第一次一般不會被炸死,所以這裡應該設定第一次不被炸死的情況
void Myboard::First_safe()
{
	int x = 0;
	int y = 0;
	char ch = 0; // 顯示周圍雷的個數
	int set = 1;
	cout << "請輸入座標掃雷" << endl;
	while (1)
	{
		cin >> x >> y;
		if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
		{
			if (menu[x][y] == '1')  // 第一次被雷炸死
			{
				menu[x][y] = '0';
				ch = count_mine(x, y);
				show[x][y] = ch + '0'; //展示給玩家看的,ch為多少,就代表這個點周圍有多少雷
				open_mine(x, y);  // 周圍座標,,那個座標沒有雷就展開那個座標的周圍
				while (set)
				{
					int x = rand() % 10 + 1;
					int y = rand() % 10 + 1;
					// 因為第一次被雷炸死了,所以就要重新隨機設定沒有雷的地方為雷
					if (menu[x][y] == '0')
					{
						menu[x][y] = '1';
						set--;
						break;
					}
				}break; // 跳出函式
			}
			if (menu[x][y] == '0')
			{
				ch = count_mine(x, y);
				show[x][y] = ch + '0';
				open_mine(x, y);
				break;
			}
		}
		else
		{
			cout << "輸入錯誤,請重新輸入!" << endl;
		}
	}
}
  • 6.如果掃的座標點的周圍的八個點沒有雷,就統計雷的個數,判斷玩家是否勝利的條件
int Myboard::count_mine(int x, int y)
{
	int count = 0;
	if (menu[x - 1][y - 1] == '1')
		count++;
	if (menu[x - 1][y] == '1')
		count++;
	if (menu[x - 1][y + 1] == '1')
		count++;
	if (menu[x][y - 1] == '1')
		count++;
	if (menu[x][y + 1] == '1')
		count++;
	if (menu[x + 1][y - 1] == '1')
		count++;
	if (menu[x + 1][y] == '1')
		count++;
	if (menu[x + 1][y + 1] == '1')
		count++;
	return count;
}
  • 7.如果掃雷周圍的八個點沒有雷,那麼繼續檢測這八個點的周圍是否有雷,使用遞迴
void Myboard::open_mine(int x,int y)
{
   if (menu[x - 1][y - 1] == '0')
   	show[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';

   if (menu[x - 1][y] == '0')
   	show[x - 1][y] = count_mine(x - 1, y) + '0';

   if (menu[x - 1][y + 1] == '0')
   	show[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';

   if (menu[x][y - 1] == '0')
   	show[x][y - 1] = count_mine(x, y - 1) + '0';

   if (menu[x][y + 1] == '0')
   	show[x][y + 1] = count_mine(x, y + 1) + '0';

   if (menu[x + 1][y - 1] == '0')
   	show[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';

   if (menu[x + 1][y] == '0')
   	show[x + 1][y] = count_mine(x + 1, y) + '0';

   if (menu[x + 1][y + 1] == '0')
   	show[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';
}
  • 8.最後進行掃雷
int Myboard::Sao_mine()
{
   int x = 0;
   int y = 0;
   char ch = 0;
   cout << "請輸入座標掃雷\n";
   cin >> x >> y;
   if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
   {
   	if (menu[x][y] == '0')  
   	{
   		ch = count_mine(x, y);
   		show[x][y] = ch + '0';
   		open_mine(x, y);  // 周圍座標,,那個座標沒有雷就展開那個座標的周圍
   		if (Count_show_mine() == Count)
   		{
   			DisplayBoard(show, ROWS, COLS);
   			cout << "玩家贏" << endl;
   			return 0;
   		}
   	}
   	else if(menu[x][y]=='1')
   	{
   		return 1;
   	}
   }
   return 0;
}
  • 8.勝利判斷條件,如果最後剩餘的*等於設定的雷數,則玩家勝利
int Myboard::Count_show_mine()
{
   int count = 0;
   int i = 0;
   int j = 0;
   for (i = 1; i < ROWS; i++)
   {
   	for (j = 1; j < COLS; j++)
   	{
   		if (show[i][j] == '*')
   		{
   			count++;
   		}
   	}
   }
   return count;
}
//標頭檔案展示
#pragma once
#include<iostream>
using namespace std;
#include<ctime>
#define ROWS 11
#define COLS 11
#define Count 10  // 雷個數
class Myboard
{
public:
	char menu[ROWS][COLS]; // 設計者棋盤
	char show[ROWS][COLS]; // 玩家棋盤
	// 1.初始化棋盤
	void InitBoard(char board[ROWS][COLS], int row, int col, char ret);

	// 2.列印棋盤
	void DisplayBoard(char board[ROWS][COLS], int row, int col);

	// 3.雷個數
	void Set_mine(char board[ROWS][COLS]);

	// 4.統計座標周圍雷個數
	int count_mine(int x, int y);

	// 5.避免第一次被炸死
	void First_safe();

	// 6.如果周圍的八個座標都沒有雷,則繼續展開  可以使用遞迴
	void open_mine(int x, int y);

	// 7.掃雷
	int Sao_mine();

	// 8.判斷未知區域剩餘個數
	int Count_show_mine();
};
// 主函式展示
#include"game.h"
void menu()
{
	cout << "***********************" << endl;
	cout << "******   1.play   *****" << endl;
	cout << "******   0.exit   *****" << endl;
	cout << "***********************" << endl;
}
void Game()
{
	Myboard game;
	// 1.初始化玩家棋盤和設計者棋盤
	game.InitBoard(game.menu, ROWS, COLS, '0');
	game.InitBoard(game.show, ROWS, COLS, '*');
	// 2.給設計者棋盤佈雷
	game.Set_mine(game.menu);
	// 3.列印設計者棋盤
	game.DisplayBoard(game.menu, ROWS, COLS);
	cout << endl;
	// 4.列印玩家棋盤
	game.DisplayBoard(game.show, ROWS, COLS);
	// 5.避免第一次被炸死
	game.First_safe();
	cout << endl;
	// 6.一步就贏的情況
	if (game.Count_show_mine() == Count)
	{
		game.DisplayBoard(game.menu, ROWS, COLS);
		cout << "玩家贏" << endl;
	}game.DisplayBoard(game.show, ROWS, COLS);

	while (1)
	{
		int count = game.Sao_mine();
		if (game.Count_show_mine() == Count)
		{
			game.DisplayBoard(game.menu, ROWS, COLS); //列印設計者棋盤
			cout << "玩家贏" << endl;
			break;
		}
		if (count)
		{
			cout << "很不幸,你被雷炸死了!" << endl;
			game.DisplayBoard(game.menu, ROWS, COLS); //列印設計者棋盤
			break;
		}game.DisplayBoard(game.show, ROWS, COLS);
	}

	//game.Set_menu(game.show);
	//game.DisplayBoard(game.show, ROWS, COLS);
}
int main()
{
	int input;
	
	do
	{
		menu();
		cout << "請選擇: 1.開始遊戲 0.退出遊戲" << endl;
		cin >> input;
		switch (input)
		{
		case 1:Game(); break;
		case 0:cout << "退出遊戲!"<<endl; exit(0); break;
		default:
			cout << "選擇錯誤,請重新選擇" << endl;
			break;
		}
	} while (1);
}

// 下面是功能函式展示   功能函式+主函式+標頭檔案即可開始遊戲
#include"game.h"
//初始化棋盤
void Myboard::InitBoard(char board[ROWS][COLS],int row,int col,char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ret;  // 初始化放*  0
		}
	}
}
// 列印棋盤
void Myboard::DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		cout << i << "  ";
	}
	cout << endl;
	for (i = 1; i < row-1; i++)
	{
		cout << i << "  ";
		for (j = 1; j < col; j++)
		{
			cout << board[i][j] << "  ";
		}
		cout << endl;
	}
	cout << 10 << " ";
	for (i = 1; i < row; i++)
	{
		cout << board[10][i] << "  ";
	}
	cout << endl;
}

 //設定雷個數
void Myboard::Set_mine(char board[ROWS][COLS])
{
	int x = 0, y = 0;
	srand((unsigned)time(NULL));
	int count = Count;
	while (count)
	{
		x = rand() % 10 + 1;  // 產生1-10的隨機數填入棋盤中
		y = rand() % 10 + 1;
		if (board[x][y] =='0')
		{
			board[x][y] = '1';
		}
		count--;
	}
}

// 統計當前座標周圍雷的個數
int Myboard::count_mine(int x, int y)
{
	int count = 0;
	if (menu[x - 1][y - 1] == '1')
		count++;
	if (menu[x - 1][y] == '1')
		count++;
	if (menu[x - 1][y + 1] == '1')
		count++;
	if (menu[x][y - 1] == '1')
		count++;
	if (menu[x][y + 1] == '1')
		count++;
	if (menu[x + 1][y - 1] == '1')
		count++;
	if (menu[x + 1][y] == '1')
		count++;
	if (menu[x + 1][y + 1] == '1')
		count++;
	return count;
}

// 避免第一次被炸死
void Myboard::First_safe()
{
	int x = 0;
	int y = 0;
	char ch = 0; // 顯示周圍雷的個數
	int set = 1;
	cout << "請輸入座標掃雷" << endl;
	while (1)
	{
		cin >> x >> y;
		if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
		{
			if (menu[x][y] == '1')  // 第一次被雷炸死
			{
				menu[x][y] = '0';
				ch = count_mine(x, y);
				show[x][y] = ch + '0';
				open_mine(x, y);  // 周圍座標,,那個座標沒有雷就展開那個座標的周圍
				while (set)
				{
					int x = rand() % 10 + 1;
					int y = rand() % 10 + 1;
					// 因為第一次被雷炸死了,所以就要重新隨機設定沒有雷的地方為雷
					if (menu[x][y] == '0')
					{
						menu[x][y] = '1';
						set--;
						break;
					}
				}break; // 跳出函式
			}
			if (menu[x][y] == '0')
			{
				ch = count_mine(x, y);
				show[x][y] = ch + '0';
				open_mine(x, y);
				break;
			}
		}
		else
		{
			cout << "輸入錯誤,請重新輸入!" << endl;
		}
	}
}

// 如果周圍的八個座標都沒有雷,則繼續展開  可以使用遞迴
void Myboard::open_mine(int x,int y)
{
	if (menu[x - 1][y - 1] == '0')
		show[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';

	if (menu[x - 1][y] == '0')
		show[x - 1][y] = count_mine(x - 1, y) + '0';

	if (menu[x - 1][y + 1] == '0')
		show[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';

	if (menu[x][y - 1] == '0')
		show[x][y - 1] = count_mine(x, y - 1) + '0';

	if (menu[x][y + 1] == '0')
		show[x][y + 1] = count_mine(x, y + 1) + '0';

	if (menu[x + 1][y - 1] == '0')
		show[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';

	if (menu[x + 1][y] == '0')
		show[x + 1][y] = count_mine(x + 1, y) + '0';

	if (menu[x + 1][y + 1] == '0')
		show[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';
}

// 掃雷
int Myboard::Sao_mine()
{
	int x = 0;
	int y = 0;
	char ch = 0;
	cout << "請輸入座標掃雷\n";
	cin >> x >> y;
	if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
	{
		if (menu[x][y] == '0')  
		{
			ch = count_mine(x, y);
			show[x][y] = ch + '0';
			open_mine(x, y);  // 周圍座標,,那個座標沒有雷就展開那個座標的周圍
			if (Count_show_mine() == Count)
			{
				DisplayBoard(show, ROWS, COLS);
				cout << "玩家贏" << endl;
				return 0;
			}
		}
		else if(menu[x][y]=='1')
		{
			return 1;
		}
	}
	else
	{
		cout << "輸入錯誤,請重新輸入!" << endl;
	}
	return 0;
}

// 判斷剩餘個數
int Myboard::Count_show_mine()
{
	int count = 0;
	int i = 0;
	int j = 0;
	for (i = 1; i < ROWS; i++)
	{
		for (j = 1; j < COLS; j++)
		{
			if (show[i][j] == '*')
			{
				count++;
			}
		}
	}
	return count;
}