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
|
//
// Created by thecharlesblake on 1/3/18.
//
#include "game_state.h"
using namespace std;
typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
vector<game_state::move> game_state::get_legal_moves() const {
// The next legal moves
vector<move> moves;
// 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, the waste, or an empty pile
if (rem_ref == hole || piles[rem_ref].empty()) continue;
// Stock cards can only be moved to the waste
if (stock != PILE_REF_MAX && rem_ref == stock) {
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 (hole != PILE_REF_MAX && 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
}
return 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 (rules.build_ord == ord::DESCENDING
&& rem_c.get_rank() + 1 != add_c.get_rank()) {
return false;
} else if (rules.build_ord == ord::ASCENDING
&& 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 (rem_ref == add_ref) return false;
card rem_c = piles[rem_ref].top_card();
// Checks violation of same suit policy
if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) {
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);
}
|