欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實現(xiàn)掃雷游戲(控制臺版)

 更新時間:2022年05月07日 11:43:27   作者:yangshuolll  
這篇文章主要為大家詳細介紹了C++實現(xiàn)掃雷游戲,控制臺版的掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內容如下

需要開一個map.txt  寫入地圖

地圖中 *表示空地   ; x表示地雷

**********
**********
**x*******
**********
**********
**********
**********
**********
**********

然后就是掃雷的控制臺代碼了,只簡單的檢測了一下

#include <stdio.h>
#include <string.h>
#define SIZE 10
char img_map[SIZE + 2][SIZE + 2]; // the image of a map
int num_map[SIZE + 2][SIZE + 2]; // calculate the total number of mine in one block.
int open_map[SIZE + 2][SIZE + 2]; // which img shoud user open.
int sumMine = 0;
int sumBlock = 0;
int dir[8][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
int beyond_board (int x,int y){ // judge whether the step is out of board;
 if( x < 0 || x >= SIZE || y < 0 || y >= SIZE ){
 return 1; // beyond board return 1;
 }
 return 0;
}
void read_img_map(){  // get data from map.txt
 FILE *p_file = fopen("map.txt","r");
 int i = 0,j;
 for (i = 0;i < SIZE;i++){
 fread(img_map[i],sizeof(char),SIZE+1,p_file);
 }
/* for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if(img_map[i][j] == '*'){
 img_map[i][j] = ' ';
 }
 }
 }
*/
}
 
void write_num_map(){  // transfer img_map to num_map
 int i = 0,j = 0,k = 0;
 for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if (img_map[i][j] == 'x'){
 sumMine++;  // the total number of mine in the map
 num_map[i][j] = 9; // 9 represent a mine here
 continue;
 }
 for (k = 0;k < 8;k++){
 int stepx = i + dir[k][0],stepy = j + dir[k][1];
 if ( !beyond_board (stepx,stepy) ){
 if (img_map[stepx][stepy] == 'x'){
  num_map[i][j] += 1;
 }
 }
 } 
 }
 }
/* for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
  printf("%d",num_map[i][j]);
 }
 printf("\n");
 } 
 */
}
void show_all_mine(){ // 在地圖中顯示所有的地雷的位置
 int i,j;
 for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if (num_map[i][j] == 9) { 
 open_map[i][j] = 1; // 找到地雷后在 openmap 中標記 
 }
 }
 }
}
void show_all_map(){
 int i,j;
 for (i = 0;i < SIZE;i++){
 for (j = 0;j < SIZE;j++){
 if(open_map[i][j]){
 if(num_map[i][j] == 9){ 
 printf("X"); // x represetn mine
 }
 else{
  printf("%d",num_map[i][j]); // show the number has been opened
 }
 }
 else{
  printf("*");  // the block is coverd;
 }
 }
 printf("\n");
 }
}
void find_empty(int x,int y){  //搜索算法
 // printf("x = %d y = %d\n",x,y);
 // show_all_map();
 if (beyond_board(x,y)){
 return ;
 }
 if (open_map[x][y]){
 return ; 
 }
 if (!num_map[x][y]){ // 遇到零時還要繼續(xù)翻上下左右
 open_map[x][y] = 1;
 }else if(num_map[x][y] != 9){ // 遇到數(shù)字了即搜索停止
 open_map[x][y] = 1;
 return ;
 }
 //else {  //****遇到雷時搜索停止
 // return ;
 // }
 int i;
 for (i = 0 ;i < 8;i++){
 find_empty(x + dir[i][0],y + dir[i][1]);
 }
 
}
int sum_one_open_map(){
 int i,j;
 int s = 0;
 for (i = 0;i < SIZE;i++){
 for ( j = 0;j < SIZE;j++)
 if (open_map[i][j]){
 s++;
 }
 } 
 return s;
}
int main()
{
 read_img_map();
 write_num_map();
 show_all_map();
 memset(open_map,0,sizeof(open_map)); // reset the open_map.
 int x,y; // empty = 0 , mine = 9, number = others
 sumBlock = SIZE * SIZE - sumMine;
 int sum = 0;
 while(sumBlock != sum){
 printf("please input the postion x,y: ");
 scanf("%d %d",&x,&y);
 scanf("%*[^\n]"); //clean the buffer
 scanf("%*c");
 x--;
 y--;
 if(beyond_board(x,y)){ // the position is beyond the board
 printf("beyond the board and please input the position again:");
 continue;
 }
 if(!num_map[x][y]){ //is empty
  find_empty(x,y); 
  show_all_map(x,y); 
 }else if(num_map[x][y] == 9){ // is mine
 show_all_mine();
 show_all_map();
 break;
 }else{    // is number
 open_map[x][y] = 1;
 show_all_map();
 }
 sum = sum_one_open_map();
 }
 if (sum==sumBlock) printf("YOU WIN! \n");
 else {
 printf("YOU LOSE!\n");
 }
 
 return 0;
}

 

運行截圖:

更多精彩游戲小代碼,請點擊《游戲專題》閱讀

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C++?QT?QThread啟動、停止、暫停和恢復的實現(xiàn)

    C++?QT?QThread啟動、停止、暫停和恢復的實現(xiàn)

    本文主要介紹了C++?QT?QThread啟動、停止、暫停和恢復的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • 淺談 C++17 里的 Visitor 模式

    淺談 C++17 里的 Visitor 模式

    Visitor模式經(jīng)常用于將更新的設計封裝在一個類中,并且由待更改的類提供一個接受接口,其關鍵技術在于雙分派技術,本文主要介紹 C++17 里的 Visitor 模式的相關資料,需要的朋友可以參考下面文章的具體內容
    2021-09-09
  • OpenCV實現(xiàn)透視變換矯正

    OpenCV實現(xiàn)透視變換矯正

    這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)透視變換矯正,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語言趣味編程之平分七筐魚

    C語言趣味編程之平分七筐魚

    這篇文章介紹了C語言趣味編程之平分七筐魚,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-11-11
  • C語言楊氏矩陣實例教你編寫

    C語言楊氏矩陣實例教你編寫

    楊氏矩陣是一個數(shù)字矩陣,矩陣的每一行從左到右一次遞增,矩陣從上到下遞增,在這樣的矩陣中查找一個數(shù)字是否存在。時間復雜度小于O(N),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2023-02-02
  • C++內存泄漏及檢測工具詳解

    C++內存泄漏及檢測工具詳解

    最簡單的方法當然是借助于專業(yè)的檢測工具,比較有名如BoundsCheck,功能非常強大,相信做C++開發(fā)的人都離不開它。此外就是不使用任何工具,而是自己來實現(xiàn)對內存泄露的監(jiān)控
    2013-10-10
  • C語言實現(xiàn)猜數(shù)字小游戲

    C語言實現(xiàn)猜數(shù)字小游戲

    這篇文章主要介紹了C語言實現(xiàn)猜數(shù)字小游戲,附有詳細代碼,需要的小伙伴可以參考一下,希望對你的遼西有所幫助
    2021-10-10
  • C++ std::bind用法詳解

    C++ std::bind用法詳解

    這篇文章主要介紹了C++ std::bind用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09
  • Qt繪制簡單時鐘

    Qt繪制簡單時鐘

    這篇文章主要為大家詳細介紹了Qt繪制簡單時鐘效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C語言初識變量常量字符串轉義符及注釋方式簡介

    C語言初識變量常量字符串轉義符及注釋方式簡介

    最強的C語言筆記,此處對于C語言的基礎部分做一個簡要的介紹,作者實屬初學,寫博客也是作者學習的一個過程,若文中內容有理解不到位或者有不當之處,還請朋友們不吝指正
    2021-11-11

最新評論