aboutsummaryrefslogtreecommitdiff
path: root/include/game.hpp
blob: 031f4fed7a5c45f4b89e471a0d56b7a4c6b100ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef GAME_HPP
# define GAME_HPP

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>

class Game
{
public:
    Game(std::string fmt);
    Game(std::ifstream &file);
    ~Game();

    enum Direction
    {
        DirectionUp,
        DirectionDown,
        DirectionLeft,
        DirectionRight,
    };

    enum Cell
    {
        CellEmpty = 0,
        CellWall,
        CellCrate,
        CellPayload,
        CellCrateSolved
    };

    struct Position
    {
        size_t y, x;
    };

    bool     won();
    void     move(Direction direction);
    void     undo();
    void     reset();
    Cell     get(int y, int x) const;
    Cell     get(Position pos) const;
    size_t   getHeight() const;
    size_t   getWidth() const;
    Position const &getPlayer() const;
    Direction getPlayerDirection() const;

private:

    size_t    m_width;
    size_t    m_height;
    Cell      **m_grid;
    Position  m_playerPos;
    Direction m_playerDirection;
    std::vector<Position> m_cratePos;
    std::vector<Position> m_payloadPos;
    std::stack< std::pair<Position, std::vector<Position> > > m_history;

    void construct(std::string fmt);
    void findWidth(std::string fmt);
    bool tryMoveCrate(Position &pos, Direction direction);
    bool validPosition(Position pos);

    static Position makePos(int y, int x);
};

bool operator==(Game::Position const &a, Game::Position const &b);
bool operator!=(Game::Position const &a, Game::Position const &b);

#endif