From 895afc162184f80b9c12e4a7042bd9e6680af30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atle=20Hellvik=20Havs=C3=B8?= Date: Sat, 20 Nov 2010 11:38:54 +0100 Subject: Created a Wall-class. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Atle Hellvik Havsø --- server/wall.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ server/wall.h | 32 +++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 server/wall.cpp create mode 100644 server/wall.h 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 +#include +#include + +#include + +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 > 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 +#include + + +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 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 -- cgit v1.2.3