PowerSlaveAlfons

Untitled

Oct 24th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6.  
  7. void InitGame(void);
  8. void PlaceBomb(void);
  9. int Count(int, int);
  10. int GetMine(int, int);
  11. void DrawField(void);
  12. void InitChar(void);
  13. void RestartGame(void);
  14.  
  15. int Mines[9][9] = {{0}};
  16. char Draw[9][9] = {{0}};
  17. int TotalMines = 0;
  18. int HiddenCells = 0;
  19. int MarkedCells = 0;
  20. int RowIn = 0;
  21. int ColIn = 0;
  22. char Mark = 0;
  23. int Win = 0;
  24.  
  25. int main()
  26. {
  27.  
  28.     srand(time(NULL)); //inits the RNG
  29.  
  30.     InitGame();
  31.  
  32.     while (69)
  33.     {
  34.  
  35.         printf("Bombs: %d Marked: %d\n", TotalMines, MarkedCells);
  36.         printf("  1 2 3 4 5 6 7 8 9\n");
  37.         DrawField();
  38.  
  39.         printf("Your move: ");
  40.         scanf("%1d%1d%1c", &RowIn, &ColIn, &Mark);
  41.         RowIn--;
  42.         ColIn--;
  43.         if (Mark == '-')
  44.         {
  45.             if (Draw[RowIn][ColIn] == 'B')
  46.                 Draw[RowIn][ColIn] = '-';
  47.             else
  48.                 Draw[RowIn][ColIn] = 'B';
  49.             MarkedCells++;
  50.         }
  51.         if (Mark == '+')
  52.         {
  53.             if (Mines[RowIn][ColIn] != 0)
  54.             {
  55.                 Draw[RowIn][ColIn] = 'X';
  56.                 printf("Row: %d, Col: %d \n", RowIn, ColIn);
  57.                 printf("BOOM. \n");
  58.                 RestartGame();
  59.             }
  60.             else if (Count(RowIn, ColIn))
  61.             {
  62.                 Draw[RowIn][ColIn] = Count(RowIn, ColIn);
  63.                 printf("Count: %d \n", Draw[RowIn][ColIn]);
  64.             }
  65.             else
  66.                 Draw[RowIn][ColIn] = '0';
  67.         }
  68.         Win = 0;
  69.         for (int i = 0; i < 9; i++)
  70.         {
  71.             for (int j = 0; j < 9; j++)
  72.             {
  73.                 if (Draw[i][j] != '-' && !Mines[i][j])
  74.                 {
  75.                     Win++;
  76.                 }
  77.             }
  78.         }
  79.         if (Win >= (81 - TotalMines))
  80.         {
  81.             printf("You won! Now the ladies will love you!");
  82.             RestartGame();
  83.         }
  84.     }
  85.  
  86.     return 0;
  87. }
  88.  
  89. void InitGame(void) //this function resets the game;
  90. {
  91.     int i = 0;
  92.     memset(Mines, 0, sizeof Mines); //sets the whole array of Mines to 0
  93.     MarkedCells = 0;
  94.     HiddenCells = 0;
  95.     InitChar();
  96.  
  97.     printf("*************************\n");
  98.     printf("* M I N E S W E E P E R *\n");
  99.     printf("*************************\n");
  100.     printf("Enter number of bombs (1-80): ");
  101.     scanf("%d", &TotalMines);
  102.  
  103.     for (i = 0; i < TotalMines; i++)
  104.     {
  105.         PlaceBomb();
  106.     }
  107. }
  108.  
  109. void PlaceBomb(void)
  110. {
  111.     int randX = 0;
  112.     int randY = 0;
  113.     int i = 0;
  114.     int j = 0;
  115.  
  116.     while (2)
  117.     {
  118.         randX = rand() % 9;
  119.         randY = rand() % 9;
  120.         if (!Mines[randX][randY])
  121.         {
  122.             Mines[randX][randY] = 1;
  123.             break;
  124.         }
  125.     }
  126. }
  127.  
  128. int Count(int x, int y) //bit of a yikes to read, also improvable through recursion to work like MS Minesweeper; not declared in the example though
  129. {
  130.     if (x == 0 && y == 0)
  131.         return GetMine(x, y + 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
  132.  
  133.     if (x == 0)
  134.         return GetMine(x, y - 1) + GetMine(x, y + 1) + GetMine(x + 1, y - 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
  135.     if (y == 0)
  136.         return GetMine(x - 1, y) + GetMine(x - 1, y + 1) + GetMine(x, y + 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
  137.     if (x == 8 && y == 8)
  138.         return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x, y - 1);
  139.     if (x == 8)
  140.         return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x - 1, y + 1) + GetMine(x, y - 1) + GetMine(x, y + 1);
  141.     if (y == 8)
  142.         return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x, y - 1) + GetMine(x + 1, y - 1) + GetMine(x + 1, y);
  143.  
  144.     return GetMine(x - 1, y - 1) + GetMine(x - 1, y) + GetMine(x - 1, y + 1) + GetMine(x, y - 1) + GetMine(x, y + 1) + GetMine(x + 1, y - 1) + GetMine(x + 1, y) + GetMine(x + 1, y + 1);
  145. }
  146.  
  147. int GetMine(int x, int y)
  148. {
  149.     return Mines[x][y];
  150. }
  151.  
  152. void DrawField(void)
  153. {
  154.     for (int i = 0; i < 9; ++i)
  155.     {
  156.         printf("%d ", i + 1);
  157.         for (int j = 0; j < 9; ++j)
  158.         {
  159.             if (Draw[i][j] < 10)
  160.                 printf("%d ", Draw[i][j]); //using this to make Count() work; Stupid Chars!
  161.             else
  162.                 printf("%c ", Draw[i][j]);
  163.         }
  164.         printf("\n");
  165.     }
  166. }
  167.  
  168. void InitChar(void) //sets the Array that's being drawn to default
  169. {
  170.     for (int i = 0; i < 9; i++)
  171.     {
  172.         for (int j = 0; j < 9; j++)
  173.         {
  174.             Draw[i][j] = '-';
  175.         }
  176.     }
  177. }
  178.  
  179.  
  180. void RestartGame(void)
  181. {
  182.     char Reset;
  183.  
  184.     printf("Do you want to play again (y/n)?");
  185.     scanf(" %c", &Reset);
  186.  
  187.     if (Reset == 'y')
  188.     {
  189.         system("CLS");   //clears the screen, loses Linux Compat though
  190.         InitGame();
  191.         return;
  192.     }
  193.  
  194.     else
  195.  
  196.     {
  197.         printf("Coward!");
  198.         exit(0);  //ends the program without having to break out of the main loop
  199.     }
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment