Advertisement
MeehoweCK

Untitled

Feb 22nd, 2021
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 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 uzupelnij_plansze()
  34. {
  35.     srand(time(nullptr));
  36.     vector<wspolrzedne> puste_pola;
  37.     wspolrzedne nowe;
  38.     for(short i = 0; i < N; ++i)
  39.         for(short j = 0; j < N; ++j)
  40.             if(i == 0 || i == N - 1 || j == 0 || j == N - 1 || ((i-1) * (j-1)) % 2 != 0)
  41.                 plansza[i][j] = pole::mur;
  42.             else
  43.             {
  44.                 plansza[i][j] = pole::puste;
  45.                 nowe.i = i;
  46.                 nowe.j = j;
  47.                 nowe.los = rand();
  48.                 puste_pola.push_back(nowe);
  49.             }
  50.     unsigned n = puste_pola.size();
  51.     for(unsigned i = 0; i < n - 1; ++i)
  52.         for(unsigned j = 0; j < n - i - 1; ++j)
  53.             if(puste_pola[j].los > puste_pola[j + 1].los)
  54.                 swap(puste_pola[j], puste_pola[j + 1]);
  55.     for(unsigned i = 0; i < 10; ++i)
  56.     {
  57.         plansza[puste_pola[i].i][puste_pola[i].j] = pole::przeciwnik;
  58.         przeciwnicy[i].i = puste_pola[i].i;
  59.         przeciwnicy[i].j = puste_pola[i].j;
  60.     }
  61.     plansza[puste_pola[10].i][puste_pola[10].j] = pole::poszukiwacz;
  62.     bohater.i = puste_pola[10].i;
  63.     bohater.j = puste_pola[10].j;
  64.     plansza[puste_pola[11].i][puste_pola[11].j] = pole::skarb;
  65. }
  66.  
  67. void ruch_bohaterem()
  68. {
  69.     // sprawdzamy czy bohater ma się gdzie ruszyć:
  70.     vector<wspolrzedne> dostepne_pola;
  71.     wspolrzedne nowe;
  72.     if(plansza[bohater.i - 1][bohater.j] == pole::puste || plansza[bohater.i - 1][bohater.j] == pole::skarb)
  73.     {
  74.         nowe.i = bohater.i - 1;
  75.         nowe.j = bohater.j;
  76.         dostepne_pola.push_back(nowe);
  77.     }
  78.     if(plansza[bohater.i + 1][bohater.j] == pole::puste || plansza[bohater.i + 1][bohater.j] == pole::skarb)
  79.     {
  80.         nowe.i = bohater.i + 1;
  81.         nowe.j = bohater.j;
  82.         dostepne_pola.push_back(nowe);
  83.     }
  84.     if(plansza[bohater.i][bohater.j - 1] == pole::puste || plansza[bohater.i][bohater.j - 1] == pole::skarb)
  85.     {
  86.         nowe.i = bohater.i;
  87.         nowe.j = bohater.j - 1;
  88.         dostepne_pola.push_back(nowe);
  89.     }
  90.     if(plansza[bohater.i][bohater.j + 1] == pole::puste || plansza[bohater.i][bohater.j + 1] == pole::skarb)
  91.     {
  92.         nowe.i = bohater.i;
  93.         nowe.j = bohater.j + 1;
  94.         dostepne_pola.push_back(nowe);
  95.     }
  96.     if(dostepne_pola.size() == 0)
  97.     {
  98.         stan = stan_gry::porazka;
  99.         return;
  100.     }
  101.  
  102.     // właściwy ruch:
  103.     char direction;
  104.     bool flaga = true;      // dopóki flaga podniesiona, wykonujemy ruch
  105.     while(flaga)
  106.     {
  107.         do
  108.         {
  109.             direction = getch();
  110.             direction = toupper(direction);
  111.         }
  112.         while(direction != 'J' && direction != 'K' && direction != 'L' && direction != 'I');
  113.         switch(direction)
  114.         {
  115.         case 'J':   // ruch w lewo
  116.             if(plansza[bohater.i][bohater.j - 1] == pole::puste)
  117.             {
  118.                 // wykonujemy ruch
  119.             }
  120.             if(plansza[bohater.i][bohater.j - 1] == pole::skarb)
  121.             {
  122.                 // wykonujemy ruch i kończymy grę
  123.             }
  124.             break;
  125.         // K, L, I
  126.         }
  127.     }
  128. }
  129.  
  130. void wypisz_plansze()
  131. {
  132.     for(short i = 0; i < N; ++i)
  133.     {
  134.         for(short j = 0; j < N; ++j)
  135.         {
  136.             switch(plansza[i][j])
  137.             {
  138.             case pole::puste:
  139.                 cout << ' ';
  140.                 break;
  141.             case pole::mur:
  142.                 cout << '#';
  143.                 break;
  144.             case pole::poszukiwacz:
  145.                 cout << 'O';
  146.                 break;
  147.             case pole::skarb:
  148.                 cout << 'X';
  149.                 break;
  150.             case pole::przeciwnik:
  151.                 cout << '@';
  152.                 break;
  153.             }
  154.             cout << ' ';
  155.         }
  156.         cout << endl;
  157.     }
  158. }
  159.  
  160. int main()
  161. {
  162.     uzupelnij_plansze();
  163.     wypisz_plansze();
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement