/*
  crossword -- a crossword game
  Copyright (C) 2000 Falk Hueffner

  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

  $Id: Bag.hh,v 1.2 2000/10/18 17:53:25 falk Exp $
*/

#ifndef BAG_HH
#define BAG_HH

#include <vector>
#include <stdlib.h>


#include "Tile.hh"

class Bag {
public:
    Bag() {
	for (unsigned int t = 0; t <= Rules::NUM_CHARS; ++t)
	    for (unsigned int j = 0; j < Rules::frequency(t); ++j)
		_tiles.push_back(t);
    }

    Tile draw() {
	int p = myrand(_tiles.size());
	Tile t = _tiles[p];
	//swap(_tiles[p], _tiles.back());
	//_tiles.pop_back();
	_tiles.erase(_tiles.begin() + p);

	return t;
    }

    bool empty() { return _tiles.empty(); }

private:
    static int myrand(int max) {
	return (int) (((double) max * rand()) / (double) RAND_MAX);
    }

    vector<Tile> _tiles;
};

#endif

