game_io.old.cpp

Go to the documentation of this file.
00001 
00002 #if 0
00003 Game * Game::load(FILE *fin, const Dictionary &iDic)
00004 {
00005     char buff[4096];
00006     char delim[] = " \t\n|";
00007     char *token;
00008 
00009     // Check characteristic string
00010     if (fgets(buff, sizeof(buff), fin) == NULL)
00011         return NULL;
00012     if ((token = strtok(buff, delim)) == NULL)
00013         return NULL;
00014     if (string(token) != IDENT_STRING)
00015         return NULL;
00016 
00017     int num;
00018     char rack[20];
00019     char word[20];
00020     char ref[4];
00021     int pts;
00022     int player;
00023     char *pos;
00024     Tile tile;
00025     Game *pGame = NULL;
00026 
00027     while (fgets(buff, sizeof(buff), fin))
00028     {
00029         // Indication of game type
00030         pos = strstr(buff, "Game type: ");
00031         if (pos != NULL)
00032         {
00033             // No Game object should have been created yet
00034             if (pGame != NULL)
00035             {
00036                 delete pGame;
00037                 return NULL;
00038             }
00039             // Create the correct Game object
00040             if (strstr(buff, "Training"))
00041                 pGame = GameFactory::Instance()->createTraining(iDic);
00042             else if (strstr(buff, "Free game"))
00043                 pGame = GameFactory::Instance()->createFreeGame(iDic);
00044             else if (strstr(buff, "Duplicate"))
00045                 pGame = GameFactory::Instance()->createDuplicate(iDic);
00046             else
00047                 return NULL;
00048             // Read next line
00049             continue;
00050         }
00051 
00052         // Players type
00053         pos = strstr(buff, "Player ");
00054         if (pos != NULL && pGame != NULL)
00055         {
00056             int nb = 0;
00057             char type[20];
00058             if (sscanf(pos, "Player %d: %19s", &nb, type) > 1)
00059             {
00060                 if (string(type) == "Human")
00061                     pGame->addHumanPlayer();
00062                 else if (string(type) == "Computer")
00063                     pGame->addAIPlayer();
00064                 else
00065                     ;
00066             }
00067             // Read next line
00068             continue;
00069         }
00070 
00071         // Last racks
00072         pos = strstr(buff, "Rack ");
00073         if (pos != NULL && pGame != NULL)
00074         {
00075             int nb = 0;
00076             char letters[20];
00077             if (sscanf(pos, "Rack %d: %19s", &nb, letters) > 1)
00078             {
00079                 // Create the played rack
00080                 PlayedRack pldrack;
00081                 char *r = letters;
00082                 if (strchr(r, '+'))
00083                 {
00084                     while (*r != '+')
00085                     {
00086                         pldrack.addOld(Tile(*r));
00087                         r++;
00088                     }
00089                     r++;
00090                 }
00091                 while (*r)
00092                 {
00093                     pldrack.addNew(Tile(*r));
00094                     r++;
00095                 }
00096 
00097                 // Give the rack to the player
00098                 pGame->m_players[nb]->setCurrentRack(pldrack);
00099             }
00100             // Read next line
00101             continue;
00102         }
00103 
00104         // Skip columns title
00105         if (strstr(buff, "==") != NULL ||
00106             strstr(buff, "| PTS | P |") != NULL)
00107         {
00108             continue;
00109         }
00110 
00111         if (string(buff) != "\n" && pGame != NULL)
00112         {
00113             char bonus = 0;
00114             int res = sscanf(buff, "   %2d | %8s | %s | %3s | %3d | %1d | %c",
00115                              &num, rack, word, ref, &pts, &player, &bonus);
00116             if (res < 6)
00117                 continue;
00118 
00119             // Integrity checks
00120             // TODO: add more checks
00121             if (pts < 0)
00122                 continue;
00123             if (player < 0 || player > pGame->getNPlayers())
00124                 continue;
00125             if (bonus && bonus != '*')
00126                 continue;
00127 
00128             // Build a rack for the correct player
00129             PlayedRack pldrack;
00130             char *r = rack;
00131             if (strchr(r, '+'))
00132             {
00133                 while (*r != '+')
00134                 {
00135                     pldrack.addOld(Tile(*r));
00136                     r++;
00137                 }
00138                 r++;
00139             }
00140 
00141             while (*r)
00142             {
00143                 pldrack.addNew(Tile(*r));
00144                 r++;
00145             }
00146 
00147             // Build a round
00148             Round round;
00149             round.accessCoord().setFromString(ref);
00150             if (!round.getCoord().isValid())
00151                 continue;
00152 
00153             round.setPoints(pts);
00154             if (bonus == '*')
00155                 round.setBonus(1);
00156 
00157             for (unsigned int i = 0; i < strlen(word); i++)
00158             {
00159                 tile = Tile(word[i]);
00160 
00161                 if (round.getCoord().getDir() == Coord::HORIZONTAL)
00162                 {
00163                     if (!pGame->m_board.getTile(round.getCoord().getRow(),
00164                                                 round.getCoord().getCol() + i).isEmpty())
00165                     {
00166                         round.addRightFromBoard(tile);
00167                     }
00168                     else
00169                     {
00170                         round.addRightFromRack(tile, islower(word[i]));
00171                         pGame->m_bag.takeTile((islower(word[i])) ? Tile::Joker() : tile);
00172                     }
00173                 }
00174                 else
00175                 {
00176                     if (!pGame->m_board.getTile(round.getCoord().getRow() + i,
00177                                                 round.getCoord().getCol()).isEmpty())
00178                     {
00179                         round.addRightFromBoard(tile);
00180                     }
00181                     else
00182                     {
00183                         round.addRightFromRack(tile, islower(word[i]));
00184                         pGame->m_bag.takeTile((islower(word[i])) ? Tile::Joker() : tile);
00185                     }
00186                 }
00187             }
00188 
00189             pGame->m_currPlayer = player;
00190             // Update the rack for the player
00191             pGame->m_players[player]->setCurrentRack(pldrack);
00192             // End the turn for the current player (this creates a new rack)
00193             pGame->m_players[player]->endTurn(round, num - 1);
00194             // Add the points
00195             pGame->m_players[player]->addPoints(pts);
00196             // Play the round
00197             pGame->helperPlayRound(round);
00198         }
00199     }
00200 
00201     // Finalize the game
00202     if (pGame)
00203     {
00204         // We don't really know whose turn it is, but at least we know that
00205         // the game was saved while a human was to play.
00206         for (int i = 0; i < pGame->getNPlayers(); i++)
00207         {
00208             if (pGame->getPlayer(i).isHuman())
00209             {
00210                 pGame->m_currPlayer = i;
00211                 break;
00212             }
00213         }
00214     }
00215     return pGame;
00216 }
00217 #endif
00218 
00219 #if 0
00220 void Game::save(ostream &out) const
00221 {
00222     const string decal = "   ";
00223     // "Header" of the game
00224     out << IDENT_STRING << endl << endl;
00225     out << "Game type: " << getModeAsString() << endl;
00226     for (int i = 0; i < getNPlayers(); i++)
00227     {
00228         out << "Player " << i << ": ";
00229         if (getPlayer(i).isHuman())
00230             out << "Human" << endl;
00231         else
00232             out << "Computer" << endl;
00233     }
00234     out << endl;
00235 
00236     // Title of the columns
00237     char line[100];
00238     out << decal << " N |   RACK   |    SOLUTION     | REF | PTS | P | BONUS" << endl;
00239     out << decal << "===|==========|=================|=====|=====|===|======" << endl;
00240 
00241     // Print the game itself
00242     for (int i = 0; i < m_history.getSize(); i++)
00243     {
00244         const Turn& t = m_history.getTurn(i);
00245         string word = t.getRound().getWord();
00246         string coord = t.getRound().getCoord().toString();
00247         sprintf(line, "%2d | %8s | %s%s | %3s | %3d | %1d | %c",
00248                 i + 1, t.getPlayedRack().toString().c_str(), word.c_str(),
00249                 string(15 - word.size(), ' ').c_str(),
00250                 coord.c_str(), t.getRound().getPoints(),
00251                 t.getPlayer(), t.getRound().getBonus() ? '*' : ' ');
00252 
00253         out << decal << line << endl;
00254     }
00255     out << endl << decal << "Total: " << m_points << endl;
00256 
00257     // Print current rack for all the players
00258     out << endl;
00259     for (int i = 0; i < getNPlayers(); i++)
00260     {
00261         string rack = getPlayer(i).getCurrentRack().toString();
00262         out << "Rack " << i << ": " << rack << endl;
00263     }
00264 }
00265 #endif

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5