00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <getopt.h>
00021
00022 #include "config.h"
00023 #include "dic.h"
00024 #include "game_factory.h"
00025
00026
00027 GameFactory *GameFactory::m_factory = NULL;
00028
00029
00030 GameFactory::GameFactory(): m_dic(NULL), m_human(0), m_ai(0), m_joker(false)
00031 {
00032 }
00033
00034
00035 GameFactory::~GameFactory()
00036 {
00037 if (m_dic)
00038 Dic_destroy(m_dic);
00039 }
00040
00041
00042 GameFactory *GameFactory::Instance()
00043 {
00044 if (m_factory == NULL)
00045 m_factory = new GameFactory;
00046 return m_factory;
00047 }
00048
00049
00050 void GameFactory::Destroy()
00051 {
00052 if (m_factory)
00053 delete m_factory;
00054 m_factory = NULL;
00055 }
00056
00057
00058 Training *GameFactory::createTraining(const Dictionary &iDic)
00059 {
00060 Training *game = new Training(iDic);
00061 return game;
00062 }
00063
00064
00065 FreeGame *GameFactory::createFreeGame(const Dictionary &iDic)
00066 {
00067 FreeGame *game = new FreeGame(iDic);
00068 return game;
00069 }
00070
00071
00072 Duplicate *GameFactory::createDuplicate(const Dictionary &iDic)
00073 {
00074 Duplicate *game = new Duplicate(iDic);
00075 return game;
00076 }
00077
00078
00079 Game *GameFactory::createFromCmdLine(int argc, char **argv)
00080 {
00081
00082 static struct option long_options[] =
00083 {
00084 {"help", no_argument, NULL, 'h'},
00085 {"version", no_argument, NULL, 'v'},
00086 {"dictionary", required_argument, NULL, 'd'},
00087 {"dict", required_argument, NULL, 'd'},
00088 {"mode", required_argument, NULL, 'm'},
00089 {"human", no_argument, NULL, 300},
00090 {"ai", no_argument, NULL, 400},
00091 {"joker", no_argument, NULL, 500},
00092 {0, 0, 0, 0}
00093 };
00094 static char short_options[] = "hvd:m:";
00095
00096 int option_index = 1;
00097 int res;
00098 bool found_d = false;
00099 bool found_m = false;
00100 while ((res = getopt_long(argc, argv, short_options,
00101 long_options, &option_index)) != -1)
00102 {
00103 switch (res)
00104 {
00105 case 'h':
00106
00107 printUsage(argv[0]);
00108 return NULL;
00109 case 'v':
00110
00111 printVersion();
00112 return NULL;
00113 case 'd':
00114 m_dicStr = optarg;
00115 found_d = true;
00116 break;
00117 case 'm':
00118 m_modeStr = optarg;
00119 found_m = true;
00120 break;
00121 case 300:
00122 m_human++;
00123 break;
00124 case 400:
00125 m_ai++;
00126 break;
00127 case 500:
00128 m_joker = true;
00129 break;
00130 }
00131 }
00132
00133
00134 if (!found_d || !found_m)
00135 {
00136 cerr << "Mandatory option missing: ";
00137 if (!found_d)
00138 cerr << "dict";
00139 else if (!found_m)
00140 cerr << "mode";
00141 cerr << "\n";
00142
00143 printUsage(argv[0]);
00144 return NULL;
00145 }
00146
00147
00148 if (Dic_load(&m_dic, m_dicStr.c_str()))
00149 {
00150 cerr << "Could not load dictionary '" << m_dicStr << "'\n";
00151 return NULL;
00152 }
00153
00154
00155 Game *game = NULL;
00156 if (m_modeStr == "training" || m_modeStr == "t")
00157 {
00158 game = createTraining(m_dic);
00159 }
00160 else if (m_modeStr == "freegame" || m_modeStr == "f")
00161 {
00162 game = createFreeGame(m_dic);
00163 }
00164 else if (m_modeStr == "duplicate" || m_modeStr == "d")
00165 {
00166 game = createDuplicate(m_dic);
00167 }
00168 else
00169 {
00170 cerr << "Invalid game mode '" << m_modeStr << "'\n";
00171 return NULL;
00172 }
00173
00174
00175 for (int i = 0; i < m_human; i++)
00176 game->addHumanPlayer();
00177 for (int i = 0; i < m_ai; i++)
00178 game->addAIPlayer();
00179
00180
00181 if (m_joker)
00182 game->setVariant(Game::kJOKER);
00183
00184 return game;
00185 }
00186
00187
00188 void GameFactory::releaseGame(Game &iGame)
00189 {
00190 delete &iGame;
00191 }
00192
00193
00194 void GameFactory::printUsage(const string &iBinaryName) const
00195 {
00196 cout << "Usage: " << iBinaryName << " [options]\n"
00197 << "Options:\n"
00198 << " -h, --help Print this help and exit\n"
00199 << " -v, --version Print version information and exit\n"
00200 << " -m, --mode {duplicate,d,freegame,f,training,t}\n"
00201 << " Choose game mode (mandatory)\n"
00202 << " -d, --dict <string> Choose a dictionary (mandatory)\n"
00203 << " --human Add a human player\n"
00204 << " --ai Add a AI (Artificial Intelligence) player\n"
00205 << " --joker Play with the \"Joker game\" variant\n";
00206 }
00207
00208
00209 void GameFactory::printVersion() const
00210 {
00211 cout << PACKAGE_STRING << "\n"
00212 << "This program comes with NO WARRANTY, to the extent permitted by "
00213 << "law.\nYou may redistribute it under the terms of the GNU General "
00214 << "Public License;\nsee the file named COPYING for details.\n";
00215 }
00216
00217
00218
00219
00220
00221
00222
00223