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 ++++++++++++++++----------------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) (limited to 'server/wall.cpp') 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; } -- cgit v1.2.3