/* Eliot */ /* Copyright (C) 1999-2004 Eliot */ /* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* $Id: eliottxt.c,v 1.2 2004/08/07 18:10:42 ipkiss Exp $ */ #include #include #include #include #include #include #include "dic.h" #include "dic_search.h" #include "game.h" #include "gameio.h" #if 0 // test #include "tiles.h" #include "bag.h" #include "rack.h" #include "round.h" #include "pldrack.h" #include "results.h" #include "board.h" #include "player.h" #include "game_internals.h" void test2(Game g, char *coord, char *word, int expected) { int res; res = Game_freegame_play(g, coord, word); fprintf(stderr, "res (%d) = %d score: %3d%3d\n", expected, res, Game_getplayerpoints(g, 0), Game_getplayerpoints(g, 1)); } void test(Game g) { int res; Game_addhumanplayer(g); Game_addhumanplayer(g); Game_nextplayer(g); Game_training_setrackmanual(g, 0, "AVNZERI"); test2(g, "Z2", "RAVINEZ", 2); test2(g, "H0", "RAVINEZ", 2); test2(g, "H51", "RAVINEZ", 2); test2(g, "HH", "RAVINEZ", 2); /* test2(g, "8H", "RAVINEZ", 2); */ test2(g, "H5Z2hello world", "RAVINZ", 2); // XXX test2(g, "H5", "RAVINZ", 3); test2(g, "H10", "RAVINEZ", 6); // XXX test2(g, "H5", "RAVIVEZ", 4); test2(g, "H5", "RAVINEZ", 0); Game_nextplayer(g); Game_training_setrackmanual(g, 0, "AEI?NRS"); test2(g, "H5", "RAvINE", 7); test2(g, "A1", "SIgNERA", 8); test2(g, "H4", "SIgNERAI", 5); test2(g, "9E", "SIgNERAI", 4); test2(g, "I1", "SIgNERA", 6); test2(g, "8A", "SIgNERAI", 0); Game_nextplayer(g); Game_training_setrackmanual(g, 0, "ACIORU?"); test2(g, "I1", "COURAIS", 4); test2(g, "I2", "COURAIs", 6); test2(g, "10F", "COeUR", 0); Game_nextplayer(g); test2(g, "I1", "COURAIs", 0); } #endif 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] && (isalpha(token[i]) || token[i] == '.' || token[i] == '_'); i++) ; token[i] = '\0'; return token; } void eliottxt_get_cross(Dictionary dic, 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(dic, cros, wordlist); for (i = 0; i "); fgets(commande, sizeof(commande), stdin); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help_training(); break; case 'a': display_data(game, delim); break; case 'd': token = next_token_alpha(NULL, delim); if (token == NULL) help_training(); else { if (Dic_search_word(Game_getdic(game), 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_training(); break; } if ((res = Game_training_play(game, 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) Game_back(game, n == 0 ? 1 : -n); else { if (Game_training_playresult(game, --n)) printf("mauvais argument\n"); } } break; case 'r': Game_training_search(game); break; case 't': token = next_token_alphaplusjoker(NULL, delim); if (token == NULL) help_training(); else if (Game_training_setrackmanual(game, 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(Game_getdic(game), token); break; case '*': Game_training_setrackrandom(game, 0, RACK_ALL); break; case '+': Game_training_setrackrandom(game, 0, RACK_NEW); break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } printf("fin du mode entraînement\n"); } void loop_freegame(Game game) { char *token; char commande[100]; char delim[] = " \t"; int quit = 0; printf("mode partie libre\n"); printf("[?] pour l'aide\n"); while (quit == 0) { printf("commande> "); fgets(commande, sizeof(commande), stdin); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help_freegame(); break; case 'a': display_data(game, delim); break; case 'd': token = next_token_alpha(NULL, delim); if (token == NULL) help_freegame(); else { if (Dic_search_word(Game_getdic(game), 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 = Game_freegame_play(game, 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 (Game_freegame_pass(game, token, Game_currplayer(game)) != 0) break; break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } printf("fin du mode partie libre\n"); } void loop_duplicate(Game game) { char *token; char commande[100]; char delim[] = " \t"; int quit = 0; printf("mode duplicate\n"); printf("[?] pour l'aide\n"); while (quit == 0) { printf("commande> "); fgets(commande, sizeof(commande), stdin); token = strtok(commande, delim); if (token) { switch (token[0]) { case '?': help_duplicate(); break; case 'a': display_data(game, delim); break; case 'd': token = next_token_alpha(NULL, delim); if (token == NULL) help_duplicate(); else { if (Dic_search_word(Game_getdic(game), 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 = Game_duplicate_play(game, coord, token)) != 0) { fprintf(stderr, "Mot incorrect ou mal placé (%i)\n", res); break; } } break; case 's': token = next_token_digit(NULL, delim); if (token == NULL) help_duplicate(); else { int res = Game_duplicate_setplayer(game, 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 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } printf("fin du mode duplicate\n"); } void main_loop(Game game) { char *token; char commande[100]; char delim[] = " \t"; int quit = 0; printf("[?] pour l'aide\n"); while (quit == 0) { printf("commande> "); fgets(commande, sizeof(commande), stdin); 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; fprintf(stderr, "chargement de -%s-\n", token); if ((fin = fopen(token, "r")) == NULL) { printf("impossible d'ouvrir %s\n", token); break; } Game_init(game); switch (Game_load(game, fin)) { case 0: /* ok */ break; case 1: fprintf(stderr, "format non reconnu\n"); break; default: fprintf(stderr, "erreur pendant le chargement\n"); break; } fclose(fin); } break; case 's': token = next_token_filename(NULL, delim); if (token == NULL) {} else { FILE *out; if ((out = fopen(token, "w")) == NULL) { printf("impossible d'ouvrir %s\n", token); break; } Game_save(game, out); fclose(out); } break; case 'e': /* Re-init the game */ Game_init(game); Game_training_start(game); loop_training(game); break; case 'd': { int i; /* Re-init the game */ Game_init(game); token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } for (i = 0; i < atoi(token); i++) Game_addhumanplayer(game); token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } for (i = 0; i < atoi(token); i++) Game_addaiplayer(game); Game_duplicate_start(game); loop_duplicate(game); break; } case 'l': { int i; /* Re-init the game */ Game_init(game); token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } for (i = 0; i < atoi(token); i++) Game_addhumanplayer(game); token = next_token_digit(NULL, delim); if (token == NULL) { help(); break; } for (i = 0; i < atoi(token); i++) Game_addaiplayer(game); Game_freegame_start(game); loop_freegame(game); break; } case 'D': /* Re-init the game */ Game_init(game); Game_addhumanplayer(game); Game_addaiplayer(game); Game_duplicate_start(game); loop_duplicate(game); break; case 'L': /* Re-init the game */ Game_init(game); Game_addhumanplayer(game); Game_addaiplayer(game); Game_freegame_start(game); loop_freegame(game); break; case 'q': quit = 1; break; default: printf("commande inconnue\n"); break; } } } } int main(int argc, char *argv[]) { char dic_path[100]; Game game = NULL; Dictionary dic = NULL; srand(time(NULL)); if (argc != 2) { fprintf(stdout, "Usage: eliot /chemin/vers/ods4.dawg\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; } game = Game_create(dic); main_loop(game); Game_destroy(game); Dic_destroy(dic); return 0; }