From c84d8ccf133bc49c44c185dac7b49a46b8878ec1 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 28 Jun 2018 14:19:31 +0100 Subject: Implement to pass unit tests --- src/main/game/search-state/game_state.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 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 7870b1b..d974c26 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -385,16 +385,39 @@ void game_state::undo_built_group_move(move m) { void game_state::make_stock_to_waste_move(const move m) { assert(rules.stock_deal_t == sdt::WASTE); - for (int i = 0; i < m.count; i++) { - place_card(waste, take_card(stock)); + // 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)); + + 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)); + } } } 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)); + auto count = m.count % (piles[stock].size() + piles[waste].size() + 1); + + for (int i = count; i >= 0; 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)); + } + + if (i < count) place_card(stock, take_card(waste)); + else place_card(stock, take_card(m.to )); } } -- cgit v1.2.1 From 5fde1f9563c98e2d661ac495e448d35eeb97e68c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 29 Jun 2018 11:34:09 +0100 Subject: Fix bugs + correct built pile kings. Cell + founds --- src/main/game/search-state/game_state.cpp | 72 ++++++++++++++++--------------- 1 file changed, 37 insertions(+), 35 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 d974c26..276d582 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -257,14 +257,11 @@ void game_state::make_move(const move m) { case move::mtype::built_group: make_built_group_move(m); break; - case move::mtype::stock_to_waste: - make_stock_to_waste_move(m); + case move::mtype::stock_k_plus: + make_stock_k_plus_move(m); break; - case move::mtype::stock_to_tableau: - make_stock_to_tableau_move(m); - break; - case move::mtype::redeal: - make_redeal_move(); + case move::mtype::stock_to_all_tableau: + make_stock_to_all_tableau_move(m); break; case move::mtype::null: assert(false); @@ -285,14 +282,11 @@ void game_state::undo_move(const move m) { case move::mtype::built_group: undo_built_group_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); + case move::mtype::stock_k_plus: + undo_stock_k_plus_move(m); break; - case move::mtype::redeal: - undo_redeal_move(); + case move::mtype::stock_to_all_tableau: + undo_stock_to_all_tableau_move(m); break; case move::mtype::null: assert(false); @@ -382,8 +376,14 @@ void game_state::undo_built_group_move(move m) { } } -void game_state::make_stock_to_waste_move(const move m) { +void game_state::make_stock_k_plus_move(const move m) { assert(rules.stock_deal_t == sdt::WASTE); + assert(m.count < piles[stock].size() + 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()); @@ -400,10 +400,22 @@ void game_state::make_stock_to_waste_move(const move m) { 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); } -void game_state::undo_stock_to_waste_move(const move m) { +void game_state::undo_stock_k_plus_move(move m) { assert(rules.stock_deal_t == sdt::WASTE); + assert(m.count < piles[stock].size() + piles[waste].size() + 1); + 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); @@ -419,9 +431,16 @@ void game_state::undo_stock_to_waste_move(const move m) { if (i < count) place_card(stock, take_card(waste)); else place_card(stock, take_card(m.to )); } + +#ifndef NDEBUG + auto sz_before = 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); } -void game_state::make_stock_to_tableau_move(const move m) { +void game_state::make_stock_to_all_tableau_move(move m) { assert(rules.stock_deal_t == sdt::TABLEAU_PILES); for (pile::ref tab_pr = original_tableau_piles.front(); @@ -431,7 +450,7 @@ void game_state::make_stock_to_tableau_move(const move m) { } } -void game_state::undo_stock_to_tableau_move(const move m) { +void game_state::undo_stock_to_all_tableau_move(move m) { assert(rules.stock_deal_t == sdt::TABLEAU_PILES); for (pile::ref tab_pr = original_tableau_piles.front() + m.count; @@ -441,23 +460,6 @@ void game_state::undo_stock_to_tableau_move(const move m) { } } -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)); - } -} - // 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) { -- cgit v1.2.1