Advertisement
rexinsteroids

Minesweeper_DGJP_Final

Apr 8th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     string outLettersUp = "ABCDEFGHIJKLMNOPQRSTUVWXY", outLettersLow = "abcdefghijklmnopqrstuvwxy";
  11.     string BoutLettersUp = "ABCDEFGHIJKLMNOPQRSTUVWXY", BoutLettersLow = "abcdefghijklmnopqrstuvwxy";
  12.     string intLettersUp = outLettersUp;
  13.     string guess;
  14.     int loop = 1, guessNo, guessCounter, yeps = 0;
  15.     char guessChar;
  16.     int bombCount[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  17.     srand(time(0));
  18.     cout<<"MINESWEEPER"<<endl;
  19.  
  20.     for (int a = 0; a < 5; a++)
  21.     {
  22.         int b = rand()% 25;
  23.         intLettersUp.replace(b, 1, "Z");
  24.     }
  25.  
  26.     //special case 1: position 0
  27.     for (int i = 0; i < 3; i++)
  28.     {
  29.         if (i == 0)
  30.         {
  31.             if (intLettersUp.substr(1, 1) == "Z")
  32.             {
  33.                 bombCount[0] += 1;
  34.             }
  35.         }
  36.         if (i == 1)
  37.         {
  38.             if (intLettersUp.substr(5, 1) == "Z")
  39.             {
  40.                 bombCount[0] += 1;
  41.             }
  42.         }
  43.         if (i == 2)
  44.         {
  45.             if (intLettersUp.substr(6, 1) == "Z")
  46.             {
  47.                 bombCount[0] += 1;
  48.             }
  49.         }
  50.     }
  51.     //special case 2: position 20
  52.     for (int j = 0; j < 3; j++)
  53.     {
  54.         if (j == 0)
  55.         {
  56.             if (intLettersUp.substr(15, 1) == "Z")
  57.             {
  58.                 bombCount[20] += 1;
  59.             }
  60.         }
  61.         if (j == 1)
  62.         {
  63.             if (intLettersUp.substr(16, 1) == "Z")
  64.             {
  65.                 bombCount[20] += 1;
  66.             }
  67.         }
  68.         if (j == 2)
  69.         {
  70.             if (intLettersUp.substr(21, 1) == "Z")
  71.             {
  72.                 bombCount[20] += 1;
  73.             }
  74.         }
  75.     }
  76.  
  77.     // special case: position 4
  78.     for (int k = 0; k < 3; k++)
  79.     {
  80.         if (k == 0)
  81.         {
  82.             if (intLettersUp.substr(3, 1) == "Z")
  83.             {
  84.                 bombCount[4] += 1;
  85.             }
  86.         }
  87.         if (k == 1)
  88.         {
  89.             if (intLettersUp.substr(9, 1) == "Z")
  90.             {
  91.                 bombCount[4] += 1;
  92.             }
  93.         }
  94.         if (k == 2)
  95.         {
  96.             if (intLettersUp.substr(8, 1) == "Z")
  97.             {
  98.                 bombCount[4] += 1;
  99.             }
  100.         }
  101.     }
  102.     // special case: position 24
  103.     for (int l = 0; l < 3; l++)
  104.     {
  105.         if (l == 0)
  106.         {
  107.             if (intLettersUp.substr(23, 1) == "Z")
  108.             {
  109.                 bombCount[24] += 1;
  110.             }
  111.         }
  112.         if (l == 1)
  113.         {
  114.             if (intLettersUp.substr(19, 1) == "Z")
  115.             {
  116.                 bombCount[24] += 1;
  117.             }
  118.         }
  119.         if (l == 2)
  120.         {
  121.             if (intLettersUp.substr(18, 1) == "Z")
  122.             {
  123.                 bombCount[24] += 1;
  124.             }
  125.         }
  126.     }
  127.     // case left; positions 5, 10, 15
  128.     for (int m = 5; m < 16; m += 5)
  129.     {
  130.         for (int n = 0; n < 5; n++)
  131.         {
  132.             if (n == 0)
  133.             {
  134.                 if (intLettersUp.substr(m-5, 1) == "Z")
  135.                 {
  136.                     bombCount[m] += 1;
  137.                 }
  138.             }
  139.             if (n == 1)
  140.             {
  141.                 if (intLettersUp.substr((m-4), 1) == "Z")
  142.                 {
  143.                     bombCount[m] += 1;
  144.                 }
  145.             }
  146.             if (n == 2)
  147.             {
  148.                 if (intLettersUp.substr((m+1), 1) == "Z")
  149.                 {
  150.                     bombCount[m] += 1;
  151.                 }
  152.             }
  153.             if (n == 3)
  154.             {
  155.                 if (intLettersUp.substr((m+5), 1) == "Z")
  156.                 {
  157.                     bombCount[m] += 1;
  158.                 }
  159.             }
  160.             if (n == 4)
  161.             {
  162.                 if (intLettersUp.substr((m+6), 1) == "Z")
  163.                 {
  164.                     bombCount[m] += 1;
  165.                 }
  166.             }
  167.         }
  168.  
  169.     }
  170.  
  171.     // case right; positions 9, 14, 19
  172.     for (int o = 9; o < 20; o = o + 5)
  173.     {
  174.         for (int p = 0; p < 5; p++)
  175.         {
  176.             if (p == 0)
  177.             {
  178.                 if (intLettersUp.substr(o-6, 1) == "Z")
  179.                 {
  180.                     bombCount[o] += 1;
  181.                 }
  182.             }
  183.             if (p == 1)
  184.             {
  185.                 if (intLettersUp.substr(o-5, 1) == "Z")
  186.                 {
  187.                     bombCount[o] += 1;
  188.                 }
  189.             }
  190.             if (p == 2)
  191.             {
  192.                 if (intLettersUp.substr(o-1, 1) == "Z")
  193.                 {
  194.                     bombCount[o] += 1;
  195.                 }
  196.             }
  197.             if (p == 3)
  198.             {
  199.                 if (intLettersUp.substr(o+4, 1) == "Z")
  200.                 {
  201.                     bombCount[o] += 1;
  202.                 }
  203.             }
  204.             if (p == 4)
  205.             {
  206.                 if (intLettersUp.substr(o+5, 1) == "Z")
  207.                 {
  208.                     bombCount[o] += 1;
  209.                 }
  210.             }
  211.         }
  212.     }
  213.  
  214.     // case up; positions 1, 2, 3
  215.     for (int q = 1; q < 4; q++)
  216.     {
  217.         for (int r = 0; r < 5; r++)
  218.         {
  219.             if (r == 0)
  220.             {
  221.                 if (intLettersUp.substr(q-1, 1) == "Z")
  222.                 {
  223.                     bombCount[q] += 1;
  224.                 }
  225.             }
  226.             if (r == 1)
  227.             {
  228.                 if (intLettersUp.substr(q+1, 1) == "Z")
  229.                 {
  230.                     bombCount[q] += 1;
  231.                 }
  232.             }
  233.             if (r == 2)
  234.             {
  235.                 if (intLettersUp.substr(q+4, 1) == "Z")
  236.                 {
  237.                     bombCount[q] += 1;
  238.                 }
  239.             }
  240.             if (r == 3)
  241.             {
  242.                 if (intLettersUp.substr(q+5, 1) == "Z")
  243.                 {
  244.                     bombCount[q] += 1;
  245.                 }
  246.             }
  247.             if (r == 4)
  248.             {
  249.                 if (intLettersUp.substr(q+6, 1) == "Z")
  250.                 {
  251.                     bombCount[q] += 1;
  252.                 }
  253.             }
  254.         }
  255.     }
  256.  
  257.     // case down; positions 21, 22, 23
  258.     for (int s = 21; s < 24; s++)
  259.     {
  260.         for (int t = 0; t < 5; t++)
  261.         {
  262.             if (t == 0)
  263.             {
  264.                 if (intLettersUp.substr(s-6, 1) == "Z")
  265.                 {
  266.                     bombCount[s] += 1;
  267.                 }
  268.             }
  269.             if (t == 1)
  270.             {
  271.                 if (intLettersUp.substr(s-5, 1) == "Z")
  272.                 {
  273.                     bombCount[s] += 1;
  274.                 }
  275.             }
  276.             if (t == 2)
  277.             {
  278.                 if (intLettersUp.substr(s-4, 1) == "Z")
  279.                 {
  280.                     bombCount[s] += 1;
  281.                 }
  282.             }
  283.             if (t == 3)
  284.             {
  285.                 if (intLettersUp.substr(s-1, 1) == "Z")
  286.                 {
  287.                     bombCount[s] += 1;
  288.                 }
  289.             }
  290.             if (t == 4)
  291.             {
  292.                 if (intLettersUp.substr(s+1, 1) == "Z")
  293.                 {
  294.                     bombCount[s] += 1;
  295.                 }
  296.             }
  297.         }
  298.     }
  299.  
  300.     // case middle; positions 6, 7, 8, 11, 12, 13, 16, 17, 18
  301.     for (int u = 6; u < 19; u++)
  302.     {
  303.         if (u == 9)
  304.         {
  305.             u = 11;
  306.         }
  307.         if (u == 14)
  308.         {
  309.             u = 16;
  310.         }
  311.  
  312.         for (int v = 0; v < 9; v++)
  313.         {
  314.             if (v == 0)
  315.             {
  316.                 if (intLettersUp.substr(u-6, 1) == "Z")
  317.                 {
  318.                     bombCount[u] += 1;
  319.                 }
  320.             }
  321.             if (v == 1)
  322.             {
  323.                 if (intLettersUp.substr(u-5, 1) == "Z")
  324.                 {
  325.                     bombCount[u] += 1;
  326.                 }
  327.             }
  328.             if (v == 2)
  329.             {
  330.                 if (intLettersUp.substr(u-4, 1) == "Z")
  331.                 {
  332.                     bombCount[u] += 1;
  333.                 }
  334.             }
  335.             if (v == 3)
  336.             {
  337.                 if (intLettersUp.substr(u-1, 1) == "Z")
  338.                 {
  339.                     bombCount[u] += 1;
  340.                 }
  341.             }
  342.             if (v == 4)
  343.             {
  344.                 if (intLettersUp.substr(u+1, 1) == "Z")
  345.                 {
  346.                     bombCount[u] += 1;
  347.                 }
  348.             }
  349.             if (v == 5)
  350.             {
  351.                 if (intLettersUp.substr(u+4, 1) == "Z")
  352.                 {
  353.                     bombCount[u] += 1;
  354.                 }
  355.             }
  356.             if (v == 6)
  357.             {
  358.                 if (intLettersUp.substr(u+5, 1) == "Z")
  359.                 {
  360.                     bombCount[u] += 1;
  361.                 }
  362.             }
  363.             if (v == 7)
  364.             {
  365.                 if (intLettersUp.substr(u+6, 1) == "Z")
  366.                 {
  367.                     bombCount[u] += 1;
  368.                 }
  369.             }
  370.         }
  371.     }
  372.  
  373.     for (int w = 0; w < 25; w++)
  374.     {
  375.         if (bombCount[w] == 0)
  376.         {
  377.             if (intLettersUp.substr(w, 1) != "Z")
  378.             {
  379.                 intLettersUp.replace(w, 1, "0");
  380.             }
  381.         }
  382.         if (bombCount[w] == 1)
  383.         {
  384.             if (intLettersUp.substr(w, 1) != "Z")
  385.             {
  386.                 intLettersUp.replace(w, 1, "1");
  387.             }
  388.         }
  389.         if (bombCount[w] == 2)
  390.         {
  391.             if (intLettersUp.substr(w, 1) != "Z")
  392.             {
  393.                 intLettersUp.replace(w, 1, "2");
  394.             }
  395.         }
  396.         if (bombCount[w] == 3)
  397.         {
  398.             if (intLettersUp.substr(w, 1) != "Z")
  399.             {
  400.                 intLettersUp.replace(w, 1, "3");
  401.             }
  402.         }
  403.         if (bombCount[w] == 4)
  404.         {
  405.             if (intLettersUp.substr(w, 1) != "Z")
  406.             {
  407.                 intLettersUp.replace(w, 1, "4");
  408.             }
  409.         }
  410.         if (bombCount[w] == 5)
  411.         {
  412.             if (intLettersUp.substr(w, 1) != "Z")
  413.             {
  414.                 intLettersUp.replace(w, 1, "5");
  415.             }
  416.         }
  417.     }
  418.  
  419.     Start:
  420.     while (loop == 1)
  421.     {
  422.         for (int d=0; d<5; d++)
  423.         {
  424.             cout<<outLettersUp.substr(d, 1)<<" ";
  425.         }
  426.         cout<<endl;
  427.         for (int e=5; e<10; e++)
  428.         {
  429.             cout<<outLettersUp.substr(e, 1)<<" ";
  430.         }
  431.         cout<<endl;
  432.         for (int f=10; f<15; f++)
  433.         {
  434.             cout<<outLettersUp.substr(f, 1)<<" ";
  435.         }
  436.         cout<<endl;
  437.         for (int g=15; g<20; g++)
  438.         {
  439.             cout<<outLettersUp.substr(g, 1)<<" ";
  440.         }
  441.         cout<<endl;
  442.         for (int h=20; h<25; h++)
  443.         {
  444.             cout<<outLettersUp.substr(h, 1)<<" ";
  445.         }
  446.         cout<<endl;
  447.         cout<<endl<<"Choose a letter."<<endl;
  448.         guessChar = getch();
  449.         guess += guessChar;
  450.  
  451.         for (int x = 0; x < 25; x++)
  452.         {
  453.             if ((guess.substr(0, 1) == BoutLettersUp.substr(x, 1)) || (guess.substr(0, 1) == BoutLettersLow.substr(x, 1)))
  454.                 {
  455.                     for (int x = 0; x < 25; x++)
  456.                     {
  457.                         if ((guess.substr(0, 1) == BoutLettersUp.substr(x, 1)) || (guess.substr(0, 1) == BoutLettersLow.substr(x, 1)))
  458.                         {
  459.                             yeps += 1;
  460.                             guessNo = x;
  461.                             break;
  462.                         }
  463.                     }
  464.                 }
  465.             if ((x == 24) && (yeps == 0))
  466.             {
  467.                 cout<<endl<<"The character you entered is not in the grid. Try again."<<endl;
  468.                 guess.clear();
  469.                 guessChar = ' ';
  470.                 system("pause");
  471.                 system("cls");
  472.                 goto Start;
  473.             }
  474.         }
  475.         cout<<endl<<"Your guess is: "<<guess<<endl;
  476.         system("pause");
  477.  
  478.         for (int z = 0; z < 25; z++)
  479.         {
  480.             if (intLettersUp.substr(guessNo, 1) != "Z")
  481.             {
  482.                 if (outLettersUp.substr(guessNo, 1) == intLettersUp.substr(guessNo, 1))
  483.                 {
  484.                     cout<<endl<<"You've already entered that character."<<endl;
  485.                     guess.clear();
  486.                     guessChar = ' ';
  487.                     system("pause");
  488.                     system("cls");
  489.                     break;
  490.                 } else {
  491.                 outLettersUp.replace(guessNo, 1, intLettersUp.substr(guessNo, 1));
  492.                 guessCounter += 1;
  493.                 guess.clear();
  494.                 guessChar = ' ';
  495.                 system("cls");
  496.                 break;
  497.                 }
  498.             }
  499.             else
  500.             {
  501.                 goto Lose;
  502.             }
  503.         }
  504.     if (guessCounter < 20)
  505.         {
  506.             continue;
  507.         }
  508.         else {
  509.         cout<<endl<<"Congratulations! You won!"<<endl;
  510.         system("pause");
  511.         return 0;
  512.         }
  513.     }
  514.  
  515.  
  516.     Lose:
  517.     system("cls");
  518.     cout<<"MINESWEEPER"<<endl;
  519.     // for checking
  520.     for (int d=0; d<5; d++)
  521.         {
  522.             cout<<intLettersUp.substr(d, 1)<<" ";
  523.         }
  524.     cout<<endl;
  525.     for (int e=5; e<10; e++)
  526.     {
  527.         cout<<intLettersUp.substr(e, 1)<<" ";
  528.     }
  529.     cout<<endl;
  530.     for (int f=10; f<15; f++)
  531.     {
  532.         cout<<intLettersUp.substr(f, 1)<<" ";
  533.     }
  534.     cout<<endl;
  535.     for (int g=15; g<20; g++)
  536.     {
  537.         cout<<intLettersUp.substr(g, 1)<<" ";
  538.     }
  539.     cout<<endl;
  540.         for (int h=20; h<25; h++)
  541.     {
  542.         cout<<intLettersUp.substr(h, 1)<<" ";
  543.     }
  544.     cout<<endl;
  545.     cout<<endl<<"GAME OVER!"<<endl<<"The tile you chose had a bomb!"<<endl;
  546.     system("pause");
  547.     return 0;
  548. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement