From fa8f99cb63cd04bb8006e3f568d7f2494723528f Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Nov 2010 06:44:40 +0100 Subject: Added Tiles::sort(). --- common/tile.cpp | 16 +++++++++++++--- common/tile.h | 20 +++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) (limited to 'common') diff --git a/common/tile.cpp b/common/tile.cpp index 15d0d9d..29e0c84 100644 --- a/common/tile.cpp +++ b/common/tile.cpp @@ -1,5 +1,7 @@ #include "tile.h" +#include + Tile::Tile() { } @@ -8,7 +10,7 @@ Tile::Tile(Tile::Type t, bool re, bool ro) : type(t), red(re), rotated(ro){ } -int Tile::get_num() { +int Tile::get_num() const { if(type >= Man_1 && type <= Sou_9) { return (type - Man_1) % 9 + 1; } else { @@ -16,11 +18,19 @@ int Tile::get_num() { } } -bool Tile::operator==(const Tile& other) { +bool Tile::operator==(const Tile& other) const { return type == other.type; } +bool Tile::operator<(const Tile& other) const { + return type == other.type ? red : type < other.type; +} + Tile Tile::operator++(int) { type = Type(type + 1); return *this; -} \ No newline at end of file +} + +void Tiles::sort() { + std::sort(begin(), end()); +} diff --git a/common/tile.h b/common/tile.h index 93bdb8d..84552aa 100644 --- a/common/tile.h +++ b/common/tile.h @@ -3,6 +3,7 @@ #include #include +#include class Tile { public: @@ -60,10 +61,13 @@ class Tile { virtual ~Tile(){}; //! Get the numeric value of the tile (if one of the man/pin/sou-sets). - int get_num(); + int get_num() const; //! Compare two tiles. Equal if type matches; flags are not compared. - bool operator==(const Tile& other); + bool operator==(const Tile& other) const; + + //! Compare two tiles. Ordered by type, red < non-red. + bool operator<(const Tile& other) const; //! Increment type. Useful for iterating over all possible types. Tile operator++(int); @@ -76,6 +80,16 @@ class Tile { } }; -typedef std::vector Tiles; +//! List of tiles. +class Tiles : public std::vector { + public: + //! Sort the list of tiles. + void sort(); + + template + void serialize(Archive & ar, const unsigned int version) { + ar & boost::serialization::base_object >(*this); + } +}; #endif -- cgit v1.2.3