Groups.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef GROUPS_HPP
00007 #define GROUPS_HPP
00008
00009 #include "Hex.hpp"
00010 #include "SafeBool.hpp"
00011 #include "StoneBoard.hpp"
00012 #include <boost/utility.hpp>
00013
00014 _BEGIN_BENZENE_NAMESPACE_
00015
00016
00017
00018 class Groups;
00019
00020
00021
00022
00023
00024
00025 class Group
00026 {
00027 public:
00028
00029
00030
00031
00032 Group();
00033
00034
00035 std::size_t Size() const;
00036
00037
00038 HexColor Color() const;
00039
00040
00041 HexPoint Captain() const;
00042
00043
00044 bool IsMember(HexPoint point) const;
00045
00046
00047 const bitset_t& Members() const;
00048
00049
00050 const bitset_t& Nbs() const;
00051
00052
00053
00054
00055 const bitset_t& Nbs(HexColorSet colorset) const;
00056
00057 private:
00058
00059 friend class GroupBuilder;
00060
00061 HexColor m_color;
00062
00063 HexPoint m_captain;
00064
00065 bitset_t m_members;
00066
00067 bitset_t m_nbs;
00068
00069
00070 const Groups* m_groups;
00071
00072
00073
00074
00075 std::vector<std::size_t> m_nbs_index;
00076
00077
00078 mutable bool m_colorsets_computed;
00079
00080
00081 mutable bitset_t m_nbs_colorset[NUM_COLOR_SETS];
00082
00083 Group(const Groups* groups, HexColor color, HexPoint captain,
00084 const bitset_t& members, const bitset_t& nbs);
00085
00086 void ComputeColorsetNbs() const;
00087 };
00088
00089 inline Group::Group()
00090 : m_captain(INVALID_POINT),
00091 m_groups(0),
00092 m_colorsets_computed(false)
00093 {
00094 }
00095
00096
00097 inline Group::Group(const Groups* groups, HexColor color, HexPoint captain,
00098 const bitset_t& members, const bitset_t& nbs)
00099 : m_color(color),
00100 m_captain(captain),
00101 m_members(members),
00102 m_nbs(nbs),
00103 m_groups(groups),
00104 m_colorsets_computed(false)
00105 {
00106 }
00107
00108 inline std::size_t Group::Size() const
00109 {
00110
00111 return m_members.count();
00112 }
00113
00114 inline HexColor Group::Color() const
00115 {
00116 return m_color;
00117 }
00118
00119 inline HexPoint Group::Captain() const
00120 {
00121 return m_captain;
00122 }
00123
00124 inline bool Group::IsMember(HexPoint point) const
00125 {
00126 return m_members.test(point);
00127 }
00128
00129 inline const bitset_t& Group::Members() const
00130 {
00131 return m_members;
00132 }
00133
00134 inline const bitset_t& Group::Nbs() const
00135 {
00136 return m_nbs;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 class Groups
00147 {
00148 public:
00149
00150
00151 Groups();
00152
00153
00154 const Group& GetGroup(HexPoint point) const;
00155
00156
00157 HexPoint CaptainOf(HexPoint point) const;
00158
00159
00160 bool IsCaptain(HexPoint point) const;
00161
00162
00163
00164
00165
00166 std::size_t NumGroups() const;
00167
00168
00169 std::size_t NumGroups(HexColorSet colorset) const;
00170
00171
00172 std::size_t NumGroups(HexColor color) const;
00173
00174
00175
00176
00177
00178 std::size_t GroupIndex(HexPoint point, HexColorSet colorset) const;
00179
00180
00181
00182
00183 std::size_t GroupIndex(HexPoint point, HexColor color) const;
00184
00185
00186
00187
00188
00189
00190 bitset_t Nbs(HexPoint point) const;
00191
00192 bitset_t Nbs(HexPoint point, HexColorSet colorset) const;
00193
00194 bitset_t Nbs(HexPoint point, HexColor color) const;
00195
00196 bitset_t Nbs(const Group& group) const;
00197
00198 bitset_t Nbs(const Group& group, HexColorSet colorset) const;
00199
00200 bitset_t Nbs(const Group& group, HexColor color) const;
00201
00202
00203
00204
00205 bool IsGameOver() const;
00206
00207
00208
00209 HexColor GetWinner() const;
00210
00211
00212 bitset_t CaptainizeBitset(bitset_t locations) const;
00213
00214
00215
00216
00217 StoneBoard& Board();
00218
00219
00220 const StoneBoard& Board() const;
00221
00222 private:
00223
00224 friend class Group;
00225
00226 friend class GroupBuilder;
00227
00228 friend class GroupIterator;
00229
00230 StoneBoard* m_brd;
00231
00232 std::vector<Group> m_groups;
00233
00234 std::vector<std::size_t> m_group_index;
00235 };
00236
00237 inline Groups::Groups()
00238 : m_brd(0)
00239 {
00240 }
00241
00242 inline std::size_t Groups::NumGroups() const
00243 {
00244 return m_groups.size();
00245 }
00246
00247 inline std::size_t Groups::NumGroups(HexColor color) const
00248 {
00249 return NumGroups(HexColorSetUtil::Only(color));
00250 }
00251
00252 inline std::size_t Groups::GroupIndex(HexPoint point, HexColor color) const
00253 {
00254 return GroupIndex(point, HexColorSetUtil::Only(color));
00255 }
00256
00257 inline const Group& Groups::GetGroup(HexPoint point) const
00258 {
00259 return m_groups[m_group_index[point]];
00260 }
00261
00262 inline bitset_t Groups::Nbs(HexPoint point) const
00263 {
00264 return GetGroup(point).Nbs();
00265 }
00266
00267 inline bitset_t Groups::Nbs(HexPoint point, HexColor color) const
00268 {
00269 return GetGroup(point).Nbs(HexColorSetUtil::Only(color));
00270 }
00271
00272 inline bitset_t Groups::Nbs(HexPoint point, HexColorSet colorset) const
00273 {
00274 return GetGroup(point).Nbs(colorset);
00275 }
00276
00277 inline bitset_t Groups::Nbs(const Group& group) const
00278 {
00279 return GetGroup(group.Captain()).Nbs();
00280 }
00281
00282 inline bitset_t Groups::Nbs(const Group& group, HexColor color) const
00283 {
00284 return GetGroup(group.Captain()).Nbs(HexColorSetUtil::Only(color));
00285 }
00286
00287 inline bitset_t Groups::Nbs(const Group& group, HexColorSet colorset) const
00288 {
00289 return GetGroup(group.Captain()).Nbs(colorset);
00290 }
00291
00292 inline HexPoint Groups::CaptainOf(HexPoint point) const
00293 {
00294 return GetGroup(point).Captain();
00295 }
00296
00297 inline bool Groups::IsCaptain(HexPoint point) const
00298 {
00299 return GetGroup(point).Captain() == point;
00300 }
00301
00302 inline StoneBoard& Groups::Board()
00303 {
00304 return *m_brd;
00305 }
00306
00307 inline const StoneBoard& Groups::Board() const
00308 {
00309 return *m_brd;
00310 }
00311
00312
00313
00314
00315 class GroupIterator : public SafeBool<GroupIterator>
00316 {
00317 public:
00318
00319
00320 GroupIterator(const Groups& groups);
00321
00322
00323 GroupIterator(const Groups& groups, HexColorSet colorset);
00324
00325
00326 GroupIterator(const Groups& groups, HexColor color);
00327
00328
00329 const Group& operator*() const;
00330
00331
00332 const Group* operator->() const;
00333
00334
00335 void operator++();
00336
00337
00338 bool boolean_test() const;
00339
00340 private:
00341
00342 const Groups* m_groups;
00343
00344 HexColorSet m_colorset;
00345
00346 std::size_t m_index;
00347
00348 void FindNextInColorSet();
00349 };
00350
00351 inline GroupIterator::GroupIterator(const Groups& groups)
00352 : m_groups(&groups),
00353 m_colorset(ALL_COLORS),
00354 m_index(0)
00355 {
00356 }
00357
00358 inline GroupIterator::GroupIterator(const Groups& groups, HexColorSet colorset)
00359 : m_groups(&groups),
00360 m_colorset(colorset),
00361 m_index(0)
00362 {
00363 FindNextInColorSet();
00364 }
00365
00366 inline GroupIterator::GroupIterator(const Groups& groups, HexColor color)
00367 : m_groups(&groups),
00368 m_colorset(HexColorSetUtil::Only(color)),
00369 m_index(0)
00370 {
00371 FindNextInColorSet();
00372 }
00373
00374 inline const Group& GroupIterator::operator*() const
00375 {
00376 return m_groups->m_groups[m_index];
00377 }
00378
00379 inline const Group* GroupIterator::operator->() const
00380 {
00381 return &m_groups->m_groups[m_index];
00382 }
00383
00384 inline void GroupIterator::operator++()
00385 {
00386 ++m_index;
00387 FindNextInColorSet();
00388 }
00389
00390 inline void GroupIterator::FindNextInColorSet()
00391 {
00392 while (m_index < m_groups->m_groups.size()
00393 && !HexColorSetUtil::InSet(m_groups->m_groups[m_index].Color(),
00394 m_colorset))
00395 ++m_index;
00396 }
00397
00398 inline bool GroupIterator::boolean_test() const
00399 {
00400 return m_index < m_groups->m_groups.size();
00401 }
00402
00403
00404
00405
00406 class GroupBuilder
00407 {
00408 public:
00409
00410
00411 static void Build(const StoneBoard& brd, Groups& groups);
00412
00413 private:
00414
00415 };
00416
00417
00418
00419 _END_BENZENE_NAMESPACE_
00420
00421 #endif // GROUPS_HPP