Cluedo Solver v1.0
Cluedo game solver making deductions.
Loading...
Searching...
No Matches
Solver.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "CardSet.hpp"
4#include "Error.hpp"
5#include "Player.hpp"
6#include "utils/Result.hpp"
7
10
11namespace Cluedo {
12
14struct PlayerData {
15 std::string name;
16 std::size_t card_count;
17};
18
24class Solver {
25public:
26 static constexpr std::size_t MIN_PLAYER_COUNT = 2;
27 static constexpr std::size_t MAX_PLAYER_COUNT = 6;
28 static constexpr std::size_t SOLUTION_CARD_COUNT = 3;
29
37 static Result<Solver, Error> create(std::vector<PlayerData> const& players_data);
38
42 std::size_t player_count() const { return m_players.size() - 1; }
43
49 Player const& player(std::size_t player_index) const { return m_players.at(player_index); }
55 Player& player(std::size_t player_index) { return m_players.at(player_index); }
56
64 void learn_player_card_state(std::size_t player_index, Card card, bool has_card, bool infer_new_info = true);
65
72 void learn_player_has_any_of_cards(std::size_t player_index, CardSet const& card_set, bool infer_new_info = true);
73
75 struct Suggestion {
80 std::optional<std::size_t> responding_player_index;
81 std::optional<Card> response_card;
82 };
83
89 void learn_from_suggestion(Suggestion const& suggestion, bool infer_new_info = true);
90
92 bool are_constraints_satisfied() const;
93
96 using SolutionProbabilityPair = std::pair<std::tuple<Card, Card, Card>, float>;
97
101 std::vector<SolutionProbabilityPair> find_most_likely_solutions() const;
102
103private:
104 static constexpr std::size_t MAX_ITERATIONS = 1'000'000;
105
106 explicit Solver(std::vector<Player>&& players)
107 : m_players(std::move(players)) {}
108
109 std::size_t solution_player_index() const { return m_players.size() - 1; }
110
111 void infer_new_information();
112 bool assign_cards_to_players(std::vector<Card> const& cards);
113 bool are_constraints_satisfied_for_solution_search() const;
114
115 std::vector<Player> m_players;
116};
117
118};
The file that contains the definition of the Cluedo::CardSet class.
Card
All the cards in Cluedo.
Definition Card.hpp:66
This file contains the list of errors that can occur in the application.
The file that contains the definition of the Cluedo::Player class.
The file that defines the Result class and the TRY and MUST macros.
A set of cards.
Definition CardSet.hpp:20
The player of a game.
Definition Player.hpp:27
static constexpr std::size_t MAX_PLAYER_COUNT
The maximum number of players that can play a game.
Definition Solver.hpp:27
std::vector< SolutionProbabilityPair > find_most_likely_solutions() const
Definition Solver.cpp:249
Player & player(std::size_t player_index)
Definition Solver.hpp:55
void learn_player_has_any_of_cards(std::size_t player_index, CardSet const &card_set, bool infer_new_info=true)
Definition Solver.cpp:51
bool are_constraints_satisfied() const
Checks if the constraints of the game are satisfied.
Definition Solver.cpp:98
static constexpr std::size_t SOLUTION_CARD_COUNT
The number of cards that make a solution (a suspect, a weapon and a room).
Definition Solver.hpp:28
void learn_player_card_state(std::size_t player_index, Card card, bool has_card, bool infer_new_info=true)
Definition Solver.cpp:34
static constexpr std::size_t MIN_PLAYER_COUNT
The minimum number of players that can play a game.
Definition Solver.hpp:26
Player const & player(std::size_t player_index) const
Definition Solver.hpp:49
std::pair< std::tuple< Card, Card, Card >, float > SolutionProbabilityPair
A pair that contains a solution (a suspect, a weapon and a room) and its probability.
Definition Solver.hpp:96
std::size_t player_count() const
Definition Solver.hpp:42
void learn_from_suggestion(Suggestion const &suggestion, bool infer_new_info=true)
Definition Solver.cpp:74
static Result< Solver, Error > create(std::vector< PlayerData > const &players_data)
Definition Solver.cpp:16
A class that represents a result that can either be a value or an error.
Definition Result.hpp:28
A struct that contains the data of a player.
Definition Solver.hpp:14
std::string name
The name of the player.
Definition Solver.hpp:15
std::size_t card_count
The number of cards held by the player.
Definition Solver.hpp:16
A struct that contains the data of a suggestion.
Definition Solver.hpp:75
std::size_t suggesting_player_index
The index of the player who made the suggestion.
Definition Solver.hpp:76
Card suspect
The suspect suggested.
Definition Solver.hpp:77
std::optional< std::size_t > responding_player_index
The index of the player who responded, if any.
Definition Solver.hpp:80
std::optional< Card > response_card
The card with which the player responded, if known.
Definition Solver.hpp:81
Card weapon
The weapon suggested.
Definition Solver.hpp:78
Card room
The room suggested.
Definition Solver.hpp:79