From 819a1064c5142a9b2f04ff8149d34fc915c18322 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 10:31:22 +0100 Subject: Refactor legal moves gen and moves logic --- src/main/game/search-state/game_state.cpp | 558 ++++++++++++++++++++++++++++++ 1 file changed, 558 insertions(+) create mode 100644 src/main/game/search-state/game_state.cpp (limited to 'src/main/game/search-state/game_state.cpp') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp new file mode 100644 index 0000000..bd533f1 --- /dev/null +++ b/src/main/game/search-state/game_state.cpp @@ -0,0 +1,558 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#include +#include +#include +#include +#include + +#include +#include + +#include "game_state.h" +#include "document.h" +#include "../../input-output/input/json-parsing/deal_parser.h" +#include "../../input-output/output/state_printer.h" +#include "../../input-output/output/log_helper.h" +#include "../move.h" + +using namespace rapidjson; +using std::vector; +using std::list; +using std::find; +using std::begin; +using std::end; +using std::rbegin; +using std::rend; +using std::runtime_error; +using std::default_random_engine; +using std::max; +using std::ostream; + +typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; + +////////////////// +// CONSTRUCTORS // +////////////////// + +// A private constructor used by both of the public ones. Initializes all of the +// piles and pile refs specified by the rules +game_state::game_state(const sol_rules& s_rules) : rules(s_rules), + stock(255), + waste(255), + hole(255) { + // If there is a hole, creates pile + if (rules.hole) { + piles.emplace_back(); + hole = static_cast(piles.size() - 1); + } + + // If there are foundation piles, creates the relevant pile vectors + if (rules.foundations) { + for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { + piles.emplace_back(); + foundations.push_back(static_cast(piles.size() - 1)); + auto_foundation_moves.push_back(true); + } + } + + // If there are cell piles, creates the relevant cell vectors + if (rules.cells > 0) { + for (uint8_t i = 0; i < rules.cells; i++) { + piles.emplace_back(); + cells.push_back(static_cast(piles.size() - 1)); + original_cells.push_back(static_cast(piles.size() - 1)); + } + } + + // If there is a stock, creates a pile + if (rules.stock_size > 0) { + piles.emplace_back(); + stock = static_cast(piles.size() - 1); + + // If the stock deals to a waste, create the waste + if (rules.stock_deal_t == sdt::WASTE) { + piles.emplace_back(); + waste = static_cast(piles.size() - 1); + } + } + + // If there is a reserve, creates piles + if (rules.reserve_size > 0) { + uint8_t pile_count = rules.reserve_stacked ? + uint8_t(1) : rules.reserve_size; + for (uint8_t i = 0; i < pile_count; i++) { + piles.emplace_back(); + reserve.push_back(static_cast(piles.size() - 1)); + original_reserve.push_back(static_cast(piles.size() - 1)); + } + } + + // Creates the tableau piles + for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { + piles.emplace_back(); + tableau_piles.push_back(static_cast(piles.size() - 1)); + original_tableau_piles.push_back(static_cast(piles.size() - 1)); + } +} + +// Constructs an initial game state from a JSON doc +game_state::game_state(const sol_rules& s_rules, const Document& doc) + : game_state(s_rules) { + deal_parser::parse(*this, 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 deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); + + // If there is a hole, moves the ace of spades to it + if (rules.hole) { + deck.erase(find(begin(deck), end(deck), card("AS"))); + place_card(hole, card("AS")); + } + + // If the foundations begin filled, then fills them + if (rules.foundations_init_card) { + for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { + card c = card(f_idx % uint8_t(4), 1); + + deck.erase(find(begin(deck), end(deck), c)); + place_card(foundations[f_idx], c); + } + } + + // If there is a stock, deals to it and set up a waste pile too + if (rules.stock_size > 0) { + for (unsigned int i = 0; i < rules.stock_size; i++) { + place_card(stock, deck.back()); + deck.pop_back(); + } + } + + // If there is a reserve, deals to it. + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile. + if (rules.reserve_size > 0) { + for (unsigned int i = 0; i < rules.reserve_size; i++) { + int idx = rules.reserve_stacked ? 0 : i; + place_card(original_reserve[idx], deck.back()); + deck.pop_back(); + } + } + + // This only occurs during testing + if (rules.tableau_pile_count == 0) return; + + // Deals to the tableau piles (row-by-row) + for (int t = 0; !deck.empty(); t++) { + card c = deck.back(); + + // Add the randomly generated card to the tableau piles + auto p = t % original_tableau_piles.size(); + + // If we are doing a diagonal deal, each row should have one fewer card. + // Leftover cards are dealt normally in full rows. + auto row_idx = t / original_tableau_piles.size(); + if (rules.diagonal_deal && row_idx < original_tableau_piles.size()) { + p = original_tableau_piles.size()-p-1; + pile::ref tableau_pile = original_tableau_piles[p]; + + if (p >= row_idx) { + place_card(tableau_pile, c); + deck.pop_back(); + } + } else { + pile::ref tableau_pile = original_tableau_piles[p]; + place_card(tableau_pile, c); + deck.pop_back(); + } + } + + // The size of all piles must equal the deck size + int piles_sz = 0; + for (auto& p : piles) piles_sz += p.size(); + if (piles_sz != rules.max_rank * (rules.two_decks ? 8:4)) { + throw runtime_error("Error: incorrect number of cards in starting piles"); + } +} + +game_state::game_state(const sol_rules& s_rules, + std::initializer_list il) + : game_state(s_rules) { + pile::ref pr = 0; + for (const pile& p : il) { + for (const card c : p.pile_vec) { + place_card(pr, c); + } + pr++; + } +} + +// Generates a randomly ordered vector of cards +vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, + bool two_decks) { + vector values; + vector v_ptrs; + for (int i = 0; i < max_rank * 4; i++) { + values.emplace_back(i); + if (two_decks) values.emplace_back(i); + } + for (auto& v : values) { + v_ptrs.emplace_back(&v); + } + + // Randomly shuffle the pointers + auto rng = default_random_engine(seed+1); + shuffle(begin(v_ptrs), end(v_ptrs), rng); + + vector deck; + for (uint8_t *i : v_ptrs) { + auto r = static_cast(((*i) % max_rank) + 1); + card::suit_t s = (*i) / max_rank; + deck.emplace_back(card(s, r)); + } + + assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4))); + return deck; +} + + +///////////////////////// +// MODIFYING FUNCTIONS // +///////////////////////// + +void game_state::make_move(const move m) { + // Handles stock moves + if (m.from == stock) { + // Handles special stock-to-tableau-piles move + if (rules.stock_deal_t == sdt::TABLEAU_PILES) { + assert(m.from == stock); + assert(m.to == 255); + + for (pile::ref tab_pr = original_tableau_piles.front(); + tab_pr < pile::ref(original_tableau_piles.front() + m.count); + tab_pr++) { + place_card(tab_pr, take_card(stock)); + } + } + // Handles stock-to-waste moves + else { + if (piles[stock].empty()) { + // Re-deal waste back to stock + while (!piles[waste].empty()) { + place_card(stock, take_card(waste)); + } + } else { + assert(m.to == waste); + for (int i = 0; i < m.count; i++) { + place_card(waste, take_card(stock)); + } + } + } + } + + // If this is not a built-pile move + else if (m.count == 1 || m.is_dominance()) { + assert(m.from < piles.size()); + assert(m.to < piles.size()); + + place_card(m.to, take_card(m.from)); +#ifndef NO_AUTO_FOUNDATIONS + update_auto_foundation_moves(m.to); +#endif + } + + // If this is a built-pile move + else { + assert(m.from < piles.size() ); + assert(m.to < piles.size() ); + assert(m.count <= rules.max_rank); + + // Adds the cards to the 'to' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + place_card(m.to, piles[m.from][pile_idx]); + } + + // Removes the cards from the 'from' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + take_card(m.from); + } + } +} + +void game_state::undo_move(const move m) { + assert(m.from < piles.size()); + + // Handles stock moves + if (m.from == stock) { + // Handles special stock-to-tableau-piles move + if (rules.stock_deal_t == sdt::TABLEAU_PILES) { + for (pile::ref tab_pr = original_tableau_piles.front() + m.count; + tab_pr-- > original_tableau_piles.front(); + ) { + place_card(stock, take_card(tab_pr)); + } + } + // Handles stock-to-waste moves + else { + if (piles[waste].empty()) { + // Re-deal waste back to stock + while (!piles[stock].empty()) { + place_card(waste, take_card(stock)); + } + } else { + for (int i = 0; i < m.count; i++) { + place_card(stock, take_card(waste)); + } + } + } + } + + // If this is not a built-pile move + else if (m.count == 1 || m.is_dominance()) { + assert(m.to < piles.size()); + place_card(m.from, take_card(m.to)); +#ifndef NO_AUTO_FOUNDATIONS + update_auto_foundation_moves(m.from); +#endif + } + + // If this is a built-pile move + else { + assert(m.to < piles.size()); + assert(m.count <= rules.max_rank); + // Adds the cards to the 'from' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + place_card(m.from, piles[m.to][pile_idx]); + } + + // Removes the cards from the 'to' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + take_card(m.to); + } + } +} + +// Places a card on a pile and if it is on a tableau, cell or reserve pile, +// reorders the pile refs so that the largest pile is first +void game_state::place_card(pile::ref pr, card c) { + piles[pr].place(c); +#ifndef NO_PILE_SYMMETRY + // If the stock deals to the tableau piles, there is no pile symmetry + if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) { + eval_pile_order(pr, true); + } +#endif +} + +// Same as above but for taking cards +card game_state::take_card(pile::ref pr) { + card c = piles[pr].take(); +#ifndef NO_PILE_SYMMETRY + // If the stock deals to the tableau piles, there is no pile symmetry + if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) { + eval_pile_order(pr, false); + } +#endif + return c; +} + +// Assesses whether the pile ref that was modified was a tableau, cell or +// reserve pile, and if so makes the relevant function call +void game_state::eval_pile_order(pile::ref pr, bool is_place) { + if (!original_tableau_piles.empty() + && pr >= original_tableau_piles[0] + && pr < original_tableau_piles[0] + original_tableau_piles.size()) { + + eval_pile_order(tableau_piles, pr, is_place); + } else if (!original_cells.empty() + && pr >= original_cells[0] + && pr < original_cells[0] + original_cells.size()) { + + eval_pile_order(cells, pr, is_place); + } else if (!original_reserve.empty() + && pr >= original_reserve[0] + && pr < original_reserve[0] + original_reserve.size()) { + + eval_pile_order(reserve, pr, is_place); + } +} + +// Finds the pile ref in the list and evaluates whether it should be moved to +// maintain the "pile order" +void game_state::eval_pile_order(list& pile_lst, pile::ref changed_pr, + bool is_place) { + // Finds the first pile that is larger/smaller than the ref pile + // (the comp pile). + // If the card has been placed, search for the ref from the right hand side + // and if not from the lhs + if (is_place) { + auto changed_ref_iter = find(rbegin(pile_lst), rend(pile_lst), changed_pr); + auto comp_iter = changed_ref_iter; + + while (++comp_iter != rend(pile_lst) + && piles[*comp_iter] < piles[*changed_ref_iter]); + + if (comp_iter != ++changed_ref_iter) { + // Places the ref pile before the comp pile + pile_lst.insert(comp_iter.base(), changed_pr); + pile_lst.erase(changed_ref_iter.base()); + } + } else { + auto changed_ref_iter = find(begin(pile_lst), end(pile_lst), changed_pr); + auto comp_iter = changed_ref_iter; + + while (++comp_iter != end(pile_lst) + && piles[*comp_iter] > piles[*changed_ref_iter]); + + if (comp_iter != ++changed_ref_iter) { + // Places the ref pile before the comp pile + pile_lst.insert(comp_iter, changed_pr); + pile_lst.erase(--changed_ref_iter); + } + } + +#ifndef NDEBUG + // Makes sure the piles are in order + if (pile_lst.size() > 1) { + auto prev = begin(pile_lst); + auto cur = begin(pile_lst); + cur++; + + while (cur != end(pile_lst)) { + assert(piles[*prev] >= piles[*cur]); + prev++; cur++; + } + } +#endif +} + +// Decides which 'auto foundation' booleans should be 'true' currently +void game_state::update_auto_foundation_moves(pile::ref target_pile) { + // If there are no foundations, or this is not a foundation move, do nothing + if (!rules.foundations + || target_pile < foundations.front() + || target_pile > foundations.back()) { + return; + } + + // Turns the 'auto' boolean for the target foundation false + + for (pile::ref pr : foundations) { + auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr); + } +} + +bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { +#ifndef AUTO_FOUNDATIONS_STREAMLINER + if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) +#endif + return true; + + card::suit_t target_suit(target_pile - foundations.front()); + card::rank_t target_rank = piles[target_pile].empty() + ? card::rank_t(0) + : piles[target_pile].top_card().get_rank(); + // The card to be moved up + card move_up_card(target_suit, card::rank_t(target_rank + 1)); + + // The max difference between the 'move up' card and the foundations of the + // same colour and the other colour + int same_rank_diff = 0; + int other_rank_diff = 0; + for (pile::ref pr : foundations) { + if (pr == target_pile) continue; + + card::suit_t s(pr - foundations.front()); + card::rank_t r = piles[pr].empty() ? card::rank_t(0) + : piles[pr].top_card().get_rank(); + card c(s, r); + + auto rank_diff = int(move_up_card.get_rank() - r); + if (move_up_card.get_suit() == c.get_suit() + || (rules.build_pol == pol::RED_BLACK + && move_up_card.get_colour() == c.get_colour())) { + same_rank_diff = rank_diff; + } else { + other_rank_diff = max(other_rank_diff, rank_diff); + } + } + + bool other_within_1 = other_rank_diff <= 1; + bool other_within_2 = other_rank_diff <= 2; + bool same_within_3 = same_rank_diff <= 3; + + if (rules.build_pol == pol::RED_BLACK) { + return other_within_1 || (other_within_2 && same_within_3); + } else { + assert(rules.build_pol == pol::ANY_SUIT); + return other_within_2; + } +} + + +//////////////////////// +// INSPECT GAME STATE // +//////////////////////// + +bool game_state::is_solved() const { + bool solved = true; + if (rules.hole) { + solved = piles[hole].size() + == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); + } else { + assert(rules.foundations); + for (auto f : foundations) { + if (piles[f].size() != rules.max_rank) { + solved = false; + } + } + } + + // Runs some alternative checks in debug mode to make sure the game state + // is consistent +#ifndef NDEBUG + bool rest_empty = true; + for (pile::ref pr = 0; pr < piles.size(); pr++) { + // All piles other than the hole must be empty + if (rules.hole) { + if (pr != hole && !piles[pr].empty()) { + rest_empty = false; + break; + } + } + // All piles other than the foundations must be empty + else { + bool non_foundation_ref = + pr < foundations.front() + || pr >= foundations.front() + foundations.size(); + if (non_foundation_ref && !piles[pr].empty()) { + rest_empty = false; + break; + } + } + } + assert(solved == rest_empty); +#endif + + return solved; +} + +const std::vector& game_state::get_data() const { + return piles; +} + + +/////////// +// PRINT // +/////////// + +ostream& operator<< (ostream& str, const game_state& gs) { + return state_printer::print(str, gs); +} + -- cgit v1.2.1 From 4c030e52c70bdd3e04a14a2e29df93457032c45f Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 10:41:04 +0100 Subject: Move pile order logic into sep. file --- src/main/game/search-state/game_state.cpp | 77 ++----------------------------- 1 file changed, 4 insertions(+), 73 deletions(-) (limited to 'src/main/game/search-state/game_state.cpp') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index bd533f1..8045e76 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -222,9 +222,9 @@ vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, } -///////////////////////// -// MODIFYING FUNCTIONS // -///////////////////////// +//////////////////// +// ALTERING STATE // +//////////////////// void game_state::make_move(const move m) { // Handles stock moves @@ -342,6 +342,7 @@ void game_state::undo_move(const move m) { // reorders the pile refs so that the largest pile is first void game_state::place_card(pile::ref pr, card c) { piles[pr].place(c); + #ifndef NO_PILE_SYMMETRY // If the stock deals to the tableau piles, there is no pile symmetry if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) { @@ -362,76 +363,6 @@ card game_state::take_card(pile::ref pr) { return c; } -// Assesses whether the pile ref that was modified was a tableau, cell or -// reserve pile, and if so makes the relevant function call -void game_state::eval_pile_order(pile::ref pr, bool is_place) { - if (!original_tableau_piles.empty() - && pr >= original_tableau_piles[0] - && pr < original_tableau_piles[0] + original_tableau_piles.size()) { - - eval_pile_order(tableau_piles, pr, is_place); - } else if (!original_cells.empty() - && pr >= original_cells[0] - && pr < original_cells[0] + original_cells.size()) { - - eval_pile_order(cells, pr, is_place); - } else if (!original_reserve.empty() - && pr >= original_reserve[0] - && pr < original_reserve[0] + original_reserve.size()) { - - eval_pile_order(reserve, pr, is_place); - } -} - -// Finds the pile ref in the list and evaluates whether it should be moved to -// maintain the "pile order" -void game_state::eval_pile_order(list& pile_lst, pile::ref changed_pr, - bool is_place) { - // Finds the first pile that is larger/smaller than the ref pile - // (the comp pile). - // If the card has been placed, search for the ref from the right hand side - // and if not from the lhs - if (is_place) { - auto changed_ref_iter = find(rbegin(pile_lst), rend(pile_lst), changed_pr); - auto comp_iter = changed_ref_iter; - - while (++comp_iter != rend(pile_lst) - && piles[*comp_iter] < piles[*changed_ref_iter]); - - if (comp_iter != ++changed_ref_iter) { - // Places the ref pile before the comp pile - pile_lst.insert(comp_iter.base(), changed_pr); - pile_lst.erase(changed_ref_iter.base()); - } - } else { - auto changed_ref_iter = find(begin(pile_lst), end(pile_lst), changed_pr); - auto comp_iter = changed_ref_iter; - - while (++comp_iter != end(pile_lst) - && piles[*comp_iter] > piles[*changed_ref_iter]); - - if (comp_iter != ++changed_ref_iter) { - // Places the ref pile before the comp pile - pile_lst.insert(comp_iter, changed_pr); - pile_lst.erase(--changed_ref_iter); - } - } - -#ifndef NDEBUG - // Makes sure the piles are in order - if (pile_lst.size() > 1) { - auto prev = begin(pile_lst); - auto cur = begin(pile_lst); - cur++; - - while (cur != end(pile_lst)) { - assert(piles[*prev] >= piles[*cur]); - prev++; cur++; - } - } -#endif -} - // Decides which 'auto foundation' booleans should be 'true' currently void game_state::update_auto_foundation_moves(pile::ref target_pile) { // If there are no foundations, or this is not a foundation move, do nothing -- cgit v1.2.1 From 367a3a59530eeb8f299a213f9f5cf803f1dd8f4c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 10:50:21 +0100 Subject: Move auto found. logic into sep. file --- src/main/game/search-state/game_state.cpp | 63 ------------------------------- 1 file changed, 63 deletions(-) (limited to 'src/main/game/search-state/game_state.cpp') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 8045e76..42f9b40 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -363,69 +363,6 @@ card game_state::take_card(pile::ref pr) { return c; } -// Decides which 'auto foundation' booleans should be 'true' currently -void game_state::update_auto_foundation_moves(pile::ref target_pile) { - // If there are no foundations, or this is not a foundation move, do nothing - if (!rules.foundations - || target_pile < foundations.front() - || target_pile > foundations.back()) { - return; - } - - // Turns the 'auto' boolean for the target foundation false - - for (pile::ref pr : foundations) { - auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr); - } -} - -bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { -#ifndef AUTO_FOUNDATIONS_STREAMLINER - if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) -#endif - return true; - - card::suit_t target_suit(target_pile - foundations.front()); - card::rank_t target_rank = piles[target_pile].empty() - ? card::rank_t(0) - : piles[target_pile].top_card().get_rank(); - // The card to be moved up - card move_up_card(target_suit, card::rank_t(target_rank + 1)); - - // The max difference between the 'move up' card and the foundations of the - // same colour and the other colour - int same_rank_diff = 0; - int other_rank_diff = 0; - for (pile::ref pr : foundations) { - if (pr == target_pile) continue; - - card::suit_t s(pr - foundations.front()); - card::rank_t r = piles[pr].empty() ? card::rank_t(0) - : piles[pr].top_card().get_rank(); - card c(s, r); - - auto rank_diff = int(move_up_card.get_rank() - r); - if (move_up_card.get_suit() == c.get_suit() - || (rules.build_pol == pol::RED_BLACK - && move_up_card.get_colour() == c.get_colour())) { - same_rank_diff = rank_diff; - } else { - other_rank_diff = max(other_rank_diff, rank_diff); - } - } - - bool other_within_1 = other_rank_diff <= 1; - bool other_within_2 = other_rank_diff <= 2; - bool same_within_3 = same_rank_diff <= 3; - - if (rules.build_pol == pol::RED_BLACK) { - return other_within_1 || (other_within_2 && same_within_3); - } else { - assert(rules.build_pol == pol::ANY_SUIT); - return other_within_2; - } -} - //////////////////////// // INSPECT GAME STATE // -- cgit v1.2.1 From a7396c5497601855fad019cfd145ca6c93e1c40f Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 21:54:48 +0100 Subject: Refactor moves logic --- src/main/game/search-state/game_state.cpp | 223 ++++++++++++++++++------------ 1 file changed, 133 insertions(+), 90 deletions(-) (limited to 'src/main/game/search-state/game_state.cpp') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 42f9b40..ac57142 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -227,114 +227,157 @@ vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, //////////////////// void game_state::make_move(const move m) { - // Handles stock moves - if (m.from == stock) { - // Handles special stock-to-tableau-piles move - if (rules.stock_deal_t == sdt::TABLEAU_PILES) { - assert(m.from == stock); - assert(m.to == 255); - - for (pile::ref tab_pr = original_tableau_piles.front(); - tab_pr < pile::ref(original_tableau_piles.front() + m.count); - tab_pr++) { - place_card(tab_pr, take_card(stock)); - } - } - // Handles stock-to-waste moves - else { - if (piles[stock].empty()) { - // Re-deal waste back to stock - while (!piles[waste].empty()) { - place_card(stock, take_card(waste)); - } - } else { - assert(m.to == waste); - for (int i = 0; i < m.count; i++) { - place_card(waste, take_card(stock)); - } - } - } + switch (m.type) { + case move::mtype::regular: + case move::mtype::dominance: + make_regular_move(m); + break; + case move::mtype::built_group: + make_built_pile_move(m); + break; + case move::mtype::stock_to_waste: + make_stock_to_waste_move(m); + break; + case move::mtype::stock_to_tableau: + make_stock_to_tableau_move(m); + break; + case move::mtype::redeal: + make_redeal_move(); + break; + case move::mtype::null: + assert(false); + break; + } +} + +void game_state::undo_move(const move m) { + switch (m.type) { + case move::mtype::regular: + case move::mtype::dominance: + undo_regular_move(m); + break; + case move::mtype::built_group: + undo_built_pile_move(m); + break; + case move::mtype::stock_to_waste: + undo_stock_to_waste_move(m); + break; + case move::mtype::stock_to_tableau: + undo_stock_to_tableau_move(m); + break; + case move::mtype::redeal: + undo_redeal_move(); + break; + case move::mtype::null: + assert(false); + break; } +} - // If this is not a built-pile move - else if (m.count == 1 || m.is_dominance()) { - assert(m.from < piles.size()); - assert(m.to < piles.size()); +void game_state::make_regular_move(const move m) { + assert(m.from < piles.size()); + assert(m.to < piles.size()); + + place_card(m.to, take_card(m.from)); - place_card(m.to, take_card(m.from)); #ifndef NO_AUTO_FOUNDATIONS - update_auto_foundation_moves(m.to); + update_auto_foundation_moves(m.to); #endif +} + +void game_state::undo_regular_move(const move m) { + assert(m.to < piles.size()); + + place_card(m.from, take_card(m.to)); + +#ifndef NO_AUTO_FOUNDATIONS + update_auto_foundation_moves(m.from); +#endif +} + +void game_state::make_built_pile_move(const move m) { + assert(rules.move_built_group); + assert(m.from < piles.size() ); + assert(m.to < piles.size() ); + assert(m.count <= rules.max_rank); + + // Adds the cards to the 'to' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + place_card(m.to, piles[m.from][pile_idx]); } - // If this is a built-pile move - else { - assert(m.from < piles.size() ); - assert(m.to < piles.size() ); - assert(m.count <= rules.max_rank); + // Removes the cards from the 'from' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + take_card(m.from); + } +} - // Adds the cards to the 'to' pile - for (auto pile_idx = m.count; pile_idx-- > 0;) { - place_card(m.to, piles[m.from][pile_idx]); - } +void game_state::undo_built_pile_move(const move m) { + assert(rules.move_built_group); + assert(m.to < piles.size()); + assert(m.count <= rules.max_rank); - // Removes the cards from the 'from' pile - for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { - take_card(m.from); - } + // Adds the cards to the 'from' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + place_card(m.from, piles[m.to][pile_idx]); + } + + // Removes the cards from the 'to' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + take_card(m.to); } } -void game_state::undo_move(const move m) { - assert(m.from < piles.size()); +void game_state::make_stock_to_waste_move(const move m) { + assert(rules.stock_deal_t == sdt::WASTE); - // Handles stock moves - if (m.from == stock) { - // Handles special stock-to-tableau-piles move - if (rules.stock_deal_t == sdt::TABLEAU_PILES) { - for (pile::ref tab_pr = original_tableau_piles.front() + m.count; - tab_pr-- > original_tableau_piles.front(); - ) { - place_card(stock, take_card(tab_pr)); - } - } - // Handles stock-to-waste moves - else { - if (piles[waste].empty()) { - // Re-deal waste back to stock - while (!piles[stock].empty()) { - place_card(waste, take_card(stock)); - } - } else { - for (int i = 0; i < m.count; i++) { - place_card(stock, take_card(waste)); - } - } - } + for (int i = 0; i < m.count; i++) { + place_card(waste, take_card(stock)); } +} - // If this is not a built-pile move - else if (m.count == 1 || m.is_dominance()) { - assert(m.to < piles.size()); - place_card(m.from, take_card(m.to)); -#ifndef NO_AUTO_FOUNDATIONS - update_auto_foundation_moves(m.from); -#endif +void game_state::undo_stock_to_waste_move(const move m) { + assert(rules.stock_deal_t == sdt::WASTE); + + for (int i = 0; i < m.count; i++) { + place_card(stock, take_card(waste)); } +} - // If this is a built-pile move - else { - assert(m.to < piles.size()); - assert(m.count <= rules.max_rank); - // Adds the cards to the 'from' pile - for (auto pile_idx = m.count; pile_idx-- > 0;) { - place_card(m.from, piles[m.to][pile_idx]); - } +void game_state::make_stock_to_tableau_move(const move m) { + assert(rules.stock_deal_t == sdt::TABLEAU_PILES); - // Removes the cards from the 'to' pile - for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { - take_card(m.to); - } + for (pile::ref tab_pr = original_tableau_piles.front(); + tab_pr < pile::ref(original_tableau_piles.front() + m.count); + tab_pr++) { + place_card(tab_pr, take_card(stock)); + } +} + +void game_state::undo_stock_to_tableau_move(const move m) { + assert(rules.stock_deal_t == sdt::TABLEAU_PILES); + + for (pile::ref tab_pr = original_tableau_piles.front() + m.count; + tab_pr-- > original_tableau_piles.front(); + ) { + place_card(stock, take_card(tab_pr)); + } +} + +void game_state::make_redeal_move() { + assert(piles[stock].empty()); + + // Re-deal waste back to stock + while (!piles[waste].empty()) { + place_card(stock, take_card(waste)); + } +} + +void game_state::undo_redeal_move() { + assert(piles[waste].empty()); + + while (!piles[stock].empty()) { + place_card(waste, take_card(stock)); } } -- cgit v1.2.1 From f4bfb7e972fda893240899b44534c65e4a10e636 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Sun, 10 Jun 2018 00:28:24 +0100 Subject: Finish refactoring game state class --- src/main/game/search-state/game_state.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/main/game/search-state/game_state.cpp') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index ac57142..0a20f60 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -233,7 +233,7 @@ void game_state::make_move(const move m) { make_regular_move(m); break; case move::mtype::built_group: - make_built_pile_move(m); + make_built_group_move(m); break; case move::mtype::stock_to_waste: make_stock_to_waste_move(m); @@ -257,7 +257,7 @@ void game_state::undo_move(const move m) { undo_regular_move(m); break; case move::mtype::built_group: - undo_built_pile_move(m); + undo_built_group_move(m); break; case move::mtype::stock_to_waste: undo_stock_to_waste_move(m); @@ -295,7 +295,7 @@ void game_state::undo_regular_move(const move m) { #endif } -void game_state::make_built_pile_move(const move m) { +void game_state::make_built_group_move(move m) { assert(rules.move_built_group); assert(m.from < piles.size() ); assert(m.to < piles.size() ); @@ -312,7 +312,7 @@ void game_state::make_built_pile_move(const move m) { } } -void game_state::undo_built_pile_move(const move m) { +void game_state::undo_built_group_move(move m) { assert(rules.move_built_group); assert(m.to < piles.size()); assert(m.count <= rules.max_rank); -- cgit v1.2.1