aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorCharlie Blake2018-07-06 17:25:53 +0100
committerCharlie Blake2018-07-06 17:25:53 +0100
commit4c209e2f5ab9ed7574a9b73eb96687dc5ae77391 (patch)
tree634eede8d39d3e313c1206cb2b9a6f6205570f8c /src/main/game
parentRefactored game rules JSON (diff)
downloadmetasol-4c209e2f5ab9ed7574a9b73eb96687dc5ae77391.tar.gz
metasol-4c209e2f5ab9ed7574a9b73eb96687dc5ae77391.zip

Refactor k-plus impl to simplify and fix deal1 bug

Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/move.cpp9
-rw-r--r--src/main/game/move.h7
-rw-r--r--src/main/game/search-state/game_state.cpp75
-rw-r--r--src/main/game/search-state/game_state.h4
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp90
5 files changed, 82 insertions, 103 deletions
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
index 915bac6..528b631 100644
--- a/src/main/game/move.cpp
+++ b/src/main/game/move.cpp
@@ -4,11 +4,12 @@
#include "move.h"
-move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_move_) :
- type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) {
+move::move(mtype ty, pile::ref f, pile::ref t, int8_t count_, bool reveal_move_, bool flip_waste_) :
+ type(ty), from(f), to(t), count(count_), reveal_move(reveal_move_), flip_waste(flip_waste_) {
#ifndef NDEBUG
- if (ty == mtype::regular) assert(i == 1);
- else if (ty != mtype::stock_k_plus) assert(i >= 1);
+ if (ty == mtype::regular) assert(count == 1);
+ if (ty == mtype::stock_k_plus) assert(!reveal_move_);
+ if (ty != mtype::stock_k_plus) assert(count >= 1 && !flip_waste_);
#endif
}
diff --git a/src/main/game/move.h b/src/main/game/move.h
index 5e6892f..27ba279 100644
--- a/src/main/game/move.h
+++ b/src/main/game/move.h
@@ -10,7 +10,7 @@
struct move {
enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, null};
- explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false);
+ explicit move(mtype, pile::ref = 255 , pile::ref = 255, int8_t = 1, bool reveal_move_ = false, bool flip_waste_ = false);
friend bool operator==(const move&, const move&);
void make_reveal_move();
@@ -18,8 +18,11 @@ struct move {
mtype type;
pile::ref from;
pile::ref to;
- pile::size_type count;
+ // For built piles the size of the pile, or for stock k-plus moves,
+ // the number of cards to be moved from the stock to the waste before the waste card is played on the foundations
+ int8_t count;
bool reveal_move;
+ bool flip_waste;
};
#endif //SOLVITAIRE_MOVE_H
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) {
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index ecb8ace..3f354d1 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -90,11 +90,11 @@ private:
bool stock_can_deal_all_tableau() const;
move get_stock_to_all_tableau_move() const;
- std::set<pile::size_type> generate_stock_moves_to_check() const;
+ std::set<std::pair<int8_t, bool>> generate_k_plus_moves_to_check() const;
void add_stock_to_cell_move(std::vector<move>&, pile::ref) const;
void add_stock_to_tableau_moves(std::vector<move>&) const;
void add_stock_to_hole_foundation_moves(std::vector<move>&) const;
- card stock_card_from_index(pile::size_type) const;
+ card stock_card_from_count(int8_t) const;
void add_foundation_complete_piles_moves(std::vector<move> &) const;
bool is_valid_tableau_move(pile::ref, pile::ref) const;
diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp
index f68df0b..f6fb68f 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -171,95 +171,67 @@ move game_state::get_stock_to_all_tableau_move() const {
return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves);
}
-set<pile::size_type> game_state::generate_stock_moves_to_check() const {
- set<pile::size_type> stock_moves_to_check;
+set<pair<int8_t, bool>> game_state::generate_k_plus_moves_to_check() const {
+ set<pair<int8_t, bool>> stock_moves_to_check;
+
+ // If the waste isn't empty, adds the move from the top of the current waste
+ if (piles[waste].size() != 0) stock_moves_to_check.insert(pair<int8_t, bool>(0, false));
// Adds multiples of deal count (excluding last card)
- for (auto i = static_cast<pile::size_type>(rules.stock_deal_count - 1);
- i < piles[stock].size() - 1;
- i += rules.stock_deal_count) {
- stock_moves_to_check.insert(i);
- }
+ for (int8_t count = rules.stock_deal_count;
+ count < piles[stock].size();
+ count += rules.stock_deal_count)
+ stock_moves_to_check.insert(pair<int8_t, bool>(count, false));
// Adds last card
- stock_moves_to_check.insert(static_cast<pile::size_type>(piles[stock].size() - 1));
+ bool flip_waste = rules.stock_redeal;
+ stock_moves_to_check.insert(pair<int8_t, bool>(static_cast<int8_t>(piles[stock].size()), flip_waste));
// If the stock can be redealt, searches through the waste then the stock again
- if (rules.stock_redeal) {
- auto stock_waste_sz = piles[waste].size() + piles[stock].size();
- for (auto i = piles[stock].size() - 1 + rules.stock_deal_count;
- i < piles[stock].size() + stock_waste_sz - 1;
- i += rules.stock_deal_count) {
- if (i == stock_waste_sz - 1) continue; // No need to add top of waste card as it is a regular move
- stock_moves_to_check.insert(static_cast<pile::size_type>(i % stock_waste_sz));
- }
- }
+ if (rules.stock_redeal)
+ for (auto count = static_cast<int8_t>(-piles[waste].size() + rules.stock_deal_count);
+ count < piles[stock].size();
+ count += rules.stock_deal_count)
+ stock_moves_to_check.insert(pair<int8_t, bool>(count, false));
return stock_moves_to_check;
}
void game_state::add_stock_to_cell_move(std::vector<move>& moves, pile::ref empty_cell) const {
- for (auto i : generate_stock_moves_to_check())
- moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, i);
-
- // If there is no redeal option and the k-plus representation is not in effect,
- // adds a regular move from the waste
- if (!piles[waste].empty())
- moves.emplace_back(move::mtype::regular, waste, empty_cell);
+ for (auto k_plus_mv : generate_k_plus_moves_to_check())
+ moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, k_plus_mv.first, false, k_plus_mv.second);
}
void game_state::add_stock_to_tableau_moves(std::vector<move>& moves) const {
// For each stock move to check, if it can be moved legally to one of the tableau
// piles, adds this as a move
- for (auto i : generate_stock_moves_to_check()) {
- card from = stock_card_from_index(i);
+ for (auto k_plus_mv : generate_k_plus_moves_to_check()) {
+ card from = stock_card_from_count(k_plus_mv.first);
- for (auto t : tableau_piles) {
+ for (auto t : tableau_piles)
if (is_valid_tableau_move(from, t))
- moves.emplace_back(move::mtype::stock_k_plus, stock, t, i);
- }
- }
-
- // If there is no redeal option and the k-plus representation is not in effect,
- // adds a regular move from the waste to valid tableau piles
- if (!piles[waste].empty()) {
- for (auto t : tableau_piles) {
- if (is_valid_tableau_move(waste, t))
- moves.emplace_back(move::mtype::regular, waste, t);
- }
+ moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second);
}
}
void game_state::add_stock_to_hole_foundation_moves(std::vector<move>& moves) const {
- for (auto i : generate_stock_moves_to_check()) {
- card from = stock_card_from_index(i);
+ for (auto k_plus_mv : generate_k_plus_moves_to_check()) {
+ card from = stock_card_from_count(k_plus_mv.first);
for (auto f : foundations)
if (is_valid_foundations_move(from, f))
- moves.emplace_back(move::mtype::stock_k_plus, stock, f, i);
- if (rules.hole && is_valid_hole_move(from))
- moves.emplace_back(move::mtype::stock_k_plus, stock, hole, i);
- }
-
- // If there is no redeal option and the k-plus representation is not in effect,
- // adds a regular move from the waste to valid foundation piles
- if (!piles[waste].empty()) {
- for (auto f : foundations)
- if (is_valid_foundations_move(waste, f))
- moves.emplace_back(move::mtype::regular, waste, f);
+ moves.emplace_back(move::mtype::stock_k_plus, stock, f, k_plus_mv.first, false, k_plus_mv.second);
- if (rules.hole && is_valid_hole_move(waste))
- moves.emplace_back(move::mtype::regular, waste, hole);
+ if (rules.hole && is_valid_hole_move(from))
+ moves.emplace_back(move::mtype::stock_k_plus, stock, hole, k_plus_mv.first, false, k_plus_mv.second);
}
}
-card game_state::stock_card_from_index(pile::size_type i) const {
- if (i < piles[stock].size()) {
- return piles[stock][i];
+card game_state::stock_card_from_count(int8_t count) const {
+ if (count > 0) {
+ return piles[stock][count - 1];
} else {
- // If we are moving from the waste in a readeal (by looping), finds the correct card
- auto _i = static_cast<pile::size_type>(piles[stock].size() + piles[waste].size() - 1 - i);
- return piles[waste][_i];
+ return piles[waste][-count];
}
}