sliding-puzzle/board.h

80 lines
1.8 KiB
C
Raw Normal View History

2020-12-13 19:45:52 +01:00
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
2021-01-08 22:14:28 +01:00
#include <SFML/Graphics/RectangleShape.hpp>
2020-12-13 19:45:52 +01:00
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <array>
using pos = std::pair<int, int>;
enum class DIRECTION
{
UP,
DOWN,
RIGHT,
LEFT,
NONE
};
2020-12-14 20:22:52 +01:00
//////////////////////////////////
2020-12-13 19:45:52 +01:00
class Board
{
public:
explicit Board();
2020-12-13 19:45:52 +01:00
~Board();
2021-01-08 22:14:28 +01:00
// Set play image
bool init(const std::string& path, int splitting, const sf::RenderWindow& window);
2020-12-14 20:22:52 +01:00
// Output current graphical state on application window
2020-12-13 19:45:52 +01:00
void draw(sf::RenderWindow& window);
2020-12-14 20:22:52 +01:00
// Move cursor to next tile by given direction
2020-12-13 19:45:52 +01:00
bool moveSelection(const DIRECTION& direction);
2020-12-14 20:22:52 +01:00
// Go to or leave from selection mode
2020-12-13 19:45:52 +01:00
void onSelectionMode();
2020-12-14 20:22:52 +01:00
// Did player win the game
2020-12-13 19:45:52 +01:00
bool isWinCondition() const;
2021-01-08 22:14:28 +01:00
// Show or hide selection cursos
void setCursorVisibility(bool visible = false);
2020-12-13 19:45:52 +01:00
private:
2020-12-14 20:22:52 +01:00
// Game tile
2020-12-13 19:45:52 +01:00
struct Cell
{
std::vector<Cell*>::size_type inital_index;
std::vector<Cell*>::size_type current_index;
2020-12-13 19:45:52 +01:00
sf::Sprite *sprite;
2020-12-14 20:22:52 +01:00
static int side_length;
2020-12-13 19:45:52 +01:00
};
2020-12-14 20:22:52 +01:00
using Cells = std::vector<Cell*>;
2020-12-13 19:45:52 +01:00
2021-01-08 22:14:28 +01:00
sf::RectangleShape rect_filling;
2020-12-13 19:45:52 +01:00
sf::VertexArray rect_selection;
Cells::size_type selection_index;
Cells::size_type cells_on_width; // amount of cells on horizontal side of board
Cells::size_type cells_on_height; // amount of cells on vertical side of board
2020-12-13 19:45:52 +01:00
Cells vec_field;
2021-01-08 22:14:28 +01:00
Cells::size_type solved_tiles; // amount of tiles placed on initial positions
2020-12-13 19:45:52 +01:00
sf::Texture global_texture;
bool on_selection;
2021-01-08 22:14:28 +01:00
bool is_cursor_visible;
2020-12-13 19:45:52 +01:00
2020-12-14 20:22:52 +01:00
void swapCells(Cells::size_type curr_index, Cells::size_type swap_index);
// Draw selection cursor on given tile
2020-12-13 19:45:52 +01:00
void setSelectionVertex(Cells::size_type index);
};