2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
2008-11-23 09:18:03 +01:00
|
|
|
* Copyright (C) 1999-2008 Antoine Fraboulet & Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
|
|
|
* Olivier Teulière <ipkiss @@ gmail.com>
|
2005-02-05 12:14:56 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
#ifndef _GAME_H_
|
|
|
|
#define _GAME_H_
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include "bag.h"
|
|
|
|
#include "board.h"
|
2005-12-26 23:58:58 +01:00
|
|
|
#include "history.h"
|
2004-04-08 11:43:06 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
class Dictionary;
|
2005-02-05 12:14:56 +01:00
|
|
|
class Player;
|
|
|
|
class PlayedRack;
|
|
|
|
class Round;
|
|
|
|
class Rack;
|
2005-11-06 00:22:41 +01:00
|
|
|
class Turn;
|
2008-11-23 09:18:03 +01:00
|
|
|
class TurnCmd;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
using namespace std;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
|
2005-03-27 19:30:48 +02:00
|
|
|
/**
|
|
|
|
* Parent class of all the Game types.
|
|
|
|
* It offers the common attributes (Board, Bag, etc...) as well as useful
|
|
|
|
* "helper" methods to factorize some code.
|
|
|
|
*/
|
2005-02-05 12:14:56 +01:00
|
|
|
class Game
|
|
|
|
{
|
|
|
|
public:
|
2007-08-04 22:01:27 +02:00
|
|
|
/// Game specs.
|
2008-01-08 14:52:32 +01:00
|
|
|
static const unsigned int RACK_SIZE;
|
2007-08-04 22:01:27 +02:00
|
|
|
static const int BONUS_POINTS;
|
|
|
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
Game(const Dictionary &iDic);
|
|
|
|
virtual ~Game();
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Game type
|
|
|
|
***************/
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
/// Game mode: each one of these modes is implemented in an inherited class
|
2005-02-05 12:14:56 +01:00
|
|
|
enum GameMode
|
|
|
|
{
|
|
|
|
kTRAINING,
|
|
|
|
kFREEGAME,
|
|
|
|
kDUPLICATE
|
|
|
|
};
|
|
|
|
virtual GameMode getMode() const = 0;
|
2005-02-09 23:33:56 +01:00
|
|
|
virtual string getModeAsString() const = 0;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
/// Game variant: it slightly modifies the rules of the game
|
|
|
|
enum GameVariant
|
|
|
|
{
|
|
|
|
kNONE, // Normal game rules
|
|
|
|
kJOKER // Joker game
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessors for the variant of the game.
|
|
|
|
* The variant can be changed during a game without any problem
|
|
|
|
* (though it seems rather useless...)
|
|
|
|
*/
|
|
|
|
void setVariant(GameVariant iVariant) { m_variant = iVariant; }
|
|
|
|
GameVariant getVariant() const { return m_variant; }
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Various getters
|
|
|
|
***************/
|
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Get the dictionary associated with the game.
|
|
|
|
* You should never create a new dictionary object while a Game
|
|
|
|
* object still exists
|
2005-03-29 00:07:22 +02:00
|
|
|
*/
|
2008-01-08 14:52:32 +01:00
|
|
|
const Dictionary & getDic() const { return m_dic; }
|
2005-03-29 00:07:22 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/// Get the board
|
|
|
|
const Board& getBoard() const { return m_board; }
|
2008-11-23 09:18:03 +01:00
|
|
|
Board & accessBoard() { return m_board; }
|
2008-01-08 14:52:32 +01:00
|
|
|
/// Get the bag
|
2008-11-22 14:09:28 +01:00
|
|
|
#ifdef REAL_BAG_MODE
|
|
|
|
Bag getBag() const;
|
|
|
|
#else
|
2008-01-08 14:52:32 +01:00
|
|
|
const Bag& getBag() const { return m_bag; }
|
2008-11-23 09:18:03 +01:00
|
|
|
Bag & accessBag() { return m_bag; }
|
|
|
|
/**
|
|
|
|
* The realBag is the current bag minus all the racks
|
|
|
|
* present in the game. It represents the actual
|
|
|
|
* letters that are left in the bag.
|
|
|
|
* FIXME: in Duplicate mode, this method uses m_currPlayer to find the
|
|
|
|
* rack of the player. Since not all the players played the same word,
|
|
|
|
* it is important to set m_currPlayer accurately before!
|
|
|
|
*/
|
|
|
|
void realBag(Bag &iBag) const;
|
2008-11-22 14:09:28 +01:00
|
|
|
#endif
|
2008-11-23 09:18:03 +01:00
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/// Get the history of the game */
|
|
|
|
const History& getHistory() const { return m_history; }
|
2008-11-23 09:18:03 +01:00
|
|
|
History & accessHistory() { return m_history; }
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/***************
|
|
|
|
* Methods to access players.
|
|
|
|
***************/
|
|
|
|
|
|
|
|
const Player& getPlayer(unsigned int iNum) const;
|
2005-12-26 19:09:52 +01:00
|
|
|
const Player& getCurrentPlayer() const { return getPlayer(currPlayer()); };
|
2008-01-08 14:52:32 +01:00
|
|
|
unsigned int getNPlayers() const { return m_players.size(); }
|
|
|
|
unsigned int getNHumanPlayers() const;
|
|
|
|
unsigned int currPlayer() const { return m_currPlayer; }
|
|
|
|
|
2008-01-28 20:17:33 +01:00
|
|
|
/**
|
|
|
|
* Add a player to the game.
|
|
|
|
* The Game object takes ownership of the given player
|
|
|
|
*/
|
|
|
|
virtual void addPlayer(Player *iPlayer);
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Game handling
|
|
|
|
***************/
|
2005-11-05 16:48:59 +01:00
|
|
|
|
2006-01-01 20:48:36 +01:00
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Start the game.
|
|
|
|
* AI players are handled automatically, so if the game only has AI
|
|
|
|
* players, it will play until the end.
|
2006-01-01 20:48:36 +01:00
|
|
|
*/
|
2008-01-08 14:52:32 +01:00
|
|
|
virtual int start() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method used by human players to play the word iWord at coordinates
|
|
|
|
* iCoord, and end the turn (if possible)
|
|
|
|
* Possible return values:
|
|
|
|
* 0: correct word, the Round can be used by the caller
|
|
|
|
* 1: one letter of the word is invalid in the current dictionary
|
|
|
|
* 2: invalid coordinates (unreadable or out of the board)
|
|
|
|
* 3: word not present in the dictionary
|
|
|
|
* 4: not enough letters in the rack to play the word
|
|
|
|
* 5: word is part of a longer one
|
|
|
|
* 6: word overwriting an existing letter
|
2008-09-13 23:32:45 +02:00
|
|
|
* 7: invalid crosscheck
|
2008-01-08 14:52:32 +01:00
|
|
|
* 8: word already present on the board (no new letter from the rack)
|
|
|
|
* 9: isolated word (not connected to the rest)
|
|
|
|
* 10: first word not horizontal
|
|
|
|
* 11: first word not covering the H8 square
|
2008-09-13 23:32:45 +02:00
|
|
|
* 12: word going out of the board
|
2008-01-08 14:52:32 +01:00
|
|
|
*/
|
|
|
|
virtual int play(const wstring &iCoord, const wstring &iWord) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go back to turn iTurn.
|
|
|
|
* We must have: iTurn < getHistory().getSize()
|
|
|
|
* Possible return values:
|
|
|
|
* 0: everything went fine
|
|
|
|
* 1: iTurn is invalid
|
|
|
|
*/
|
|
|
|
int back(unsigned int iTurn);
|
|
|
|
|
2008-09-14 19:56:18 +02:00
|
|
|
/// Shuffle the rack of the current player
|
|
|
|
void shuffleRack();
|
|
|
|
|
|
|
|
/// Return true if the player has played for the current turn
|
|
|
|
// XXX: not very nice API, should be a player property...
|
|
|
|
virtual bool hasPlayed(unsigned int player) const { return player != currPlayer(); }
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Saved games handling
|
|
|
|
***************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Possible formats for the saved games
|
|
|
|
*/
|
|
|
|
enum game_file_format
|
|
|
|
{
|
|
|
|
FILE_FORMAT_STANDARD,
|
|
|
|
FILE_FORMAT_ADVANCED
|
|
|
|
};
|
2006-01-01 20:48:36 +01:00
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
/**
|
2005-02-05 12:14:56 +01:00
|
|
|
* load() returns the loaded game, or NULL if there was a problem
|
2008-01-08 14:52:32 +01:00
|
|
|
* load() does need some more work to be robust enough to
|
2005-02-05 12:14:56 +01:00
|
|
|
* handle "hand written" files
|
2005-03-29 00:07:22 +02:00
|
|
|
*/
|
2005-02-05 12:14:56 +01:00
|
|
|
static Game * load(FILE *fin, const Dictionary &iDic);
|
2006-01-01 20:48:36 +01:00
|
|
|
|
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Save a game to a file
|
2006-01-01 20:48:36 +01:00
|
|
|
* Standard format is used for training games so that it is compatible
|
|
|
|
* with previous versions of Eliot.
|
|
|
|
*
|
2008-01-08 14:52:32 +01:00
|
|
|
* Saving can be forced to advanced format for training games by
|
2006-01-01 20:48:36 +01:00
|
|
|
* setting the last parameter to FILE_FORMAT_ADVANCED
|
|
|
|
*/
|
2008-01-08 14:52:32 +01:00
|
|
|
void save(ostream &out, game_file_format format = FILE_FORMAT_STANDARD) const;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Setting the rack
|
|
|
|
***************/
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-12-26 23:58:58 +01:00
|
|
|
enum set_rack_mode {RACK_ALL, RACK_NEW, RACK_MANUAL};
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
void addPoints(int iPoints) { m_points += iPoints; }
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
void prevTurn();
|
|
|
|
void nextTurn();
|
|
|
|
void firstTurn();
|
|
|
|
void lastTurn();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-22 15:40:25 +01:00
|
|
|
private:
|
2005-03-29 00:07:22 +02:00
|
|
|
/// Variant
|
|
|
|
GameVariant m_variant;
|
|
|
|
|
|
|
|
/// Dictionary currently associated to the game
|
2008-01-08 14:52:32 +01:00
|
|
|
const Dictionary & m_dic;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-03-29 00:07:22 +02:00
|
|
|
/**
|
|
|
|
* History of the game.
|
|
|
|
*/
|
2005-12-26 23:58:58 +01:00
|
|
|
History m_history;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
int m_points;
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
|
|
|
|
// TODO: check what should be private and what should be protected
|
2008-11-22 15:40:25 +01:00
|
|
|
protected:
|
2008-11-23 09:18:03 +01:00
|
|
|
/// All the players, indexed by their ID
|
|
|
|
vector<Player*> m_players;
|
|
|
|
/// ID of the "current" player
|
|
|
|
unsigned int m_currPlayer;
|
|
|
|
|
2008-11-22 15:40:25 +01:00
|
|
|
/// Board
|
|
|
|
Board m_board;
|
|
|
|
|
|
|
|
/// Bag
|
|
|
|
Bag m_bag;
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
vector<TurnCmd *> m_turnCommands;
|
|
|
|
unsigned int m_currTurn;
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
bool m_finished;
|
|
|
|
|
|
|
|
/*********************************************************
|
|
|
|
* Helper functions
|
|
|
|
*********************************************************/
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
void newTurn();
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/**
|
2008-11-22 14:09:28 +01:00
|
|
|
* Complete the given rack randomly.
|
2008-01-08 14:52:32 +01:00
|
|
|
*
|
|
|
|
* Completing a rack randomly is more complex than it seems, because we
|
|
|
|
* must take into account several constraints:
|
|
|
|
* - if iCheck is true, we must ensure that the rack contains a minimum
|
|
|
|
* number of vowels and consonants (2 of each in the 15 first moves of
|
|
|
|
* the game, 1 of each after)
|
|
|
|
* - the game is over if the (real) bag contains only vowels or only
|
|
|
|
* consonants, and in particular if it contains only one letter
|
|
|
|
* - some letters (in particular the joker) can count both as a vowel and
|
|
|
|
* as a consonant (but not at the same time)
|
|
|
|
* - in a joker game, the joker must be present in the rack unless there
|
|
|
|
* is no joker left in the bag. In addition, we must prevent that both
|
|
|
|
* jokers are present in the rack at the same time
|
|
|
|
* - if completing a rack doesn't meet the requirements on the vowels and
|
|
|
|
* consonants, we must reject the rack completely (but only once,
|
|
|
|
* otherwise we have no guarantee that the rejects will stop eventually).
|
|
|
|
* This also means we have to check whether completing the rack with the
|
|
|
|
* requirements is possible...
|
|
|
|
*/
|
2008-11-22 14:09:28 +01:00
|
|
|
PlayedRack helperSetRackRandom(const PlayedRack &iPld,
|
|
|
|
bool iCheck, set_rack_mode mode) const;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the rack for the player p with the given letters
|
|
|
|
* Possible return values:
|
|
|
|
* 0: everything went fine
|
|
|
|
* 1: the bag doesn't have the wanted letters
|
|
|
|
* 2: the rack was checked for vowels/consonants and was not correct
|
|
|
|
*/
|
|
|
|
int helperSetRackManual(unsigned int p, bool iCheck, const wstring &iLetters);
|
2005-03-27 19:30:48 +02:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
void prevPlayer();
|
|
|
|
void nextPlayer();
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the players rack can be obtained from the bag.
|
|
|
|
* Since letters are removed from the bag only when the
|
|
|
|
* round is played we need to check that ALL the racks
|
|
|
|
* are in the bag simultaneously.
|
|
|
|
*
|
|
|
|
* FIXME: since we do not check for all racks it works
|
|
|
|
* for training and duplicate but it won't work for
|
|
|
|
* freegames.
|
|
|
|
*/
|
2005-02-05 12:14:56 +01:00
|
|
|
bool rackInBag(const Rack &iRack, const Bag &iBag) const;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function checks whether it is legal to play the given word at the
|
|
|
|
* given coordinates. If so, the function fills a Round object, also given
|
|
|
|
* as a parameter.
|
|
|
|
* Possible return values: same as the play() method
|
|
|
|
*/
|
2008-11-23 09:18:03 +01:00
|
|
|
int checkPlayedWord(const wstring &iCoord,
|
|
|
|
const wstring &iWord, Round &oRound) const;
|
2006-01-01 20:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* load games from File using the first format.
|
|
|
|
* This format is used for Training games
|
|
|
|
*/
|
|
|
|
static Game* gameLoadFormat_14(FILE *fin, const Dictionary& iDic);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* load games from File using advanced format (since Eliot 1.5)
|
|
|
|
* This format is used for Duplicate, Freegame, ...
|
|
|
|
*/
|
|
|
|
static Game* gameLoadFormat_15(FILE *fin, const Dictionary& iDic);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Training games ares saved using the initial Eliot format
|
|
|
|
*/
|
2006-08-12 00:13:41 +02:00
|
|
|
void gameSaveFormat_14(ostream &out) const;
|
2006-01-01 20:48:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Advanced game file format output
|
|
|
|
*/
|
2006-08-12 00:13:41 +02:00
|
|
|
void gameSaveFormat_15(ostream &out) const;
|
2006-01-01 20:48:36 +01:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _GAME_H_ */
|
2006-01-01 20:48:36 +01:00
|
|
|
|