From 77df00db4e91eae3e7d488aaa09f2fb01665f802 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 6 Apr 2018 22:40:18 +0100 Subject: Add streamliner & both, fix auto-founds bug --- CMakeLists.txt | 21 +++++--- src/main/game/game_state.cpp | 65 +++++++++++++++--------- src/main/game/game_state.h | 6 +-- src/main/game/game_state.legal_moves.cpp | 8 +-- src/main/input-output/input/sol_preset_types.cpp | 14 +++++ src/main/solver/solver.cpp | 4 -- 6 files changed, 69 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 125e121..65b973d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,9 +41,9 @@ set(sources src/main/input-output/input/command_line_helper.cpp src/main/evaluation/solvability_calc.h src/main/evaluation/benchmark.cpp src/main/evaluation/benchmark.h - lib/rapidjson/document.h - lib/rapidjson/schema.h - lib/rapidjson/stringbuffer.h) + lib/rapidjson/document.h + lib/rapidjson/schema.h + lib/rapidjson/stringbuffer.h) set(sources_test src/test/test_helper.cpp src/test/test_helper.h @@ -150,6 +150,16 @@ add_executable(solvitaire-suit-reduction-streamliner ${main} ${sources}) target_link_libraries(solvitaire-suit-reduction-streamliner LINK_PUBLIC ${Boost_LIBRARIES}) set_target_properties(solvitaire-suit-reduction-streamliner PROPERTIES COMPILE_FLAGS -DSUIT_REDUCTION_STREAMLINER) +# add a separete executable for the 'auto foundations streamliner' version of the solver +add_executable(solvitaire-auto-foundations-streamliner ${main} ${sources}) +target_link_libraries(solvitaire-auto-foundations-streamliner LINK_PUBLIC ${Boost_LIBRARIES}) +set_target_properties(solvitaire-auto-foundations-streamliner PROPERTIES COMPILE_FLAGS -DAUTO_FOUNDATIONS_STREAMLINER) + +# add a separete executable for the 'both streamliners' version of the solver +add_executable(solvitaire-both-streamliners ${main} ${sources}) +target_link_libraries(solvitaire-both-streamliners LINK_PUBLIC ${Boost_LIBRARIES}) +set_target_properties(solvitaire-both-streamliners PROPERTIES COMPILE_FLAGS "-DSUIT_REDUCTION_STREAMLINER -DAUTO_FOUNDATIONS_STREAMLINER") + # add a separete executable for the 'no pile symmetry' version of the solver add_executable(solvitaire-no-pile-symmetry ${main} ${sources}) target_link_libraries(solvitaire-no-pile-symmetry LINK_PUBLIC ${Boost_LIBRARIES}) @@ -165,11 +175,6 @@ add_executable(solvitaire-no-auto-foundations ${main} ${sources}) target_link_libraries(solvitaire-no-auto-foundations LINK_PUBLIC ${Boost_LIBRARIES}) set_target_properties(solvitaire-no-auto-foundations PROPERTIES COMPILE_FLAGS -DNO_AUTO_FOUNDATIONS) -# add a separete executable for the 'no card just moved' version of the solver -add_executable(solvitaire-no-card-just-moved ${main} ${sources}) -target_link_libraries(solvitaire-no-card-just-moved LINK_PUBLIC ${Boost_LIBRARIES}) -set_target_properties(solvitaire-no-card-just-moved PROPERTIES COMPILE_FLAGS -DNO_CARD_JUST_MOVED) - ############################################################################### ## testing #################################################################### diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 50b8f19..9c32b4f 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -450,33 +450,48 @@ void game_state::update_auto_foundation_moves(pile_ref target_pile) { } bool game_state::is_valid_auto_foundation_move(pile_ref target_pile) const { - if (rules.build_pol == pol::ANY_SUIT || rules.build_pol == pol::RED_BLACK) { - 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(); - card target_card = card(target_suit, target_rank); - - for (pile_ref pr : foundations) { - if (pr == target_pile) continue; - - card::suit_t foundation_suit(pr - foundations.front()); - card::rank_t foundation_rank = piles[pr].empty() ? card::rank_t(0) - : piles[pr].top_card().get_rank(); - card foundation_card = card(foundation_suit, foundation_rank); - int rank_diff = int(target_rank) - - int(foundation_card.get_rank()); - - if (rules.build_pol == pol::RED_BLACK - && foundation_card.get_colour() == target_card.get_colour()) { - if (rank_diff >= 3) { - return false; - } - } else if (rank_diff >= 2) { - return false; - } +#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_col_rank_diff = 0; + int other_col_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_colour() == c.get_colour()) { + same_col_rank_diff = rank_diff; + } else { + other_col_rank_diff = max(other_col_rank_diff, rank_diff); } } - return true; + + bool other_col_within_1 = other_col_rank_diff <= 1; + bool other_col_within_2 = other_col_rank_diff <= 2; + bool same_col_within_3 = same_col_rank_diff <= 3; + + if (rules.build_pol == pol::RED_BLACK) { + return other_col_within_1 || (other_col_within_2 && same_col_within_3); + } else { + assert(rules.build_pol == pol::ANY_SUIT); + return other_col_within_2; + } } diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 0be07ae..1e527ea 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -50,11 +50,7 @@ public: void place_card(pile_ref, card); card take_card(pile_ref); - std::vector get_legal_moves( -#ifndef NO_CARD_JUST_MOVED - move = move(255, 255, 255) -#endif - ); + std::vector get_legal_moves(move = move(255, 255, 255)); // Inspects state bool is_solved() const; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 24304a1..5a49ec9 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -13,11 +13,7 @@ typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; -vector game_state::get_legal_moves( -#ifndef NO_CARD_JUST_MOVED - move parent_move -#endif -) { +vector game_state::get_legal_moves(move parent_move) { // The next legal moves vector moves; @@ -45,11 +41,9 @@ vector game_state::get_legal_moves( // just moved a card to if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty() -#ifndef NO_CARD_JUST_MOVED || (rem_ref == parent_move.to && parent_move.count <= 1 && parent_move.from != stock) -#endif ) continue; // If we have a foundations card as the rem_ref diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 08f79e7..e4d06a3 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -622,6 +622,20 @@ map sol_preset_types::mp = { R"( { + "tableau piles": { + "build policy": "same-suit", + "spaces policy": "kings" + }, + "cells": 8 +} +)" + + }, + { + "eight-off-any-card-spaces", + + R"( +{ "tableau piles": { "build policy": "same-suit" }, diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 60b1351..884b2e5 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -118,11 +118,7 @@ void solver::add_children(std::vector& moves) { } vector solver::get_next_moves() { -#ifndef NO_CARD_JUST_MOVED return state.get_legal_moves(current_node->move); -#else - return state.get_legal_moves(); -#endif } void solver::add_child(game_state::move move) { -- cgit v1.2.1