MeehoweCK

Untitled

May 30th, 2024
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. // obiekt.h
  2. #ifndef OBIEKT_H
  3. #define OBIEKT_H
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class Symbol //klasa abstrakcyjna
  8. {
  9. protected:
  10.     char symbol;
  11. public:
  12.     Symbol(char s) : symbol{ s } {}
  13.     virtual void drukuj(ostream&) const = 0;
  14.     virtual ~Symbol() = default;
  15.     char getSymbol() const { return symbol; }
  16. };
  17.  
  18. class Kolko : public Symbol
  19. {
  20. public:
  21.     Kolko() : Symbol{ 'O' } {}
  22.     void drukuj(ostream& os) const
  23.     {
  24.         os << "O ";
  25.     }
  26. };
  27.  
  28. class Krzyzyk : public Symbol
  29. {
  30. public:
  31.     Krzyzyk() : Symbol{ 'X' } {}
  32.     void drukuj(ostream& os) const
  33.     {
  34.         os << "X ";
  35.     }
  36. };
  37. #endif // OBIEKT_H
  38.  
  39. // gra.h
  40. #ifndef GRA_H
  41. #define GRA_H
  42. #include <iostream>
  43. #include "obiekt.h"
  44. using namespace std;
  45.  
  46. class Gra
  47. {
  48.     int w;
  49.     Symbol* **tab;
  50. public:
  51.     Gra(int w);
  52.     ~Gra();
  53.     friend ostream& operator<<(ostream &os, const Gra &g);
  54.     void wstaw(int wiersz, int kolumna, Symbol* znak);
  55.     void operator-=(char znak);
  56. };
  57. #endif // GRA_H
  58.  
  59. //dodac element
  60.  
  61. // gra.cpp
  62. #include "gra.h"
  63.  
  64. Gra::Gra(int w) : w(w)
  65. {
  66.     tab = new Symbol**[w];
  67.     for (int i=0; i<w; i++)
  68.     {
  69.         tab[i] = new Symbol *[w] {nullptr};
  70.     }
  71. }
  72.  
  73. Gra::~Gra()
  74. {
  75.     for (int i=0; i<w; i++)
  76.     {
  77.         for (int j=0; j<w; j++)
  78.         {
  79.             delete tab[i][j];
  80.         }
  81.         delete[] tab[i];
  82.     }
  83.     delete[] tab;
  84. }
  85.  
  86. ostream& operator<<(ostream &os, const Gra &g)
  87. {
  88.     for (int i=0; i<g.w; i++)
  89.     {
  90.         for (int j=0; j<g.w; j++)
  91.         {
  92.             if(g.tab[i][j] != nullptr)
  93.             {
  94.                 g.tab[i][j]->drukuj(os);
  95.             }
  96.             else
  97.             {
  98.                 os << ". ";
  99.             }
  100.         }
  101.         os << endl;
  102.     }
  103.     return os;
  104. }
  105.  
  106. void Gra::wstaw(int x, int y, Symbol* znak)
  107. {
  108.     delete tab[y][x];
  109.     tab[y][x] = znak;
  110. }
  111.  
  112. void Gra::operator-=(char znak) {
  113.     for (int i{}; i < w; ++i) {
  114.         for (int j{}; j < w; ++j) {
  115.             if (tab[i][j]) {
  116.                 if (tab[i][j]->getSymbol() == znak) {
  117.                     delete tab[i][j];
  118.                     tab[i][j] = nullptr;
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment