2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
* Copyright (C) 1999-2012 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
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
#ifndef GAME_H_
|
|
|
|
#define GAME_H_
|
2004-04-08 11:43:06 +02:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-08-27 19:21:26 +02:00
|
|
|
#include "game_params.h"
|
2011-01-30 00:47:20 +01:00
|
|
|
#include "logging.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "bag.h"
|
|
|
|
#include "board.h"
|
2005-12-26 23:58:58 +01:00
|
|
|
#include "history.h"
|
2008-11-23 17:55:28 +01:00
|
|
|
#include "navigation.h"
|
2008-11-23 18:07:42 +01:00
|
|
|
#include "command.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;
|
2012-10-06 01:57:39 +02:00
|
|
|
class TurnData;
|
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
|
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
DEFINE_LOGGER();
|
2005-02-05 12:14:56 +01:00
|
|
|
public:
|
|
|
|
virtual ~Game();
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Game type
|
|
|
|
***************/
|
|
|
|
|
2011-08-28 18:20:00 +02:00
|
|
|
GameParams::GameMode getMode() const { return m_params.getMode(); }
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/***************
|
|
|
|
* Various getters
|
|
|
|
***************/
|
|
|
|
|
2011-08-27 19:21:26 +02:00
|
|
|
/// Get the game characteristics
|
|
|
|
const GameParams & getParams() const { return m_params; }
|
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
bool hasMasterGame() const { return m_masterGame != NULL; }
|
|
|
|
|
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
|
|
|
*/
|
2011-08-28 18:44:34 +02:00
|
|
|
const Dictionary & getDic() const { return m_params.getDic(); }
|
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
|
|
|
|
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-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.
|
|
|
|
***************/
|
|
|
|
|
2012-03-19 12:39:13 +01:00
|
|
|
Player & accessPlayer(unsigned int iNum);
|
2008-01-08 14:52:32 +01:00
|
|
|
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-11-23 18:04:40 +01:00
|
|
|
virtual void start() = 0;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2011-01-30 01:23:45 +01:00
|
|
|
/**
|
|
|
|
* Indicate whether we reached the end of the game.
|
|
|
|
* This should be checked regularly.
|
|
|
|
* XXX: using a signal would be nice here...
|
|
|
|
*/
|
|
|
|
virtual bool isFinished() const = 0;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
2009-03-18 19:54:41 +01:00
|
|
|
* 10: first word not horizontal (can only happen in duplicate mode)
|
2008-01-08 14:52:32 +01:00
|
|
|
* 11: first word not covering the H8 square
|
2008-09-13 23:32:45 +02:00
|
|
|
* 12: word going out of the board
|
2011-08-27 19:21:26 +02:00
|
|
|
* 13: too many letters played from the rack
|
2008-01-08 14:52:32 +01:00
|
|
|
*/
|
|
|
|
virtual int play(const wstring &iCoord, const wstring &iWord) = 0;
|
|
|
|
|
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
|
|
|
/***************
|
|
|
|
* Setting the rack
|
|
|
|
***************/
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-01-11 15:20:35 +01:00
|
|
|
enum set_rack_mode {RACK_ALL, RACK_NEW};
|
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 17:55:28 +01:00
|
|
|
const Navigation & getNavigation() const { return m_navigation; }
|
|
|
|
Navigation & accessNavigation() { return m_navigation; }
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
/**
|
|
|
|
* This function checks whether it is legal to play the given word at the
|
2012-12-23 19:31:03 +01:00
|
|
|
* given coordinates. If so, the function fills a Move object, also given
|
2009-11-29 17:01:31 +01:00
|
|
|
* as a parameter.
|
|
|
|
* Possible return values: same as the play() method
|
|
|
|
* If checkRack is false, the return value 4 is impossible to get
|
|
|
|
* (no check is done on the rack letters).
|
|
|
|
*/
|
|
|
|
int checkPlayedWord(const wstring &iCoord,
|
|
|
|
const wstring &iWord,
|
2012-12-23 19:31:03 +01:00
|
|
|
Move &oMove,
|
2012-12-24 20:32:47 +01:00
|
|
|
bool checkRack = true,
|
|
|
|
bool checkWordAndJunction = true) const;
|
2009-11-29 17:01:31 +01:00
|
|
|
|
2008-11-22 15:40:25 +01:00
|
|
|
private:
|
2011-08-27 19:21:26 +02:00
|
|
|
/// Game characteristics
|
|
|
|
GameParams m_params;
|
2005-03-29 00:07:22 +02:00
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
/// Master game (can be NULL)
|
|
|
|
const Game *m_masterGame;
|
|
|
|
|
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
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
Navigation m_navigation;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
int m_points;
|
2008-11-23 09:18:03 +01:00
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
|
2008-11-23 18:07:42 +01:00
|
|
|
/// Change the player who is supposed to play
|
|
|
|
void setCurrentPlayer(unsigned int iPlayerId) { m_currPlayer = iPlayerId; }
|
|
|
|
|
|
|
|
/// Command used to keep track of the current player changes
|
|
|
|
class CurrentPlayerCmd: public Command
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CurrentPlayerCmd(Game &ioGame,
|
|
|
|
unsigned int iPlayerId);
|
|
|
|
|
|
|
|
virtual wstring toString() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void doExecute();
|
|
|
|
virtual void doUndo();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Game &m_game;
|
|
|
|
unsigned int m_newPlayerId;
|
|
|
|
unsigned int m_oldPlayerId;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2013-01-07 18:06:00 +01:00
|
|
|
/**
|
|
|
|
* Protected constructor.
|
|
|
|
* The iMasterGame parameter is optional (i.e. it can be NULL).
|
|
|
|
* This game takes ownership of the master game, if one is provided.
|
|
|
|
*/
|
|
|
|
Game(const GameParams &iParams, const Game *iMasterGame);
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
/*********************************************************
|
|
|
|
* Helper functions
|
|
|
|
*********************************************************/
|
|
|
|
|
2012-10-05 01:19:32 +02:00
|
|
|
/**
|
|
|
|
* Check if it is possible to draw a new rack from the bag,
|
|
|
|
* after we put back the contents of iPld.
|
|
|
|
* @param iCheck if true, the number of consonants and vowels will
|
|
|
|
* be checked.
|
|
|
|
* @param oReason if the pointer is valid, and the method returns false,
|
|
|
|
* oReason will point to the error code. Possible values:
|
|
|
|
* 1: The bag is empty
|
|
|
|
* 2: Not enough vowels or consonants to complete the rack
|
|
|
|
* @return true if a rack can be drawn, false otherwise
|
|
|
|
*/
|
|
|
|
bool canDrawRack(const PlayedRack &iPld, bool iCheck, int *oReason = NULL) const;
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2009-01-24 11:28:20 +01:00
|
|
|
* Return a rack for the given letters, after performing some checks.
|
|
|
|
* The '+' and '-' signs are accepted in the letters but ignored.
|
2008-01-08 14:52:32 +01:00
|
|
|
*/
|
2009-01-24 11:28:20 +01:00
|
|
|
PlayedRack helperSetRackManual(bool iCheck, const wstring &iLetters) const;
|
2005-03-27 19:30:48 +02:00
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
/**
|
|
|
|
* Helper method to retrieve the rack for the current turn
|
|
|
|
* in the master game. Should only be called when hasMasterGame()
|
|
|
|
* returns true.
|
|
|
|
*/
|
|
|
|
PlayedRack getRackFromMasterGame() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to retrieve the move played for the current turn
|
|
|
|
* in the master game. Should only be called when hasMasterGame()
|
|
|
|
* returns true.
|
|
|
|
*/
|
|
|
|
Move getMoveFromMasterGame() const;
|
|
|
|
|
2012-12-25 18:25:22 +01:00
|
|
|
/**
|
|
|
|
* Helper function to set the game rack and the players rack at the same time.
|
|
|
|
* Shouldn't be used in free game mode.
|
|
|
|
*/
|
|
|
|
void setGameAndPlayersRack(const PlayedRack &iRack);
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
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
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _GAME_H_ */
|
2006-01-01 20:48:36 +01:00
|
|
|
|