diff options
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/game_state.cpp | 130 | ||||
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 135 |
2 files changed, 135 insertions, 130 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 1a7310d..11e74ff 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -82,7 +82,6 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc) : // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { - vector<card> deck = gen_shuffled_deck(seed, rules.max_rank); // If there is a hole, moves the ace of spades to it @@ -163,135 +162,6 @@ void game_state::undo_move(const move m) { } -/////////////////////////// -// LEGAL MOVE GENERATION // -/////////////////////////// - -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 - || rem_ref == waste - || 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); - } - } - - // Any card can be added to the waste - if (waste != PILE_REF_MAX && waste != rem_ref) { - moves.emplace_back(rem_ref, waste); - } - - // 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); - } - } - - 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); -} - - //////////////////////// // INSPECT GAME STATE // //////////////////////// diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp new file mode 100644 index 0000000..27d7c8f --- /dev/null +++ b/src/main/game/game_state.legal_moves.cpp @@ -0,0 +1,135 @@ +// +// 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 + || rem_ref == waste + || 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); + } + } + + // Any card can be added to the waste + if (waste != PILE_REF_MAX && waste != rem_ref) { + moves.emplace_back(rem_ref, waste); + } + + // 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); + } + } + + 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); +} |
