/***************************************************************************** * Copyright (C) 2005 Eliot * Authors: Antoine Fraboulet * Olivier Teuliere * * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ #include #include #include #include #include #include #include #include #include "dic.h" #include "dic_search.h" #include "regexp.h" #include "game_io.h" #include "game_factory.h" #include "training.h" #include "duplicate.h" #include "freegame.h" /* A static variable for holding the line. */ static char *line_read = NULL; /** * Read a string, and return a pointer to it. * Returns NULL on EOF. */ char *rl_gets() { // If the buffer has already been allocated, return the memory to the free // pool if (line_read) { free(line_read); line_read = NULL; } // Get a line from the user line_read = readline("commande> "); // If the line has any text in it, save it on the history if (line_read && *line_read) add_history(line_read); return line_read; } char * next_token_alpha(char *cmd, const char *delim) { int i; char *token = strtok(cmd, delim); if (token == NULL) return NULL; for (i = 0; token[i] && isalpha(token[i]); i++) ; token[i] = '\0'; return token; } char * next_token_alphanum(char *cmd, const char *delim) { int i; char *token = strtok(cmd, delim); if (token == NULL) return NULL; for (i = 0; token[i] && isalnum(token[i]); i++) ; token[i] = '\0'; return token; } char * next_token_alphaplusjoker(char *cmd, const char *delim) { int i; char *token = strtok(cmd, delim); if (token == NULL) return NULL; for (i = 0; token[i] && (isalpha(token[i]) || token[i] == '?' || token[i] == '+'); i++) ; token[i] = '\0'; return token; } char * next_token_digit(char *cmd, const char *delim) { int i; char *token = strtok(cmd, delim); if (token == NULL) return NULL; for (i = 0; token[i] && (isdigit(token[i]) || token[i] == '-'); i++) ; token[i] = '\0'; return token; } char * next_token_cross(char *cmd, const char *delim) { int i; char *token = strtok(cmd, delim); if (token == NULL) return NULL; for (i = 0; token[i] && (isalpha(token[i]) || token[i] == '.'); i++) ; token[i] = '\0'; return token; } char * next_token_filename(char *cmd, const char *delim) { int i; char *token = strtok(cmd, delim); if (token == NULL) return NULL; for (i = 0; token[i] && (isalnum(token[i]) || token[i] == '.' || token[i] == '_'); i++) ; token[i] = '\0'; return token; } void eliottxt_get_cross(const Dictionary &iDic, char* cros) { int i; // (Dictionary dic, char* regx, char wordlist[RES_REGX_MAX][DIC_WORD_MAX]) char wordlist[RES_CROS_MAX][DIC_WORD_MAX]; Dic_search_Cros(iDic, cros, wordlist); for (i = 0; i(iGame), 10); else GameIO::printSearchResults(cout, static_cast(iGame), atoi(token)); break; case 's': GameIO::printPoints(cout, iGame); break; case 'S': GameIO::printAllPoints(cout, iGame); break; case 't': GameIO::printPlayedRack(cout, iGame, iGame.getHistory().getSize()); break; case 'T': GameIO::printAllRacks(cout, iGame); break; default: cout << "commande inconnue\n"; break; } } void loop_training(Training &iGame) { char *token; char *commande = NULL; char delim[] = " \t"; int quit = 0; cout << "mode entraînement\n"; cout << "[?] pour l'aide\n"; while (quit == 0) { commande = rl_gets(); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help_training(); break; case 'a': display_data(iGame, delim); break; case 'd': token = next_token_alpha(NULL, delim); if (token == NULL) help_training(); else { if (Dic_search_word(iGame.getDic(), token)) printf("le mot -%s- existe\n", token); else printf("le mot -%s- n'existe pas\n", token); } break; case 'j': token = next_token_alpha(NULL, delim); if (token == NULL) help_training(); else { int res; char *coord = next_token_alphanum(NULL, delim); if (coord == NULL) { help_training(); break; } if ((res = iGame.play(coord, token)) != 0) { fprintf(stderr, "Mot incorrect ou mal placé (%i)\n", res); break; } } break; case 'n': token = next_token_digit(NULL, delim); if (token == NULL) help_training(); else { int n = atoi(token); if (n <= 0) iGame.back(n == 0 ? 1 : -n); else { if (iGame.playResult(--n)) printf("mauvais argument\n"); } } break; case 'r': iGame.search(); break; case 't': token = next_token_alphaplusjoker(NULL, delim); if (token == NULL) help_training(); else if (iGame.setRackManual(0, token)) printf("le sac ne contient pas assez de lettres\n"); break; case 'x': token = next_token_cross(NULL, delim); if (token == NULL) help_training(); else eliottxt_get_cross(iGame.getDic(), token); break; case '*': iGame.setRackRandom(false, Game::RACK_ALL); break; case '+': iGame.setRackRandom(false, Game::RACK_NEW); break; case 's': token = next_token_filename(NULL, delim); if (token != NULL) { ofstream fout(token); if (fout.rdstate() == ios::failbit) { printf("impossible d'ouvrir %s\n", token); break; } iGame.save(fout); fout.close(); } break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } printf("fin du mode entraînement\n"); } void loop_freegame(FreeGame &iGame) { char *token; char *commande = NULL; char delim[] = " \t"; int quit = 0; printf("mode partie libre\n"); printf("[?] pour l'aide\n"); while (quit == 0) { commande = rl_gets(); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help_freegame(); break; case 'a': display_data(iGame, delim); break; case 'd': token = next_token_alpha(NULL, delim); if (token == NULL) help_freegame(); else { if (Dic_search_word(iGame.getDic(), token)) printf("le mot -%s- existe\n", token); else printf("le mot -%s- n'existe pas\n", token); } break; case 'j': token = next_token_alpha(NULL, delim); if (token == NULL) help_freegame(); else { int res; char *coord = next_token_alphanum(NULL, delim); if (coord == NULL) { help_freegame(); break; } if ((res = iGame.play(coord, token)) != 0) { fprintf(stderr, "Mot incorrect ou mal placé (%i)\n", res); break; } } break; case 'p': token = next_token_alpha(NULL, delim); /* You can pass your turn without changing any letter */ if (token == NULL) token = ""; if (iGame.pass(token, iGame.currPlayer()) != 0) break; break; case 's': token = next_token_filename(NULL, delim); if (token != NULL) { ofstream fout(token); if (fout.rdstate() == ios::failbit) { printf("impossible d'ouvrir %s\n", token); break; } iGame.save(fout); fout.close(); } break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } printf("fin du mode partie libre\n"); } void loop_duplicate(Duplicate &iGame) { char *token; char *commande = NULL; char delim[] = " \t"; int quit = 0; printf("mode duplicate\n"); printf("[?] pour l'aide\n"); while (quit == 0) { commande = rl_gets(); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help_duplicate(); break; case 'a': display_data(iGame, delim); break; case 'd': token = next_token_alpha(NULL, delim); if (token == NULL) help_duplicate(); else { if (Dic_search_word(iGame.getDic(), token)) printf("le mot -%s- existe\n", token); else printf("le mot -%s- n'existe pas\n", token); } break; case 'j': token = next_token_alpha(NULL, delim); if (token == NULL) help_duplicate(); else { int res; char *coord = next_token_alphanum(NULL, delim); if (coord == NULL) { help_duplicate(); break; } if ((res = iGame.play(coord, token)) != 0) { fprintf(stderr, "Mot incorrect ou mal placé (%i)\n", res); break; } } break; case 'n': token = next_token_digit(NULL, delim); if (token == NULL) help_duplicate(); else { int res = iGame.setPlayer(atoi(token)); if (res == 1) fprintf(stderr, "Numéro de joueur invalide\n"); else if (res == 2) fprintf(stderr, "Impossible de choisir un joueur non humain\n"); } break; case 's': token = next_token_filename(NULL, delim); if (token != NULL) { ofstream fout(token); if (fout.rdstate() == ios::failbit) { printf("impossible d'ouvrir %s\n", token); break; } iGame.save(fout); fout.close(); } break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } printf("fin du mode duplicate\n"); } void eliot_regexp_build_default_llist(struct search_RegE_list_t &llist) { memset (&llist,0,sizeof(llist)); llist.minlength = 1; llist.maxlength = 15; llist.symbl[0] = RE_ALL_MATCH; llist.symbl[1] = RE_VOWL_MATCH; llist.symbl[2] = RE_CONS_MATCH; llist.symbl[3] = RE_USR1_MATCH; llist.symbl[5] = RE_USR2_MATCH; llist.valid[0] = 1; // all letters llist.valid[1] = 1; // vowels llist.valid[2] = 1; // consonants llist.valid[3] = 0; // user defined list 1 llist.valid[4] = 0; // user defined list 2 for(int i=0; i < DIC_SEARCH_REGE_LIST; i++) { memset(llist.letters[i],0,sizeof(llist.letters[i])); } const list& allTiles = Tile::getAllTiles(); list::const_iterator it; for (it = allTiles.begin(); it != allTiles.end(); it++) { if (! it->isJoker() && ! it->isEmpty()) { // all tiles llist.letters[0][it->toCode()] = 1; // vowels if (it->isVowel()) { llist.letters[1][it->toCode()] = 1; } // consonants if (it->isConsonant()) { llist.letters[2][it->toCode()] = 1; } } } } void eliot_regexp(const Dictionary iDic) { /* printf(" x [] {1} {2} {3} : expressions rationnelles\n"); printf(" [] expression à rechercher\n"); printf(" {1} nombre de résultats à afficher\n"); printf(" {2} longueur minimum d'un mot\n"); printf(" {3} longueur maximum d'un mot\n"); */ #define DIC_RE_MAX (3*DIC_WORD_MAX) // yes, it's 3 char re[DIC_RE_MAX]; char buff[RES_REGE_MAX][DIC_WORD_MAX]; struct search_RegE_list_t llist; eliot_regexp_build_default_llist(llist); char *exp, *cnres, *clmin, *clmax; char delim[] = " \t"; exp = strtok(NULL,delim); cnres = strtok(NULL,delim); clmin = strtok(NULL,delim); clmax = strtok(NULL,delim); if (exp == NULL) { return; } int nres = cnres ? atoi(cnres) : 50; int lmin = clmin ? atoi(clmin) : 1; int lmax = clmax ? atoi(clmax) : DIC_WORD_MAX - 1; if (lmax <= (DIC_WORD_MAX - 1) && lmin >= 1 && lmin <= lmax) { llist.minlength = lmin; llist.maxlength = lmax; } else { printf("bad length -%s,%s-\n",(const char*)clmin,(const char*)clmax); return; } strncpy(re,exp,DIC_RE_MAX); printf("search for %s (%d,%d,%d)\n",re,nres,lmin,lmax); Dic_search_RegE(iDic,re,buff,&llist); int nresult = 0; for(int i=0; i < RES_REGE_MAX && i < nres && buff[i][0]; i++) { printf("%s\n",buff[i]); nresult++; } printf("%d printed results\n",nresult); } void main_loop(const Dictionary &iDic) { char *token; char *commande = NULL; char delim[] = " \t"; int quit = 0; printf("[?] pour l'aide\n"); while (quit == 0) { commande = rl_gets(); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help(); break; case 'c': token = next_token_filename(NULL, delim); if (token == NULL) {} else { FILE* fin; if ((fin = fopen(token, "r")) == NULL) { printf("impossible d'ouvrir %s\n", token); break; } Game *game = Game::load(fin, iDic); fclose(fin); if (game == NULL) { printf("erreur pendant le chargement\n"); } else { if (game->getMode() == Game::kTRAINING) loop_training((Training&)*game); else if (game->getMode() == Game::kFREEGAME) loop_freegame((FreeGame&)*game); else loop_duplicate((Duplicate&)*game); } } break; case 'e': { // New training game Training *game = GameFactory::Instance()->createTraining(iDic); game->start(); loop_training(*game); GameFactory::Instance()->releaseGame(*game); break; } case 'd': { int i; // New duplicate game token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } Duplicate *game = GameFactory::Instance()->createDuplicate(iDic); for (i = 0; i < atoi(token); i++) game->addHumanPlayer(); token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } for (i = 0; i < atoi(token); i++) game->addAIPlayer(); game->start(); loop_duplicate(*game); GameFactory::Instance()->releaseGame(*game); break; } case 'l': { int i; // New free game token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } FreeGame *game = GameFactory::Instance()->createFreeGame(iDic); for (i = 0; i < atoi(token); i++) game->addHumanPlayer(); token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } for (i = 0; i < atoi(token); i++) game->addAIPlayer(); game->start(); loop_freegame(*game); GameFactory::Instance()->releaseGame(*game); break; } case 'D': { // New duplicate game Duplicate *game = GameFactory::Instance()->createDuplicate(iDic); game->addHumanPlayer(); game->addAIPlayer(); game->start(); loop_duplicate(*game); GameFactory::Instance()->releaseGame(*game); break; } case 'L': { // New free game FreeGame *game = GameFactory::Instance()->createFreeGame(iDic); game->addHumanPlayer(); game->addAIPlayer(); game->start(); loop_freegame(*game); GameFactory::Instance()->releaseGame(*game); break; } case 'x': // Regular expression tests eliot_regexp(iDic); break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } } int main(int argc, char *argv[]) { char dic_path[100]; Dictionary dic = NULL; if (argc != 2 && argc != 3) { fprintf(stdout, "Usage: eliot /chemin/vers/ods4.dawg [random_seed]\n"); exit(1); } else strcpy(dic_path, argv[1]); switch (Dic_load(&dic, dic_path)) { case 0: /* Normal case */ break; case 1: printf("chargement: problème d'ouverture de %s\n", argv[1]); exit(1); break; case 2: printf("chargement: mauvais en-tete de dictionnaire\n"); exit(2); break; case 3: printf("chargement: problème 3 d'allocation mémoire\n"); exit(3); break; case 4: printf("chargement: problème 4 d'alocation mémoire\n"); exit(4); break; case 5: printf("chargement: problème de lecture des arcs du dictionnaire\n"); exit(5); break; default: printf("chargement: problème non-repertorié\n"); exit(6); break; } if (argc == 3) srand(atoi(argv[2])); else srand(time(NULL)); main_loop(dic); GameFactory::Destroy(); Dic_destroy(dic); // Free the readline static variable if (line_read) free(line_read); return 0; } /// Local Variables: /// mode: c++ /// mode: hs-minor /// c-basic-offset: 4 /// indent-tabs-mode: nil /// End: