eliot/game/bag.cpp
Olivier Teulière b7032e2d78 - The Game library is now in C++.
This simplifies the code in many places, and allows inheritance of the
   game mode classes (Training, Duplicate and FreeGame).
 - A Tile is no more an unsigned char, but a class: it should help supporting
   i18n of the game. The Dic library still uses unsigned chars though.
 - Improved the configure script, to enable/disable the compilation of some
   interfaces.
 - Added a ncurses interface, much nicer than the text one. The game mode and
   the number of players are currently hardcoded, it is not possible to change
   them interactively (yet).
 - Repaired the save/load functions.

NOTE: The wxWindows interface compiles, but is completely broken. I'm afraid it
needs a full rewrite (to support the various game modes in particular).
2005-02-05 11:14:56 +00:00

148 lines
3.2 KiB
C++

/*****************************************************************************
* Copyright (C) 1999-2005 Eliot
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
* Olivier Teuliere <ipkiss@via.ecp.fr>
*
* $Id: bag.cpp,v 1.1 2005/02/05 11:14:56 ipkiss 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
#include "tile.h"
#include "bag.h"
#include "debug.h"
Bag::Bag()
{
init();
}
void Bag::init()
{
m_ntiles = 0;
const list<Tile>& allTiles = Tile::getAllTiles();
list<Tile>::const_iterator it;
for (it = allTiles.begin(); it != allTiles.end(); it++)
{
m_tilesMap[*it] = it->maxNumber();
m_ntiles += it->maxNumber();
}
}
int Bag::in(const Tile &iTile) const
{
map<Tile, int>::const_iterator it = m_tilesMap.find(iTile);
if (it != m_tilesMap.end())
return (*it).second;
return 0;
}
int Bag::nVowels() const
{
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;
}
int Bag::nConsonants() const
{
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;
}
int Bag::takeTile(const Tile &iTile)
{
if (in(iTile))
{
m_tilesMap[iTile]--;
m_ntiles--;
}
else
{
return 1;
}
return 0;
}
int Bag::replaceTile(const Tile &iTile)
{
if (in(iTile) < iTile.maxNumber())
{
m_tilesMap[iTile]++;
m_ntiles++;
}
else
{
return 1;
}
return 0;
}
Tile Bag::selectRandom()
{
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;
}
PDEBUG(1, "BAG: we should not come here\n");
return Tile::dummy();
}
void Bag::operator=(const Bag &iOther)
{
m_tilesMap = iOther.m_tilesMap;
m_ntiles = iOther.m_ntiles;
}
void Bag::dumpAll() const
{
map<Tile, int>::const_iterator it;
for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
{
if (it->second)
fprintf(stderr, "%c[%i] ", it->first.toChar(), it->second);
}
fprintf(stderr, "\n");
}