/*
  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: main.cc,v 1.12 2000/11/28 18:30:40 falk Exp $
*/

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <time.h>

#include "Board.hh"
#include "Bot.hh"
#include "Bag.hh"
#include "HumanPlayer.hh"

int main(int argc, char* argv[]) {
    assert(argc == 2 || argc == 3);
    srand(time(NULL));

    string bagFile = string(argv[1]) + string(".bag");
    ifstream bagStream(bagFile.c_str());
    assert(bagStream);
    Rules::readRules(bagStream);

    string layoutFile = string(argv[1]) + string(".layout");
    ifstream layoutStream(layoutFile.c_str());
    assert(layoutStream);
    Rules::readLayout(layoutStream);

    //Board b(cin);
    Board b;
    cout << b;

    string dictFile = string(argv[1]) + string(".trie");
    Bot bot1(dictFile), bot2(dictFile);
    HumanPlayer human("Imke");

    Bag bag;

    vector<Player*> players;
    players.push_back(&bot1);
    players.push_back(&bot2);
    if (argc == 2)
	players.push_back(&human);

    vector<Player*>::iterator currentPlayer = players.begin();

    while (!bag.empty()) {
	while ((*currentPlayer)->numLetters()
	       < Rules::maxRackTiles() && !bag.empty()) {
	    Tile t = bag.draw();
	    (*currentPlayer)->receiveTile(t);
	}

	cout << "It's " << (*currentPlayer)->name() << "'s move!\n";
	Move move = (*currentPlayer)->getMove();
	if (move.empty())
	    break;

	b.apply(move);
	for (vector<Player*>::iterator p = players.begin();
	     p != players.end(); ++p) {
	    (*p)->apply(move);
	}

	for (Move::const_iterator i = move.begin(); i != move.end(); ++i)
	    (*currentPlayer)->loseTile(i->tile());

	if (++currentPlayer == players.end())
	    currentPlayer = players.begin();

	cout << b;
    }

    if (bag.empty())
	cout << "Bag empty!\n";
    else
	cout << "No more moves!\n";

    return 0;
}
// alt
// ./crossword de  1.06s user 0.02s system 44% cpu 2.411 total
// neu
// ./crossword de  1.24s user 0.01s system 45% cpu 2.729 total
// alt, own board bookkeeping
// ./crossword de  1.13s user 0.02s system 45% cpu 2.552 total
// incrementally updated letterOk
// ./crossword de  0.70s user 0.02s system 42% cpu 1.712 total
// iterate rack and node simultaneously
// ./crossword de  0.40s user 0.01s system 101% cpu 0.402 total

// blanks etc.
// gcc version 2.95.2 20000220
// ./crossword de  0.62s user 0.05s system 25% cpu 2.649 total

