2005-02-24 09:06:24 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
* Copyright (C) 2005-2012 Olivier Teulière & Antoine Fraboulet
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
|
|
|
* Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
2005-02-24 09:06:24 +01:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-23 16:53:42 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-02-24 09:06:24 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-08-28 16:33:50 +02:00
|
|
|
#include <boost/format.hpp>
|
2005-03-03 23:14:41 +01:00
|
|
|
#include <getopt.h>
|
2006-08-12 00:14:21 +02:00
|
|
|
#include <string>
|
2008-01-08 14:52:32 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <exception>
|
2005-03-03 23:14:41 +01:00
|
|
|
|
2008-01-19 20:33:08 +01:00
|
|
|
#if ENABLE_NLS
|
|
|
|
# include <libintl.h>
|
|
|
|
# define _(String) gettext(String)
|
|
|
|
#else
|
|
|
|
# define _(String) String
|
|
|
|
#endif
|
|
|
|
|
2005-02-24 09:06:24 +01:00
|
|
|
#include "game_factory.h"
|
2011-08-27 19:21:26 +02:00
|
|
|
#include "game_params.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "game.h"
|
|
|
|
#include "training.h"
|
|
|
|
#include "freegame.h"
|
|
|
|
#include "duplicate.h"
|
2012-03-05 01:27:56 +01:00
|
|
|
#include "arbitration.h"
|
2012-12-25 17:30:03 +01:00
|
|
|
#include "topping.h"
|
2008-01-19 20:33:08 +01:00
|
|
|
#include "player.h"
|
2008-01-28 20:17:33 +01:00
|
|
|
#include "ai_percent.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "dic.h"
|
2008-01-19 20:33:08 +01:00
|
|
|
#include "encoding.h"
|
2009-11-29 17:01:31 +01:00
|
|
|
#include "xml_reader.h"
|
2005-02-24 09:06:24 +01:00
|
|
|
|
|
|
|
|
2011-01-30 00:47:20 +01:00
|
|
|
INIT_LOGGER(game, GameFactory);
|
|
|
|
|
2005-02-24 09:06:24 +01:00
|
|
|
GameFactory *GameFactory::m_factory = NULL;
|
|
|
|
|
|
|
|
|
2008-01-19 20:33:08 +01:00
|
|
|
GameFactory::GameFactory(): m_dic(NULL), m_joker(false)
|
2005-03-03 23:14:41 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GameFactory::~GameFactory()
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
delete m_dic;
|
2005-03-03 23:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-24 09:06:24 +01:00
|
|
|
GameFactory *GameFactory::Instance()
|
|
|
|
{
|
|
|
|
if (m_factory == NULL)
|
|
|
|
m_factory = new GameFactory;
|
|
|
|
return m_factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameFactory::Destroy()
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
delete m_factory;
|
2005-02-24 09:06:24 +01:00
|
|
|
m_factory = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
Game *GameFactory::createGame(const GameParams &iParams, const Game *iMasterGame)
|
2005-02-24 09:06:24 +01:00
|
|
|
{
|
2011-08-28 18:20:00 +02:00
|
|
|
if (iParams.getMode() == GameParams::kTRAINING)
|
|
|
|
{
|
|
|
|
LOG_INFO("Creating a training game");
|
2012-12-30 16:14:13 +01:00
|
|
|
Training *game = new Training(iParams, iMasterGame);
|
2011-08-28 18:20:00 +02:00
|
|
|
return game;
|
|
|
|
}
|
|
|
|
if (iParams.getMode() == GameParams::kFREEGAME)
|
|
|
|
{
|
|
|
|
LOG_INFO("Creating a free game");
|
2012-12-30 16:14:13 +01:00
|
|
|
FreeGame *game = new FreeGame(iParams, iMasterGame);
|
2011-08-28 18:20:00 +02:00
|
|
|
return game;
|
|
|
|
}
|
|
|
|
if (iParams.getMode() == GameParams::kDUPLICATE)
|
|
|
|
{
|
|
|
|
LOG_INFO("Creating a duplicate game");
|
2012-12-30 16:14:13 +01:00
|
|
|
Duplicate *game = new Duplicate(iParams, iMasterGame);
|
2011-08-28 18:20:00 +02:00
|
|
|
return game;
|
|
|
|
}
|
2012-03-05 01:27:56 +01:00
|
|
|
if (iParams.getMode() == GameParams::kARBITRATION)
|
|
|
|
{
|
|
|
|
LOG_INFO("Creating an arbitration game");
|
2012-12-30 16:14:13 +01:00
|
|
|
Arbitration *game = new Arbitration(iParams, iMasterGame);
|
2012-03-05 01:27:56 +01:00
|
|
|
return game;
|
|
|
|
}
|
2012-12-25 17:30:03 +01:00
|
|
|
if (iParams.getMode() == GameParams::kTOPPING)
|
|
|
|
{
|
|
|
|
LOG_INFO("Creating a topping game");
|
2012-12-30 16:14:13 +01:00
|
|
|
Topping *game = new Topping(iParams, iMasterGame);
|
2012-12-25 17:30:03 +01:00
|
|
|
return game;
|
|
|
|
}
|
2011-08-28 18:20:00 +02:00
|
|
|
throw GameException("Unknown game type");
|
2005-02-24 09:06:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-03 23:14:41 +01:00
|
|
|
Game *GameFactory::createFromCmdLine(int argc, char **argv)
|
|
|
|
{
|
|
|
|
// 1) Parse command-line and store everything in member variables
|
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{"dictionary", required_argument, NULL, 'd'},
|
|
|
|
{"dict", required_argument, NULL, 'd'},
|
|
|
|
{"mode", required_argument, NULL, 'm'},
|
2008-01-19 20:33:08 +01:00
|
|
|
{"human", required_argument, NULL, 'u'},
|
|
|
|
{"ai", required_argument, NULL, 'a'},
|
2005-03-29 00:07:22 +02:00
|
|
|
{"joker", no_argument, NULL, 500},
|
2005-03-03 23:14:41 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2008-01-19 20:33:08 +01:00
|
|
|
static char short_options[] = "hvd:m:u:a:";
|
2005-03-03 23:14:41 +01:00
|
|
|
|
|
|
|
int option_index = 1;
|
|
|
|
int res;
|
2005-10-23 18:12:29 +02:00
|
|
|
bool found_d = false;
|
|
|
|
bool found_m = false;
|
2005-03-03 23:14:41 +01:00
|
|
|
while ((res = getopt_long(argc, argv, short_options,
|
|
|
|
long_options, &option_index)) != -1)
|
|
|
|
{
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
// Help requested, display it and exit
|
|
|
|
printUsage(argv[0]);
|
|
|
|
return NULL;
|
|
|
|
case 'v':
|
|
|
|
// Version requested, display it and exit
|
|
|
|
printVersion();
|
|
|
|
return NULL;
|
|
|
|
case 'd':
|
|
|
|
m_dicStr = optarg;
|
2005-10-23 18:12:29 +02:00
|
|
|
found_d = true;
|
2005-03-03 23:14:41 +01:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
m_modeStr = optarg;
|
2005-10-23 18:12:29 +02:00
|
|
|
found_m = true;
|
2005-03-03 23:14:41 +01:00
|
|
|
break;
|
2008-01-19 20:33:08 +01:00
|
|
|
case 'u':
|
|
|
|
case 'a':
|
|
|
|
// Handle both types of players together
|
|
|
|
{
|
|
|
|
wstring name;
|
|
|
|
if (optarg == NULL)
|
|
|
|
{
|
2011-08-28 16:33:50 +02:00
|
|
|
string s = str(boost::format(_("Player %1%")) % (m_players.size() + 1));
|
2011-07-30 21:48:05 +02:00
|
|
|
name = wfl(s);
|
2008-01-19 20:33:08 +01:00
|
|
|
}
|
|
|
|
else
|
2011-07-30 21:48:05 +02:00
|
|
|
name = wfl(optarg);
|
2012-10-13 00:07:34 +02:00
|
|
|
m_players.push_back(make_pair(res == 'u', name));
|
2008-01-19 20:33:08 +01:00
|
|
|
}
|
2005-03-29 00:07:22 +02:00
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
m_joker = true;
|
|
|
|
break;
|
2005-03-03 23:14:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-23 18:12:29 +02:00
|
|
|
// 2) Make sure the mandatory options are present
|
|
|
|
if (!found_d || !found_m)
|
|
|
|
{
|
|
|
|
cerr << "Mandatory option missing: ";
|
|
|
|
if (!found_d)
|
|
|
|
cerr << "dict";
|
|
|
|
else if (!found_m)
|
|
|
|
cerr << "mode";
|
2008-01-08 14:52:32 +01:00
|
|
|
cerr << endl;
|
2005-10-23 18:12:29 +02:00
|
|
|
|
|
|
|
printUsage(argv[0]);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) Try to load the dictionary
|
2008-01-08 14:52:32 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_dic = new Dictionary(m_dicStr);
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
2005-03-29 00:07:22 +02:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
cerr << e.what() << endl;
|
2005-03-03 23:14:41 +01:00
|
|
|
return NULL;
|
2005-03-29 00:07:22 +02:00
|
|
|
}
|
2005-03-03 23:14:41 +01:00
|
|
|
|
2011-08-27 19:21:26 +02:00
|
|
|
// 4) Prepare game parameters
|
2011-08-28 19:16:59 +02:00
|
|
|
GameParams params(*m_dic);
|
2011-08-27 19:21:26 +02:00
|
|
|
if (m_joker)
|
2011-08-28 19:24:37 +02:00
|
|
|
params.addVariant(GameParams::kJOKER);
|
2011-08-27 19:21:26 +02:00
|
|
|
|
2005-03-03 23:14:41 +01:00
|
|
|
if (m_modeStr == "training" || m_modeStr == "t")
|
2011-08-28 19:16:59 +02:00
|
|
|
params.setMode(GameParams::kTRAINING);
|
2005-03-03 23:14:41 +01:00
|
|
|
else if (m_modeStr == "freegame" || m_modeStr == "f")
|
2011-08-28 19:16:59 +02:00
|
|
|
params.setMode(GameParams::kFREEGAME);
|
2005-03-03 23:14:41 +01:00
|
|
|
else if (m_modeStr == "duplicate" || m_modeStr == "d")
|
2011-08-28 19:16:59 +02:00
|
|
|
params.setMode(GameParams::kDUPLICATE);
|
2005-03-03 23:14:41 +01:00
|
|
|
else
|
2005-03-29 00:07:22 +02:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
cerr << "Invalid game mode '" << m_modeStr << "'" << endl;
|
2005-03-03 23:14:41 +01:00
|
|
|
return NULL;
|
2005-03-29 00:07:22 +02:00
|
|
|
}
|
2005-03-03 23:14:41 +01:00
|
|
|
|
2011-08-28 19:16:59 +02:00
|
|
|
// 5) Try to create a game object
|
|
|
|
Game *game = createGame(params);
|
2011-08-28 18:20:00 +02:00
|
|
|
|
2011-08-27 19:21:26 +02:00
|
|
|
// 6) Add the players
|
2008-01-19 20:33:08 +01:00
|
|
|
for (unsigned int i = 0; i < m_players.size(); ++i)
|
|
|
|
{
|
|
|
|
// Human?
|
2008-01-28 20:17:33 +01:00
|
|
|
Player *player;
|
2008-01-19 20:33:08 +01:00
|
|
|
if (m_players[i].first)
|
2008-01-28 20:17:33 +01:00
|
|
|
player = new HumanPlayer;
|
2008-01-19 20:33:08 +01:00
|
|
|
else
|
2008-01-28 20:17:33 +01:00
|
|
|
player = new AIPercent(1);
|
|
|
|
player->setName(m_players[i].second);
|
|
|
|
game->addPlayer(player);
|
2008-01-19 20:33:08 +01:00
|
|
|
}
|
2005-03-03 23:14:41 +01:00
|
|
|
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
Game* GameFactory::load(const string &iFileName, const Dictionary &iDic)
|
2006-08-12 00:14:21 +02:00
|
|
|
{
|
2009-11-29 17:01:31 +01:00
|
|
|
return XmlReader::read(iFileName, iDic);
|
2006-08-12 00:14:21 +02:00
|
|
|
}
|
2005-03-03 23:14:41 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2005-03-03 23:14:41 +01:00
|
|
|
void GameFactory::printUsage(const string &iBinaryName) const
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
cout << "Usage: " << iBinaryName << " [options]" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " -h, --help Print this help and exit" << endl
|
|
|
|
<< " -v, --version Print version information and exit" << endl
|
|
|
|
<< " -m, --mode {duplicate,d,freegame,f,training,t}" << endl
|
|
|
|
<< " Choose game mode (mandatory)" << endl
|
2008-01-19 20:33:08 +01:00
|
|
|
<< " -d, --dict <path> Choose a dictionary (mandatory)" << endl
|
|
|
|
<< " -u --human <name> Add a human player" << endl
|
|
|
|
<< " -a --ai <name> Add a AI (Artificial Intelligence) player" << endl
|
2008-01-08 14:52:32 +01:00
|
|
|
<< " --joker Play with the \"Joker game\" variant" << endl;
|
2005-03-03 23:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameFactory::printVersion() const
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
cout << PACKAGE_STRING << endl
|
2005-03-03 23:14:41 +01:00
|
|
|
<< "This program comes with NO WARRANTY, to the extent permitted by "
|
2008-01-08 14:52:32 +01:00
|
|
|
<< "law." << endl << "You may redistribute it under the terms of the "
|
|
|
|
<< "GNU General Public License;" << endl
|
|
|
|
<< "see the file named COPYING for details." << endl;
|
2005-03-03 23:14:41 +01:00
|
|
|
}
|
|
|
|
|