2005-02-24 09:06:24 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Copyright (C) 2005 Eliot
|
|
|
|
* Authors: Olivier Teuliere <ipkiss@via.ecp.fr>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*****************************************************************************/
|
|
|
|
|
2005-03-03 23:14:41 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "dic.h"
|
2005-02-24 09:06:24 +01:00
|
|
|
#include "game_factory.h"
|
|
|
|
|
|
|
|
|
|
|
|
GameFactory *GameFactory::m_factory = NULL;
|
|
|
|
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
GameFactory::GameFactory(): m_dic(NULL), m_human(0), m_ai(0), m_joker(false)
|
2005-03-03 23:14:41 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GameFactory::~GameFactory()
|
|
|
|
{
|
|
|
|
if (m_dic)
|
|
|
|
Dic_destroy(m_dic);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (m_factory)
|
|
|
|
delete m_factory;
|
|
|
|
m_factory = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Training *GameFactory::createTraining(const Dictionary &iDic)
|
|
|
|
{
|
|
|
|
Training *game = new Training(iDic);
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FreeGame *GameFactory::createFreeGame(const Dictionary &iDic)
|
|
|
|
{
|
|
|
|
FreeGame *game = new FreeGame(iDic);
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Duplicate *GameFactory::createDuplicate(const Dictionary &iDic)
|
|
|
|
{
|
|
|
|
Duplicate *game = new Duplicate(iDic);
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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'},
|
|
|
|
{"human", no_argument, NULL, 300},
|
|
|
|
{"ai", no_argument, NULL, 400},
|
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}
|
|
|
|
};
|
|
|
|
static char short_options[] = "hvd:m:";
|
|
|
|
|
|
|
|
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;
|
|
|
|
case 300:
|
|
|
|
m_human++;
|
|
|
|
break;
|
|
|
|
case 400:
|
|
|
|
m_ai++;
|
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";
|
|
|
|
cerr << "\n";
|
|
|
|
|
|
|
|
printUsage(argv[0]);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) Try to load the dictionary
|
2005-03-03 23:14:41 +01:00
|
|
|
if (Dic_load(&m_dic, m_dicStr.c_str()))
|
2005-03-29 00:07:22 +02:00
|
|
|
{
|
|
|
|
cerr << "Could not load dictionary '" << m_dicStr << "'\n";
|
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
|
|
|
|
2005-10-23 18:12:29 +02:00
|
|
|
// 4) Try to create a game object
|
2005-03-03 23:14:41 +01:00
|
|
|
Game *game = NULL;
|
|
|
|
if (m_modeStr == "training" || m_modeStr == "t")
|
|
|
|
{
|
|
|
|
game = createTraining(m_dic);
|
|
|
|
}
|
|
|
|
else if (m_modeStr == "freegame" || m_modeStr == "f")
|
|
|
|
{
|
|
|
|
game = createFreeGame(m_dic);
|
|
|
|
}
|
|
|
|
else if (m_modeStr == "duplicate" || m_modeStr == "d")
|
|
|
|
{
|
|
|
|
game = createDuplicate(m_dic);
|
|
|
|
}
|
|
|
|
else
|
2005-03-29 00:07:22 +02:00
|
|
|
{
|
|
|
|
cerr << "Invalid game mode '" << m_modeStr << "'\n";
|
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
|
|
|
|
2005-10-23 18:12:29 +02:00
|
|
|
// 5) Add the players
|
2005-03-03 23:14:41 +01:00
|
|
|
for (int i = 0; i < m_human; i++)
|
|
|
|
game->addHumanPlayer();
|
|
|
|
for (int i = 0; i < m_ai; i++)
|
|
|
|
game->addAIPlayer();
|
|
|
|
|
2005-10-23 18:12:29 +02:00
|
|
|
// 6) Set the variant
|
2005-03-29 00:07:22 +02:00
|
|
|
if (m_joker)
|
|
|
|
game->setVariant(Game::kJOKER);
|
|
|
|
|
2005-03-03 23:14:41 +01:00
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-24 09:06:24 +01:00
|
|
|
void GameFactory::releaseGame(Game &iGame)
|
|
|
|
{
|
|
|
|
delete &iGame;
|
|
|
|
}
|
|
|
|
|
2005-03-03 23:14:41 +01:00
|
|
|
|
|
|
|
void GameFactory::printUsage(const string &iBinaryName) const
|
|
|
|
{
|
|
|
|
cout << "Usage: " << iBinaryName << " [options]\n"
|
2005-10-23 18:12:29 +02:00
|
|
|
<< "Options:\n"
|
2005-03-03 23:14:41 +01:00
|
|
|
<< " -h, --help Print this help and exit\n"
|
|
|
|
<< " -v, --version Print version information and exit\n"
|
|
|
|
<< " -m, --mode {duplicate,d,freegame,f,training,t}\n"
|
2005-10-23 18:12:29 +02:00
|
|
|
<< " Choose game mode (mandatory)\n"
|
|
|
|
<< " -d, --dict <string> Choose a dictionary (mandatory)\n"
|
2005-03-03 23:14:41 +01:00
|
|
|
<< " --human Add a human player\n"
|
2005-03-29 00:07:22 +02:00
|
|
|
<< " --ai Add a AI (Artificial Intelligence) player\n"
|
|
|
|
<< " --joker Play with the \"Joker game\" variant\n";
|
2005-03-03 23:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameFactory::printVersion() const
|
|
|
|
{
|
|
|
|
cout << PACKAGE_STRING << "\n"
|
|
|
|
<< "This program comes with NO WARRANTY, to the extent permitted by "
|
|
|
|
<< "law.\nYou may redistribute it under the terms of the GNU General "
|
|
|
|
<< "Public License;\nsee the file named COPYING for details.\n";
|
|
|
|
}
|
|
|
|
|
2006-01-01 20:49:35 +01:00
|
|
|
|
|
|
|
/// Local Variables:
|
|
|
|
/// mode: c++
|
|
|
|
/// mode: hs-minor
|
|
|
|
/// c-basic-offset: 4
|
|
|
|
/// indent-tabs-mode: nil
|
|
|
|
/// End:
|