@@ -818,39 +818,6 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
818818 throw UndefinedException ();
819819 }
820820
821- // / @name Dimensions of the game
822- // @{
823- // / The number of strategies for each player
824- virtual Array<int > NumStrategies () const = 0;
825- // / Gets the i'th strategy in the game, numbered globally
826- virtual GameStrategy GetStrategy (int p_index) const = 0;
827- // / Creates a new strategy for the player
828- virtual GameStrategy NewStrategy (const GamePlayer &p_player, const std::string &p_label)
829- {
830- throw UndefinedException ();
831- }
832- // / Remove the strategy from the game
833- virtual void DeleteStrategy (const GameStrategy &p_strategy) { throw UndefinedException (); }
834- // / Returns the number of strategy contingencies in the game
835- int NumStrategyContingencies () const
836- {
837- BuildComputedValues ();
838- return std::transform_reduce (
839- m_players.begin (), m_players.end (), 0 , std::multiplies<>(),
840- [](const std::shared_ptr<GamePlayerRep> &p) { return p->m_strategies .size (); });
841- }
842- // / Returns the total number of actions in the game
843- virtual int BehavProfileLength () const = 0;
844- // / Returns the total number of strategies in the game
845- int MixedProfileLength () const
846- {
847- BuildComputedValues ();
848- return std::transform_reduce (
849- m_players.begin (), m_players.end (), 0 , std::plus<>(),
850- [](const std::shared_ptr<GamePlayerRep> &p) { return p->m_strategies .size (); });
851- }
852- // @}
853-
854821 virtual PureStrategyProfile NewPureStrategyProfile () const = 0;
855822 virtual MixedStrategyProfile<double > NewMixedStrategyProfile (double ) const = 0;
856823 virtual MixedStrategyProfile<Rational> NewMixedStrategyProfile (const Rational &) const = 0;
@@ -899,14 +866,48 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
899866 virtual GamePlayer NewPlayer () = 0;
900867 // @}
901868
869+ // / @name Dimensions of the game
870+ // @{
871+ using Strategies =
872+ NestedElementCollection<Game, &GameRep::GetPlayers, &GamePlayerRep::GetStrategies>;
873+ // / Returns the set of strategies in the game
874+ Strategies GetStrategies () const
875+ {
876+ BuildComputedValues ();
877+ return Strategies (std::const_pointer_cast<GameRep>(this ->shared_from_this ()));
878+ }
879+ // / Gets the i'th strategy in the game, numbered globally starting from 1
880+ GameStrategy GetStrategy (const std::size_t p_index) const
881+ {
882+ const auto strategies = GetStrategies ();
883+ if (p_index < 1 || p_index > strategies.size ()) {
884+ throw std::out_of_range (" Strategy index out of range" );
885+ }
886+ return *std::next (strategies.begin (), p_index - 1 );
887+ }
888+ // / Creates a new strategy for the player
889+ virtual GameStrategy NewStrategy (const GamePlayer &p_player, const std::string &p_label)
890+ {
891+ throw UndefinedException ();
892+ }
893+ // / Remove the strategy from the game
894+ virtual void DeleteStrategy (const GameStrategy &p_strategy) { throw UndefinedException (); }
895+ // / Returns the total number of actions in the game
896+ virtual int BehavProfileLength () const = 0;
897+ // @}
898+
902899 // / @name Information sets
903900 // @{
904- class Infosets ;
901+ using Infosets =
902+ NestedElementCollection<Game, &GameRep::GetPlayers, &GamePlayerRep::GetInfosets>;
905903
906904 // / Returns the iset'th information set in the game (numbered globally)
907905 virtual GameInfoset GetInfoset (int iset) const { throw UndefinedException (); }
908906 // / Returns the set of information sets in the game
909- virtual Infosets GetInfosets () const ;
907+ virtual Infosets GetInfosets () const
908+ {
909+ return Infosets (std::const_pointer_cast<GameRep>(this ->shared_from_this ()));
910+ }
910911 // / Sort the information sets for each player in a canonical order
911912 virtual void SortInfosets () {}
912913 // / Returns the set of actions taken by the infoset's owner before reaching this infoset
@@ -958,83 +959,6 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
958959 virtual void BuildComputedValues () const {}
959960};
960961
961- class GameRep ::Infosets {
962- Players m_players;
963-
964- public:
965- explicit Infosets (const Players &outer) : m_players(outer) {}
966-
967- class iterator {
968- using OuterIter = Players::iterator;
969- using InnerIter = GamePlayerRep::Infosets::iterator;
970-
971- OuterIter m_playerIterator, m_playerEnd;
972- InnerIter m_infosetIterator, m_infosetEnd;
973-
974- void next ()
975- {
976- while (m_playerIterator != m_playerEnd && m_infosetIterator == m_infosetEnd) {
977- ++m_playerIterator;
978- if (m_playerIterator != m_playerEnd) {
979- auto infosets = (*m_playerIterator)->GetInfosets ();
980- m_infosetIterator = infosets.begin ();
981- m_infosetEnd = infosets.end ();
982- }
983- }
984- }
985-
986- public:
987- using iterator_category = std::forward_iterator_tag;
988- using value_type = GameInfoset;
989- using reference = GameInfoset;
990- using pointer = GameInfoset;
991- using difference_type = std::ptrdiff_t ;
992-
993- iterator () = default ;
994-
995- iterator (const OuterIter &p_playerIterator, const OuterIter &p_playerEnd)
996- : m_playerIterator(p_playerIterator), m_playerEnd(p_playerEnd)
997- {
998- if (m_playerIterator != m_playerEnd) {
999- const auto infosets = (*m_playerIterator)->GetInfosets ();
1000- m_infosetIterator = infosets.begin ();
1001- m_infosetEnd = infosets.end ();
1002- }
1003- next ();
1004- }
1005-
1006- reference operator *() const { return *m_infosetIterator; }
1007- pointer operator ->() const { return *m_infosetIterator; }
1008-
1009- iterator &operator ++()
1010- {
1011- ++m_infosetIterator;
1012- next ();
1013- return *this ;
1014- }
1015-
1016- iterator operator ++(int )
1017- {
1018- iterator tmp = *this ;
1019- ++(*this );
1020- return tmp;
1021- }
1022-
1023- friend bool operator ==(const iterator &a, const iterator &b)
1024- {
1025- return a.m_playerIterator == b.m_playerIterator &&
1026- (a.m_playerIterator == a.m_playerEnd || a.m_infosetIterator == b.m_infosetIterator );
1027- }
1028-
1029- friend bool operator !=(const iterator &a, const iterator &b) { return !(a == b); }
1030- };
1031-
1032- iterator begin () const { return {m_players.begin (), m_players.end ()}; }
1033- iterator end () const { return {m_players.end (), m_players.end ()}; }
1034- };
1035-
1036- inline GameRep::Infosets GameRep::GetInfosets () const { return Infosets (GetPlayers ()); }
1037-
1038962// =======================================================================
1039963// Inline members of game representation classes
1040964// =======================================================================
0 commit comments