Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // obiekt.h
- #ifndef OBIEKT_H
- #define OBIEKT_H
- #include <iostream>
- using namespace std;
- class Symbol //klasa abstrakcyjna
- {
- protected:
- char symbol;
- public:
- Symbol(char s) : symbol{ s } {}
- virtual void drukuj(ostream&) const = 0;
- virtual ~Symbol() = default;
- char getSymbol() const { return symbol; }
- };
- class Kolko : public Symbol
- {
- public:
- Kolko() : Symbol{ 'O' } {}
- void drukuj(ostream& os) const
- {
- os << "O ";
- }
- };
- class Krzyzyk : public Symbol
- {
- public:
- Krzyzyk() : Symbol{ 'X' } {}
- void drukuj(ostream& os) const
- {
- os << "X ";
- }
- };
- #endif // OBIEKT_H
- // gra.h
- #ifndef GRA_H
- #define GRA_H
- #include <iostream>
- #include "obiekt.h"
- using namespace std;
- class Gra
- {
- int w;
- Symbol* **tab;
- public:
- Gra(int w);
- ~Gra();
- friend ostream& operator<<(ostream &os, const Gra &g);
- void wstaw(int wiersz, int kolumna, Symbol* znak);
- void operator-=(char znak);
- };
- #endif // GRA_H
- //dodac element
- // gra.cpp
- #include "gra.h"
- Gra::Gra(int w) : w(w)
- {
- tab = new Symbol**[w];
- for (int i=0; i<w; i++)
- {
- tab[i] = new Symbol *[w] {nullptr};
- }
- }
- Gra::~Gra()
- {
- for (int i=0; i<w; i++)
- {
- for (int j=0; j<w; j++)
- {
- delete tab[i][j];
- }
- delete[] tab[i];
- }
- delete[] tab;
- }
- ostream& operator<<(ostream &os, const Gra &g)
- {
- for (int i=0; i<g.w; i++)
- {
- for (int j=0; j<g.w; j++)
- {
- if(g.tab[i][j] != nullptr)
- {
- g.tab[i][j]->drukuj(os);
- }
- else
- {
- os << ". ";
- }
- }
- os << endl;
- }
- return os;
- }
- void Gra::wstaw(int x, int y, Symbol* znak)
- {
- delete tab[y][x];
- tab[y][x] = znak;
- }
- void Gra::operator-=(char znak) {
- for (int i{}; i < w; ++i) {
- for (int j{}; j < w; ++j) {
- if (tab[i][j]) {
- if (tab[i][j]->getSymbol() == znak) {
- delete tab[i][j];
- tab[i][j] = nullptr;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment