Advertisement
MeehoweCK

Untitled

Mar 4th, 2021
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <vector>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. enum class pole
  10. {
  11.     puste, poszukiwacz, mur, skarb, przeciwnik
  12. };
  13.  
  14. enum class stan_gry
  15. {
  16.     otwarta, zwyciestwo, porazka
  17. };
  18.  
  19. stan_gry stan = stan_gry::otwarta;
  20.  
  21. struct wspolrzedne
  22. {
  23.     short i;
  24.     short j;
  25.     short los;
  26. };
  27.  
  28. const short N = 33;
  29. pole plansza[N][N];
  30. wspolrzedne przeciwnicy[10];
  31. wspolrzedne bohater;
  32.  
  33. void ruch_przeciwnikiem(short x)
  34. {
  35.     // sprawdzenie czy wokół danego przeciwnika stoi poszukiwacz:
  36.     if(plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j - 1] == pole::poszukiwacz)
  37.     {
  38.         plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j - 1] = pole::przeciwnik;
  39.         stan = stan_gry::porazka;
  40.         return;
  41.     }
  42.     if(plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j] == pole::poszukiwacz)
  43.     {
  44.         plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j] = pole::przeciwnik;
  45.         stan = stan_gry::porazka;
  46.         return;
  47.     }
  48.     if(plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j + 1] == pole::poszukiwacz)
  49.     {
  50.         plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j + 1] = pole::przeciwnik;
  51.         stan = stan_gry::porazka;
  52.         return;
  53.     }
  54.     if(plansza[przeciwnicy[x].i][przeciwnicy[x].j - 1] == pole::poszukiwacz)
  55.     {
  56.         plansza[przeciwnicy[x].i][przeciwnicy[x].j - 1] = pole::przeciwnik;
  57.         stan = stan_gry::porazka;
  58.         return;
  59.     }
  60.     if(plansza[przeciwnicy[x].i][przeciwnicy[x].j + 1] == pole::poszukiwacz)
  61.     {
  62.         plansza[przeciwnicy[x].i][przeciwnicy[x].j + 1] = pole::przeciwnik;
  63.         stan = stan_gry::porazka;
  64.         return;
  65.     }
  66.     if(plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j - 1] == pole::poszukiwacz)
  67.     {
  68.         plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j - 1] = pole::przeciwnik;
  69.         stan = stan_gry::porazka;
  70.         return;
  71.     }
  72.     if(plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j] == pole::poszukiwacz)
  73.     {
  74.         plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j] = pole::przeciwnik;
  75.         stan = stan_gry::porazka;
  76.         return;
  77.     }
  78.     if(plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j + 1] == pole::poszukiwacz)
  79.     {
  80.         plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j + 1] = pole::przeciwnik;
  81.         stan = stan_gry::porazka;
  82.         return;
  83.     }
  84.  
  85.     // ruch na wolne miejsce:
  86.     vector<wspolrzedne> dostepne_pola;
  87.     wspolrzedne nowe;
  88.     if(plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j - 1] == pole::puste)
  89.     {
  90.         nowe.i = przeciwnicy[x].i - 1;
  91.         nowe.j = przeciwnicy[x].j - 1;
  92.         dostepne_pola.push_back(nowe);
  93.     }
  94.     if(plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j] == pole::puste)
  95.     {
  96.         nowe.i = przeciwnicy[x].i - 1;
  97.         nowe.j = przeciwnicy[x].j;
  98.         dostepne_pola.push_back(nowe);
  99.     }
  100.     if(plansza[przeciwnicy[x].i - 1][przeciwnicy[x].j + 1] == pole::puste)
  101.     {
  102.         nowe.i = przeciwnicy[x].i - 1;
  103.         nowe.j = przeciwnicy[x].j + 1;
  104.         dostepne_pola.push_back(nowe);
  105.     }
  106.     if(plansza[przeciwnicy[x].i][przeciwnicy[x].j - 1] == pole::puste)
  107.     {
  108.         nowe.i = przeciwnicy[x].i;
  109.         nowe.j = przeciwnicy[x].j - 1;
  110.         dostepne_pola.push_back(nowe);
  111.     }
  112.     if(plansza[przeciwnicy[x].i][przeciwnicy[x].j + 1] == pole::puste)
  113.     {
  114.         nowe.i = przeciwnicy[x].i;
  115.         nowe.j = przeciwnicy[x].j + 1;
  116.         dostepne_pola.push_back(nowe);
  117.     }
  118.     if(plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j - 1] == pole::puste)
  119.     {
  120.         nowe.i = przeciwnicy[x].i + 1;
  121.         nowe.j = przeciwnicy[x].j - 1;
  122.         dostepne_pola.push_back(nowe);
  123.     }
  124.     if(plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j] == pole::puste)
  125.     {
  126.         nowe.i = przeciwnicy[x].i + 1;
  127.         nowe.j = przeciwnicy[x].j;
  128.         dostepne_pola.push_back(nowe);
  129.     }
  130.     if(plansza[przeciwnicy[x].i + 1][przeciwnicy[x].j + 1] == pole::puste)
  131.     {
  132.         nowe.i = przeciwnicy[x].i + 1;
  133.         nowe.j = przeciwnicy[x].j + 1;
  134.         dostepne_pola.push_back(nowe);
  135.     }
  136.     if(dostepne_pola.size() > 0)
  137.     {
  138.         // losujemy na które pole ma pójść przeciwnik
  139.         srand(time(nullptr));
  140.         nowe = dostepne_pola[rand() % dostepne_pola.size()];
  141.         plansza[przeciwnicy[x].i][przeciwnicy[x].j] = pole::puste;
  142.         przeciwnicy[x].i = nowe.i;
  143.         przeciwnicy[x].j = nowe.j;
  144.         plansza[przeciwnicy[x].i][przeciwnicy[x].j] = pole::przeciwnik;
  145.     }
  146. }
  147.  
  148. void ruch_przeciwnikow()
  149. {
  150.     for(short i = 0; i < 10; ++i)
  151.         ruch_przeciwnikiem(i);
  152. }
  153.  
  154. void uzupelnij_plansze()
  155. {
  156.     srand(time(nullptr));
  157.     vector<wspolrzedne> puste_pola;
  158.     wspolrzedne nowe;
  159.     for(short i = 0; i < N; ++i)
  160.         for(short j = 0; j < N; ++j)
  161.             if(i == 0 || i == N - 1 || j == 0 || j == N - 1 || ((i-1) * (j-1)) % 2 != 0)
  162.                 plansza[i][j] = pole::mur;
  163.             else
  164.             {
  165.                 plansza[i][j] = pole::puste;
  166.                 nowe.i = i;
  167.                 nowe.j = j;
  168.                 nowe.los = rand();
  169.                 puste_pola.push_back(nowe);
  170.             }
  171.     unsigned n = puste_pola.size();
  172.     for(unsigned i = 0; i < n - 1; ++i)
  173.         for(unsigned j = 0; j < n - i - 1; ++j)
  174.             if(puste_pola[j].los > puste_pola[j + 1].los)
  175.                 swap(puste_pola[j], puste_pola[j + 1]);
  176.     for(unsigned i = 0; i < 10; ++i)
  177.     {
  178.         plansza[puste_pola[i].i][puste_pola[i].j] = pole::przeciwnik;
  179.         przeciwnicy[i].i = puste_pola[i].i;
  180.         przeciwnicy[i].j = puste_pola[i].j;
  181.     }
  182.     plansza[puste_pola[10].i][puste_pola[10].j] = pole::poszukiwacz;
  183.     bohater.i = puste_pola[10].i;
  184.     bohater.j = puste_pola[10].j;
  185.     plansza[puste_pola[11].i][puste_pola[11].j] = pole::skarb;
  186. }
  187.  
  188. void ruch_bohaterem()
  189. {
  190.     // sprawdzamy czy bohater ma siê gdzie ruszyæ:
  191.     vector<wspolrzedne> dostepne_pola;
  192.     wspolrzedne nowe;
  193.     if(plansza[bohater.i - 1][bohater.j] == pole::puste || plansza[bohater.i - 1][bohater.j] == pole::skarb)
  194.     {
  195.         nowe.i = bohater.i - 1;
  196.         nowe.j = bohater.j;
  197.         dostepne_pola.push_back(nowe);
  198.     }
  199.     if(plansza[bohater.i + 1][bohater.j] == pole::puste || plansza[bohater.i + 1][bohater.j] == pole::skarb)
  200.     {
  201.         nowe.i = bohater.i + 1;
  202.         nowe.j = bohater.j;
  203.         dostepne_pola.push_back(nowe);
  204.     }
  205.     if(plansza[bohater.i][bohater.j - 1] == pole::puste || plansza[bohater.i][bohater.j - 1] == pole::skarb)
  206.     {
  207.         nowe.i = bohater.i;
  208.         nowe.j = bohater.j - 1;
  209.         dostepne_pola.push_back(nowe);
  210.     }
  211.     if(plansza[bohater.i][bohater.j + 1] == pole::puste || plansza[bohater.i][bohater.j + 1] == pole::skarb)
  212.     {
  213.         nowe.i = bohater.i;
  214.         nowe.j = bohater.j + 1;
  215.         dostepne_pola.push_back(nowe);
  216.     }
  217.     if(dostepne_pola.size() == 0)
  218.     {
  219.         stan = stan_gry::porazka;
  220.         return;
  221.     }
  222.  
  223.     // w³aœciwy ruch:
  224.     char direction;
  225.     while(true)
  226.     {
  227.         do
  228.         {
  229.             direction = getch();
  230.             direction = toupper(direction);
  231.         }
  232.         while(direction != 'J' && direction != 'K' && direction != 'L' && direction != 'I');
  233.         switch(direction)
  234.         {
  235.         case 'J':   // ruch w lewo
  236.             if(plansza[bohater.i][bohater.j - 1] == pole::puste)
  237.             {
  238.                 plansza[bohater.i][bohater.j - 1] = pole::poszukiwacz;
  239.                 plansza[bohater.i][bohater.j] = pole::mur;
  240.                 bohater.j--;
  241.                 return;
  242.             }
  243.             if(plansza[bohater.i][bohater.j - 1] == pole::skarb)
  244.             {
  245.                 plansza[bohater.i][bohater.j - 1] = pole::poszukiwacz;
  246.                 plansza[bohater.i][bohater.j] = pole::mur;
  247.                 bohater.j--;
  248.                 stan = stan_gry::zwyciestwo;
  249.                 return;
  250.             }
  251.             break;
  252.         case 'K':   // ruch w dol
  253.             if(plansza[bohater.i + 1][bohater.j] == pole::puste)
  254.             {
  255.                 plansza[bohater.i + 1][bohater.j] = pole::poszukiwacz;
  256.                 plansza[bohater.i][bohater.j] = pole::mur;
  257.                 bohater.i++;
  258.                 return;
  259.             }
  260.             if(plansza[bohater.i + 1][bohater.j] == pole::skarb)
  261.             {
  262.                 plansza[bohater.i + 1][bohater.j] = pole::poszukiwacz;
  263.                 plansza[bohater.i][bohater.j] = pole::mur;
  264.                 bohater.i++;
  265.                 stan = stan_gry::zwyciestwo;
  266.                 return;
  267.             }
  268.             break;
  269.         case 'I':   // ruch w gore
  270.             if(plansza[bohater.i - 1][bohater.j] == pole::puste)
  271.             {
  272.                 plansza[bohater.i - 1][bohater.j] = pole::poszukiwacz;
  273.                 plansza[bohater.i][bohater.j] = pole::mur;
  274.                 bohater.i--;
  275.                 return;
  276.             }
  277.             if(plansza[bohater.i - 1][bohater.j] == pole::skarb)
  278.             {
  279.                 plansza[bohater.i - 1][bohater.j] = pole::poszukiwacz;
  280.                 plansza[bohater.i][bohater.j] = pole::mur;
  281.                 bohater.i--;
  282.                 stan = stan_gry::zwyciestwo;
  283.                 return;
  284.             }
  285.             break;
  286.         case 'L':   // ruch w prawo
  287.             if(plansza[bohater.i][bohater.j + 1] == pole::puste)
  288.             {
  289.                 plansza[bohater.i][bohater.j + 1] = pole::poszukiwacz;
  290.                 plansza[bohater.i][bohater.j] = pole::mur;
  291.                 bohater.j++;
  292.                 return;
  293.             }
  294.             if(plansza[bohater.i][bohater.j + 1] == pole::skarb)
  295.             {
  296.                 plansza[bohater.i][bohater.j + 1] = pole::poszukiwacz;
  297.                 plansza[bohater.i][bohater.j] = pole::mur;
  298.                 bohater.j++;
  299.                 stan = stan_gry::zwyciestwo;
  300.                 return;
  301.             }
  302.             break;
  303.         }
  304.     }
  305. }
  306.  
  307. void wypisz_plansze()
  308. {
  309.     for(short i = 0; i < N; ++i)
  310.     {
  311.         for(short j = 0; j < N; ++j)
  312.         {
  313.             switch(plansza[i][j])
  314.             {
  315.             case pole::puste:
  316.                 cout << ' ';
  317.                 break;
  318.             case pole::mur:
  319.                 cout << '#';
  320.                 break;
  321.             case pole::poszukiwacz:
  322.                 cout << 'O';
  323.                 break;
  324.             case pole::skarb:
  325.                 cout << 'X';
  326.                 break;
  327.             case pole::przeciwnik:
  328.                 cout << '@';
  329.                 break;
  330.             }
  331.             cout << ' ';
  332.         }
  333.         cout << endl;
  334.     }
  335. }
  336.  
  337. int main()
  338. {
  339.     uzupelnij_plansze();
  340.     wypisz_plansze();
  341.     while(stan == stan_gry::otwarta)
  342.     {
  343.         ruch_bohaterem();
  344.         ruch_przeciwnikow();
  345.         system("cls");      // wyzerowanie konsoli
  346.         wypisz_plansze();
  347.     }
  348.     if(stan == stan_gry::zwyciestwo)
  349.         cout << "Gratulacje! Wygrales/wygralas!\n";
  350.     else
  351.         cout << "Niestety, porazka :(\n";
  352.     return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement