Console games: MineSweeper and minesweeper

Source: Internet
Author: User

Console games: MineSweeper and minesweeper

Console clearance game.

Material: EasyX graphics library

1 # include <set> 2 # include <array> 3 # include <string> 4 # include <time. h> 5 # include <graphics. h> 6 7 using namespace std; 8 # define Num 20 // number of small squares Num * Num // rand () so that the Num value is too small to enter the endless loop 9 # define Size 25 // the Size of each small square 10 using UInt16 = unsigned short int; 11 using TheMap = array <UInt16, Num + 2>, num + 2>; // map 12 # define Trap (UInt16) 0X0000 // mine 13 # define InitMap (UInt16) 0X0001 // indicates no mines, each time X2 represents the number of surrounding mines + 1 14 # Define Asked (UInt16) 0 XFFFF // The flag has been Asked 15 16 void Init (TheMap & Map); // generate Map and mine, draw window 17 bool MainLoop (TheMap & Map); // game loop 18 void DisplayEmpty (TheMap & Map, int I, int j ); // draw 19 void DisplayNum (TheMap & Map, int I, int j) for the prompt area 0; // draw 20 int DisplayFlag (TheMap & Map, int I, int j); // plot the flag 21 22 int main () {23 auto hwnd = initgraph (Num * Size, Num * Size ); 24 // game implementation 25 TheMap Map; 26 Init (Map); 27 if (MainLoop (Map) 28 MessageBox (hwnd, L "Win", L "Mine Clerance", MB_ OK); 29 else 30 MessageBox (hwnd, L "Defeated", L "Mine Clerance", MB_ OK); 31 // leave game 32 closegraph (); 33 return 0; 34} 35 void Init (TheMap & Map) {36 // initialize Map 37 for (auto & It: Map) 38 It. assign (InitMap); 39 // randomly generated mine 40 srand (unsigned) time (NULL); 41 for (UInt16 k = 0; k <2 * Num ;) {42 auto I = rand () % Num + 1; 43 auto J = rand () % Num + 1; 44 if (Map [I] [j] = InitMap) {45 Map [I] [j] = Trap; 46 for (int m =-1; m <= 1; ++ m) 47 for (int n =-1; n <= 1; ++ n) 48 Map [I + m] [j + n] <= 1; 49 + + k; 50} 51} 52 // value assignment Asked, prevent out-of-bounds 53 for (size_t I = 0; I <Num + 2; ++ I) {54 Map [I] [0] = Asked; 55 Map [I] [Num + 1] = Asked; 56 Map [0] [I] = Asked; 57 Map [Num + 1] [I] = Asked; 58} 59 // draw a visual map 60 setlinecolor (LIGHTGREEN); 61 (Size_t I = Size; I <Size * Num; I + = Size) {62 line (I, 0, I, Size * Num); 63 line (0, I, size * Num, I); 64} 65} 66 bool MainLoop (TheMap & Map) {67 int Ret = 0; 68 MOUSEMSG Point; 69 while (Ret! = 1) {70 Point = GetMouseMsg (); 71 if (Point. mkLButton) {// left mouse button -- open the small square 72 Point = GetMouseMsg (); 73 size_t I = Point. x/Size + 1; 74 size_t j = Point. y/Size + 1; 75 switch (Map [I] [j]) {76 case Trap: // press to mine 77 for (size_t I = 1; I <Num + 1; ++ I) 78 for (size_t j = 1; j <Num + 1; ++ j) 79 if (Map [I] [j] = Trap) 80 outtextxy (I * Size-Size/2, j * Size-3 * Size/4, L "X"); 81 return false; 82 c Ase InitMap: // press 0 83 DisplayEmpty (Map, I, j); 84 break; 85 case InitMap <1: 86 case InitMap <2: 87 case InitMap <3: 88 case InitMap <4: 89 case InitMap <5: 90 case InitMap <6: 91 case InitMap <7: 92 case InitMap <8: 93 DisplayNum (Map, I, j); // press 1-8 94 break; 95} 96} 97 else if (Point. mkRButton) {// right-click and Mark 98 Point = GetMouseMsg (); 99 auto I = Point. x/Size + 1; 100 auto j = P Oint. y/Size + 1; 101 Ret = DisplayFlag (Map, I, j); 102 if (Ret =-1) {103 DisplayFlag (Map, I, j ); 104 MessageBox (GetHWnd (), L "NO More Mark", L "MineSweeper", MB_ OK); 105} 106} 107 return true; 109} 110 void DisplayEmpty (TheMap & Map, int I, int j) {111 // recursive 112 for (int m = I-1; m <= I + 1; ++ m) 113 for (int n = j-1; n <= j + 1; ++ n) {114 if (Map [m] [n] = InitMap) {115 Map [m] [n] = Asked; 1 16 outtextxy (m * Size-Size/2, n * Size-3 * Size/4, wstring (to_wstring (0 )). c_str (); 117 DisplayEmpty (Map, m, n); 118} 119 else if (Map [m] [n]! = Asked) 120 DisplayNum (Map, m, n); 121} 122 void DisplayNum (TheMap & Map, int I, int j) {123 UInt16 num = 8; 125 switch (Map [I] [j]) {126 case 2: -- num; 127 case 4: -- num; 128 case 8: -- num; 129 case 16: -- num; 130 case 32: -- num; 131 case 64: -- num; 132 case 128: -- num; 133 case 256: num; 134 Map [I] [j] = Asked; 135 outtextxy (I * Size-Size/2, j * Size-3 * Size/4, wstring (to_wstring (num )). c_str (); 136 break; 137} 138} 139 int DisplayFlag (TheMap & Map, int I, int j) {140 static UInt16 TrueFlags = 0; // define the number of mines correctly marked 141 static UInt16 FlagTimes = 0; // define the number of mines marked by the user 142 static set <UInt16> Record; // define the container, used to record the flag point 143 if (Map [I] [j] = Asked) // do not operate on the accessed point 144 return 0; 145 UInt16 ret = I <8 | j; // applicable: Map subscript maximum 0XFFFF146 auto Iter = Record. insert (ret); 147 if (Iter. second) {// mark 148 outtextxy (I * Size-Size/2, j * Size-3 * Size/4, L "\? "); 149 if (Map [I] [j] = Trap) 150 + + TrueFlags; 151 + FlagTimes; 152} 153 else {// clear mark 154 Record. erase (ret); 155 setfillcolor (BLACK); 156 fillrectangle (I * Size-Size, j * Size-Size, I * Size, j * Size ); 157 if (Map [I] [j] = Trap) 158 -- TrueFlags; 159 -- FlagTimes; 160} 161 if (TrueFlags = 2 * Num) 162 return 1; 163 if (FlagTimes> 2 * Num) // control the number of tags
164 return-1; 165 return 0; 166}

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.