From b044ca495c6214250628fc984863eb45c5d60852 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 6 Jul 2018 11:14:04 +0100 Subject: Refactored game rules JSON --- src/main/game/search-state/game_state.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 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 a1d60bf..169c96e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -58,7 +58,7 @@ game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts } // If there are foundation piles, creates the relevant pile vectors - if (rules.foundations) { + if (rules.foundations_present) { for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { piles.emplace_back(); foundations.push_back(static_cast(piles.size() - 1)); @@ -389,9 +389,8 @@ void game_state::make_stock_k_plus_move(const move m) { // Index of the card to be moved auto count = m.count % (piles[stock].size() + piles[waste].size()); - for (int i = 0; i <= count; i++) { - if (i < count) place_card(waste, take_card(stock)); - else place_card(m.to, take_card(stock)); + for (int i = 0; i < count; i++) { + place_card(waste, take_card(stock)); if (rules.stock_redeal) { // If the stock is empty, flips it @@ -402,6 +401,11 @@ void game_state::make_stock_k_plus_move(const move m) { } } + place_card(m.to, take_card(stock)); + if (rules.stock_redeal && piles[stock].empty() && count != 0) + while (!piles[waste].empty()) + place_card(stock, take_card(waste)); + #ifndef NDEBUG auto sz_after = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); @@ -420,7 +424,12 @@ void game_state::undo_stock_k_plus_move(move m) { auto count = m.count % (piles[stock].size() + piles[waste].size() + 1); - for (int i = count; i >= 0; i--) { + if (rules.stock_redeal && piles[waste].empty() && count != 0) + while (!piles[stock].empty()) + place_card(waste, take_card(stock)); + place_card(stock, take_card(m.to)); + + for (int i = 0; i < count; i++) { if (rules.stock_redeal) { // If the waste is empty, flips it // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal) @@ -429,8 +438,7 @@ void game_state::undo_stock_k_plus_move(move m) { place_card(waste, take_card(stock)); } - if (i < count) place_card(stock, take_card(waste)); - else place_card(stock, take_card(m.to )); + place_card(stock, take_card(waste)); } #ifndef NDEBUG @@ -514,7 +522,7 @@ bool game_state::is_solved() const { solved = piles[hole].size() == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); } else { - assert(rules.foundations); + assert(rules.foundations_present); for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { solved = false; -- cgit v1.2.1 From 4c209e2f5ab9ed7574a9b73eb96687dc5ae77391 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 6 Jul 2018 17:25:53 +0100 Subject: Refactor k-plus impl to simplify and fix deal1 bug --- src/main/game/search-state/game_state.cpp | 75 ++++++++++++++++--------------- 1 file changed, 39 insertions(+), 36 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 169c96e..0720f6e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -378,75 +378,78 @@ void game_state::undo_built_group_move(move m) { } void game_state::make_stock_k_plus_move(const move m) { +#ifndef NDEBUG assert(rules.stock_deal_t == sdt::WASTE); - assert(m.count < piles[stock].size() + piles[waste].size()); + assert(m.from == stock); + if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size()); assert(!rules.stock_redeal || m.to != waste); -#ifndef NDEBUG auto sz_before = piles[stock].size() + piles[waste].size(); #endif - // if redeal -> stock tableau move implies s+w-=1 - - // Index of the card to be moved - auto count = m.count % (piles[stock].size() + piles[waste].size()); - - for (int i = 0; i < count; i++) { - place_card(waste, take_card(stock)); - if (rules.stock_redeal) { - // If the stock is empty, flips it - // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal) - if (piles[stock].empty()) - while (!piles[waste].empty()) - place_card(stock, take_card(waste)); + // Transfers count cards from the stock to the waste + if (m.count > 0) { + for (int i = 0; i < m.count; i++) { + place_card(waste, take_card(stock)); + } + } else { + for (int i = 0; i > m.count; i--) { + place_card(stock, take_card(waste)); } } - place_card(m.to, take_card(stock)); - if (rules.stock_redeal && piles[stock].empty() && count != 0) - while (!piles[waste].empty()) + // Moves the card on top of the waste to the target pile + place_card(m.to, take_card(waste)); + + // Flips the waste back on to the empty stock (if necessary) + if (m.flip_waste) { + assert(rules.stock_redeal && piles[stock].empty()); + while (!piles[waste].empty()) { place_card(stock, take_card(waste)); + } + } #ifndef NDEBUG auto sz_after = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); -#endif assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty())); assert(piles[stock].size() <= rules.stock_size); +#endif } void game_state::undo_stock_k_plus_move(move m) { +#ifndef NDEBUG assert(rules.stock_deal_t == sdt::WASTE); - assert(m.count < piles[stock].size() + piles[waste].size() + 1); + assert(m.from == stock); assert(!rules.stock_redeal || m.to != waste); -#ifndef NDEBUG auto sz_after = piles[stock].size() + piles[waste].size(); #endif - auto count = m.count % (piles[stock].size() + piles[waste].size() + 1); - - if (rules.stock_redeal && piles[waste].empty() && count != 0) - while (!piles[stock].empty()) + if (m.flip_waste) { + assert(rules.stock_redeal && piles[waste].empty()); + while (!piles[stock].empty()) { place_card(waste, take_card(stock)); - place_card(stock, take_card(m.to)); - - for (int i = 0; i < count; i++) { - if (rules.stock_redeal) { - // If the waste is empty, flips it - // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal) - if (piles[waste].empty()) - while (!piles[stock].empty()) - place_card(waste, take_card(stock)); } + } + + place_card(waste, take_card(m.to)); - place_card(stock, take_card(waste)); + if (m.count > 0) { + for (int i = 0; i < m.count; i++) { + place_card(stock, take_card(waste)); + } + } else { + for (int i = 0; i > m.count; i--) { + place_card(waste, take_card(stock)); + } } #ifndef NDEBUG auto sz_before = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); -#endif + if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size()); assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty())); assert(piles[stock].size() <= rules.stock_size); +#endif } void game_state::make_stock_to_all_tableau_move(move m) { -- cgit v1.2.1