StoneBoard.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef STONEBOARD_H
00007 #define STONEBOARD_H
00008
00009 #include "Hex.hpp"
00010 #include "ConstBoard.hpp"
00011 #include "ZobristHash.hpp"
00012
00013 _BEGIN_BENZENE_NAMESPACE_
00014
00015
00016
00017
00018
00019 typedef std::vector<byte> BoardID;
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class StoneBoard
00036 {
00037 public:
00038
00039
00040
00041 StoneBoard();
00042
00043
00044 explicit StoneBoard(unsigned size);
00045
00046
00047 StoneBoard(unsigned width, unsigned height);
00048
00049
00050
00051 StoneBoard(unsigned width, unsigned height, const std::string& str);
00052
00053 ~StoneBoard();
00054
00055
00056
00057
00058 const ConstBoard& Const() const;
00059
00060
00061 int Width() const;
00062
00063
00064 int Height() const;
00065
00066
00067
00068
00069
00070
00071
00072 hash_t Hash() const;
00073
00074
00075
00076
00077 hash_t Hash(HexColor toPlay) const;
00078
00079
00080
00081 BoardID GetBoardID() const;
00082
00083
00084
00085 std::string GetBoardIDString() const;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 std::size_t NumStones() const;
00096
00097
00098
00099 HexColor WhoseTurn() const;
00100
00101
00102
00103
00104 bool IsStandardPosition() const;
00105
00106
00107
00108
00109 std::string Write() const;
00110
00111
00112
00113 std::string Write(const bitset_t& b) const;
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 void SetPosition(const BoardID& id);
00125
00126
00127 void SetPosition(const StoneBoard& brd);
00128
00129
00130 void SetPositionOnlyPlayed(const StoneBoard& brd);
00131
00132
00133
00134
00135
00136
00137
00138 void SetPosition(const std::string& str);
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 bitset_t GetBlack() const;
00155
00156
00157 bitset_t GetWhite() const;
00158
00159
00160 bitset_t GetColor(HexColor color) const;
00161
00162
00163 bitset_t GetEmpty() const;
00164
00165
00166 bitset_t GetOccupied() const;
00167
00168
00169 bool IsColor(HexPoint cell, HexColor color) const;
00170
00171
00172 bool IsEmpty(HexPoint cell) const;
00173
00174
00175 bool IsOccupied(HexPoint cell) const;
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 bool IsBlack(HexPoint cell) const;
00186
00187
00188 bool IsWhite(HexPoint cell) const;
00189
00190
00191 HexColor GetColor(HexPoint cell) const;
00192
00193
00194 bitset_t GetPlayed() const;
00195 bitset_t GetPlayed(HexColor color) const;
00196
00197
00198
00199 bitset_t GetLegal() const;
00200
00201
00202 bool IsPlayed(HexPoint cell) const;
00203
00204
00205 bool IsLegal(HexPoint cell) const;
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 void AddColor(HexColor color, const bitset_t& b);
00217
00218
00219 void RemoveColor(HexColor color, const bitset_t& b);
00220
00221
00222 void SetColor(HexColor color, HexPoint cell);
00223
00224
00225 void SetColor(HexColor color, const bitset_t& bs);
00226
00227
00228 bool IsSelfRotation() const;
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 void StartNewGame();
00239
00240
00241
00242
00243 void SetPlayed(const bitset_t& p);
00244
00245
00246
00247
00248
00249
00250 void PlayMove(HexColor color, HexPoint cell);
00251
00252
00253
00254 void UndoMove(HexPoint cell);
00255
00256
00257
00258 void RotateBoard();
00259
00260
00261
00262
00263 void MirrorBoard();
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 bool operator==(const StoneBoard& other) const;
00275
00276
00277 bool operator!=(const StoneBoard& other) const;
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 BoardIterator Stones(HexColorSet colorset) const;
00288
00289
00290 BoardIterator Stones(HexColor color) const;
00291
00292
00293
00294
00295
00296 private:
00297 ConstBoard* m_const;
00298
00299 bitset_t m_played;
00300
00301 bitset_t m_stones[BLACK_AND_WHITE];
00302
00303
00304 ZobristHash m_hash;
00305
00306 mutable bool m_stones_calculated;
00307
00308 mutable std::vector<HexPoint> m_stones_list[NUM_COLOR_SETS];
00309
00310 void ComputeHash();
00311
00312 void MarkAsDirty();
00313
00314 bool IsBlackWhiteDisjoint();
00315 };
00316
00317 inline const ConstBoard& StoneBoard::Const() const
00318 {
00319 return *m_const;
00320 }
00321
00322 inline int StoneBoard::Width() const
00323 {
00324 return m_const->Width();
00325 }
00326
00327 inline int StoneBoard::Height() const
00328 {
00329 return m_const->Height();
00330 }
00331
00332 inline hash_t StoneBoard::Hash() const
00333 {
00334 return m_hash.Hash(EMPTY);
00335 }
00336
00337 inline hash_t StoneBoard::Hash(HexColor toPlay) const
00338 {
00339 return m_hash.Hash(toPlay);
00340 }
00341
00342 inline bitset_t StoneBoard::GetBlack() const
00343 {
00344 return m_stones[BLACK] & Const().GetLocations();
00345 }
00346
00347 inline bitset_t StoneBoard::GetWhite() const
00348 {
00349 return m_stones[WHITE] & Const().GetLocations();
00350 }
00351
00352 inline bitset_t StoneBoard::GetColor(HexColor color) const
00353 {
00354 HexAssert(HexColorUtil::isValidColor(color));
00355 if (color == EMPTY) return GetEmpty();
00356 return m_stones[color] & Const().GetLocations();
00357 }
00358
00359 inline bitset_t StoneBoard::GetEmpty() const
00360 {
00361 return Const().GetLocations() - GetOccupied();
00362 }
00363
00364 inline bitset_t StoneBoard::GetOccupied() const
00365 {
00366 return (GetBlack() | GetWhite()) & Const().GetLocations();
00367 }
00368
00369 inline bool StoneBoard::IsBlack(HexPoint cell) const
00370 {
00371 HexAssert(Const().IsValid(cell));
00372 return m_stones[BLACK].test(cell);
00373 }
00374
00375 inline bool StoneBoard::IsWhite(HexPoint cell) const
00376 {
00377 HexAssert(Const().IsValid(cell));
00378 return m_stones[WHITE].test(cell);
00379 }
00380
00381 inline bool StoneBoard::IsColor(HexPoint cell, HexColor color) const
00382 {
00383 HexAssert(HexColorUtil::isBlackWhite(color));
00384 HexAssert(Const().IsLocation(cell));
00385 return m_stones[color].test(cell);
00386 }
00387
00388 inline bool StoneBoard::IsEmpty(HexPoint cell) const
00389 {
00390 HexAssert(Const().IsLocation(cell));
00391 return !IsOccupied(cell);
00392 }
00393
00394 inline bool StoneBoard::IsOccupied(HexPoint cell) const
00395 {
00396 HexAssert(Const().IsLocation(cell));
00397 return (IsBlack(cell) || IsWhite(cell));
00398 }
00399
00400 inline bitset_t StoneBoard::GetPlayed() const
00401 {
00402 return m_played;
00403 }
00404
00405 inline bitset_t StoneBoard::GetPlayed(HexColor color) const
00406 {
00407 HexAssert(HexColorUtil::isBlackWhite(color));
00408 return m_played & GetColor(color);
00409 }
00410
00411 inline bool StoneBoard::IsPlayed(HexPoint cell) const
00412 {
00413 HexAssert(Const().IsValid(cell));
00414 return m_played.test(cell);
00415 }
00416
00417 inline std::size_t StoneBoard::NumStones() const
00418 {
00419 return (GetOccupied() & GetPlayed() & Const().GetCells()).count();
00420 }
00421
00422 inline bool StoneBoard::operator==(const StoneBoard& other) const
00423 {
00424 return (m_const == other.m_const
00425 && m_stones[BLACK] == other.m_stones[BLACK]
00426 && m_stones[WHITE] == other.m_stones[WHITE]
00427 && m_played == other.m_played);
00428 }
00429
00430 inline bool StoneBoard::operator!=(const StoneBoard& other) const
00431 {
00432 return !(operator==(other));
00433 }
00434
00435 inline HexColor StoneBoard::WhoseTurn() const
00436 {
00437 HexAssert(IsStandardPosition());
00438 bitset_t mask = GetPlayed() & Const().GetCells();
00439 std::size_t first = (GetColor(FIRST_TO_PLAY) & mask).count();
00440 std::size_t second = (GetColor(!FIRST_TO_PLAY) & mask).count();
00441 return (first > second) ? !FIRST_TO_PLAY : FIRST_TO_PLAY;
00442 }
00443
00444 inline bool StoneBoard::IsStandardPosition() const
00445 {
00446 std::size_t diff = GetPlayed(BLACK).count() - GetPlayed(WHITE).count();
00447 return (diff == 1) || (diff == 0);
00448 }
00449
00450 inline BoardIterator StoneBoard::Stones(HexColor color) const
00451 {
00452 return Stones(HexColorSetUtil::Only(color));
00453 }
00454
00455
00456 inline std::ostream& operator<<(std::ostream &os, const StoneBoard& b)
00457 {
00458 os << b.Write();
00459 return os;
00460 }
00461
00462
00463
00464 _END_BENZENE_NAMESPACE_
00465
00466 #endif // STONEBOARD_HPP