aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/game_state.legal_moves.cpp
blob: f0502d1013a3ebb0f6e572f0b55511c432361f32 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
// Created by thecharlesblake on 1/3/18.
//

#include <boost/optional.hpp>

#include "game_state.h"

using namespace std;
using namespace boost;

typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;

vector<game_state::move> game_state::get_legal_moves(
#ifndef NO_CARD_JUST_MOVED
        move parent_move
#endif
) {
    // The next legal moves
    vector<move> moves;

    // If we have a stock that can deal to the tableau piles, adds this move
    if (rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty()) {
        // Only does this if all the tableau piles are empty
        bool no_tableau_empty = true;
        for (auto tab_ref : tableau_piles) {
            if (piles[tab_ref].empty()) {
                no_tableau_empty = false;
                break;
            }
        }
        if (no_tableau_empty) {
            moves.emplace_back(get_stock_tableau_move());
        }
    }

    // Cycles through each pile which we may be able to remove a card from
    for (pile_ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) {
        // Never removes a card from the hole, an empty pile, or the pile we
        // just moved a card to
        if ((rules.hole && rem_ref == hole)
            || piles[rem_ref].empty()
#ifndef NO_CARD_JUST_MOVED
            || (rem_ref == parent_move.to
                && parent_move.count <= 1
                && parent_move.from != stock)
#endif
                ) continue;

        // If we have a foundations card as the rem_ref
        if (rules.foundations
            && rem_ref >= foundations.front()
            && rem_ref <= foundations.back()) {

            // If the foundations are removable, checks to see if a dominance
            // prevents us from removing from it. Otherwise, skips the pile ref
            if (!rules.foundations_removable
#ifndef NO_AUTO_FOUNDATIONS
                || dominance_blocks_foundation_move(rem_ref)
#endif
                    ) {
                continue;
            }
        }

        // Stock cards can only be moved to the waste (assuming there is a waste)
        if (rules.stock_size > 0 && rem_ref == stock) {
            if (rules.stock_deal_t == sdt::WASTE) {
                moves.emplace_back(stock, waste);
            }
            continue;
        }

        // For each tableau pile, check if moving from the rem pile is a valid
        // tableau move according to the rules
        for (auto add_ref : tableau_piles) {
            if (is_valid_tableau_move(rem_ref, add_ref)) {
                moves.emplace_back(rem_ref, add_ref);
            }
        }

        // Any card can be moved to a cell, so long as the cell isn't full
        for (auto add_ref : cells) {
            if (add_ref != rem_ref && piles[add_ref].empty()) {
                moves.emplace_back(rem_ref, add_ref);
            }
        }

        // For each foundation pile, check if moving from the rem pile is a valid
        // move according to the rules
        for (auto add_ref : foundations) {
            if (is_valid_foundations_move(rem_ref, add_ref)) {
                moves.emplace_back(rem_ref, add_ref);
            }
        }

        // Does the same for the hole
        if (rules.hole && is_valid_hole_move(rem_ref)) {
            moves.emplace_back(rem_ref, hole);
        }

        // Note that the stock and the reserve aren't mentioned here, as
        // they cannot be added to
    }

    if (rules.move_built_group) {
        get_built_group_moves(moves);
    }

    // If only complete built piles can be moved to the foundations,
    if (rules.foundations_comp_piles) {
        for (pile_ref tab_pr : tableau_piles) {
            if (is_ordered_pile(tab_pr)) {

                for (pile_ref found_pr : foundations) {
                    if (piles[found_pr].empty()) {
                        moves.emplace_back(tab_pr, found_pr, rules.max_rank);
                        break;
                    }
                }
            }
        }
    }


    return moves;
}

// This is a special kind of move which the solver handles differently. Hence
// the arbitrary second param. We must supply the number of stock cards
// that we will deal, so that the move can be undone in backtracking
game_state::move game_state::get_stock_tableau_move() const {
    pile::size_type stock_moves =
            piles[stock].size() >= tableau_piles.size()
            ? pile::size_type(tableau_piles.size())
            : piles[stock].size();

    assert(stock_moves > 0 && stock_moves <= piles[stock].size());
    return {stock, 255, stock_moves};
}

// If a tableau pile is empty and the space policy is
// no-build, do not add. Otherwise any card with the right
// suit/rank can be moved to a tableau
bool game_state::is_valid_tableau_move(const pile_ref rem_ref,
                                       const pile_ref add_ref) const {
    if (rem_ref == add_ref) return false;

    else if (rules.build_pol == pol::NO_BUILD) return false;

    else if (piles[add_ref].empty()) {
        return rules.spaces_pol == s_pol::ANY;
    }

    card rem_c = piles[rem_ref].top_card();
    card add_c = piles[add_ref].top_card();

    // Checks violation of same suit policy
    if (rules.build_pol == pol::SAME_SUIT
        && rem_c.get_suit() != add_c.get_suit()) {
        return false;
    }

        // Checks violation of red-black suit policy
    else if (rules.build_pol == pol::RED_BLACK
             && rem_c.get_colour() == add_c.get_colour()) {
        return false;
    }

        // Checks rank
    else if (rem_c.get_rank() + 1 != add_c.get_rank()) {
        return false;
    } else {
        return true;
    }
}

bool game_state::is_valid_foundations_move(const pile_ref rem_ref,
                                           const pile_ref add_ref) const {
    if (rules.foundations_comp_piles || rem_ref == add_ref) return false;

    card rem_c = piles[rem_ref].top_card();

    // Checks violation of same suit policy
    uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4);
    if (rem_c.get_suit() != suit_idx) {
        return false;
    }

        // Checks rank
    else {
        card::rank_t add_rank = piles[add_ref].empty()
                                ? card::rank_t(0)
                                : piles[add_ref].top_card().get_rank();
        return rem_c.get_rank() - 1 == add_rank;
    }
}

bool game_state::is_valid_hole_move(const pile_ref rem_ref) const {
    if (rem_ref == hole) return false;
    card::rank_t rank = piles[rem_ref].top_card().get_rank();
    card::rank_t hole_rank = piles[hole].top_card().get_rank();
    return rank + 1 == hole_rank
           || rank - 1 == hole_rank
           || (rank == rules.max_rank && hole_rank == 1)
           || (rank == 1 && hole_rank == rules.max_rank);
}


///////////////////////
// BUILT-GROUP MOVES //
///////////////////////

void game_state::get_built_group_moves(vector<move>& moves) const {
    assert(rules.built_group_pol != pol::NO_BUILD);

    // Cycles through each pile to see if it contains a built group of size > 1
    for (auto rem_ref : tableau_piles) {
        if (piles[rem_ref].size() < 2) continue;

        auto built_group_height = get_built_group_height(rem_ref);
        if (built_group_height == 1) continue;

        // We have found a built group, so cycles through each pile to see if
        // this group can be added to it
        for (auto add_ref : tableau_piles) {
            if (add_ref == rem_ref) continue;

            card bg_low = piles[rem_ref].top_card();
            card bg_high = piles[rem_ref][built_group_height - 1];

            // If the pile is empty, and we can move to empty piles, does so
            if (piles[add_ref].empty()) {
                if (rules.spaces_pol == s_pol::ANY) {
                    add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high);
                }
            }
                // Otherwise, checks to see if it is a valid move
            else {
                card add_card = piles[add_ref].top_card();

                // Adds the move to the list of moves, as a special built group move
                if (valid_built_group_move(bg_low, bg_high, add_card)) {
                    add_built_group_move(moves, rem_ref, add_ref);
                }
            }
        }
    }
}

// Finds the size of the built group at the top of a pile
pile::size_type game_state::get_built_group_height(pile_ref ref) const {

    // The index of the currently examined card, the current card, previous card
    // and whether we currently have a valid built group.
    pile::size_type group_card_idx = 0;
    bool built_group = true;

    do {
        card prev_c = piles[ref][group_card_idx++];
        card c = piles[ref][group_card_idx];

        // If the build policy is same suit, makes sure group has same suit
        if (rules.built_group_pol == pol::SAME_SUIT
            && c.get_suit() != prev_c.get_suit()) {
            built_group = false;
        }
            // If the build policy is red black, makes sure group alternates
            // in correct phase
        else if (rules.built_group_pol == pol::RED_BLACK
                 && c.get_colour() == prev_c.get_colour()) {
            built_group = false;
        }

        // Makes sure the rank of the cards is sequential
        built_group = built_group
                      && c.get_rank() == prev_c.get_rank()+1;

    }
        // Continues until group_card_idx points to a card outside the built
        // Group
    while (built_group && group_card_idx < piles[ref].size());

    return group_card_idx;
}

bool game_state::valid_built_group_move(card bg_low, card bg_high, card add_card)
const {
    // The rank of the card we're moving the built group to must be greater than
    // the second-from-top card of the built group, and less than or equal to the
    // card that would come next at the top of the built pile
    if (add_card.get_rank() <= bg_low.get_rank() + 1
        || add_card.get_rank() > bg_high.get_rank() + 1) {
        return false;
    }

    // Checks to see if the suits are correct
    switch (rules.built_group_pol) {
        case pol::SAME_SUIT:
            return bg_low.get_suit() == add_card.get_suit();

        case pol::RED_BLACK: {
            bool same_suit = bg_low.get_suit() == add_card.get_suit();
            bool even_diff = (add_card.get_rank() - bg_low.get_rank()) % 2 == 0;
            return (same_suit && even_diff) || (!same_suit && !even_diff);
        }
        default: return true;
    }
}

void game_state::add_built_group_move(vector<move>& moves, pile_ref rem_ref,
                                      pile_ref add_ref) const {
    moves.emplace_back(rem_ref, add_ref, 2);
}

void game_state::add_empty_built_group_moves(vector<move>& moves,
                                             pile_ref rem_ref,
                                             pile_ref add_ref,
                                             card bg_high) const {
    // Loops through each possible built group move to an empty pile and adds it
    // to the list
    pile::size_type card_idx = 1;
    do {
        moves.emplace_back(
                rem_ref, add_ref, static_cast<pile_ref>(card_idx + 1)
        );
    } while (piles[rem_ref][card_idx++] != bg_high);
}

/////////////////////
// Dominance Moves //
/////////////////////

// Returns a dominance move if one is available
optional<game_state::move> game_state::get_dominance_move() const {
#ifndef NO_AUTO_FOUNDATIONS
    // If there are 2 decks or no foundations, return
    if (!rules.foundations || rules.two_decks)
        return none;

    // Cycles through the piles and sees if any cards can be automatically moved
    // to the foundations
    for (pile_ref pr = 0; pr < piles.size(); pr++) {
        // Don't move foundation cards, hole or stock cards to the foundations
        if ((pr >= foundations.front() && pr <= foundations.back())
            || (rules.hole && pr == hole)
            || (rules.stock_size > 0 && pr == stock)
            || (piles[pr].empty())) {
            continue;
        }

        card c = piles[pr].top_card();
        pile_ref target_foundation = foundations[c.get_suit()];

        // If the card is the right rank and the auto-move boolean is true, then
        // returns the move
        card::rank_t target_rank = piles[target_foundation].empty()
                                   ? card::rank_t(1) :
                                   piles[target_foundation].top_card().get_rank()
                                   + card::rank_t(1);
        if (target_rank == c.get_rank() &&
            auto_foundation_moves[c.get_suit()]) {
            return move(pr, target_foundation, move::dominance_flag);
        }
    }
#endif

    return none;
}

bool game_state::is_ordered_pile(pile_ref pr) const {
    if (piles[pr].size() != rules.max_rank) return false;

    card::suit_t s = piles[pr][0].get_suit();
    for (card::rank_t r = 0; r < rules.max_rank; r++) {
        if (piles[pr][r] != card(s, card::rank_t(r + 1))) return false;
    }
    return true;
}

// For games where the foundations can be removed from, this dominance blocks
// the foundations being removed from if the card that were to be removed would
// go 'automatically' up
bool game_state::dominance_blocks_foundation_move(pile_ref target_pile) {
    assert(!piles[target_pile].empty());

    card target_card = piles[target_pile].top_card();
    piles[target_pile].take();

    bool blocked = is_valid_auto_foundation_move(target_pile);
    piles[target_pile].place(target_card);

    return blocked;
}