C + + Replay Classic game-Minesweeper

Source: Internet
Author: User

National Day small holiday, when everyone to see the people, I am alone crazy code. What you want to accomplish these two days is a classic game on Windows-Minesweeper. Believe that the new and some of the office workers are not unfamiliar. However, from Win8, minesweeper is no longer the default self-bring game on windows, but it can be downloaded and installed via Microsoft's App Store (the interface is more cool and the game mode is richer). The game is still not found on win10.

These two days again with the QT Basic graphical interface framework, to achieve the function of minesweeper game. I want to do cool cool and cool, but really write up, only to find that there are still a lot of technical pass, so the interface is ugly. This process realizes the basic game function, including random mine, blank block automatic expansion, winning and losing judgment and so on.

The program interface is as follows:

Game interface

The main part of the whole minesweeper game is how to expand when you click into a blank block. Before I write the program, also on the Internet to check the mine-related things, found that mine clearance procedures have been written rotten, many people have been reproduced, graduation design, curriculum design and so on. In other words, minesweeper is really suitable for practiced hand, and the entire program function is not particularly complex.

Here's what I think of the automatic extension algorithm for points to blank blocks.

(1) Click to blank block

(2) Calculate the number of a circle of thunder around the blank block. If zero, open the block and jump to (3). If it is not zero, open and show the number of thunder, jump to (4).

(3) Repeat the second step for the surrounding eight blocks.

(4) End

The above four steps is the blank block automatic expansion algorithm, is not very simple. But this thing was a long time since I started it. When writing a program to implement the algorithm, there is a point to pay attention to the place, is to open the block need to make a mark. For the third step, the block that has been opened does not need to perform step (2), otherwise it may enter the dead loop. In addition, the algorithm can be implemented using either recursive or non-recursive implementations.

Here is my specific implementation code:

1 /*2 * Statistics (x, y) of a circle of thunder around the number3  */4 intMainwindow::summine (intXinty)5 {6     intminenum=0;7     if(X-1>=0&&y-1>=0&&map[x-1][y-1]==1) minenum++;8     if(X-1>=0&&map[x-1][y]==1) minenum++;9     if(X-1>=0&&y+1<mapcols&&map[x-1][y+1]==1) minenum++;Ten     if(Y1>=0&&map[x][y-1]==1) minenum++; One     if(y+1<mapcols&&map[x][y+1]==1) minenum++; A     if(x+1<maprows&&y-1>=0&&map[x+1][y-1]==1) minenum++; -     if(x+1<maprows&&map[x+1][y]==1) minenum++; -     if(x+1<maprows&&y+1<mapcols&&map[x+1][y+1]==1) minenum++; the     returnMinenum; -  - } -  + /* - * Extended from (x, y) +  */ A voidMainwindow::expendblock (intXinty) at { -     intMinearound=summine (x, y); -     if(minearound!=0){ -         //displays the number of mines (x, Y) and then ends the recursion -Qtablewidgetitem * temp=NewQtablewidgetitem; -Temp->settext (Qstring::number (Minearound,Ten)); inTemp->settextalignment (qt::aligncenter); -Temp->setbackgroundcolor (Qcolor (251,246,246)); toMapui->SetItem (x,y,temp); +mapflag[x][y]=1; -         return ; the     } *     //Turn over (x, y) and recursively from the surrounding eight directions to determine the bounds $ Panax NotoginsengList<point>expendlist; - Point point ; thepoint.x=x; +point.y=y; A Expendlist.push_back (point); the      while(!Expendlist.empty ()) { +Point=Expendlist.front (); - Expendlist.pop_front (); $x=Point.x; $y=Point.y; -Minearound=summine (x, y); -         if(minearound!=0){ theQtablewidgetitem * temp=NewQtablewidgetitem; -Temp->settext (Qstring::number (Minearound,Ten));WuyiTemp->settextalignment (qt::aligncenter); theTemp->setbackgroundcolor (Qcolor (251,246,246)); -Mapui->SetItem (x,y,temp); Wumapflag[x][y]=1; -             Continue; About         } $Qtablewidgetitem * temp1=NewQtablewidgetitem; -Temp1->setbackgroundcolor (Qcolor (251,246,246)); -Temp1->settextalignment (qt::aligncenter); -Mapui->SetItem (X,Y,TEMP1); Amapflag[x][y]=1; +         if(X-1>=0&&y-1>=0&&mapflag[x-1][y-1]==0){ thepoint.x=x-1; -point.y=y-1; $  the Expendlist.push_back (point); the         } the         if(X-1>=0&&mapflag[x-1][y]==0){ thepoint.x=x-1; -point.y=y; in  the Expendlist.push_back (point); the         } About         if(X-1>=0&&y+1<mapcols&&mapflag[x-1][y+1]==0){ thepoint.x=x-1; thepoint.y=y+1; the  + Expendlist.push_back (point); -         } the         if(Y1>=0&&mapflag[x][y-1]==0){Bayipoint.x=x; thepoint.y=y-1; the  - Expendlist.push_back (point); -         } the         if(y+1<mapcols&&mapflag[x][y+1]==0){ thepoint.x=x; thepoint.y=y+1; the  - Expendlist.push_back (point); the         } the         if(x+1<maprows&&y-1>=0&&mapflag[x+1][y-1]==0){ thepoint.x=x+1;94point.y=y-1; the  the Expendlist.push_back (point); the         }98         if(x+1<maprows&&mapflag[x+1][y]==0){ Aboutpoint.x=x+1; -point.y=y;101 102 Expendlist.push_back (point);103         }104         if(x+1<maprows&&y+1<mapcols&&mapflag[x+1][y+1]==0){ thepoint.x=x+1;106point.y=y+1;107 Expendlist.push_back (point);108         }109     } the 111}

Where map[][] holds the map information. Mapflag[][] is a map flag, when the block has been opened is set to 1, otherwise 0.

  

C + + Replay Classic game-Minesweeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.