[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Eliot-dev] Changes to eliot/game/bag.cpp [antoine-1]
From: |
eliot-dev |
Subject: |
[Eliot-dev] Changes to eliot/game/bag.cpp [antoine-1] |
Date: |
Sun, 23 Oct 2005 13:16:26 -0400 |
Index: eliot/game/bag.cpp
diff -u /dev/null eliot/game/bag.cpp:1.4.2.1
--- /dev/null Sun Oct 23 17:16:26 2005
+++ eliot/game/bag.cpp Sun Oct 23 17:16:23 2005
@@ -0,0 +1,182 @@
+/*****************************************************************************
+ * Copyright (C) 1999-2005 Eliot
+ * Authors: Antoine Fraboulet <address@hidden>
+ * Olivier Teuliere <address@hidden>
+ *
+ * $Id: bag.cpp,v 1.4.2.1 2005/10/23 17:16:23 afrab Exp $
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *****************************************************************************/
+
+#include <string>
+
+#include "tile.h"
+#include "bag.h"
+#include "debug.h"
+
+
+Bag::Bag()
+{
+ init();
+}
+
+
+void Bag::init()
+{
+ m_ntiles = 0;
+ const std::list<Tile>& allTiles = Tile::getAllTiles();
+ std::list<Tile>::const_iterator it;
+ for (it = allTiles.begin(); it != allTiles.end(); it++)
+ {
+ m_tilesMap[*it] = it->maxNumber();
+ m_ntiles += it->maxNumber();
+ }
+}
+
+
+unsigned int Bag::in(const Tile &iTile) const
+{
+ std::map<Tile, int>::const_iterator it = m_tilesMap.find(iTile);
+ if (it != m_tilesMap.end())
+ return (*it).second;
+ return 0;
+}
+
+
+unsigned int Bag::nVowels() const
+{
+ std::map<Tile, int>::const_iterator it;
+ int v = 0;
+
+ for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
+ {
+ if (it->first.isVowel())
+ v += it->second;
+ }
+ return v;
+}
+
+
+unsigned int Bag::nConsonants() const
+{
+ std::map<Tile, int>::const_iterator it;
+ int c = 0;
+
+ for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
+ {
+ if (it->first.isConsonant())
+ c += it->second;
+ }
+ return c;
+}
+
+
+Tile Bag::selectRandom()
+{
+ std::map<Tile, int>::const_iterator it;
+ int n;
+ double max = m_ntiles;
+
+ n = (int)(max * rand() / (RAND_MAX + 1.0));
+ for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
+ {
+ if (n < it->second)
+ return it->first;
+ n -= it->second;
+ }
+ ASSERT(false, "We should not come here");
+ return Tile::dummy();
+}
+
+
+void Bag::takeTile(const Tile &iTile)
+{
+ ASSERT(in(iTile), std::string("The bag does not contain the letter ") +
iTile.toChar());
+ m_tilesMap[iTile]--;
+ m_ntiles--;
+}
+
+
+int Bag::takeRandomTiles(int n, std::vector< Tile >& oTiles)
+{
+ int i = 0;
+ Tile t;
+ oTiles.clear();
+ while (nTiles() > 0 && i < n)
+ {
+ t = selectRandom();
+ takeTile(t);
+ oTiles.push_back(t);
+ i++;
+ }
+ return (i == n) ? 0 : 1;
+}
+
+
+void Bag::putbackTile(const Tile &iTile)
+{
+ ASSERT(in(iTile) < iTile.maxNumber(),
+ std::string("Cannot replace tile: ") + iTile.toChar());
+
+ m_tilesMap[iTile]++;
+ m_ntiles++;
+}
+
+
+void Bag::putbackTiles(const std::vector<Tile>& tiles)
+{
+ unsigned int i;
+ for(i=0; i < tiles.size(); i++)
+ {
+ putbackTile(tiles[i]);
+ }
+}
+
+
+void Bag::operator=(const Bag &iOther)
+{
+ m_tilesMap = iOther.m_tilesMap;
+ m_ntiles = iOther.m_ntiles;
+}
+
+
+std::string Bag::toString() const
+{
+ char buff[20];
+ std::string rs = "";
+ std::map<Tile, int>::const_iterator it;
+ for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
+ {
+ if (it->second)
+ {
+#ifdef DEBUG
+ sprintf(buff, "%c[%02d][%02d] ",
+ it->first.toChar(),
+ it->second,
+ it->second - it->first.maxNumber());
+#else
+ sprintf(buff, "%c[%02d] ", it->first.toChar(), it->second);
+#endif
+ rs += buff;
+ }
+ }
+ return rs;
+}
+
+
+/// Local Variables:
+/// mode: hs-minor
+/// c-basic-offset: 4
+/// End:
- [Eliot-dev] Changes to eliot/game/bag.cpp [antoine-1],
eliot-dev <=