Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

ConstBoard.hpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file ConstBoard.hpp
00003  */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef CONSTBOARD_H
00007 #define CONSTBOARD_H
00008 
00009 #include "Hex.hpp"
00010 #include "BoardIterator.hpp"
00011 #include "Pattern.hpp"
00012 
00013 _BEGIN_BENZENE_NAMESPACE_
00014 
00015 //----------------------------------------------------------------------------
00016 
00017 /** @page boardrepresentation Board Representation
00018     The HexPoints on the board are laid out as in the following diagram
00019     (see @ref hexpoints for information on HexPoints):
00020   
00021     @verbatim
00022 
00023                     NORTH
00024                \--a--b--c-...-\
00025                1\  0  1  2 ... \ 1
00026       WEST      2\ 16 17 18 ... \ 2  EAST
00027                  3\ 32 33 34 ... \ 3
00028                    \--a--b--c-...-\
00029                         SOUTH
00030     @endverbatim
00031 */
00032 
00033 /** @page cellneighbours Cell Neighbours 
00034     The neighbour lists for the interior cells behave as you would
00035     expect, e.q. a1 is adjacent to b1, NORTH, WEST, and a2. For edges,
00036     adjacent edges are added to the neighbour lists for all radius',
00037     but the closure of this is not computed.  For example, WEST is in
00038     the radius 1 neighbour list of NORTH, but SOUTH is not in the
00039     radius 2 neighbour list of NORTH.  Nor is this closure computed
00040     for interior cells over edges; e.g, a1 is distance 1 from NORTH
00041     but not distance 2 from EAST (except on a 1x1 board, of course).
00042 */
00043 
00044 /** ConstBoard contains data and methods for dealing with the constant
00045     aspects of a Hex board. That is, ConstBoard stores a cell's
00046     neighbours, cell-to-cell distances, etc. It also offers iterators
00047     to run over the board and the neighbours of a cell.
00048 
00049     Only a single instance of ConstBoard exists for each boardsize.
00050 
00051     See @ref boardrepresentation for how the points are laid out on an
00052     example board, and @ref cellneighbours for a discussion on how
00053     neighbours are calculated.
00054 
00055     This class does not track played stones. To keep track of played
00056     stone information see StoneBoard.
00057  */
00058 class ConstBoard
00059 {
00060 public:
00061 
00062     /** Creates a square board or returns pre-existing instance of a
00063         board of that size. */
00064     static ConstBoard& Get(int size);
00065 
00066     /** Creates a non-square board or returns pre-existing instance of
00067         a board of that size. */
00068     static ConstBoard& Get(int width, int height);
00069     
00070     //------------------------------------------------------------------------
00071 
00072     /** Returns the width of the board. */
00073     int Width() const;
00074 
00075     /** Returns the height of the board. */
00076     int Height() const;
00077 
00078     /** Returns a bitset with all valid board cells. */
00079     bitset_t GetCells() const;
00080 
00081     /** Returns a bitset with all valid board locations
00082     (cells and edges). */
00083     bitset_t GetLocations() const;
00084 
00085     /** Returns a bitset of cells comprising all valid moves
00086     (this includes swap and resign). */
00087     bitset_t GetValid() const;
00088     
00089     /** Returns true if cell is a valid cell on this board. */
00090     bool IsCell(HexPoint cell) const;
00091 
00092     /** Returns true if bs encodes a set of valid cells. */
00093     bool IsCell(const bitset_t& bs) const;
00094     
00095     /** Returns true if cell is a location on this board. */
00096     bool IsLocation(HexPoint cell) const;
00097 
00098     /** Returns true if bs encodes a set of valid locations. */
00099     bool IsLocation(const bitset_t& bs) const;
00100     
00101     /** Returns true if cell is a valid move on this board. */
00102     bool IsValid(HexPoint cell) const;
00103 
00104     /** Returns true if bs encodes a set of valid moves. */
00105     bool IsValid(const bitset_t& bs) const;
00106 
00107     /** Returns true if p1 is adjacent to p2. Iterates over neighbour
00108         list of p1, so not O(1). */
00109     bool Adjacent(HexPoint p1, HexPoint p2) const;
00110 
00111     /** Returns the distance between two valid HexPoints. */
00112     int Distance(HexPoint x, HexPoint y) const;
00113 
00114     //------------------------------------------------------------------------
00115 
00116     /** Returns iterator to the interior board cells. */
00117     BoardIterator Interior() const;
00118 
00119     /** Returns iterator to the board cells, starting on
00120         the outer edges. */
00121     BoardIterator EdgesAndInterior() const;
00122 
00123     /** Returns iterator that runs over all valid moves. */
00124     BoardIterator AllValid() const;
00125 
00126     /** Returns iterator to the first neighbour of cell. 
00127         @see @ref cellneighbours
00128     */
00129     BoardIterator Nbs(HexPoint cell) const;
00130 
00131     /** Returns iterator to the neighbourhood extending outward by
00132         radius cells of cell. */
00133     BoardIterator Nbs(HexPoint cell, int radius) const;
00134 
00135     //------------------------------------------------------------------------
00136 
00137     /** @name Operators */
00138     // @{
00139 
00140     bool operator==(const ConstBoard& other) const;
00141 
00142     bool operator!=(const ConstBoard& other) const;
00143 
00144     // @}
00145 
00146 private:
00147 
00148     int m_width;
00149     
00150     int m_height;
00151 
00152     /** The set of all valid cells/moves. Assumed to be in the
00153         following order: special moves, edges, interior cells. */
00154     std::vector<HexPoint> m_points;
00155 
00156     /** Will probably always be zero. 
00157         @todo remove? */
00158     int m_all_index;
00159 
00160     /** Index in points where edges start. */
00161     int m_locations_index;
00162 
00163     /** Index in points where interior cells start. */
00164     int m_cells_index;
00165 
00166     /** All valid moves/cells. */
00167     bitset_t m_valid;
00168         
00169     /** All valid locations. */
00170     bitset_t m_locations;
00171 
00172     /** All valid interior cells. */
00173     bitset_t m_cells;
00174 
00175     /** Neigbour lists for each location and radius. */
00176     std::vector<HexPoint> m_neighbours[BITSETSIZE][Pattern::MAX_EXTENSION+1];
00177 
00178     //------------------------------------------------------------------------
00179 
00180     /** Constructs a square board. */
00181     ConstBoard(int size);
00182 
00183     /** Constructs a rectangular board. */
00184     ConstBoard(int width, int height);
00185 
00186     /** Destructor. */
00187     ~ConstBoard();
00188 
00189     void Init();
00190 
00191     void ComputeNeighbours();
00192 
00193     void ComputePointList();
00194 
00195     void CreateIterators();
00196 
00197     void ComputeValid();
00198 };
00199 
00200 inline int ConstBoard::Width() const
00201 { 
00202     return m_width; 
00203 }
00204 
00205 inline int ConstBoard::Height() const
00206 { 
00207     return m_height;
00208 }
00209 
00210 inline bitset_t ConstBoard::GetCells() const
00211 {
00212     return m_cells;
00213 }
00214 
00215 inline bitset_t ConstBoard::GetLocations() const
00216 {
00217     return m_locations;
00218 }
00219 
00220 inline bitset_t ConstBoard::GetValid() const
00221 {
00222     return m_valid;
00223 }
00224 
00225 inline bool ConstBoard::IsCell(HexPoint cell) const
00226 {
00227     return m_cells.test(cell);
00228 }
00229 
00230 inline bool ConstBoard::IsCell(const bitset_t& bs) const
00231 {
00232     return BitsetUtil::IsSubsetOf(bs, m_cells);
00233 }
00234 
00235 inline bool ConstBoard::IsLocation(HexPoint cell) const
00236 {
00237     return m_locations.test(cell);
00238 }
00239 
00240 inline bool ConstBoard::IsLocation(const bitset_t& bs) const
00241 {
00242     return BitsetUtil::IsSubsetOf(bs, m_locations);
00243 }
00244 
00245 inline bool ConstBoard::IsValid(HexPoint cell) const
00246 {
00247     return m_valid.test(cell);
00248 }
00249 
00250 inline bool ConstBoard::IsValid(const bitset_t& bs) const
00251 {
00252     return BitsetUtil::IsSubsetOf(bs, m_valid);
00253 }
00254 
00255 inline BoardIterator ConstBoard::Interior() const
00256 {
00257     return BoardIterator(&m_points[m_cells_index]);
00258 }
00259 
00260 inline BoardIterator ConstBoard::EdgesAndInterior() const
00261 { 
00262     return BoardIterator(&m_points[m_locations_index]);
00263 }
00264 
00265 inline BoardIterator ConstBoard::AllValid() const
00266 { 
00267     return BoardIterator(&m_points[m_all_index]);
00268 }
00269 
00270 inline BoardIterator ConstBoard::Nbs(HexPoint cell) const
00271 {
00272     HexAssert(IsLocation(cell));
00273     return BoardIterator(m_neighbours[cell][1]);
00274 }
00275 
00276 inline BoardIterator ConstBoard::Nbs(HexPoint cell, int radius) const
00277 {
00278     HexAssert(IsLocation(cell));
00279     HexAssert(0 <= radius && radius <= Pattern::MAX_EXTENSION);
00280     return BoardIterator(m_neighbours[cell][radius]);
00281 }
00282 
00283 inline bool ConstBoard::operator==(const ConstBoard& other) const
00284 {
00285     return m_width == other.m_width 
00286         && m_height == other.m_height;
00287 }
00288 
00289 inline bool ConstBoard::operator!=(const ConstBoard& other) const
00290 {
00291     return !operator==(other);
00292 }
00293 
00294 //----------------------------------------------------------------------------
00295 
00296 _END_BENZENE_NAMESPACE_
00297 
00298 #endif  // CONSTBOARD_HPP


6 Jan 2011 Doxygen 1.6.3