MeehoweCK

Untitled

Mar 25th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.60 KB | None | 0 0
  1. // Head.h
  2. #ifndef HEAD_H
  3. #define HEAD_H
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. class Gra;
  10. class Gracz;
  11. class Gracz_cmp;
  12. class Gracz_user;
  13. class Interfejs;
  14. class Plansza;
  15.  
  16. struct wspolrzedne
  17. {
  18.     int x;
  19.     int y;
  20. };
  21.  
  22. wspolrzedne konwertuj(string);
  23.  
  24. #endif // HEAD_H
  25.  
  26. // Funkcje.cpp
  27. #include "Head.h"
  28.  
  29. wspolrzedne konwertuj(string pole)
  30. {
  31.     wspolrzedne wynik;
  32.     wynik.x = static_cast<int>(pole[0] - 65);
  33.     wynik.y = static_cast<int>(pole[1] - 48);
  34.     return wynik;
  35. }
  36.  
  37. // main.cpp
  38. #include "Interfejs.h"
  39.  
  40. using namespace std;
  41.  
  42. int main()
  43. {
  44.     Interfejs interfejs;
  45.     interfejs.start();
  46.  
  47.     return 0;
  48. }
  49.  
  50. // Gra.h
  51. #ifndef GRA_H
  52. #define GRA_H
  53.  
  54. #include "Head.h"
  55.  
  56. class Gra
  57. {
  58.     friend class Gracz_cmp;
  59.     friend class Gracz_user;
  60.     friend class Interfejs;
  61. public:
  62.     Gra(Plansza*, Gracz*, Gracz*);
  63.     void graj();
  64. private:
  65.     bool aktywna;
  66.     Gracz* gracz1;
  67.     Gracz* gracz2;
  68.     Plansza* plansza;
  69. };
  70.  
  71. #endif // GRA_H
  72.  
  73. // Gra.cpp
  74. #include <conio.h>
  75. #include <iostream>
  76. #include "Gra.h"
  77. #include "Gracz.h"
  78. #include "Plansza.h"
  79.  
  80. using namespace std;
  81.  
  82. /*class Gra
  83. {
  84.     friend class Gracz_cmp;
  85.     friend class Gracz_user;
  86.     friend class Interfejs;
  87. public:
  88.     Gra(Plansza*, Gracz*, Gracz*);
  89.     void graj();
  90. private:
  91.     bool aktywna;
  92.     Gracz* gracz1;
  93.     Gracz* gracz2;
  94.     Plansza* plansza;
  95. };*/
  96.  
  97. Gra::Gra(Plansza* board, Gracz* p1, Gracz* p2) : plansza(board), gracz1(p1), gracz2(p2), aktywna(true)
  98. {
  99. }
  100.  
  101. void Gra::graj()
  102. {
  103.     cout << "Uruchamiam gre\n";
  104.     while (aktywna)
  105.     {
  106.         plansza->wyswietl();
  107.         gracz1->ruch();
  108.         if (plansza->czy_koniec())
  109.         {
  110.             aktywna = false;
  111.             break;
  112.         }
  113.         plansza->wyswietl();
  114.         gracz2->ruch();
  115.         if (plansza->czy_koniec())
  116.         {
  117.             aktywna = false;
  118.             break;
  119.         }
  120.         cout << "Tura zostala wykonana, wcisnij dowolny klawisz...\n";
  121.         _getch();
  122.     }
  123. }
  124.  
  125. // Gracz.h
  126. #ifndef GRACZ_H
  127. #define GRACZ_H
  128.  
  129. #include "Head.h"
  130.  
  131. class Gracz
  132. {
  133. public:
  134.     Gracz(char);
  135.     virtual void ruch() = 0;
  136.     void set_gra(Gra*);
  137. protected:
  138.     char symbol;
  139.     Gra* gra;
  140. };
  141.  
  142. #endif // GRACZ_H
  143.  
  144. // Gracz.cpp
  145. #include "Gracz.h"
  146.  
  147. /*class Gracz
  148. {
  149. public:
  150.     Gracz(char);
  151.     virtual void ruch() = 0;
  152.     void set_gra(Gra*);
  153. protected:
  154.     char symbol;
  155.     Gra* gra;
  156. };*/
  157.  
  158. Gracz::Gracz(char znak) : symbol(znak), gra(nullptr)
  159. {
  160. }
  161.  
  162. void Gracz::set_gra(Gra* game)
  163. {
  164.     gra = game;
  165. }
  166.  
  167. // Gracz_cmp.h
  168. #ifndef GRACZ_CMP_H
  169. #define GRACZ_CMP_H
  170.  
  171. #include "Gracz.h"
  172. #include "Head.h"
  173.  
  174. class Gracz_cmp : public Gracz
  175. {
  176. public:
  177.     Gracz_cmp(char);
  178.     void ruch();
  179. };
  180.  
  181. #endif // GRACZ_CMP_H
  182.  
  183. // Gracz_cmp.cpp
  184. #include <conio.h>
  185. #include <ctime>
  186. #include <iostream>
  187. #include "Gra.h"
  188. #include "Gracz_cmp.h"
  189. #include "Plansza.h"
  190.  
  191. using namespace std;
  192.  
  193. /*class Gracz_cmp : public Gracz
  194. {
  195. public:
  196.     Gracz_cmp(char);
  197.     void ruch();
  198. };*/
  199.  
  200. Gracz_cmp::Gracz_cmp(char znak) : Gracz(znak)
  201. {
  202. }
  203.  
  204. void Gracz_cmp::ruch()
  205. {
  206.     cout << "Komputer wykonuje ruch\n";
  207.     gra->plansza->lista_wolnych_pol();
  208.     //gra->plansza->wypisz_wolne_pola();
  209.     srand(static_cast<unsigned>(time(nullptr)));
  210.  
  211.     int wybor = rand() % gra->plansza->wolne_pola.size();
  212.     gra->plansza->zapelnij_pole(gra->plansza->wolne_pola[wybor], symbol);
  213.     cout << "Ruch komputera wykonany, wcisnij dowolny klawisz...\n";
  214.     _getch();
  215. }
  216.  
  217. // Gracz_user.h
  218. #ifndef GRACZ_USER_H
  219. #define GRACZ_USER_H
  220.  
  221. #include <iostream>
  222. #include "Gracz.h"
  223. #include "Head.h"
  224.  
  225. using namespace std;
  226.  
  227. class Gracz_user : public Gracz
  228. {
  229. public:
  230.     Gracz_user(string, char);
  231.     void ruch();
  232. private:
  233.     string imie;
  234. };
  235.  
  236. #endif // GRACZ_USER_H
  237.  
  238. // Gracz_user.cpp
  239. #include "Gra.h"
  240. #include "Gracz_user.h"
  241. #include "Plansza.h"
  242.  
  243. /*class Gracz_user : public Gracz
  244. {
  245. public:
  246.     Gracz_user(string, char);
  247.     void ruch();
  248. private:
  249.     string imie;
  250. };*/
  251.  
  252. Gracz_user::Gracz_user(string name, char znak) : Gracz(znak), imie(name)
  253. {
  254. }
  255.  
  256. void Gracz_user::ruch()
  257. {
  258.     cout << "Uzytkownik wykonuje ruch\n";
  259.     cout << "Podaj wspolrzedne, gdzie chcesz wstawic " << symbol << ": (np. B3)";
  260.     string pole;
  261.     cin >> pole;
  262.  
  263.     // !!!! wpisaæ sprawdzenie wspó³rzêdnych!
  264.     wspolrzedne xy = konwertuj(pole);
  265.     gra->plansza->zapelnij_pole(xy, symbol);
  266.     cout << "Ruch gracza wykonany, wcisnij dowolny klawisz...\n";
  267. }
  268.  
  269. // Interfejs.h
  270. #ifndef INTERFEJS_H
  271. #define INTERFEJS_H
  272.  
  273. #include "Head.h"
  274.  
  275. class Interfejs
  276. {
  277. public:
  278.     Interfejs();
  279.     ~Interfejs();
  280.     void start();
  281. private:
  282.     Gra* gra;
  283.     Gracz* gracz1;
  284.     Gracz* gracz2;
  285.     Plansza* plansza;
  286. };
  287.  
  288. #endif // INTERFEJS_H
  289.  
  290. // Interfejs.cpp
  291. #include <conio.h>
  292. #include <iostream>
  293. #include "Gra.h"
  294. #include "Gracz_cmp.h"
  295. #include "Gracz_user.h"
  296. #include "Interfejs.h"
  297. #include "Plansza.h"
  298.  
  299. using namespace std;
  300.  
  301. /*class Interfejs
  302. {
  303. public:
  304.     Interfejs();
  305.     ~Interfejs();
  306.     void start();
  307. private:
  308.     Gra* gra;
  309.     Gracz* gracz1;
  310.     Gracz* gracz2;
  311.     Plansza* plansza;
  312. };*/
  313.  
  314. Interfejs::Interfejs() : plansza(nullptr), gra(nullptr), gracz1(nullptr), gracz2(nullptr)
  315. {
  316. }
  317.  
  318. Interfejs::~Interfejs()
  319. {
  320.     if (plansza)
  321.         delete plansza;
  322.     if (gra)
  323.         delete gra;
  324.     if (gracz1)
  325.         delete gracz1;
  326.     if (gracz2)
  327.         delete gracz2;
  328.     cout << "Intefejs usuniety\n";
  329. }
  330.  
  331. void Interfejs::start()
  332. {
  333.     cout << "Wpisz swoje imie: ";
  334.     string name;
  335.     cin >> name;
  336.  
  337.     cout << "Kto ma zaczynac gre? (U - uzytkownik, K - komputer): ";
  338.     char wybor;
  339.     do
  340.     {
  341.         wybor = _getch();
  342.     } while (wybor != 'k' && wybor != 'K' && wybor != 'u' && wybor != 'U');
  343.     wybor = toupper(wybor);
  344.     cout << wybor << endl;
  345.  
  346.     if (wybor == 'K')        // zaczyna komputer
  347.     {
  348.         gracz1 = new Gracz_cmp('O');
  349.         gracz2 = new Gracz_user(name, 'X');
  350.     }
  351.  
  352.     else                    // zaczyna u¿ytkownik
  353.     {
  354.         gracz1 = new Gracz_user(name, 'O');
  355.         gracz2 = new Gracz_cmp('X');
  356.     }
  357.  
  358.     cout << "Okresl jak duza ma byc plansza (minimum 5, maksimum 26): ";
  359.     int rozmiar;
  360.     cin >> rozmiar;
  361.     while (rozmiar < 5 || rozmiar > 26)
  362.     {
  363.         cout << "Podany przez Ciebie rozmiar nie jest prawidlowy, wpisz jeszcze raz: ";
  364.         cin >> rozmiar;
  365.     }
  366.  
  367.     // tworzymy planszę
  368.     plansza = new Plansza(rozmiar);
  369.  
  370.     // tworzymy grę
  371.     gra = new Gra(plansza, gracz1, gracz2);
  372.     gracz1->set_gra(gra);
  373.     gracz2->set_gra(gra);
  374.  
  375.     gra->graj();
  376. }
  377.  
  378. // Plansza.h
  379. #ifndef PLANSZA_H
  380. #define PLANSZA_H
  381.  
  382. #include <vector>
  383. #include "Head.h"
  384.  
  385. using namespace std;
  386.  
  387. class Plansza
  388. {
  389.     friend class Gracz_cmp;
  390.     friend class Gracz_user;
  391. private:
  392.     char** tablica;
  393.     int rozmiar;
  394.     vector<wspolrzedne> wolne_pola;
  395. public:
  396.     Plansza(int);
  397.     ~Plansza();
  398.     bool czy_koniec();
  399.     bool czywolne(wspolrzedne);
  400.     void lista_wolnych_pol();
  401.     void wypisz_wolne_pola();
  402.     void wyswietl();
  403.     void zapelnij_pole(wspolrzedne, char);
  404. };
  405.  
  406. #endif // PLANSZA_H
  407.  
  408. // Plansza.cpp
  409. #include <iostream>
  410. #include "Plansza.h"
  411.  
  412. using namespace std;
  413.  
  414. /*class Plansza
  415. {
  416.     friend class Gracz_cmp;
  417.     friend class Gracz_user;
  418. private:
  419.     char** tablica;
  420.     int rozmiar;
  421.     vector<wspolrzedne> wolne_pola;
  422. public:
  423.     Plansza(int);
  424.     ~Plansza();
  425.     bool czy_koniec();
  426.     bool czywolne(wspolrzedne);
  427.     void lista_wolnych_pol();
  428.     void wypisz_wolne_pola();
  429.     void wyswietl();
  430.     void zapelnij_pole(wspolrzedne, char);
  431. };*/
  432.  
  433. Plansza::Plansza(int wielkosc) : rozmiar(wielkosc)
  434. {
  435.     tablica = new char* [rozmiar];
  436.     for (int i = 0; i < rozmiar; ++i)
  437.     {
  438.         tablica[i] = new char[rozmiar];
  439.         for (int j = 0; j < rozmiar; ++j)
  440.             tablica[i][j] = ' ';
  441.     }
  442. }
  443. Plansza::~Plansza()
  444. {
  445.     for (int i = 0; i < rozmiar; ++i)
  446.     {
  447.         delete[] tablica[i];
  448.     }
  449.     delete[] tablica;
  450.     cout << "Plansza zostala usunieta" << endl;
  451. }
  452.  
  453. bool Plansza::czy_koniec()
  454. {
  455.     cout << "Sprawdzam czy gra ma byc kontynuowana\n";
  456.     return false;
  457. }
  458.  
  459. bool Plansza::czywolne(wspolrzedne xy)
  460. {
  461.     if (tablica[xy.x][xy.y] == ' ')
  462.         return true;
  463.     return false;
  464. }
  465.  
  466. void Plansza::lista_wolnych_pol()
  467. {
  468.     wspolrzedne xy;
  469.     for (int i = 0; i < rozmiar; ++i)
  470.         for (int j = 0; j < rozmiar; ++j)
  471.         {
  472.             xy.x = i;
  473.             xy.y = j;
  474.             if (czywolne(xy))
  475.                 wolne_pola.push_back(xy);
  476.         }
  477. }
  478.  
  479. void Plansza::wypisz_wolne_pola()
  480. {
  481.     for (int i = 0; i < wolne_pola.size(); ++i)
  482.     {
  483.         cout << "(" << wolne_pola[i].x << "," << wolne_pola[i].y << "), ";
  484.     }
  485.     cout << endl;
  486. }
  487.  
  488. void Plansza::wyswietl()
  489. {
  490.     for (int i = 0; i < rozmiar + 2; ++i)
  491.         cout << '_';
  492.     cout << endl;
  493.     for (int i = 0; i < rozmiar; ++i)
  494.     {
  495.         cout << '|';
  496.         for (int j = 0; j < rozmiar; ++j)
  497.             cout << tablica[i][j];
  498.         cout << '|' << endl;
  499.     }
  500.     for (int i = 0; i < rozmiar + 2; ++i)
  501.         cout << '_';
  502.     cout << endl;
  503. }
  504.  
  505. void Plansza::zapelnij_pole(wspolrzedne xy, char symbol)
  506. {
  507.     tablica[xy.x][xy.y] = symbol;
  508. }
Advertisement
Add Comment
Please, Sign In to add comment