C++實(shí)現(xiàn)掃雷程序開(kāi)發(fā)
C++程序開(kāi)發(fā)實(shí)現(xiàn)掃雷游戲,供大家參考,具體內(nèi)容如下
//掃雷的類的定義
#pragma once
class Game{
public:
//開(kāi)始游戲
void play();
//退出游戲
int quit();
//游戲規(guī)則
void rule();
private:
//踩雷次數(shù),作為失敗條件
int error = 0;
//分?jǐn)?shù)
int score = 0;
//最高分記錄
int Rocord[5] = { 0,0,0,0,0 };
//地圖
int map[40][40];
//地圖的大小Size*Size
int Size = 10;
//容錯(cuò)
int fault_tolerant = 10;
//困難程度
int _difficulty=1;
//初始化
void reset();
//畫(huà)地圖
void drawGrid();
//查看格子的結(jié)果
void Cheak();
//判斷是否游戲結(jié)束
int isWin();
//導(dǎo)入最高分記錄
void get_Rocord();
//導(dǎo)出最高分記錄
void put_Rocord();
//選擇難度
int Selection_difficulty();
//加載界面
void loading();
};
然后是對(duì)類的函數(shù)的定義
//對(duì)Game類的成員函數(shù)的定義
#include "掃雷.h"
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<time.h>
#include <conio.h>
#pragma warning(disable:4996) //這一行是為了能在 Visual Studio 2017內(nèi)使用getch()函數(shù)
//定義最高分記錄的存儲(chǔ)地址
#define RocordPath "D:\\VS/掃雷最高分.txt"
using namespace std;
#define none "█"
//定義5種情況,有雷和無(wú)雷,查看后的三種結(jié)果
enum players { Boom, None, Boom1, None1, Show1 };
//定義三種游戲難度
enum _Difficulty{Easy,General,Difficulty,Purgatory};
int D_size[4][2] = { {10,10} ,{15,8},{20,5},{30,3} };
//游戲規(guī)則的描述
void Game::rule() {
loading();
//清屏
system("cls");
cout << "\n\n\n\n";
cout << "游戲規(guī)則:\n\n";
cout << "1.當(dāng)查看點(diǎn)為雷時(shí),會(huì)顯示“*”,并且將扣10分" << endl;
cout << "2.當(dāng)差看點(diǎn)不為雷且周圍均無(wú)雷將顯示周圍所以格為“ ”(周圍指的是相鄰的8個(gè)格子)" << endl;
cout << "3.當(dāng)查看點(diǎn)不為雷,且周圍存在雷時(shí),將顯示周圍雷數(shù)" << endl;
cout << "4.當(dāng)踩到最大容錯(cuò)個(gè)數(shù)雷時(shí)游戲?qū)⑹? << endl;
cout << "5.當(dāng)不存在未查閱的非雷格時(shí)游戲勝利" << endl;
cout << "\n\n\n\t\t\t\t30秒后自動(dòng)退出該界面!";
Sleep(30000);
}
//退出游戲
int Game::quit() {
system("cls");
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 40, 13 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "游戲結(jié)束!!!" << endl;
Sleep(1000);
loading();
return 0;
}
//游戲模式
void Game::play() {
//導(dǎo)入最高分記錄
get_Rocord();
while (true) {
//選擇游戲難度
_difficulty=Selection_difficulty();
//默認(rèn)游戲一直進(jìn)行
int res = 1;
//初始化
reset();
//
drawGrid();
while (true) {
//查看點(diǎn)
Cheak();
//
drawGrid();
if (!isWin()) {
if (score > Rocord[_difficulty])Rocord[_difficulty] = score;
put_Rocord();
char s;
cout << "是否再來(lái)一局?是(y|Y)/否(n|N)" << endl;
cin >> s;
if (s == 'y' || s == 'Y')res = 1;
else res = 0;
break;
}
}
if (!res)break;
}
}
//更新(初始化)
void Game::reset() {
//數(shù)據(jù)初始化
score = 0;
error = 0;
//棋盤(pán)初始化
srand(time(NULL));
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
int t = rand() % 2;
if (t==1)map[j][i] = Boom;
else map[j][i] = None;
//cout << t<< " ";
}
//cout << endl;
}
}
//畫(huà)地圖
void Game::drawGrid() {
//清屏
system("cls");
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 0, 2 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
//棋局初始狀態(tài)
for (int i = 0; i <= Size; i++) {
if (i < 10)
cout << i << " ";
else cout << i;
for (int j = 0; j < Size; j++) {
if (i == 0) {
if (j < 9)
cout << j + 1 << " ";
else cout << j + 1;
}
else cout << none;
}
cout << endl;
}
for (int y = 0; y < Size; y++) {
for (int x = 0; x < Size; x++) {
if (map[x][y] == Boom1|| map[x][y] == None1) {
//光標(biāo)位置坐標(biāo)
COORD c = { x * 2 + 2, 3 + y };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle函數(shù)獲得句柄
string o;
if (map[x][y] == Boom1) o = "* ";
if (map[x][y] == None1) o = " ";
cout << o;
}
if (map[x][y] == Show1) {
int cnt = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < Size && j >= 0 && j < Size) {
if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
}
}
}
//光標(biāo)位置坐標(biāo)
COORD c = { x*2+2, 3 + y };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle函數(shù)獲得句柄
cout << cnt << " ";
}
}
}
c.Y = Size+3;
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "當(dāng)前分?jǐn)?shù)是:"<<score<<"\n最高紀(jì)錄是:"<<Rocord[_difficulty]<<"\n請(qǐng)輸入查看格的坐標(biāo)" << endl;
}
//查看點(diǎn)結(jié)果
void Game::Cheak() {
int x = 0, y = 0;
cin >> x >> y;
x -= 1, y -= 1;
while(map[x][y] == Boom1 || map[x][y] == None1 || map[x][y] == Show1 || x < 0 || x >= Size || y < 0 || y >= Size) {
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 0, 2 };
c.Y = Size+6;
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "該格以檢查過(guò)或不在棋盤(pán)內(nèi),請(qǐng)重新輸入" << endl;
cin >> x >> y;
x -= 1, y -= 1;
}
if (map[x][y] == Boom) {
map[x][y] = Boom1;
score -= 10;
error++;
}
else {
score += 10;
int cnt = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < Size && j >= 0 && j < Size) {
if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
}
}
}
if (cnt == 0) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < Size && j >= 0 && j < Size) {
map[i][j] = None1;
}
}
}
}
else map[x][y] = Show1;
}
}
//判斷是否游戲結(jié)束
int Game::isWin() {
int cnt = 0;
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
if (map[i][j] == None)cnt++;
}
}
if (cnt == 0) {
system("cls");
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 50, 15 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "You Win!!!" << endl;
return 0;
}
else if (error >= fault_tolerant) {
system("cls");
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 50, 15 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "You Loss!!!" << endl;
return 0;
}
else return 1;
}
//導(dǎo)入最高分記錄
void Game::get_Rocord() {
ifstream fin(RocordPath, ios::in);
for (int i = 0; i < 5; i++) {
fin >> Rocord[i];
}
}
//導(dǎo)出最高分記錄
void Game::put_Rocord() {
ofstream fout(RocordPath, ios::out);
for(int i=0;i<5;i++)
fout << Rocord[i] << endl;
}
//選擇難度
int Game::Selection_difficulty() {
//清屏
system("cls");
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 0, 6 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "\t\t\t\t\t\t1.簡(jiǎn)單 (10*10格 10容錯(cuò))\n\n" << endl;
cout << "\t\t\t\t\t\t2.一般 (15*15格 8容錯(cuò))\n\n" << endl;
cout << "\t\t\t\t\t\t3.困難 (20*20格 5容錯(cuò))\n\n" << endl;
cout << "\t\t\t\t\t\t4.煉獄 (30*30格 3容錯(cuò))\n\n" << endl;
cout << "\t\t\t\t\t\t5.自定義\n\n" << endl;
cout << "\t\t\t\t\t\t請(qǐng)選擇游戲難度:";
int t = 1;
cin >> t;
while (t < 1 || t>5) {
COORD c = { 0, 21 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "\t\t\t\t\t\t輸入錯(cuò)誤請(qǐng)重新輸入:" << endl;;
cin >> t;
}
switch (t) {
case 1:Size = D_size[Easy][0], fault_tolerant = D_size[Easy][1]; break;
case 2:Size = D_size[General][0], fault_tolerant = D_size[General][1]; break;
case 3:Size = D_size[Difficulty][0], fault_tolerant = D_size[Difficulty][1]; break;
case 4:Size = D_size[Purgatory][0], fault_tolerant = D_size[Purgatory][1]; break;
case 5: {
//清屏
system("cls");
cout << "\n\n\n\n\n\t\t\t\t請(qǐng)輸入地圖尺碼和最多踩雷失敗數(shù) (尺碼在10-30,容錯(cuò)在10以內(nèi))";
cout << "\t\t\t\t\t\t\t\t\t尺碼:";
cin >> Size;
cout << "\n\t\t\t\t\t容錯(cuò):";
cin >> fault_tolerant;
}break;
}
loading();
return t;
}
void Game::loading() {
COORD c = { 50,15 };
//設(shè)置控制臺(tái)光標(biāo)位置
int t = 6;
while (t--) {
system("cls");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
if(t%3==0)
cout << "loading..." << endl;
if (t % 3 == 1)
cout << "loading.." << endl;
if (t % 3 == 2)
cout << "loading." << endl;
Sleep(500);
}
}
最后就是主函數(shù)部分
//掃雷游戲的主函數(shù)
#include<iostream>
#include<Windows.h>
#include"掃雷.h"
using namespace std;
int main() {
Game game;
while (true) {
int t, g = 1;
system("cls");
//定義控制臺(tái)屏幕初始坐標(biāo)
COORD c = { 30, 10 };
//設(shè)置控制臺(tái)光標(biāo)位置
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "歡迎來(lái)到 掃雷?。?!\n\n\n\n\n\n";
cout << "\t\t\t\t\t1.開(kāi)始游戲\n\n\n\t\t\t\t\t2.閱讀規(guī)則\n\n\n\t\t\t\t\t3.退出" << endl;
cin >> t;
switch (t) {
case 1:game.play(); break;
case 2:game.rule(); break;
case 3:g=game.quit(); break;
}
if (g == 0)break;
}
return 0;
}
這是第一次寫(xiě)博客 也是第一次獨(dú)立完成項(xiàng)目,有不足的地方,希望各位大牛指教。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
分享一下8年C++面向?qū)ο笤O(shè)計(jì)的經(jīng)驗(yàn)體會(huì)
關(guān)于C++程序設(shè)計(jì)的書(shū)藉非常多,本章不講C++的語(yǔ)法,只講一些小小的編程道理。如果我能早幾年明白這些小道理,就可以大大改善數(shù)十萬(wàn)行程序的質(zhì)量了2017-07-07
C/C++: Inline function, calloc 對(duì)比 malloc
以下是對(duì)c/c++中的malloc函數(shù)與calloc函數(shù)的區(qū)別以及它們之間的聯(lián)系進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下2016-07-07
在C++17中實(shí)現(xiàn)無(wú)鎖數(shù)據(jù)結(jié)構(gòu)的方法詳解
在探索?C++17?中的無(wú)鎖數(shù)據(jù)結(jié)構(gòu)之前,我們首先需要理解無(wú)鎖編程的基本概念及其在現(xiàn)代軟件開(kāi)發(fā)中的重要性,在這個(gè)章節(jié)中,我們將深入探討無(wú)鎖編程的概念,以及它如何滿足人類對(duì)于更高效、更可靠軟件的本能需求,文中通過(guò)代碼示例介紹的非常詳細(xì),感興趣的朋友可以參考下2023-12-12
C語(yǔ)言圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言圖書(shū)管理系統(tǒng)課程設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
使用C++和代理IP實(shí)現(xiàn)天氣預(yù)報(bào)的采集
在當(dāng)今的互聯(lián)網(wǎng)時(shí)代,網(wǎng)絡(luò)信息的獲取變得日益重要,天氣預(yù)報(bào)信息作為日常生活的重要參考,其獲取方式也隨著技術(shù)的發(fā)展而不斷變化,在本文中,我們將探討如何使用C++和代理IP來(lái)采集天氣預(yù)報(bào)信息,文中通過(guò)代碼講解的非常詳細(xì),需要的朋友可以參考下2023-12-12

