aboutsummaryrefslogtreecommitdiff
path: root/src/main/game_state.cpp
blob: 28f6d3b4b0f663494fc205f0f4173d26ac665719 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// Created by thecharlesblake on 10/24/17.
//

#include <vector>
#include <ostream>
#include <numeric>
#include <random>
#include <algorithm>

#include <rapidjson/document.h>

#include "game_state.h"

using namespace rapidjson;
using namespace std;

// Construct an initial game state from a JSON doc
game_state::game_state(const Document& doc, string sol_type)
        : max_rank(1) {
    max_rank = sol_type == "simple-black-hole" ? 7 : 13;

    // Construct tableau piles
    assert(doc.HasMember("tableau piles"));
    const Value& json_tab_piles = doc["tableau piles"];
    assert(json_tab_piles.IsArray());

    for (auto& json_tab : json_tab_piles.GetArray()) {
        vector<card> tableau_pile;

        for (auto& json_card : json_tab.GetArray()) {
            card c(json_card.GetString());
            if (c.get_rank() > max_rank) max_rank = c.get_rank();
            tableau_pile.push_back(c);
        }

        tableau_piles.push_back(tableau_pile);
    }

    // Assign hole card
    assert(doc.HasMember("hole card"));
    const Value& json_h_card = doc["hole card"];
    assert(json_h_card.IsString());
    hole_card = json_h_card.GetString();
}

// Construct an initial game state from a seed
game_state::game_state(int seed, string sol_type) {
    max_rank = sol_type == "simple-black-hole" ? 7 : 13;

    vector<card> deck = shuffled_deck(seed, max_rank);

    for (vector<card>::size_type i = 0; i < deck.size(); i++) {
        // Don't add the Ace of Spades to the hole card
        card c = deck[i];
        if (c == "AS") continue;

        // Add the randomly generated card to the tableau piles
        if (i % 3 == 0) tableau_piles.push_back(vector<card>());
        tableau_piles[i / 3].push_back(deck[i]);
    }

    hole_card = "AS";
}

// Generate a randomly ordered vector of cards
vector<card> game_state::shuffled_deck(int seed, int max_rank = 13) {
    vector<int*> v;
    for (int i = 0; i < max_rank * 4; i++) {
        v.push_back(new int(i));
    }

    // Randomly shuffle the pointers
    auto rng = std::default_random_engine(seed);
    std::shuffle(std::begin(v), std::end(v), rng);

    vector<card> deck;
    for (int *i : v) {
        int r = ((*i) % max_rank) + 1;
        int s = (*i) / max_rank;

        deck.push_back(card(r, s));
    }

    // release memory
    for (int i = 0; i < max_rank * 4; i++) {
        delete v[i];
    }

    return deck;
}


vector<game_state> game_state::get_next_legal_states() const {
    vector<game_state> next;

    // Searches through each column for a top card that can be moved to the hole
    bool all_tableaux_empty = true;
    for (vector<vector<card>>::size_type i = 0; i < tableau_piles.size(); i++) {
        vector<card> tableau_pile = tableau_piles[i];

        // Ignore empty piles
        if (tableau_pile.empty()) {
            continue;
        } else {
            all_tableaux_empty = false;
        }
        card top_card = tableau_pile.back();

        // If a card at the top of a tableau pile is adjacent to the hole card,
        // move it to the hole and add that state
        if (adjacent(top_card, hole_card)) {
            game_state s = *this;
            s.move_to_hole(i);
            next.push_back(s);
        }
    }

    // If there are no cards left in play, deal solved
    solved = all_tableaux_empty;

    return next;
}

bool game_state::adjacent(const card& a, const card& b) const {
    int x = a.get_rank();
    int y = b.get_rank();

    return x == y + 1
           || x + 1 == y
           || (x == 1 && y == max_rank)
           || (x == max_rank && y == 1);
}

void game_state::move_to_hole(int tab_idx) {
    hole_card = tableau_piles[tab_idx].back();
    tableau_piles[tab_idx].pop_back();
}

bool game_state::is_solved() const {
    return solved;
}

ostream& game_state::print(ostream& stream) const {
    bool empty_row = false;
    vector<card>::size_type row_idx = 0;

    while (!empty_row) {
        empty_row = true;

        for (vector<card> tableau_pile : tableau_piles) {
            if (tableau_pile.size() > row_idx) {
                empty_row = false;
                stream << tableau_pile[row_idx];
            }
            stream << "\t";
        }

        if (!empty_row) stream << "|";
        if (row_idx == 0) {
            stream << " Hole card: " << hole_card;
        }

        stream << "\n";
        row_idx++;
    }

    return stream;
}

ostream& operator <<(ostream& stream, const game_state& gs) {
    return gs.print(stream);
}