Advertisement
marwanpro

demineur beta 2

Oct 30th, 2016
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.65 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <Grapic.h>
  7. #define CHMAX 255
  8.  
  9.  
  10. using namespace grapic;
  11.  
  12.  
  13. // Global Vars
  14. const int DIMX = 12;
  15. const int DIMY = 12;
  16. const int BOMB = 14;
  17. int glX = 0;
  18. int glY = 0;
  19. int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
  20.  
  21. // Grapic Vars
  22. const char title[CHMAX] = "Minesweeper++";
  23. const bool DEBUG = true;
  24. const int SIZE = 32;
  25. const int WINX = 20 * 2 + (DIMX - 2) * SIZE + DIMX;
  26. const int WINY = 80 + (DIMY - 2) * SIZE + DIMY;
  27. bool stop = false;
  28.  
  29. // Game Vars
  30. clock_t startTime = clock();
  31. int loop = 0;
  32. bool mgameOver = false;
  33. bool mWon = false;
  34.  
  35.  
  36.  
  37. // Struct of a case
  38. struct mCase
  39. {
  40.     int x;
  41.     int y;
  42.     int nearBomb = 0;
  43.     bool isBomb = false;
  44.     bool isVisible = true;
  45.     bool isDisplayed = false;
  46.     bool isFlagged = false;
  47.     bool isScanned = false;
  48.  
  49.     void init()
  50.     {
  51.         x = glX;
  52.         y = glY;
  53.         nearBomb = 0;
  54.         if (x == 0 || y == 0 || x == DIMX || y == DIMY) isVisible = false;
  55.         isFlagged = false;
  56.         isBomb = false;
  57.         isScanned = false;
  58.     }
  59. };
  60.  
  61.  
  62. // Init a double array of mCase
  63. void init(mCase table[DIMX][DIMY])
  64. {
  65.     // Just calling init
  66.     for (glX = 0; glX < DIMX; glX++)
  67.     {
  68.         for (glY = 0; glY < DIMY; glY++)
  69.         {
  70.             table[glX][glY].init();
  71.         }
  72.     }
  73.  
  74.     // Placing bombs
  75.     for (int i = 0; i < BOMB; i++)
  76.     {
  77.         int x = randInt(1, DIMX - 2);
  78.         int y = randInt(1, DIMY - 2);
  79.         if (table[x][y].isBomb == false && table[x][y].isVisible == true) table[x][y].isBomb = true;
  80.         else i--;
  81.     }
  82.  
  83.     // Count near bombs
  84.     for (glX = 1; glX < DIMX - 1; glX++)
  85.     {
  86.         //cout << "glX: " << glX;
  87.         for (glY = 1; glY < DIMY - 1; glY++)
  88.         {
  89.             //cout << ", glY: " << glY << endl;
  90.             for (int x = glX - 1; x <= glX + 1; x++)
  91.             {
  92.                 for (int y = glY - 1; y <= glY + 1; y++)
  93.                 {
  94.                     //cout << "Case [" << glX << "][" << glY << "] : Checking (" << x << "," << y << ")" << endl;
  95.                     if (table[x][y].isBomb == true)
  96.                     {
  97.                         table[glX][glY].nearBomb++;
  98.                         //cout << "BOMB" << endl;
  99.                     }
  100.  
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     // Params vars
  107.     mgameOver = false;
  108.     mWon = false;
  109. }
  110.  
  111.  
  112. // Recursive: Domino effect when 0 is hitten
  113. /*
  114. void recursive(mCase table[DIMX][DIMY])
  115. {
  116.     struct coord
  117.     {
  118.         bool active = false;
  119.         int x, y;
  120.     };
  121.     coord queue[10000];
  122.     int i = 0;
  123.     for (glX = 1; glX < DIMX - 1; glX++)
  124.     {
  125.         for (glY = 1; glY < DIMY - 1; glY++)
  126.         {
  127.             if (table[glX][glY].isDisplayed && !table[glX][glY].isFlagged && !table[glX][glY].isScanned && table[glX][glY].nearBomb == 0)
  128.             {
  129.                 queue[i].active = true;
  130.                 queue[i].x = glX;
  131.                 queue[i].y = glY;
  132.                 i++;
  133.             }
  134.         }
  135.     }
  136.     for (int j = 0; j < 10000; j++)
  137.     {
  138.         if (queue[j].active)
  139.         {
  140.             table[queue[j].x][queue[j].y].isScanned = true;
  141.             for (int x = queue[j].x - 1; x <= queue[j].x + 1; x++)
  142.             {
  143.                 for (int y = queue[j].y - 1; y <= queue[j].y + 1; y++)
  144.                 {
  145.                     if (table[x][y].isVisible)
  146.                     {
  147.                         table[x][y].isDisplayed = true;
  148.                         if (table[x][y].nearBomb == 0)
  149.                         {
  150.                             queue[i].active = true;
  151.                             queue[i].x = x;
  152.                             queue[i].y = y;
  153.                             i++;
  154.                         }
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.         queue[j].active = false;
  160.     }
  161. }
  162. */
  163.  
  164.  
  165. // Print in Console
  166. void mPrint(mCase table[DIMX][DIMY])
  167. {
  168.     std::cout << "To disable cheat, set DEBUG to false in Source Code" << std::endl << std::endl;
  169.     for (glX = 1; glX < DIMX - 1; glX++)
  170.     {
  171.         for (glY = 1; glY < DIMY - 1; glY++)
  172.         {
  173.             if (table[glX][glY].isBomb) std::cout << 'B';
  174.             else std::cout << table[glX][glY].nearBomb;
  175.             std::cout << " ";
  176.         }
  177.         std::cout << std::endl;
  178.     }
  179. }
  180.  
  181.  
  182. // Remaining Flags
  183. int mRemaining(mCase table[DIMX][DIMY])
  184. {
  185.     int remaining = BOMB;
  186.     for (glX = 1; glX < DIMX - 1; glX++)
  187.     {
  188.         for (glY = 1; glY < DIMY - 1; glY++)
  189.         {
  190.             if (table[glX][glY].isFlagged) remaining--;
  191.         }
  192.     }
  193.     return remaining;
  194. }
  195.  
  196.  
  197. // Game Over (On left click on a mine)
  198. void gameOver(mCase table[DIMX][DIMY])
  199. {
  200.     mgameOver = true;
  201.     for (glX = 1; glX < DIMX - 1; glX++)
  202.     {
  203.         for (glY = 1; glY < DIMY - 1; glY++)
  204.         {
  205.             if (table[glX][glY].isBomb) table[glY][glX].isDisplayed = true;
  206.         }
  207.     }
  208.     std::cout << "LOSE" << std::endl;
  209.     system("cls");
  210.     std::cout << "Vous avez fait explose une mine. Perdu :/" << std::endl;
  211. }
  212.  
  213.  
  214. // Check if the game is won
  215. void checkWin(mCase table[DIMX][DIMY])
  216. {
  217.     int tmp = (DIMX - 2)*(DIMY - 2) - BOMB;
  218.     for (glX = 1; glX < DIMX - 1; glX++)
  219.     {
  220.         for (glY = 1; glY < DIMY - 1; glY++)
  221.         {
  222.             if (table[glX][glY].isDisplayed) tmp--;
  223.         }
  224.     }
  225.     if (tmp == 0) mWon = true;
  226.     if (DEBUG)
  227.     {
  228.         std::cout << "Left Case: " << tmp << std::endl;
  229.         if (tmp == 0) std::cout << "WIN" << std::endl;
  230.     }
  231. }
  232.  
  233.  
  234. // End the game
  235. void finish()
  236. {
  237.     if (loop == 0)
  238.     {
  239.         loop++;
  240.         return;
  241.         if (mWon)
  242.         {
  243.  
  244.         }
  245.         if (mgameOver)
  246.         {
  247.  
  248.         }
  249.     }
  250.     system("pause");
  251.     winQuit();
  252.     exit(0);
  253. }
  254.  
  255.  
  256. // Display Case on Window
  257. void discover(mCase table[DIMX][DIMY], int x, int y)
  258. {
  259.     if (!table[x][y].isDisplayed && !table[x][y].isFlagged) return;
  260.    
  261.     int wX = x + SIZE * x;
  262.     int wY = 30 + -y + SIZE * (DIMY - 2 - y);
  263.  
  264.     if (table[x][y].isFlagged) {
  265.         color(255, 255, 255);
  266.         print(wX, wY, "F");
  267.     }
  268.     else if (table[y][x].isBomb) {
  269.         color(255, 0, 0);
  270.         print(wX, wY, "B");
  271.     }
  272.     else {
  273.         if (table[y][x].nearBomb == 0) color(0, 215, 0);
  274.         if (table[y][x].nearBomb == 1) color(200, 215, 0);
  275.         if (table[y][x].nearBomb == 2) color(250, 50, 30);
  276.         if (table[y][x].nearBomb == 3) color(250, 270, 20);
  277.         if (table[y][x].nearBomb >= 4) color(250, 0, 30);
  278.         print(wX, wY, table[y][x].nearBomb);
  279.     }
  280. }
  281.  
  282.  
  283. // Event Listener: Left Click
  284. void leftClick(mCase table[DIMX][DIMY])
  285. {
  286.     int x, y;
  287.     mousePos(x, y);
  288.     _sleep(150);
  289.  
  290.     // Check if coords match a case, then get case with MousePos
  291.     #pragma region casecalc
  292.     if (x < 20 || x > WINX - 20 || y < 20 || y > WINY - 60) return;
  293.     if ((x - 20) % (SIZE + 1) == 0 || (y - 20) % (SIZE + 1) == 0) return;
  294.  
  295.     int caseX = (x - 20) / (SIZE + 1) + 1;
  296.     int caseY = abs(((y - 20) / (SIZE + 1) + 1) - DIMY) - 1;
  297.     #pragma endregion
  298.  
  299.  
  300.     if (DEBUG)
  301.     {
  302.         std::cout << "Case(" << caseX << "," << caseY << ")" << std::endl;
  303.     }
  304.  
  305.     if (!table[caseX][caseY].isFlagged) table[caseX][caseY].isDisplayed = true;
  306.  
  307.     // Check if case is a Mine or not
  308.     if (!table[caseY][caseX].isFlagged && table[caseY][caseX].isBomb) gameOver(table);
  309.     else checkWin(table);
  310. }
  311.  
  312.  
  313. // Event Listener: Right Click
  314. void rightClick(mCase table[DIMX][DIMY])
  315. {
  316.     int x, y;
  317.     mousePos(x, y);
  318.     _sleep(150);
  319.  
  320.     // Check if coords match a case, then get case with MousePos
  321.     #pragma region casecalc
  322.     if (x < 20 || x > WINX - 20 || y < 20 || y > WINY - 60) return;
  323.     if ((x - 20) % (SIZE + 1) == 0 || (y - 20) % (SIZE + 1) == 0) return;
  324.    
  325.     int caseX = (x - 20) / (SIZE + 1) + 1;
  326.     int caseY = abs(((y - 20) / (SIZE + 1) + 1) - DIMY) - 1;
  327.     #pragma endregion
  328.  
  329.     if (DEBUG)
  330.     {
  331.         std::cout << "RC" << "Case(" << caseX << "," << caseY << ")" << std::endl;
  332.     }
  333.  
  334.     if (!table[caseX][caseY].isDisplayed && !table[caseX][caseY].isFlagged) table[caseX][caseY].isFlagged = true;
  335.     else if (!table[caseX][caseY].isDisplayed && table[caseX][caseY].isFlagged) table[caseX][caseY].isFlagged = false;
  336. }
  337.  
  338.  
  339. void grapicInit()
  340. {
  341.     backgroundColor(127, 127, 127);
  342. }
  343.  
  344.  
  345. void gridDrawer()
  346. {
  347.     for ( int vl = 20; vl < WINX - 20; vl += SIZE + 1 )
  348.     {
  349.         color(0, 0, 0);
  350.         line(vl, 20, vl, WINY - 62);
  351.     }
  352.    
  353.     for ( int hl = 20; hl < WINY - 60; hl += SIZE + 1 )
  354.     {
  355.         color(0, 0, 0);
  356.         line(20, hl, WINX - 22, hl);
  357.     }
  358.  
  359.     // Coord Tester
  360.     if (DEBUG)
  361.     {
  362.         int x, y;
  363.         mousePos(x, y);
  364.         color(0);
  365.         if ((x - 20) % (SIZE + 1) == 0 || (y - 20) % (SIZE + 1) == 0) color(255);
  366.         if (x < 20 || x > WINX - 20 || y < 20 || y > WINY - 60)  color(255);
  367.         print(WINX - 35, WINY - 24, x);
  368.         print(WINX - 35, WINY - 40, y);
  369.     }
  370. }
  371.  
  372.  
  373. void hud(mCase table[DIMX][DIMY])
  374. {
  375.     int remaining = mRemaining(table);
  376.     double elapsedTime = double(clock() - startTime) / CLOCKS_PER_SEC;
  377.  
  378.     color(255, 255, 255);
  379.     print(20, WINY - 32, "Time:");
  380.     print(80, WINY - 32, int(elapsedTime));
  381.     print(20, WINY - 55, "Remaining Flags:");
  382.     print(180, WINY - 55, remaining);
  383. }
  384.  
  385.  
  386. void draw(mCase table[DIMX][DIMY])
  387. {
  388.     gridDrawer();
  389.     if (isMousePressed(SDL_BUTTON_LEFT)) leftClick(table);
  390.     if (isMousePressed(SDL_BUTTON_RIGHT)) rightClick(table);
  391.     for (glX = 1; glX < DIMX - 1; glX++)
  392.     {
  393.         for (glY = 1; glY < DIMY - 1; glY++)
  394.         {
  395.             discover(table, glX, glY);
  396.         }
  397.     }
  398.     hud(table);
  399.     // recursive(table);
  400.     if (mgameOver || mWon) finish();
  401. }
  402.  
  403.  
  404. int main(int, char**)
  405. {
  406.     srand(time(NULL));
  407.     mCase table[DIMX][DIMY];
  408.     init(table);
  409.     mPrint(table);
  410.     winInit(title, WINX, WINY);
  411.     setKeyRepeatMode(true);
  412.     grapicInit();
  413.     while (!stop)
  414.     {
  415.         winClear();
  416.         draw(table);
  417.         stop = winDisplay();
  418.     }
  419.     winQuit();
  420.     //system("pause");
  421.     return 0;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement