Advertisement
rexinsteroids

MineSwCode_temp

Apr 4th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.49 KB | None | 0 0
  1. /**
  2. 0  1  2  3  4
  3. 5  6  7  8  9
  4. 10 11 12 13 14
  5. 15 16 17 18 19
  6. 20 21 22 23 24
  7.  
  8. Possible bombs:
  9.  
  10. =====================================================
  11. LEFTMOST CORNERS (3 possible)
  12.  
  13. 0:
  14.     0 + 1 = 1
  15.     0 + 5 = 5
  16.     0 + 6 = 6
  17.  
  18. 20:
  19.     20 - 5 = 15
  20.     20 - 4 = 16
  21.     20 + 1 = 21
  22.  
  23. =====================================================
  24. RIGHTMOST CORNERS (3 possible)
  25.  
  26. 4:
  27.     - 1
  28.     + 4
  29.     + 5
  30.  
  31. 24:
  32.     - 5
  33.     - 6
  34.     - 1
  35.  
  36. =====================================================
  37. LEFTMOST SIDES (5 possible)
  38.  
  39. FOR 5, 10, 15:
  40.  
  41.     - 5
  42.     - 4
  43.     + 1
  44.     + 5
  45.     + 6
  46.  
  47. =====================================================
  48. RIGHTMOST SIDES (5 possible)
  49.  
  50. FOR 9, 14, 19:
  51.  
  52.     - 6
  53.     - 5
  54.     - 1
  55.     + 4
  56.     + 5
  57.  
  58. =====================================================
  59. FOR UPPERMOST SIDES (5 possible)
  60.  
  61. FOR 1, 2, 3:
  62.  
  63.     - 1
  64.     + 1
  65.     + 4
  66.     + 5
  67.     + 6
  68.  
  69. =====================================================
  70. FOR LOWERMOST SIDES (5 possible)
  71.  
  72. FOR 21, 22, 23:
  73.     - 6
  74.     - 5
  75.     - 4
  76.     - 1
  77.     + 1
  78.  
  79. =====================================================
  80. FOR MIDDLES (8 possible) (max bombs still 5)
  81.  
  82.  
  83. FOR 6, 7, 8, 11, 12, 13, 16, 17, 18
  84.  
  85.     - 6
  86.     - 5
  87.     - 4
  88.     - 1
  89.     + 1
  90.     + 4
  91.     + 5
  92.     + 6
  93.  
  94. =====================================================
  95. **/
  96. #include <iostream>
  97. #include <stdlib.h>
  98. #include <time.h>
  99. using namespace std;
  100.  
  101. int main()
  102. {
  103.     int n = 26;
  104.     char board[n];
  105.     char boardDisp[n] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'};
  106.     char bombCount;
  107.     int bombVicinityCount(int q);
  108.     srand(time(0));
  109.     cout<<"MINESWEEPER\n\n";
  110.  
  111.  
  112.     //  Assigns bomb locations, bomb = Z
  113.  
  114.     for (int i = 0; i<5 ; i++)
  115.     {
  116.         int k = rand()% 25;
  117.         board[k] = 'Z';
  118.     }
  119.  
  120.     //if letter is not a bomb, assign letter for display
  121.     for (int l = 0; l<n; l++)
  122.     {
  123.         if (board[l] != 'Z')
  124.         {
  125.             board[l] = boardDisp[l];
  126.         }
  127.     }
  128.  
  129.     // displays the letters in a grid
  130.     for (int a=0; a<5; a++)
  131.         {
  132.             cout<<boardDisp[a]<<" ";
  133.         }
  134.     cout<<endl;
  135.     for (int b=5; b<10; b++)
  136.     {
  137.         cout<<boardDisp[b]<<" ";
  138.     }
  139.     cout<<endl;
  140.     for (int c=10; c<15; c++)
  141.     {
  142.         cout<<boardDisp[c]<<" ";
  143.     }
  144.     cout<<endl;
  145.     for (int d=15; d<20; d++)
  146.     {
  147.         cout<<boardDisp[d]<<" ";
  148.     }
  149.     cout<<endl;
  150.         for (int e=20; e<25; e++)
  151.     {
  152.         cout<<boardDisp[e]<<" ";
  153.     }
  154.     cout<<endl;
  155.  
  156.     // places value for number of bombs in board
  157.  
  158.     for (int f = 0; f<n; f++)
  159.     {
  160.         if (board[f] != 'Z')
  161.         {
  162.             int bombs = bombVicinityCount(f);
  163.             if (bombs != 0)
  164.             {
  165.                 if (bombs == 1)
  166.                 {
  167.                     bombCount = '1';
  168.                 } else if (bombs == 2)
  169.                 {
  170.                     bombCount = '2';
  171.                 } else if (bombs == 3)
  172.                 {
  173.                     bombCount = '3';
  174.                 } else if (bombs == 4)
  175.                 {
  176.                     bombCount = '4';
  177.                 } else if (bombs == 5)
  178.                 {
  179.                     bombCount = '5';
  180.                 }
  181.                 board[f] = bombCount;
  182.             } else
  183.             if (bombs == 0)
  184.             {
  185.             board[f] = boardDisp[f];
  186.             }
  187.         } else
  188.         {
  189.             break;
  190.         }
  191.     }
  192.  
  193.     // for testing; displays bomb locations and number of bombs
  194.     for (int a=0; a<5; a++)
  195.         {
  196.             cout<<board[a]<<" ";
  197.         }
  198.     cout<<endl;
  199.     for (int b=5; b<10; b++)
  200.     {
  201.         cout<<board[b]<<" ";
  202.     }
  203.     cout<<endl;
  204.     for (int c=10; c<15; c++)
  205.     {
  206.         cout<<board[c]<<" ";
  207.     }
  208.     cout<<endl;
  209.     for (int d=15; d<20; d++)
  210.     {
  211.         cout<<board[d]<<" ";
  212.     }
  213.     cout<<endl;
  214.         for (int e=20; e<25; e++)
  215.     {
  216.         cout<<board[e]<<" ";
  217.     }
  218.     cout<<endl;
  219.  
  220.  
  221.     return 0;
  222. }
  223.  
  224. // function to find number of bombs
  225. int bombVicinityCount(int q)
  226. {
  227.     int bCount;
  228.     int fN6(int w, int r);
  229.     int fN5(int x, int s);
  230.     int fN4(int y, int t);
  231.     int fN1(int z, int u);
  232.     // for pos 0 (upper left corner)
  233.     if (q==0)
  234.     {
  235.         bCount = fN1(2, 0) + fN5(2, 0) + fN6(2, 0);
  236.     }
  237.     // for pos 20 (lower left corner)
  238.     if (q==20)
  239.     {
  240.         bCount = fN1(2, 20) + fN4(1, 20) + fN5(1, 20);
  241.     }
  242.     // for pos 4 (upper right corner)
  243.     if (q==4)
  244.     {
  245.         bCount = fN1(1, 4) + fN4(2, 4) + fN5(2, 4);
  246.     }
  247.     // for pos 24 (lower right corner)
  248.     if (q==24)
  249.     {
  250.         bCount = fN1(1, 24) + fN5(1, 24) + fN6(1, 24);
  251.     }
  252.     // for pos 5, 10, 15 (left sides)
  253.     if ((q==5) || (q==10) || (q==15))
  254.     {
  255.         bCount = fN5(3, q) + fN4(1, q) + fN1(2, q) + fN6(2, q);
  256.     }
  257.     // for pos 9, 14, 19 (right sides)
  258.     if ((q==9) || (q==14) || (q==19))
  259.     {
  260.         bCount = fN5(3, q) + fN4(2, q) + fN1(1, q) + fN6(1, q);
  261.     }
  262.     // for pos 1, 2, 3 (upper sides)
  263.     if ((q==1) || (q==2) || (q==3))
  264.     {
  265.         bCount = fN1(3, q) + fN4(2, q) + fN5(2, q) + fN6(2, q);
  266.     }
  267.     // for pos 21, 22, 23 (lower sides)
  268.     if ((q==21) || (q==22) || (q==23))
  269.     {
  270.         bCount = fN1(3, q) + fN4(1, q) + fN5(1, q) + fN6(1, q);
  271.     }
  272.     // for pos 6, 7, 8, 11, 12, 13, 16, 17, 18 (middles)
  273.     if ((q==6) || (q==7) || (q==8) || (q==11) || (q==12) || (q==13) || (q==16) || (q==17) || (q==18))
  274.     {
  275.         bCount = fN5(3, q) + fN4(3, q) + fN1(3, q) + fN6(3, q);
  276.     }
  277.  
  278.     return bCount;
  279. }
  280.  
  281. int fN6 (int w, int r)
  282. {
  283.     char board[26];
  284.     int bomb6 = 0;
  285.     if (w == 1)
  286.     {
  287.         if (board[r]-6 == 'Z')
  288.         {
  289.             bomb6 += 1;
  290.         }
  291.     } else if (w == 2)
  292.     {
  293.         if (board[r]+6 == 'Z' )
  294.         {
  295.             bomb6 += 1;
  296.         }
  297.     } else if (w == 3)
  298.     {
  299.         if (board[r]-6 == 'Z')
  300.         {
  301.             bomb6 += 1;
  302.         }
  303.         if (board[r]+6 == 'Z' )
  304.         {
  305.             bomb6 += 1;
  306.         }
  307.     }
  308.     return bomb6;
  309. }
  310.  
  311. int fN5 (int x, int s)
  312. {
  313.     char board[26];
  314.     int bomb5 = 0;
  315.     if (x == 1)
  316.     {
  317.         if (board[s]-5 == 'Z')
  318.         {
  319.             bomb5 += 1;
  320.         }
  321.     } else if (x == 2)
  322.     {
  323.         if (board[s]+5 == 'Z' )
  324.         {
  325.             bomb5 += 1;
  326.         }
  327.     } else if (s == 3)
  328.     {
  329.         if (board[s]-5 == 'Z')
  330.         {
  331.             bomb5 += 1;
  332.         }
  333.         if (board[s]+5 == 'Z' )
  334.         {
  335.             bomb5 += 1;
  336.         }
  337.     }
  338.     return bomb5;
  339. }
  340. int fN4 (int y, int t)
  341. {
  342.     char board[26];
  343.     int bomb4 = 0;
  344.     if (y == 1)
  345.     {
  346.         if (board[t]-4 == 'Z')
  347.         {
  348.             bomb4 += 1;
  349.         }
  350.     } else if (y == 2)
  351.     {
  352.         if (board[t]+4 == 'Z' )
  353.         {
  354.             bomb4 += 1;
  355.         }
  356.     } else if (y == 3)
  357.     {
  358.         if (board[t]-4 == 'Z')
  359.         {
  360.             bomb4 += 1;
  361.         }
  362.         if (board[t]+4 == 'Z' )
  363.         {
  364.             bomb4 += 1;
  365.         }
  366.     }
  367.     return bomb4;
  368. }
  369. int fN1 (int z, int u)
  370. {
  371.     char board[26];
  372.     int bomb1 = 0;
  373.     if (z == 1)
  374.     {
  375.         if (board[u]-1 == 'Z')
  376.         {
  377.             bomb1 += 1;
  378.         }
  379.     } else if (z == 2)
  380.     {
  381.         if (board[u]+1 == 'Z' )
  382.         {
  383.             bomb1 += 1;
  384.         }
  385.     } else if (z == 3)
  386.     {
  387.         if (board[u]-1 == 'Z')
  388.         {
  389.             bomb1 += 1;
  390.         }
  391.         if (board[u]+1 == 'Z' )
  392.         {
  393.             bomb1 += 1;
  394.         }
  395.     }
  396.     return bomb1;
  397. }
  398. // for fNum functions, x == 2 adds Num, while x == 1 subtracts Num, x == 3 does both
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement