aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/game_state.h
blob: 6aa391891f36930b77fb9e6bfcd3d9187afc6eb0 (plain) (blame)
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
74
75
76
77
78
79
//
// Created by thecharlesblake on 10/24/17.
//

#ifndef SOLVITAIRE_GAME_STATE_H
#define SOLVITAIRE_GAME_STATE_H

#define NULL_MOVE (game_state::move(PILE_REF_MAX, PILE_REF_MAX))
#define PILE_REF_MAX (255)

#include <vector>
#include <string>

#include <rapidjson/document.h>
#include <boost/functional/hash.hpp>

#include "card.h"
#include "pile.h"
#include "sol_rules.h"

class deal_parser;

class game_state {
    friend class deal_parser;
    friend class state_printer;
public:
    // Defines types
    typedef uint8_t pile_ref;
    typedef std::pair<pile_ref, pile_ref> move;

    // Constructors
    // Creates a game state representation from a JSON doc
    game_state(const sol_rules&, const rapidjson::Document&);
    // Does the same from a 'rules' object
    game_state(const sol_rules&, int seed);

    // Alters state
    void make_move(move);
    void undo_move(move);

    std::vector<move> get_legal_moves() const;

    // Inspects state
    bool is_solved() const;
    const std::vector<pile>& get_data() const;

    friend bool operator==(const game_state&, const game_state&);
    friend std::ostream& operator<< (std::ostream&, const game_state&);

    friend std::size_t hash_value(game_state const&);
    friend std::size_t hash_value(std::vector<pile> const&);

private:
    static std::vector<card> gen_shuffled_deck(int, int);

    // Private constructor
    explicit game_state(const sol_rules&);

    // Used in get_legal_moves()
    bool is_valid_tableau_move(pile_ref, pile_ref) const;
    bool is_valid_foundations_move(pile_ref, pile_ref) const;
    bool is_valid_hole_move(pile_ref) const;

    // References to piles
    sol_rules rules;
    std::vector<pile_ref> tableau_piles;
    std::vector<pile_ref> cells;
    pile_ref stock;
    pile_ref waste;
    std::vector<pile_ref> reserve;
    std::vector<pile_ref> foundations;
    pile_ref hole;

    // The core piles
    std::vector<pile> piles;
};


#endif //SOLVITAIRE_GAME_STATE_H