最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)掃雷游戲(控制臺(tái)不閃屏版)

 更新時(shí)間:2022年05月07日 11:44:35   作者:驚嘆號(hào)的云  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)掃雷游戲,控制臺(tái)不閃屏版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

之前寫(xiě)了一個(gè)C++ 的控制臺(tái)掃雷小游戲,但由于過(guò)度使用system("cls")刷屏,導(dǎo)致閃屏,因此重寫(xiě)了一個(gè)改善的不閃屏版本,并把邏輯重新捋了一遍。

map.h

#ifndef MAP_H_
#define MAP_H_
 
#define MAX_WID 18
#define MAX_LEN 32
#define UP_EDGE 1  //上邊界
#define LEFT_EDGE 1  //左邊界
#define RIGHT_EDGE _len //右邊界
#define DOWN_EDGE _wid //下邊界
 
struct Position {  //用于表示位置
 short x;
 short y;
};
 
struct MapInfo {  //表示掃雷圖的信息
 int n;    //-1表示地雷,0表示空格,1~8表示雷數(shù)
 bool flag;   //是否已經(jīng)被打開(kāi)
};
 
void gotoxy(short, short);  //光標(biāo)移動(dòng)函數(shù)
 
class Map {
private:
 int _len, _wid;    //圖的長(zhǎng)寬
 int _mines, _blanks;   //雷數(shù)和空格數(shù)
 Position pos;     //光標(biāo)位置
 MapInfo data[MAX_WID][MAX_LEN]; //地圖
 
public:
 void ChooseMode();    //選擇游戲模式,初級(jí),中級(jí),高級(jí)
 void Draw();     //畫(huà)出地圖
 void InitMap();     //初始化地圖信息
 void SetMine();     //設(shè)置地雷
 void SetNum();     //根據(jù)周?chē)乩讛?shù)計(jì)算數(shù)字
 void Move();     //負(fù)責(zé)移動(dòng)
 void OpenBlock();    //打開(kāi)方塊
 void OpenAll();     //如果觸雷則全部打開(kāi)
 void Play();     //提供游戲操作接口
 bool IfWin();     //判斷輸贏
 bool IfLose(); 
 // void show();
};
 
#endif

map類(lèi)的實(shí)現(xiàn)

map.cpp

#include "map.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>  //提供隨機(jī)函數(shù),rand(), srand()
#include <ctime>  //提供time()函數(shù)
#include <conio.h>  //提供不回顯的輸入函數(shù)getch()
#include <windows.h> //提供system()內(nèi)命令
 
#define GOTOXY( pos ) gotoxy( 2 * (pos).x - 1, (pos).y - 1 )
#define POSITION_POS _wid+1  //游戲信息的位置,這里是位置信息
#define POSITION_BLANKS _wid+2 //空格數(shù)位置
#define POSITION_TIMES _wid+3 //時(shí)間顯示位置
#define POSITION_SITUATION _wid+4 //輸贏狀態(tài)位置
 
using std::cin;
using std::cout;
 
void gotoxy(short x, short y) { //自行百度
 COORD pos = { x, y };
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleCursorPosition(hOut, pos);
}
 
 
void Map::ChooseMode() {
 system("cls");   //清屏
 cout << "Please choose the mode\n";   
 cout << "1 : Beginner\n";
 cout << "2 : Intermediate\n";
 cout << "3 : Expert\nYour mode: ";
 
 char mode;
 cin >> mode;
 while (mode != '1' && mode != '2' && mode != '3') { //只接受 1, 2, 3
  cout <<"\nWrong mode, please input 1, 2, or 3\nYour mode: ";
  cin >> mode;
 }
 switch (mode) { //根據(jù)模式改變地圖信息
  case '1': _len = _wid = 8; _mines = 10; break;
  case '2': _len = _wid = 16; _mines = 40; break;
  default : _len = 30; _wid = 16; _mines = 99; 
 }
 _blanks = _len * _wid - _mines; //更新空格數(shù)
}
 
 
void Map::Draw() { //畫(huà)出地圖
 system("cls");
 SetConsoleOutputCP(437);  //自行百度,否則無(wú)法顯現(xiàn)方塊而是顯現(xiàn)?
 for (int i = 1; i <= _wid; i++) {
  printf("|");
  for (int j = 1; j <= _len; j++)
   printf("%c", 219);  //219是方塊
  printf("|\n");
 }
 gotoxy(0, POSITION_POS);
 printf("Position: ( %2d, %2d )\n", pos.x, pos.y);
 printf("Blanks: %2d", _blanks);
 GOTOXY(pos);  //查看 map.h 內(nèi)說(shuō)明
}
 
 
void Map::InitMap() {
 for (int i = 0; i <= _wid+1; i++)  //從0 ~ _wid+1 是因?yàn)榭梢约僭O(shè)地圖邊界的空格存在,且為0,后面計(jì)算
  for (int j = 0; j <= _len+1; j++) {
   data[i][j].flag = false;  //設(shè)置為沒(méi)有被打開(kāi)
   data[i][j].n = 0;   //全部設(shè)為空格
  }
 pos.x = pos.y = 1;
}
 
 
void Map::SetMine() {
 int mines = _mines;
 int x, y;
 Move();  //先執(zhí)行move(),避免第一個(gè)空就觸雷
 srand(time(NULL));
 while (mines) { 
  x = rand() % _wid + 1;
  y = rand() % _len + 1;
  if (0 == data[x][y].n && (x != pos.x && y != pos.y)) { //后面的條件可以避免第一個(gè)打開(kāi)的空被設(shè)置為地雷,避免第一步就觸雷
   data[x][y].n = -1;  //雷 設(shè)為 -1
   data[x][y].flag = true; //設(shè)為雷的格子 flag 置為 true
   mines--;
  }
 }
}
 
 
void Map::SetNum() {
 for (int i = 1; i <= _wid; i++) {
  for (int j = 1; j <= _len; j++) { //逐個(gè)計(jì)算格子周?chē)?8 個(gè)格子的雷數(shù)
   if (-1 == data[ i ][ j ].n) continue;
   if (-1 == data[i-1][j-1].n) data[i][j].n++;
   if (-1 == data[i-1][ j ].n) data[i][j].n++;
   if (-1 == data[i-1][j+1].n) data[i][j].n++;
   if (-1 == data[ i ][j-1].n) data[i][j].n++;
   if (-1 == data[ i ][j+1].n) data[i][j].n++;
   if (-1 == data[i+1][j-1].n) data[i][j].n++;
   if (-1 == data[i+1][ j ].n) data[i][j].n++;
   if (-1 == data[i+1][j+1].n) data[i][j].n++;
  }
 }
 OpenBlock(); //與SetMine()配套,這時(shí)才正好打開(kāi)用戶(hù)要打開(kāi)的第一個(gè)空,避免第一步就觸雷
}
 
 
void Map::Move() {
 char mv;
 while (1) {
  mv = getch();
  if (mv == ' ') break; //如果是 ‘ '(空格),那么就結(jié)束移動(dòng),打開(kāi)方塊
  if (mv != 'w' && mv != 'a' && mv != 's' && mv != 'd') continue; //移動(dòng)只接受 w a s d 四個(gè)鍵
  switch (mv) {
   case 'w': 
    if (pos.y != UP_EDGE) pos.y--;
    break;
   case 's':
    if (pos.y != DOWN_EDGE) pos.y++;
    break;
   case 'a':
    if (pos.x != LEFT_EDGE) pos.x--;
    break;
   default :
    if (pos.x != RIGHT_EDGE) pos.x++;
  }
  gotoxy(12, POSITION_POS); //12,可以不用重新輸入覆蓋已經(jīng)存在的 "Position: ",而是接著輸入
  printf("%2d, %2d", pos.x, pos.y);
  GOTOXY(pos); //回到用戶(hù)所指的位置
 }
}
 
 
#define IF_IN_EDGE(p) ((p.x >= LEFT_EDGE && p.x <= RIGHT_EDGE) && (p.y >= UP_EDGE && p.y <= DOWN_EDGE)) //判斷是否越界
#define IF_PUSHIN_STACK(p) (data[p.y][p.x].flag == false && IF_IN_EDGE(p))   //判斷是否入棧,條件是:不越界且格子未被打開(kāi)
#define PUSHIN_STACK(p) { stack[++top] = p; data[p.y][p.x].flag = true; _blanks--; } //入棧,并設(shè)置為已打開(kāi),并減少空格數(shù),用于定是否獲勝
 
void Map::OpenBlock() {
 if (data[pos.y][pos.x].flag == true) return ; //如果格子打開(kāi)過(guò),就跳出函數(shù)
 int num, top = 0;
 Position stack[_len * _wid << 1]; //棧,用于存位置
 Position temp;
 stack[top] = pos;
 data[pos.y][pos.x].flag = true; //要打開(kāi)的第一個(gè)格子設(shè)置為打開(kāi)
 _blanks--; 
 while (top != -1) {
  temp = stack[top--];
  GOTOXY(temp);
  num = data[temp.y][temp.x].n;
  if (0 == num) {
   printf(" ");   //如果是0,那么輸出空格,并且判斷一下周?chē)?個(gè)是否要打開(kāi),如果不是地雷就打開(kāi)
   temp.y--; temp.x--;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp) //格子周?chē)?個(gè)都要判斷
   temp.x++;          //因?yàn)榭崭裰車(chē)亩家蜷_(kāi)
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
   temp.x++;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
   temp.y++;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
   temp.y++;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
   temp.x--;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
   temp.x--;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
   temp.y--;
   if (IF_PUSHIN_STACK(temp)) PUSHIN_STACK(temp)
  }
  else {
   printf("%d ", num); //不是0,也即不是空格,不存在連開(kāi)
  }
 }
 gotoxy(8, POSITION_BLANKS); //更新空格數(shù)
 printf("%2d", _blanks);
 GOTOXY(pos); //回到用戶(hù)所指的位置
}
 
 
void Map::OpenAll() {
 SetConsoleOutputCP(437);
 gotoxy(0,0);
 for (int i = 1; i <= _wid; i++) {
  printf("|");
  for (int j = 1; j <= _len; j++) {
   switch (data[i][j].n) {
    case 0 : printf("%c", 219); break;
    case -1: printf("* "); break;
    default: printf("%d ", data[i][j].n);
   }
  } printf("|\n");
 }
 GOTOXY(pos);
 printf("X");
}
 
 
void Map::Play() {
 char op;
 float end, start;
 
 start = clock(); //計(jì)時(shí)用
 while (!IfWin()) { //如果還未獲勝
  Move(); //當(dāng) Move() 跳出即代表用戶(hù)輸入空格
  if (IfLose()) { OpenAll(); break; } //如果觸雷則跳出,并打開(kāi)全圖
  OpenBlock(); //開(kāi)格子
 }
 end = clock(); //計(jì)時(shí)用
 gotoxy(0, POSITION_TIMES);
 printf("Times: %.2f s\n\n", (end-start)/CLK_TCK);
}
 
 
bool Map::IfWin() {
 return _blanks == 0;
}
 
 
bool Map::IfLose() {
 return -1 == data[pos.y][pos.x].n;
}

主函數(shù)

mineweeper.cpp

#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include "map.h"
 
int main() {
 Map game;
 char ch;
 while (1) {
  game.ChooseMode(); //模式選擇
  game.InitMap();  //初始化
  game.Draw();   //畫(huà)出地圖
  game.SetMine();  //布置地雷
  game.SetNum();  //計(jì)算數(shù)字
  game.Play();   //掃雷
  if (game.IfWin())  //判定輸贏
   printf("You Win\n");
  else
   printf("You Lose\n");
  printf("\nInput q to quit or c to continue : "); //是否繼續(xù)
  ch = getch();
  while (ch != 'q' && ch != 'c') {
   ch = getch();
  }
  if (ch == 'q') break;
 }
 system("cls");
 printf("~Bye~\n\n");
 system("pause");
 return 0;

游戲截圖

更多精彩游戲小代碼,請(qǐng)點(diǎn)擊《游戲?qū)n}》閱讀

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++和java設(shè)計(jì)模式之單例模式

    C++和java設(shè)計(jì)模式之單例模式

    這篇文章主要為大家詳細(xì)介紹了C++和java設(shè)計(jì)模式之單例模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • C語(yǔ)言算法積累圖的遍歷鄰接表簡(jiǎn)單路徑

    C語(yǔ)言算法積累圖的遍歷鄰接表簡(jiǎn)單路徑

    這篇文章主要為大家介紹了C語(yǔ)言算法積累圖的遍歷鄰接表簡(jiǎn)單路徑實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • c++實(shí)現(xiàn)二叉查找樹(shù)示例

    c++實(shí)現(xiàn)二叉查找樹(shù)示例

    這篇文章主要介紹了c++實(shí)現(xiàn)二叉查找樹(shù)示例,實(shí)現(xiàn)二叉查找樹(shù)的基本功能,需要的朋友可以參考下
    2014-02-02
  • vsCode配置import@路徑提示的實(shí)現(xiàn)步驟

    vsCode配置import@路徑提示的實(shí)現(xiàn)步驟

    在導(dǎo)入文件設(shè)置路徑的時(shí)候方便了很多,本文主要介紹了vsCode配置import@路徑提示的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • C語(yǔ)言中組成不重復(fù)的三位數(shù)問(wèn)題

    C語(yǔ)言中組成不重復(fù)的三位數(shù)問(wèn)題

    這篇文章主要介紹了C語(yǔ)言中組成不重復(fù)的三位數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 使用C++制作簡(jiǎn)單的web服務(wù)器

    使用C++制作簡(jiǎn)單的web服務(wù)器

    本文給大家分享的是使用C++簡(jiǎn)單實(shí)現(xiàn)web服務(wù)器的代碼,雖然非常的簡(jiǎn)陋,功能也很少,主要是為了更好的理解WEB服務(wù)器的工作原理,推薦給大家,也希望對(duì)大家能夠有所幫助。
    2015-03-03
  • 冒泡排序的三種實(shí)現(xiàn)方法

    冒泡排序的三種實(shí)現(xiàn)方法

    本篇文章是對(duì)冒泡排序的三種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下。希望對(duì)大家有所幫助
    2013-10-10
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單電子通訊錄(2)

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單電子通訊錄(2)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單電子通訊錄的第二部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))

    C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 利用C語(yǔ)言的Cairo圖形庫(kù)繪制太極圖實(shí)例教程

    利用C語(yǔ)言的Cairo圖形庫(kù)繪制太極圖實(shí)例教程

    這幾天都在研究Cairo圖形庫(kù),這是一個(gè)開(kāi)源跨平臺(tái)的圖形庫(kù),相對(duì)于OpenGL來(lái)說(shuō)更容易上手使用。這篇文章是利用C語(yǔ)言的Cairo圖形庫(kù)繪制了一個(gè)太極圖,對(duì)大家學(xué)習(xí)Cairo圖形庫(kù)具有一定的參考借鑒價(jià)值,下面來(lái)一起看看吧。
    2016-12-12

最新評(píng)論

长治县| 集安市| 望江县| 都昌县| 澳门| 安龙县| 库伦旗| 千阳县| 清水河县| 长泰县| 高雄县| 青冈县| 云林县| 文山县| 交口县| 博乐市| 巩留县| 竹溪县| 金华市| 河津市| 治多县| 马山县| 宿州市| 孙吴县| 姚安县| 五华县| 建德市| 大名县| 公安县| 共和县| 浦县| 鄂托克旗| 赫章县| 昌黎县| 菏泽市| 佛学| 思南县| 稷山县| 宁国市| 潮州市| 吕梁市|