RaZgRiZ

minesweeper in C++ progress 30/08/14

Aug 22nd, 2014
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <array>
  4.  
  5. #define MAX_Y  24
  6. #define MAX_X  30
  7.  
  8. #define MINE   -1
  9. #define NONE    0
  10. #define FLAG    1
  11. #define QMRK    2
  12. #define HIDDEN  0
  13. #define VISIBLE 1
  14.  
  15. using namespace std;
  16.  
  17. enum GameState {
  18.     WAITING,
  19.     RUNNING,
  20.     GAMEOVER,
  21.     VICTORY
  22. };
  23.  
  24. bool  g_bUsingLives  = true;
  25. char  g_chGameState  = WAITING;
  26.  
  27. short g_chBoard_H    =  9;
  28. short g_chBoard_W    =  9;
  29. short g_nBoard_M     = 10;
  30.  
  31. short g_chBoard_tH   =  9;
  32. short g_chBoard_tW   =  9;
  33. short g_nBoard_tM    = 10;
  34.  
  35. short g_nPercentageC = 0;
  36. short g_nTotalFlagsC = 0;
  37. char  g_chLivesLeftC = 0;
  38. char  g_chMinesHitC  = 0;
  39.  
  40. //int   g_nTimerStart  = 0;
  41. //int   g_nTimerStop   = 0;
  42.  
  43. class TileData {
  44. public:
  45.     TileData() {
  46.         State  = HIDDEN;
  47.         Type   = 0;
  48.         Marker = NONE;
  49.     };
  50.     char State;
  51.     char Type;
  52.     char Marker;
  53.     void SetAll(char s, char t, char m) {
  54.         switch (s) {
  55.             case 'D': State = HIDDEN; break;
  56.             case 'N': break;
  57.             default : State = s; break;
  58.         }
  59.         switch (t) {
  60.             case 'D': Type = 0; break;
  61.             case 'N': break;
  62.             default : Type = t; break;
  63.         }
  64.         switch (m) {
  65.             case 'D': Marker = NONE; break;
  66.             case 'N': break;
  67.             default : Marker = m; break;
  68.         }
  69.     }
  70. };
  71.  
  72. TileData* g_anBoard[MAX_Y][MAX_X];
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. // incomplete
  80. void InitBoard() {
  81.     for (short y = 0; y < MAX_Y; y++) {
  82.         for (short x = 0; x < MAX_X; x++) {
  83.             g_anBoard[y][x] = new TileData();
  84.         }
  85.     }
  86. }
  87.  
  88. void InitGame(signed char, signed char);
  89. void OpenTile(signed char y, signed char x, bool bAddPercentage, char s, char t, char m) {
  90.     if (g_chGameState == WAITING) InitGame(y, x);
  91.     g_anBoard[y][x]->SetAll(s, t, m);
  92.     if (bAddPercentage) g_nPercentageC++;
  93. }
  94.  
  95. void ReadTile(signed char y, signed char x) {
  96.     switch (g_anBoard[y][x]->Type) {
  97.         case MINE:
  98.             OpenTile(y, x, false, MINE, 'N', 'N');
  99.             g_chMinesHitC++;
  100.             if (g_bUsingLives && g_chLivesLeftC) g_chLivesLeftC--;
  101.             else {
  102.                 g_chGameState = GAMEOVER;
  103.                 // g_nTimerStop = curtime;
  104.                 //UpdateMarkers();
  105.             }
  106.             break;
  107.         case 0: // BLANK
  108.             OpenTile(y, x, true, VISIBLE, 'N', 'N');
  109.             // begin searching adjacent tiles to explore
  110.             for (char sY = -1; sY < 2; sY++) {
  111.                 if (y + sY < 0 || y + sY >= g_chBoard_H) continue; // check vertical boundaries
  112.                 for (char sX = -1; sX < 2; sX++) {
  113.                     if (x + sX < 0 || x + sX >= g_chBoard_W) continue; // check horizontal boundaries
  114.                     if (g_anBoard[y + sY][x + sX]->State != HIDDEN) continue; // skip if it's not HIDDEN
  115.                     if (g_anBoard[y + sY][x + sX]->Marker == FLAG) continue; // skip if it has FLAG
  116.                     ReadTile(y + sY, x + sX); // recourse to explore
  117.                 }
  118.             }
  119.             break;
  120.         default:
  121.             OpenTile(y, x, true, VISIBLE, 'N', 'N');
  122.             break;
  123.     }
  124.     if (g_nPercentageC == g_chBoard_H * g_chBoard_W - g_nBoard_M) {
  125.         g_chGameState = VICTORY;
  126.         // g_nTimerStop = curtime;
  127.         //UpdateMarkers();
  128.     }
  129. }
  130.  
  131. void UpdateMarkers() {
  132.     for (signed char y = 0; y < g_chBoard_H; y++) {
  133.         for (signed char x = 0; x < g_chBoard_W; x++) {
  134.             // incomplete
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. void InitGame(signed char y, signed char x) {
  141.     short nMinesLeftC = g_nBoard_M;
  142.     while (nMinesLeftC > 0) {
  143.         signed char rY = rand() % g_chBoard_H;
  144.         signed char rX = rand() % g_chBoard_W;
  145.         if (rY >= y - 1 && rY <= y + 1 && rX >= x - 1 && rX <= x + 1) continue; // skip if the coord is within a 3x3 rectangle
  146.         if (g_anBoard[rY][rX]->Type == MINE) continue; // skip if it's already a mine
  147.         g_anBoard[rY][rX]->Type = MINE;
  148.         nMinesLeftC--;
  149.     }
  150.     for (y = 0; y < g_chBoard_H; y++) {
  151.         for (x = 0; x < g_chBoard_W; x++) {
  152.             if (g_anBoard[y][x]->Type != MINE) continue;
  153.             for (signed char sY = -1; sY < 2; sY++) {
  154.                 if (y + sY < 0 || y + sY >= g_chBoard_H) continue; // check vertical boundaries
  155.                 for (signed char sX = -1; sX < 2; sX++) {
  156.                     if (x + sX < 0 || x + sX >= g_chBoard_W) continue; // check horizontal boundaries
  157.                     if (g_anBoard[y + sY][x + sX]->Type == MINE) continue; // skip if it's a MINE
  158.                     g_anBoard[y + sY][x + sX]->Type++; // up the adjacent tile's value
  159.                 }
  160.             }
  161.         }
  162.     }
  163.     g_chGameState = RUNNING;
  164.     // g_nTimerStart = curtime;
  165. }
  166.  
  167. int main() {
  168.     srand(time(nullptr));
  169.         for (short y = 0; y < MAX_Y; y++) {
  170.             for (short x = 0; x < MAX_X; x++) {
  171.                 g_anBoard[y][x] = new TileData();
  172.             }
  173.         }
  174.         // incomplete
  175.         for (short y = 0; y < MAX_Y; y++) {
  176.             for (short x = 0; x < MAX_X; x++) {
  177.                 delete g_anBoard[y][x];
  178.             }
  179.         }
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment