From d5aae396856ee801eb2e2420b786fda2f13cf391 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Nov 2010 02:24:00 +0100 Subject: Changed parts of the Wall API to make it reusable. Simplified the building. --- server/wall.cpp | 62 ++++++++++++++++----------------------------------------- server/wall.h | 20 +++++++++---------- 2 files changed, 26 insertions(+), 56 deletions(-) diff --git a/server/wall.cpp b/server/wall.cpp index b19d81a..1c9ec04 100644 --- a/server/wall.cpp +++ b/server/wall.cpp @@ -6,63 +6,35 @@ #include -Wall::Wall() : rand_gen( std::time(0) ) { +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; +void Wall::build() { + // Clear any previous wall. + wall.clear(); + + for(Tile tile = Tile::Man_1; tile.type <= Tile::Hatsu; tile++) { + wall.push_back(Tile(tile.type, tile.get_num() == 5)); // Insert a copy of the current tile, and make it red if it is 5. + wall.push_back(tile); + wall.push_back(tile); + wall.push_back(tile); } } +int Wall::remaining() { + return wall.size(); +} + Tile Wall::take_one() { - - boost::uniform_int<> range( 0, num_tiles - 1 ); + boost::uniform_int<> range(0, wall.size() - 1); boost::variate_generator > die(rand_gen, range); int num = die(); - Tile to_return = m_wall[num]; - m_wall.erase( m_wall.begin() + num); - - num_tiles--; + Tile to_return = wall[num]; + wall.erase(wall.begin() + num); return to_return; } diff --git a/server/wall.h b/server/wall.h index ca48a40..aeef031 100644 --- a/server/wall.h +++ b/server/wall.h @@ -6,26 +6,24 @@ #include #include - class Wall { private: - - //! Our random number generator. Initialized with a seed of the curren time that the object is constructed. + //! Our random number generator. Initialized with a seed of the current time that the object is constructed. boost::mt19937 rand_gen; - //! Contains all the tiles in the wall - std::vector m_wall; - - //! Contains the number of tiles left in the wall - int num_tiles; + //! Contains the remaining tiles in the wall. + std::vector wall; public: Wall(); - //! Returns true if there's only 14 tiles left in the wall (Round ends) - bool is_done(); + //! Build a new wall. + void build(); + + //! Number of remaining tiles in wall. + int remaining(); - //! Returns a random tile from the wall + //! Returns a random tile from the wall. Tile take_one(); }; -- cgit v1.2.3