2005-02-05 12:14:56 +01:00
|
|
|
|
/*****************************************************************************
|
|
|
|
|
* Copyright (C) 2005 Eliot
|
|
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
|
|
|
|
|
* 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-05 12:14:56 +01:00
|
|
|
|
*****************************************************************************/
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
2005-02-05 12:14:56 +01:00
|
|
|
|
#include <fstream>
|
2005-04-27 19:55:32 +02:00
|
|
|
|
#include <readline/readline.h>
|
|
|
|
|
#include <readline/history.h>
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
|
|
#include "dic.h"
|
|
|
|
|
#include "dic_search.h"
|
2005-02-26 23:57:34 +01:00
|
|
|
|
#include "game_io.h"
|
2005-02-24 09:06:24 +01:00
|
|
|
|
#include "game_factory.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
#include "training.h"
|
|
|
|
|
#include "duplicate.h"
|
|
|
|
|
#include "freegame.h"
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
2005-04-27 19:55:32 +02:00
|
|
|
|
/* 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
|
char *
|
|
|
|
|
next_token_alpha(char *cmd, const char *delim)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char *token = strtok(cmd, delim);
|
|
|
|
|
if (token == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
return NULL;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
for (i = 0; token[i] && isalpha(token[i]); i++)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token[i] = '\0';
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
|
char *
|
|
|
|
|
next_token_alphaplusjoker(char *cmd, const char *delim)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char *token = strtok(cmd, delim);
|
|
|
|
|
if (token == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
for (i = 0; token[i] && (isalpha(token[i]) ||
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token[i] == '?' ||
|
|
|
|
|
token[i] == '+');
|
|
|
|
|
i++)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token[i] = '\0';
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
|
char *
|
|
|
|
|
next_token_digit(char *cmd, const char *delim)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char *token = strtok(cmd, delim);
|
|
|
|
|
if (token == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
return NULL;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
for (i = 0; token[i] && (isdigit(token[i]) || token[i] == '-'); i++)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token[i] = '\0';
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
|
char *
|
|
|
|
|
next_token_cross(char *cmd, const char *delim)
|
|
|
|
|
{
|
2004-08-07 20:10:42 +02:00
|
|
|
|
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;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
next_token_filename(char *cmd, const char *delim)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char *token = strtok(cmd, delim);
|
|
|
|
|
if (token == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
return NULL;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
for (i = 0; token[i] && (isalnum(token[i]) ||
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token[i] == '.' ||
|
|
|
|
|
token[i] == '_'); i++)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token[i] = '\0';
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
|
eliottxt_get_cross(const Dictionary &iDic, char* cros)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
// (Dictionary dic, char* regx, char wordlist[RES_REGX_MAX][DIC_WORD_MAX])
|
|
|
|
|
char wordlist[RES_CROS_MAX][DIC_WORD_MAX];
|
2005-02-05 12:14:56 +01:00
|
|
|
|
Dic_search_Cros(iDic, cros, wordlist);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
for (i = 0; i<RES_CROS_MAX && wordlist[i][0]; i++)
|
|
|
|
|
{
|
|
|
|
|
printf(" %s\n", wordlist[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
help_training()
|
|
|
|
|
{
|
|
|
|
|
printf(" ? : aide -- cette page\n");
|
|
|
|
|
printf(" a [g|l|p|r|t] : afficher :\n");
|
|
|
|
|
printf(" g -- grille\n");
|
|
|
|
|
printf(" gj -- grille + jokers\n");
|
|
|
|
|
printf(" gm -- grille + valeur des cases\n");
|
|
|
|
|
printf(" gn -- grille + valeur des cases (variante)\n");
|
|
|
|
|
printf(" l -- lettres non jou<6F>es\n");
|
|
|
|
|
printf(" p -- partie\n");
|
|
|
|
|
printf(" r -- recherche\n");
|
|
|
|
|
printf(" s -- score\n");
|
|
|
|
|
printf(" S -- score de tous les joueurs\n");
|
|
|
|
|
printf(" t -- tirage\n");
|
|
|
|
|
printf(" d [] : v<>rifier le mot []\n");
|
|
|
|
|
printf(" * : tirage al<61>atoire\n");
|
|
|
|
|
printf(" + : tirage al<61>atoire ajouts\n");
|
|
|
|
|
printf(" t [] : changer le tirage\n");
|
|
|
|
|
printf(" j [] {} : jouer le mot [] aux coordonn<6E>es {}\n");
|
|
|
|
|
printf(" n [] : jouer le r<>sultat num<75>ro []\n");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
printf(" s [] : sauver la partie en cours dans le fichier []\n");
|
|
|
|
|
printf(" c [] : charger la partie du fichier []\n");
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf(" q : quitter le mode entra<72>nement\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
help_freegame()
|
|
|
|
|
{
|
|
|
|
|
printf(" ? : aide -- cette page\n");
|
|
|
|
|
printf(" a [g|l|p|s|t] : afficher :\n");
|
|
|
|
|
printf(" g -- grille\n");
|
|
|
|
|
printf(" gj -- grille + jokers\n");
|
|
|
|
|
printf(" gm -- grille + valeur des cases\n");
|
|
|
|
|
printf(" gn -- grille + valeur des cases (variante)\n");
|
|
|
|
|
printf(" j -- joueur courant\n");
|
|
|
|
|
printf(" l -- lettres non jou<6F>es\n");
|
|
|
|
|
printf(" p -- partie\n");
|
|
|
|
|
printf(" s -- score\n");
|
|
|
|
|
printf(" S -- score de tous les joueurs\n");
|
|
|
|
|
printf(" t -- tirage\n");
|
|
|
|
|
printf(" T -- tirage de tous les joueurs\n");
|
|
|
|
|
printf(" d [] : v<>rifier le mot []\n");
|
|
|
|
|
printf(" j [] {} : jouer le mot [] aux coordonn<6E>es {}\n");
|
|
|
|
|
printf(" p [] : passer son tour en changeant les lettres []\n");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
printf(" s [] : sauver la partie en cours dans le fichier []\n");
|
|
|
|
|
printf(" c [] : charger la partie du fichier []\n");
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf(" q : quitter le mode partie libre\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
help_duplicate()
|
|
|
|
|
{
|
|
|
|
|
printf(" ? : aide -- cette page\n");
|
|
|
|
|
printf(" a [g|l|p|s|t] : afficher :\n");
|
|
|
|
|
printf(" g -- grille\n");
|
|
|
|
|
printf(" gj -- grille + jokers\n");
|
|
|
|
|
printf(" gm -- grille + valeur des cases\n");
|
|
|
|
|
printf(" gn -- grille + valeur des cases (variante)\n");
|
|
|
|
|
printf(" j -- joueur courant\n");
|
|
|
|
|
printf(" l -- lettres non jou<6F>es\n");
|
|
|
|
|
printf(" p -- partie\n");
|
|
|
|
|
printf(" s -- score\n");
|
|
|
|
|
printf(" S -- score de tous les joueurs\n");
|
|
|
|
|
printf(" t -- tirage\n");
|
|
|
|
|
printf(" d [] : v<>rifier le mot []\n");
|
|
|
|
|
printf(" j [] {} : jouer le mot [] aux coordonn<6E>es {}\n");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
printf(" n [] : passer au joueur n<>[]\n");
|
|
|
|
|
printf(" s [] : sauver la partie en cours dans le fichier []\n");
|
|
|
|
|
printf(" c [] : charger la partie du fichier []\n");
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf(" q : quitter le mode duplicate\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
help()
|
|
|
|
|
{
|
|
|
|
|
printf(" ? : aide -- cette page\n");
|
|
|
|
|
printf(" e : d<>marrer le mode entra<72>nement\n");
|
|
|
|
|
printf(" d [] {} : d<>marrer une partie duplicate avec\n");
|
|
|
|
|
printf(" [] joueurs humains et {} joueurs IA\n");
|
|
|
|
|
printf(" l [] {} : d<>marrer une partie libre avec\n");
|
|
|
|
|
printf(" [] joueurs humains et {} joueurs IA\n");
|
|
|
|
|
printf(" D : raccourci pour d 1 1\n");
|
|
|
|
|
printf(" L : raccourci pour l 1 1\n");
|
|
|
|
|
printf(" q : quitter\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
|
display_data(const Game &iGame, const char *delim)
|
2004-04-08 11:43:06 +02:00
|
|
|
|
{
|
2004-08-07 20:10:42 +02:00
|
|
|
|
char *token;
|
|
|
|
|
|
|
|
|
|
token = next_token_alpha(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
cout << "commande incompl<70>te\n";
|
2004-08-07 20:10:42 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
switch (token[0])
|
|
|
|
|
{
|
|
|
|
|
case 'g':
|
|
|
|
|
switch (token[1])
|
|
|
|
|
{
|
|
|
|
|
case '\0':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printBoard(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'j':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printBoardJoker(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'm':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printBoardMultipliers(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printBoardMultipliers2(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("commande inconnue\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'j':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
cout << "Joueur " << iGame.currPlayer() << endl;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printNonPlayed(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
iGame.save(cout);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printSearchResults(cout,
|
|
|
|
|
static_cast<const Training&>(iGame),
|
|
|
|
|
10);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
else
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printSearchResults(cout,
|
|
|
|
|
static_cast<const Training&>(iGame),
|
|
|
|
|
atoi(token));
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 's':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printPoints(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'S':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printAllPoints(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 't':
|
2005-12-27 00:35:03 +01:00
|
|
|
|
GameIO::printPlayedRack(cout, iGame, iGame.getHistory().getSize());
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'T':
|
2005-02-26 23:57:34 +01:00
|
|
|
|
GameIO::printAllRacks(cout, iGame);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2005-02-05 12:14:56 +01:00
|
|
|
|
cout << "commande inconnue\n";
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
|
loop_training(Training &iGame)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
char *token;
|
2005-04-27 19:55:32 +02:00
|
|
|
|
char *commande = NULL;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
char delim[] = " \t";
|
|
|
|
|
int quit = 0;
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
|
cout << "mode entra<72>nement\n";
|
|
|
|
|
cout << "[?] pour l'aide\n";
|
2004-08-07 20:10:42 +02:00
|
|
|
|
while (quit == 0)
|
|
|
|
|
{
|
2005-04-27 19:55:32 +02:00
|
|
|
|
commande = rl_gets();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = strtok(commande, delim);
|
|
|
|
|
if (token)
|
|
|
|
|
{
|
|
|
|
|
switch (token[0])
|
|
|
|
|
{
|
|
|
|
|
case '?':
|
|
|
|
|
help_training();
|
|
|
|
|
break;
|
|
|
|
|
case 'a':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
display_data(iGame, delim);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token = next_token_alpha(NULL, delim);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
if (token == NULL)
|
|
|
|
|
help_training();
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (Dic_search_word(iGame.getDic(), token))
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf("le mot -%s- existe\n", token);
|
|
|
|
|
else
|
|
|
|
|
printf("le mot -%s- n'existe pas\n", token);
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'j':
|
|
|
|
|
token = next_token_alpha(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
2005-02-05 12:14:56 +01:00
|
|
|
|
help_training();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
char *coord = next_token_alphanum(NULL, delim);
|
|
|
|
|
if (coord == NULL)
|
|
|
|
|
{
|
|
|
|
|
help_training();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if ((res = iGame.play(coord, token)) != 0)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Mot incorrect ou mal plac<61> (%i)\n",
|
|
|
|
|
res);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'n':
|
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
help_training();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int n = atoi(token);
|
|
|
|
|
if (n <= 0)
|
2005-02-05 12:14:56 +01:00
|
|
|
|
iGame.back(n == 0 ? 1 : -n);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (iGame.playResult(--n))
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf("mauvais argument\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
iGame.search();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
token = next_token_alphaplusjoker(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
help_training();
|
|
|
|
|
else
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (iGame.setRackManual(0, token))
|
2004-08-07 20:10:42 +02:00
|
|
|
|
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
|
2005-02-05 12:14:56 +01:00
|
|
|
|
eliottxt_get_cross(iGame.getDic(), token);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case '*':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
iGame.setRackRandom(0, false, Game::RACK_ALL);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case '+':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
iGame.setRackRandom(0, 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();
|
|
|
|
|
}
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'q':
|
|
|
|
|
quit = 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("commande inconnue\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("fin du mode entra<72>nement\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
|
loop_freegame(FreeGame &iGame)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
char *token;
|
2005-04-27 19:55:32 +02:00
|
|
|
|
char *commande = NULL;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
char delim[] = " \t";
|
|
|
|
|
int quit = 0;
|
|
|
|
|
|
|
|
|
|
printf("mode partie libre\n");
|
|
|
|
|
printf("[?] pour l'aide\n");
|
|
|
|
|
while (quit == 0)
|
|
|
|
|
{
|
2005-04-27 19:55:32 +02:00
|
|
|
|
commande = rl_gets();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = strtok(commande, delim);
|
|
|
|
|
if (token)
|
|
|
|
|
{
|
|
|
|
|
switch (token[0])
|
|
|
|
|
{
|
|
|
|
|
case '?':
|
|
|
|
|
help_freegame();
|
|
|
|
|
break;
|
|
|
|
|
case 'a':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
display_data(iGame, delim);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
2004-04-08 11:43:06 +02:00
|
|
|
|
token = next_token_alpha(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
help_freegame();
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (Dic_search_word(iGame.getDic(), token))
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf("le mot -%s- existe\n", token);
|
|
|
|
|
else
|
|
|
|
|
printf("le mot -%s- n'existe pas\n", token);
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'j':
|
2004-08-07 20:10:42 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if ((res = iGame.play(coord, token)) != 0)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Mot incorrect ou mal plac<61> (%i)\n",
|
|
|
|
|
res);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'p':
|
|
|
|
|
token = next_token_alpha(NULL, delim);
|
|
|
|
|
/* You can pass your turn without changing any letter */
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
token = "";
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (iGame.pass(token, iGame.currPlayer()) != 0)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
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;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'q':
|
|
|
|
|
quit = 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("commande inconnue\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("fin du mode partie libre\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
|
loop_duplicate(Duplicate &iGame)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
char *token;
|
2005-04-27 19:55:32 +02:00
|
|
|
|
char *commande = NULL;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
char delim[] = " \t";
|
|
|
|
|
int quit = 0;
|
|
|
|
|
|
|
|
|
|
printf("mode duplicate\n");
|
|
|
|
|
printf("[?] pour l'aide\n");
|
|
|
|
|
while (quit == 0)
|
|
|
|
|
{
|
2005-04-27 19:55:32 +02:00
|
|
|
|
commande = rl_gets();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = strtok(commande, delim);
|
|
|
|
|
if (token)
|
|
|
|
|
{
|
|
|
|
|
switch (token[0])
|
|
|
|
|
{
|
|
|
|
|
case '?':
|
|
|
|
|
help_duplicate();
|
|
|
|
|
break;
|
|
|
|
|
case 'a':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
display_data(iGame, delim);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
|
|
|
|
token = next_token_alpha(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
help_duplicate();
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (Dic_search_word(iGame.getDic(), token))
|
2004-08-07 20:10:42 +02:00
|
|
|
|
printf("le mot -%s- existe\n", token);
|
|
|
|
|
else
|
|
|
|
|
printf("le mot -%s- n'existe pas\n", token);
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'j':
|
|
|
|
|
token = next_token_alpha(NULL, delim);
|
2004-04-08 11:43:06 +02:00
|
|
|
|
if (token == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
help_duplicate();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
char *coord = next_token_alphanum(NULL, delim);
|
|
|
|
|
if (coord == NULL)
|
|
|
|
|
{
|
|
|
|
|
help_duplicate();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if ((res = iGame.play(coord, token)) != 0)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Mot incorrect ou mal plac<61> (%i)\n",
|
|
|
|
|
res);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
case 'n':
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
help_duplicate();
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
int res = iGame.setPlayer(atoi(token));
|
2004-08-07 20:10:42 +02:00
|
|
|
|
if (res == 1)
|
|
|
|
|
fprintf(stderr, "Num<EFBFBD>ro de joueur invalide\n");
|
|
|
|
|
else if (res == 2)
|
|
|
|
|
fprintf(stderr, "Impossible de choisir un joueur non humain\n");
|
|
|
|
|
}
|
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
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;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'q':
|
|
|
|
|
quit = 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2004-04-08 11:43:06 +02:00
|
|
|
|
printf("commande inconnue\n");
|
|
|
|
|
break;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("fin du mode duplicate\n");
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
|
main_loop(const Dictionary &iDic)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
|
|
|
|
char *token;
|
2005-04-27 19:55:32 +02:00
|
|
|
|
char *commande = NULL;
|
2004-08-07 20:10:42 +02:00
|
|
|
|
char delim[] = " \t";
|
|
|
|
|
int quit = 0;
|
|
|
|
|
|
|
|
|
|
printf("[?] pour l'aide\n");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
while (quit == 0)
|
|
|
|
|
{
|
2005-04-27 19:55:32 +02:00
|
|
|
|
commande = rl_gets();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = strtok(commande, delim);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (token)
|
|
|
|
|
{
|
2004-08-07 20:10:42 +02:00
|
|
|
|
switch (token[0])
|
|
|
|
|
{
|
|
|
|
|
case '?':
|
|
|
|
|
help();
|
|
|
|
|
break;
|
|
|
|
|
case 'c':
|
|
|
|
|
token = next_token_filename(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
{}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FILE* fin;
|
|
|
|
|
fprintf(stderr, "chargement de -%s-\n", token);
|
|
|
|
|
if ((fin = fopen(token, "r")) == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("impossible d'ouvrir %s\n", token);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
Game *game = Game::load(fin, iDic);
|
|
|
|
|
fclose(fin);
|
|
|
|
|
if (game == NULL)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
fprintf(stderr, "erreur pendant le chargement\n");
|
2004-08-07 20:10:42 +02:00
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
else
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
|
if (game->getMode() == Game::kTRAINING)
|
|
|
|
|
loop_training((Training&)*game);
|
|
|
|
|
else if (game->getMode() == Game::kFREEGAME)
|
|
|
|
|
loop_freegame((FreeGame&)*game);
|
|
|
|
|
else
|
|
|
|
|
loop_duplicate((Duplicate&)*game);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'e':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
{
|
2005-02-24 09:06:24 +01:00
|
|
|
|
// New training game
|
|
|
|
|
Training *game = GameFactory::Instance()->createTraining(iDic);
|
|
|
|
|
game->start();
|
|
|
|
|
loop_training(*game);
|
|
|
|
|
GameFactory::Instance()->releaseGame(*game);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
}
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'd':
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2005-02-24 09:06:24 +01:00
|
|
|
|
// New duplicate game
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
{
|
|
|
|
|
help();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-24 09:06:24 +01:00
|
|
|
|
Duplicate *game = GameFactory::Instance()->createDuplicate(iDic);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
for (i = 0; i < atoi(token); i++)
|
2005-02-24 09:06:24 +01:00
|
|
|
|
game->addHumanPlayer();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
{
|
|
|
|
|
help();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < atoi(token); i++)
|
2005-02-24 09:06:24 +01:00
|
|
|
|
game->addAIPlayer();
|
|
|
|
|
game->start();
|
|
|
|
|
loop_duplicate(*game);
|
|
|
|
|
GameFactory::Instance()->releaseGame(*game);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'l':
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2005-02-24 09:06:24 +01:00
|
|
|
|
// New free game
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
{
|
|
|
|
|
help();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-24 09:06:24 +01:00
|
|
|
|
FreeGame *game = GameFactory::Instance()->createFreeGame(iDic);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
for (i = 0; i < atoi(token); i++)
|
2005-02-24 09:06:24 +01:00
|
|
|
|
game->addHumanPlayer();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
token = next_token_digit(NULL, delim);
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
{
|
|
|
|
|
help();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < atoi(token); i++)
|
2005-02-24 09:06:24 +01:00
|
|
|
|
game->addAIPlayer();
|
|
|
|
|
game->start();
|
|
|
|
|
loop_freegame(*game);
|
|
|
|
|
GameFactory::Instance()->releaseGame(*game);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'D':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
{
|
2005-02-24 09:06:24 +01:00
|
|
|
|
// New duplicate game
|
|
|
|
|
Duplicate *game = GameFactory::Instance()->createDuplicate(iDic);
|
|
|
|
|
game->addHumanPlayer();
|
|
|
|
|
game->addAIPlayer();
|
|
|
|
|
game->start();
|
|
|
|
|
loop_duplicate(*game);
|
|
|
|
|
GameFactory::Instance()->releaseGame(*game);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
}
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'L':
|
2005-02-05 12:14:56 +01:00
|
|
|
|
{
|
2005-02-24 09:06:24 +01:00
|
|
|
|
// New free game
|
|
|
|
|
FreeGame *game = GameFactory::Instance()->createFreeGame(iDic);
|
|
|
|
|
game->addHumanPlayer();
|
|
|
|
|
game->addAIPlayer();
|
|
|
|
|
game->start();
|
|
|
|
|
loop_freegame(*game);
|
|
|
|
|
GameFactory::Instance()->releaseGame(*game);
|
2004-08-07 20:10:42 +02:00
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
}
|
2004-08-07 20:10:42 +02:00
|
|
|
|
case 'q':
|
|
|
|
|
quit = 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("commande inconnue\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2004-04-08 11:43:06 +02:00
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
2004-08-07 20:10:42 +02:00
|
|
|
|
char dic_path[100];
|
|
|
|
|
|
|
|
|
|
Dictionary dic = NULL;
|
|
|
|
|
|
2005-04-10 14:15:40 +02:00
|
|
|
|
if (argc != 2 && argc != 3)
|
2004-08-07 20:10:42 +02:00
|
|
|
|
{
|
2005-04-10 14:15:40 +02:00
|
|
|
|
fprintf(stdout, "Usage: eliot /chemin/vers/ods4.dawg [random_seed]\n");
|
2004-08-07 20:10:42 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
strcpy(dic_path, argv[1]);
|
|
|
|
|
|
|
|
|
|
switch (Dic_load(&dic, dic_path))
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
/* Normal case */
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
printf("chargement: probl<62>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<62>me 3 d'allocation m<>moire\n");
|
|
|
|
|
exit(3);
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
printf("chargement: probl<62>me 4 d'alocation m<>moire\n");
|
|
|
|
|
exit(4);
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
printf("chargement: probl<62>me de lecture des arcs du dictionnaire\n");
|
|
|
|
|
exit(5);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("chargement: probl<62>me non-repertori<72>\n");
|
|
|
|
|
exit(6);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-10 14:15:40 +02:00
|
|
|
|
if (argc == 3)
|
|
|
|
|
srand(atoi(argv[2]));
|
|
|
|
|
else
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
|
main_loop(dic);
|
2005-02-24 09:06:24 +01:00
|
|
|
|
GameFactory::Destroy();
|
2004-08-07 20:10:42 +02:00
|
|
|
|
|
|
|
|
|
Dic_destroy(dic);
|
2005-04-27 19:55:32 +02:00
|
|
|
|
|
|
|
|
|
// Free the readline static variable
|
|
|
|
|
if (line_read)
|
|
|
|
|
free(line_read);
|
|
|
|
|
|
2004-08-07 20:10:42 +02:00
|
|
|
|
return 0;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
}
|