diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/wall.cpp | 68 | ||||
-rw-r--r-- | server/wall.h | 32 |
2 files changed, 100 insertions, 0 deletions
diff --git a/server/wall.cpp b/server/wall.cpp new file mode 100644 index 0000000..b19d81a --- /dev/null +++ b/server/wall.cpp @@ -0,0 +1,68 @@ +#include "wall.h" + +#include <boost/random/uniform_int.hpp> +#include <boost/random/variate_generator.hpp> +#include <ctime> + +#include <iostream> + +Wall::Wall() : rand_gen( std::time(0) ) { + + // The wall consists of 136 tiles in Riichi mahjong + num_tiles = 136; + + + // Awesome algorithm that loops trough a enum and creates a wall with 4 tiles of each type (and 1 red for each 5-tile( + for( Tile::Type current = Tile::Man_1; current <= Tile::Hatsu; current = Tile::Type( current + 1 )) { + + if(current == Tile::Man_5 || current == Tile::Pin_5 || current == Tile::Sou_5) { + + Tile tile1(current, true, false); + m_wall.push_back(tile1); + + } else { + + Tile tile1(current, false, false); + m_wall.push_back(tile1); + + } + + + Tile tile2(current, false, false); + Tile tile3(current, false, false); + Tile tile4(current, false, false); + + m_wall.push_back(tile2); + m_wall.push_back(tile3); + m_wall.push_back(tile4); + + #ifdef DEBUG + std::cout << "Added 4: " << tile2.type << std::endl; + #endif + } + +} + +bool Wall::is_done() { + if (num_tiles <= 14) { + return true; + } else { + return false; + } +} + +Tile Wall::take_one() { + + boost::uniform_int<> range( 0, num_tiles - 1 ); + + boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(rand_gen, range); + + int num = die(); + + Tile to_return = m_wall[num]; + m_wall.erase( m_wall.begin() + num); + + num_tiles--; + + return to_return; +} diff --git a/server/wall.h b/server/wall.h new file mode 100644 index 0000000..8f864d0 --- /dev/null +++ b/server/wall.h @@ -0,0 +1,32 @@ +#ifndef WALL_H +#define WALL_H + +#include "..\common\tile.h" + +#include <boost/random/mersenne_twister.hpp> +#include <vector> + + +class Wall { + private: + + //! Our random number generator. Initialized with a seed of the curren time that the object is constructed. + boost::mt19937 rand_gen; + + //! Contains all the tiles in the wall + std::vector<Tile> m_wall; + + //! Contains the number of tiles left in the wall + int num_tiles; + + public: + Wall(); + + //! Returns true if there's only 14 tiles left in the wall (Round ends) + bool is_done(); + + //! Returns a random tile from the wall + Tile take_one(); +}; + +#endif // WALL_H |