game.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1999-2005 Eliot
00003  * Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
00004  *          Olivier Teuliere  <ipkiss@via.ecp.fr>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  *****************************************************************************/
00020 
00021 #ifndef _GAME_H_
00022 #define _GAME_H_
00023 
00024 #include <string>
00025 #include <vector>
00026 #include <iostream>
00027 #include "bag.h"
00028 #include "board.h"
00029 #include "history.h"
00030 
00031 class Player;
00032 class PlayedRack;
00033 class Round;
00034 class Rack;
00035 class Turn;
00036 typedef struct _Dictionary * Dictionary;
00037 
00038 using namespace std;
00039 
00040 
00041 /*************************
00042  * Ident string used to identify saved Eliot games
00043  *************************/
00044 #define IDENT_STRING "Eliot"
00045 
00046 /**
00047  * Parent class of all the Game types.
00048  * It offers the common attributes (Board, Bag, etc...) as well as useful
00049  * "helper" methods to factorize some code.
00050  */
00051 class Game
00052 {
00053 public:
00054     Game(const Dictionary &iDic);
00055     virtual ~Game();
00056 
00057     /// Game mode: each one of these modes is implemented in an inherited class
00058     enum GameMode
00059     {
00060         kTRAINING,
00061         kFREEGAME,
00062         kDUPLICATE
00063     };
00064     virtual GameMode getMode() const = 0;
00065     virtual string getModeAsString() const = 0;
00066 
00067     /// Game variant: it slightly modifies the rules of the game
00068     enum GameVariant
00069     {
00070         kNONE,      // Normal game rules
00071         kJOKER      // Joker game
00072     };
00073 
00074     /**
00075      * Accessors for the variant of the game.
00076      * The variant can be changed during a game without any problem
00077      * (though it seems rather useless...)
00078      */
00079     void setVariant(GameVariant iVariant)   { m_variant = iVariant; }
00080     GameVariant getVariant() const          { return m_variant; }
00081 
00082     /**
00083      * Dictionary associated with the game.
00084      * The dictionary can be changed during a game without problem
00085      */
00086     const Dictionary & getDic() const   { return *m_dic; }
00087     void setDic(const Dictionary &iDic) { m_dic = &iDic; }
00088 
00089     const Board&  getBoard() const { return m_board; }
00090     const Bag&    getBag()   const { return m_bag; }
00091     const Player& getPlayer(int iNum) const;
00092     const Turn&   getTurn(int iNum) const;
00093     const Player& getCurrentPlayer() const { return getPlayer(currPlayer()); };
00094 
00095     /**
00096      * Eliot file formats
00097      */
00098     typedef enum {
00099       FILE_FORMAT_STANDARD,
00100       FILE_FORMAT_ADVANCED
00101     } game_file_format;
00102 
00103     /**
00104      * Saved games handling.
00105      *
00106      * load() returns the loaded game, or NULL if there was a problem
00107      * load() might need some more work to be robust enough to
00108      * handle "hand written" files
00109      */
00110     static Game * load(FILE *fin, const Dictionary &iDic);
00111 
00112     /**
00113      * Save a game to a File
00114      * Standard format is used for training games so that it is compatible
00115      * with previous versions of Eliot.
00116      *
00117      * Saving can be forced to advanced format for training games by 
00118      * setting the last parameter to FILE_FORMAT_ADVANCED
00119      */
00120     void save(ostream &out, game_file_format format=FILE_FORMAT_STANDARD) const;
00121 
00122     /*************************
00123      * Playing the game
00124      * the int parameter should be 0 <= int < getNTurns
00125      *************************/
00126     int back(int);
00127 
00128     /*************************
00129      * Set the rack for searching
00130      *
00131      * The int parameter is a boolean, if this parameter
00132      * set the rack will check that there are at least
00133      * 2 vowels and 2 consonants before the round 15.
00134      *
00135      * The setrackmanual parameter string has to contain
00136      * 'a' <= char <= 'z' or 'A' <= char <= 'Z' or '?'
00137      *
00138      * return value
00139      *    0 : the rack has been set
00140      *    1 : the bag does not contain enough tiles
00141      *    2 : the rack check was set on and failed
00142      *    3 : the rack cannot be completed (Game_*_setrackrandom only)
00143      *************************/
00144     static const int RACK_SIZE;
00145     enum set_rack_mode {RACK_ALL, RACK_NEW, RACK_MANUAL};
00146     int setRack(int player, set_rack_mode mode, bool check, const string& str);
00147     string getPlayerRack(int, PlayedRack::display_mode mode = PlayedRack::RACK_SIMPLE) const;
00148 
00149     /**
00150      * Methods to access already played words.
00151      * The int parameter should be 0 <= int < getNTurns()
00152      */
00153     const History& getHistory() const { return m_history; }
00154 
00155     /**
00156      * Methods to access players.
00157      * The int parameter should be 0 <= int < getNPlayers()
00158      */
00159     int  getNPlayers() const    { return m_players.size(); }
00160     int  getNHumanPlayers() const;
00161     virtual void addHumanPlayer();
00162     // TODO: Ability to specify which kind of AI player is wanted
00163     virtual void addAIPlayer();
00164     int  currPlayer() const     { return m_currPlayer; }
00165 
00166     /**
00167      * Game handling
00168      */
00169     virtual int start() = 0;
00170     virtual int play(const string &iCoord, const string &iWord) = 0;
00171     virtual int endTurn() = 0;
00172 
00173 protected:
00174     /// All the players, indexed by their ID
00175     vector<Player*> m_players;
00176     /// ID of the "current" player
00177     int m_currPlayer;
00178 
00179 // TODO: check what should be private and what should be protected
00180 // private:
00181 
00182     /// Variant
00183     GameVariant m_variant;
00184 
00185     /// Dictionary currently associated to the game
00186     const Dictionary * m_dic;
00187 
00188     /// Bag
00189     Bag m_bag;
00190 
00191     /// Board
00192     Board m_board;
00193 
00194     /**
00195      * History of the game.
00196      * The vector is indexed by the number of turns in the game
00197      */
00198     History m_history;
00199 
00200     int m_points;
00201 
00202     bool m_finished;
00203 
00204     /*********************************************************
00205      * Helper functions
00206      *********************************************************/
00207 
00208     int helperPlayRound(const Round &iRound);
00209     int helperSetRackRandom(int p, bool iCheck, set_rack_mode mode);
00210     int helperSetRackManual(int p, bool iCheck, const string &iLetters);
00211 
00212     void prevPlayer();
00213     void nextPlayer();
00214     bool rackInBag(const Rack &iRack, const Bag &iBag) const;
00215     void realBag(Bag &iBag) const;
00216     int  checkPlayedWord(const string &iCoord,
00217                          const string &iWord, Round &oRound);
00218 
00219     /**
00220      * load games from File using the first format.
00221      * This format is used for Training games
00222      */
00223     static Game* gameLoadFormat_14(FILE *fin, const Dictionary& iDic);
00224 
00225     /**
00226      * load games from File using advanced format (since Eliot 1.5)
00227      * This format is used for Duplicate, Freegame, ...
00228      */
00229     static Game* gameLoadFormat_15(FILE *fin, const Dictionary& iDic);
00230 
00231     /**
00232      * Training games ares saved using the initial Eliot format
00233      */
00234     void Game::gameSaveFormat_14(ostream &out) const;
00235 
00236     /**
00237      * Advanced game file format output
00238      */
00239     void Game::gameSaveFormat_15(ostream &out) const;
00240 
00241 };
00242 
00243 #endif /* _GAME_H_ */
00244 
00245 /// Local Variables:
00246 /// mode: c++
00247 /// mode: hs-minor
00248 /// c-basic-offset: 4
00249 /// indent-tabs-mode: nil
00250 /// End:

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