diff --git a/inc/board.hh b/inc/board.hh new file mode 100644 index 0000000..82fad2a --- /dev/null +++ b/inc/board.hh @@ -0,0 +1,43 @@ +#ifndef BOARD_HH +#define BOARD_HH + +/* Plik zawiera implementację planszy(board) do gry w warcaby. */ + +class Board +{ +private: + + // Tablica pionków + // Pawn*** board +public: + + // Konstruktor inicjujący planszę + Board() {} + + // Utwórz nowy pionek na zadanej pozycji + Pawn& createPawn(Vector position); + + // Pobierz pionek z określonej pozycji + Pawn& getPawn(Vector position); + + // Przesuń pionek na określoną pozycję(target) + void setPawnPosition(Vector position, Vector target); + + // Przesuń pionek o określony wektor + void movePawn(Vector position, Vector vector); + + // Czy ruch jest możliwy (czy dana pozycja jest osiągalna) + bool isMovementPossible(Vector position, Vector target); + + // Usuń pionek z określonej pozycji + Pawn deletePawn(Vector position); + + // Rysuj planszę wraz z pionkami + void draw(sf::RenderWindow window); + + // Destruktor + ~Board(); + +}; + +#endif diff --git a/inc/def.hh b/inc/def.hh index 5815f48..407660d 100644 --- a/inc/def.hh +++ b/inc/def.hh @@ -8,6 +8,9 @@ // Długość boku pojedynczego kafelka(który jest kwadratem) const int TILE_SIZE = 64; +// Odpowiednie przeskalowanie pionka, aby zachowany był margines względem krawędzi kafelka +const int PAWN_MARGIN = 5; + // Przedefiniowanie sf::Vector2f na RealVector typedef sf::Vector2f RealVector; diff --git a/inc/misc.hh b/inc/misc.hh index c0c7e2a..c105785 100644 --- a/inc/misc.hh +++ b/inc/misc.hh @@ -25,13 +25,17 @@ struct Vector // Operator dodawania wektorów Vector operator+(const Vector& v) { return Vector(x+v.x, y+v.y); } + Vector& operator+=(const Vector& v) { return *this = *this + v; } // Operator odejmowania wektorów Vector operator-(const Vector& v) { return Vector(x-v.x, y-v.y); } + Vector& operator-=(const Vector& v) { return *this = *this - v; } // Mnożenie wektora przez liczbę - Vector operator*(int a) { return Vector(a*v.x, a*v.y); } - Vector operator*(float a) { return Vector(a*v.x, a*v.y); } + Vector operator*(int a) { return Vector(a*x, a*y); } + Vector operator*(float a) { return Vector(a*x, a*y); } + Vector operator*=(int a) { return *this = *this*a; } + Vector operator*=(float a) { return *this = *this*a; } }; diff --git a/inc/object.hh b/inc/object.hh index 3fdd55b..3a1695f 100644 --- a/inc/object.hh +++ b/inc/object.hh @@ -7,7 +7,7 @@ * Nie definiuje ona jeszcze żadnych kształtów czy kolorów, jest więc zatem klasą * w zasadzie czysto abstrakcyjną. */ -#include +#include #include "def.hh" #include "misc.hh" @@ -20,17 +20,22 @@ private: Vector position; public: + Object(Vector _position) : position(_position) {} + + // Wirtualny destruktor + virtual ~Object() {} + // Pobierz pozycję obiektu Vector getPosition() const { return position; } // Ustaw pozycję obiektu - void setPosition(const Vector& _position) { position = _position } + void setPosition(const Vector& _position) { position = _position; } // Przesuń obiekt o wektor - void move(const Vector& vector) {} + void move(const Vector& vector) { position += vector; } // Rysuj obiekt - void draw(sf::RenderWindow window) = 0; + virtual void draw(sf::RenderWindow& window) = 0; }; #endif diff --git a/inc/pawn.hh b/inc/pawn.hh new file mode 100644 index 0000000..5de47f2 --- /dev/null +++ b/inc/pawn.hh @@ -0,0 +1,41 @@ +#ifndef PAWN_HH +#define PAWN_HH + +/* Plik implementuje klasę pionka(kamienia) jako pochodną klasy Object */ + +#include +#include + +#include "def.hh" +#include "object.hh" + +// Typ wyliczeniowy definiujący możliwe kolory +enum Color + { + CL_WHITE, + CL_BLACK + }; + +// Klasa reprezentująca pionek +class Pawn : public Object +{ +private: + + // Kolor pionka + Color color; +public: + + // Konstruktor tworzący pionek na zadanej pozycji startowej i o określonym kolorze + Pawn(Vector _position, Color _color) : Object(_position), color(_color) {} + + // Pobierz kolor + Color getColor() const { return color; } + + // Rysuj pionek na obiekcie window + void draw(sf::RenderWindow& window); + + // Destruktor + ~Pawn() {} +}; + +#endif diff --git a/src/board.cpp b/src/board.cpp new file mode 100644 index 0000000..c3ec122 --- /dev/null +++ b/src/board.cpp @@ -0,0 +1 @@ +#include "../inc/board.hh" diff --git a/src/main.cpp b/src/main.cpp index 2c49a87..109163a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,8 +3,25 @@ #include "../inc/def.hh" #include "../inc/misc.hh" +#include "../inc/object.hh" +#include "../inc/pawn.hh" int main() { + sf::ContextSettings settings; + settings.antialiasingLevel = 8; + sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Warcaby", sf::Style::Default, settings); + Pawn pawn(Vector(0, 0), CL_BLACK); + while(window.isOpen()) + { + sf::Event event; + while(window.pollEvent(event)) + { + if(event.type == sf::Event::Closed) window.close(); + } + window.clear(sf::Color(255, 255, 255)); + pawn.draw(window); + window.display(); + } return 0; } diff --git a/src/pawn.cpp b/src/pawn.cpp new file mode 100644 index 0000000..7d00fc0 --- /dev/null +++ b/src/pawn.cpp @@ -0,0 +1,16 @@ +#include "../inc/pawn.hh" + +void Pawn::draw(sf::RenderWindow& window) +{ + // Tworzymy nowy kształt koła o promieniu równym połowie długości boku kafelka i odjęciu dwóch marginesów + sf::CircleShape pawn(TILE_SIZE/2 - PAWN_MARGIN*2); + + // Ustawiamy jego pozycję na pozycję zwróconą przez getPosition() uwzględniając marginesy + pawn.setPosition(getPosition().getRealVector() + sf::Vector2f(PAWN_MARGIN, PAWN_MARGIN)); + + // Wypełniamy odpowiednim kolorem + pawn.setFillColor((color==CL_WHITE)?sf::Color::White:sf::Color::Black); + + // Rysujemy na obiekcie window + window.draw(pawn); +}