From 77a7aeba7854d15b140cc6dc3c69b15ed5f3b936 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:30:14 +0000 Subject: Major refactor, putting pile logic into game_state Much of the logic that was in the pile class was beginning to seem out of place. A lot of the rules have fairly arbitrary interactons *between* piles, which weren't best represented in the pile classes themselves, which were getting unweildy and inefficient. Hence, much of this logic has been moved to the game state class. --- src/main/game/game_state.h | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'src/main/game/game_state.h') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8adc9d6..28d849e 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -5,7 +5,8 @@ #ifndef SOLVITAIRE_GAME_STATE_H #define SOLVITAIRE_GAME_STATE_H -#define NULL_MOVE (game_state::move(255, 255)) +#define NULL_MOVE (game_state::move(PILE_REF_MAX, PILE_REF_MAX)) +#define PILE_REF_MAX (255) #include #include @@ -15,28 +16,35 @@ #include #include "card.h" -#include "sol_rules.h" #include "pile.h" +#include "sol_rules.h" class deal_parser; class game_state { friend class deal_parser; public: + // Defines types typedef uint8_t pile_ref; typedef std::pair move; - // Create a game state representation from a JSON doc + // Constructors + // Creates a game state representation from a JSON doc game_state(const sol_rules&, const rapidjson::Document&); - // Do the same from a 'rules' object + // Does the same from a 'rules' object game_state(const sol_rules&, int seed); + // Alters state void make_move(move); void undo_move(move); + std::vector get_legal_moves() const; + + // Inspects state bool is_solved() const; const std::vector& get_data() const; + // Prints std::ostream& print(std::ostream&) const; friend bool operator==(const game_state&, const game_state&); @@ -47,26 +55,31 @@ public: private: static std::vector gen_shuffled_deck(int, int); + // Private constructor explicit game_state(const sol_rules&); + // Used in get_legal_moves() + bool is_valid_tableau_move(pile_ref, pile_ref) const; + bool is_valid_foundations_move(pile_ref, pile_ref) const; + bool is_valid_hole_move(pile_ref) const; + + // Prints void print_header(std::ostream&, const char*) const; void print_piles(std::ostream&, const std::vector&) const; void print_pile(std::ostream&, pile_ref) const; void print_top_of_piles(std::ostream&, const std::vector&) const; void print_top_of_pile(std::ostream&, pile_ref) const; + // References to piles sol_rules rules; - std::vector foundations; - std::vector cells; std::vector tableau_piles; - pile_ref reserve; + std::vector cells; pile_ref stock; pile_ref waste; + std::vector foundations; pile_ref hole; - std::vector addable_piles; - std::vector removable_piles; - + // The core piles std::vector piles; }; -- cgit v1.2.1 From 4208d2570e74ab54d1878c72110bd380e7b197f2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:21:47 +0000 Subject: Move game state printing into dedicated class --- src/main/game/game_state.h | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'src/main/game/game_state.h') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 28d849e..30af8a6 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -9,7 +9,6 @@ #define PILE_REF_MAX (255) #include -#include #include #include @@ -23,6 +22,7 @@ class deal_parser; class game_state { friend class deal_parser; + friend class state_printer; public: // Defines types typedef uint8_t pile_ref; @@ -44,11 +44,9 @@ public: bool is_solved() const; const std::vector& get_data() const; - // Prints - std::ostream& print(std::ostream&) const; - friend bool operator==(const game_state&, const game_state&); friend std::ostream& operator<< (std::ostream&, const game_state&); + friend std::size_t hash_value(game_state const&); friend std::size_t hash_value(std::vector const&); @@ -63,13 +61,6 @@ private: bool is_valid_foundations_move(pile_ref, pile_ref) const; bool is_valid_hole_move(pile_ref) const; - // Prints - void print_header(std::ostream&, const char*) const; - void print_piles(std::ostream&, const std::vector&) const; - void print_pile(std::ostream&, pile_ref) const; - void print_top_of_piles(std::ostream&, const std::vector&) const; - void print_top_of_pile(std::ostream&, pile_ref) const; - // References to piles sol_rules rules; std::vector tableau_piles; -- cgit v1.2.1 From 4161e60c9d332826129efddd5dabc60e3393057d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:47:00 +0000 Subject: Create game state setup for flower garden --- src/main/game/game_state.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main/game/game_state.h') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 30af8a6..6aa3918 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -67,6 +67,7 @@ private: std::vector cells; pile_ref stock; pile_ref waste; + std::vector reserve; std::vector foundations; pile_ref hole; -- cgit v1.2.1 From 067152f57ced18c5f706144909345125754e8664 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 19:02:36 +0000 Subject: Add game logic for built pile (AA) and refactor --- src/main/game/game_state.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/main/game/game_state.h') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 6aa3918..f43348f 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -5,9 +5,6 @@ #ifndef SOLVITAIRE_GAME_STATE_H #define SOLVITAIRE_GAME_STATE_H -#define NULL_MOVE (game_state::move(PILE_REF_MAX, PILE_REF_MAX)) -#define PILE_REF_MAX (255) - #include #include @@ -26,7 +23,10 @@ class game_state { public: // Defines types typedef uint8_t pile_ref; - typedef std::pair move; + struct move { + move(pile_ref, pile_ref, pile::size_type = 1); + pile_ref from; pile_ref to; pile::size_type count; + }; // Constructors // Creates a game state representation from a JSON doc @@ -45,11 +45,16 @@ public: const std::vector& get_data() const; friend bool operator==(const game_state&, const game_state&); - friend std::ostream& operator<< (std::ostream&, const game_state&); + + // Static pile ref members/vars + static pile_ref PILE_REF_MAX; + static move null_move(); friend std::size_t hash_value(game_state const&); friend std::size_t hash_value(std::vector const&); + friend std::ostream& operator<< (std::ostream&, const game_state&); + private: static std::vector gen_shuffled_deck(int, int); @@ -60,6 +65,12 @@ private: bool is_valid_tableau_move(pile_ref, pile_ref) const; bool is_valid_foundations_move(pile_ref, pile_ref) const; bool is_valid_hole_move(pile_ref) const; + void get_built_group_moves(std::vector&) const; + pile::size_type get_built_group_height(pile_ref) const; + bool valid_built_group_move(card, card, card) const; + void add_built_group_move(std::vector&, pile_ref, pile_ref) const; + void add_empty_built_group_moves(std::vector&, pile_ref, pile_ref, + card) const; // References to piles sol_rules rules; -- cgit v1.2.1 From 89bf6ecb16fa6cdf66707f0590a0c1370bf19c4d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:56:24 +0000 Subject: Add logic for stock-tableau deal & 2 deck --- src/main/game/game_state.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main/game/game_state.h') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index f43348f..8f7bf57 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -56,12 +56,13 @@ public: friend std::ostream& operator<< (std::ostream&, const game_state&); private: - static std::vector gen_shuffled_deck(int, int); + static std::vector gen_shuffled_deck(int, int, bool); // Private constructor explicit game_state(const sol_rules&); // Used in get_legal_moves() + move get_stock_tableau_move() const; bool is_valid_tableau_move(pile_ref, pile_ref) const; bool is_valid_foundations_move(pile_ref, pile_ref) const; bool is_valid_hole_move(pile_ref) const; -- cgit v1.2.1