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) 2005-2012 Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: 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
|
|
|
*****************************************************************************/
|
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
#ifndef DUPLICATE_H_
|
|
|
|
#define DUPLICATE_H_
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
#include "game.h"
|
2012-01-17 17:29:19 +01:00
|
|
|
#include "move.h"
|
2011-01-30 00:47:20 +01:00
|
|
|
#include "logging.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
class Player;
|
2012-03-11 00:24:21 +01:00
|
|
|
class Move;
|
2012-12-05 22:05:31 +01:00
|
|
|
class PlayerEventCmd;
|
2008-11-23 09:18:03 +01:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
using std::string;
|
2006-01-22 13:23:52 +01:00
|
|
|
using std::wstring;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
|
2005-03-27 19:30:48 +02:00
|
|
|
/**
|
|
|
|
* This class handles the logic specific to a duplicate game.
|
|
|
|
* The trick in this mode is that the players will not necessarily play they
|
|
|
|
* word always in the same order, so we need to implement a "synchronization":
|
|
|
|
* - when a human player wants to play a word, he plays it, and its score
|
|
|
|
* and rack are updated. He cannot change his word afterwards.
|
|
|
|
* - if there is still a human player who has not played for the current
|
|
|
|
* turn, we wait for him
|
2008-01-08 14:52:32 +01:00
|
|
|
* - if all the human players have played, it's the turn of the AI players
|
2005-03-27 19:30:48 +02:00
|
|
|
* (currently handled in a loop, but we could imagine that they are running
|
|
|
|
* in their own thread).
|
|
|
|
* - once all the players have played, we can really end the turn:
|
|
|
|
* the best word is played on the board, the history of the game is
|
|
|
|
* updated, and the new rack is chosen.
|
|
|
|
*
|
|
|
|
* AI players play after human ones, because with the current implementation
|
|
|
|
* of the interfaces it is too easy for a player to see the rack of other
|
|
|
|
* players, and in particular a human player could take advantage of that to
|
|
|
|
* have more clues about the best word.
|
|
|
|
* TODO: better isolation of the players...
|
|
|
|
*/
|
2005-02-05 12:14:56 +01:00
|
|
|
class Duplicate: public Game
|
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
DEFINE_LOGGER();
|
2005-02-24 09:06:24 +01:00
|
|
|
friend class GameFactory;
|
2009-11-29 17:01:31 +01:00
|
|
|
friend class MarkPlayedCmd;
|
2012-01-17 17:29:19 +01:00
|
|
|
friend class MasterMoveCmd;
|
2005-02-05 12:14:56 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
/*************************
|
|
|
|
* Game handling
|
|
|
|
*************************/
|
2008-01-08 14:52:32 +01:00
|
|
|
/**
|
|
|
|
* In Duplicate mode, the start() method starts a new turn, and is
|
|
|
|
* automatically called when the previous turn is finished.
|
|
|
|
*
|
|
|
|
* Pre-requisite: all the players must have the same rack when this
|
|
|
|
* method is called
|
|
|
|
*/
|
2008-11-23 18:04:40 +01:00
|
|
|
virtual void start();
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2011-01-30 01:23:45 +01:00
|
|
|
virtual bool isFinished() const;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/**
|
|
|
|
* See description of Game::play() for the possible return values.
|
|
|
|
* Note that if the "duplicate-reject-invalid" setting is set to false
|
|
|
|
* the method always returns 0 (the player will have 0 for this turn)
|
|
|
|
*/
|
2006-01-22 13:23:52 +01:00
|
|
|
virtual int play(const wstring &iCoord, const wstring &iWord);
|
2005-03-29 08:56:06 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/**
|
|
|
|
* Set the current player, given its ID.
|
2009-01-24 18:11:07 +01:00
|
|
|
* The given player ID must correspond to a human player, who did not
|
2008-01-08 14:52:32 +01:00
|
|
|
* play yet for this turn.
|
2009-01-24 18:11:07 +01:00
|
|
|
* @param p: ID of the player
|
|
|
|
* @exception GameException: Thrown if the player is not human or if
|
|
|
|
* he has already played
|
2008-01-08 14:52:32 +01:00
|
|
|
*/
|
2009-01-24 18:11:07 +01:00
|
|
|
void setPlayer(unsigned int p);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2012-01-17 17:29:19 +01:00
|
|
|
/**
|
|
|
|
* Set the master move.
|
|
|
|
* Two move types are possible: VALID_ROUND and NO_MOVE.
|
|
|
|
* Setting a master move to NO_MOVE means reseting it.
|
|
|
|
*/
|
|
|
|
void setMasterMove(const Move &iMove);
|
|
|
|
|
|
|
|
const Move &getMasterMove() const { return m_masterMove; }
|
|
|
|
|
2008-09-14 19:56:18 +02:00
|
|
|
/// Return true if the player has played for the current turn
|
2008-11-23 18:06:44 +01:00
|
|
|
virtual bool hasPlayed(unsigned int iPlayerId) const;
|
2008-09-14 19:56:18 +02:00
|
|
|
|
2012-03-05 01:27:56 +01:00
|
|
|
protected:
|
|
|
|
// Protected constructor to force using the GameFactory class
|
2012-12-30 16:14:13 +01:00
|
|
|
Duplicate(const GameParams &iParams, const Game *iMasterGame);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
/// Record a player move
|
2012-03-14 20:37:21 +01:00
|
|
|
void recordPlayerMove(Player &ioPlayer, const Move &iMove);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function does not terminate the turn itself, but performs some
|
|
|
|
* checks to know whether or not it should be terminated (with a call to
|
|
|
|
* endTurn()).
|
|
|
|
*
|
|
|
|
* For the turn to be terminated, all the players must have played.
|
|
|
|
* Since the AI players play after the human players, we check whether
|
|
|
|
* one of the human players has not played yet:
|
|
|
|
* - if so, we have nothing to do (we are waiting for him/her)
|
|
|
|
* - if not (all human players have played), the AI players can play,
|
|
|
|
* and we finish the turn.
|
|
|
|
*/
|
|
|
|
void tryEndTurn();
|
|
|
|
|
2012-04-08 00:27:48 +02:00
|
|
|
/// Finish the game
|
|
|
|
void endGame();
|
|
|
|
|
2012-12-05 22:05:31 +01:00
|
|
|
/**
|
|
|
|
* Return the first player event of the given type (for the given player)
|
|
|
|
* in the commands history for the current turn.
|
|
|
|
* If none is found, return 0.
|
|
|
|
*/
|
|
|
|
const PlayerEventCmd * getPlayerEvent(unsigned iPlayerId,
|
|
|
|
int iEventType) const;
|
|
|
|
|
2012-12-05 23:02:12 +01:00
|
|
|
/**
|
|
|
|
* Automatically set the solo for the current turn.
|
|
|
|
* First, all the existing solos (there could be several, in arbitration
|
|
|
|
* mode with a crazy arbitrator...) are removed. Then, a solo is given to
|
|
|
|
* the player deserving it, if any.
|
|
|
|
* Note that the minimum number of players specified in the preferences
|
|
|
|
* must be reached for the solo to be applicable.
|
|
|
|
*/
|
|
|
|
void setSoloAuto(unsigned int minNbPlayers, int iSoloValue);
|
|
|
|
|
2012-03-05 01:27:56 +01:00
|
|
|
private: // Used by friend classes
|
|
|
|
void innerSetMasterMove(const Move &iMove);
|
2012-10-04 23:15:35 +02:00
|
|
|
bool isArbitrationGame() const;
|
2012-03-05 01:27:56 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Make the AI player whose ID is p play its turn
|
|
|
|
void playAI(unsigned int p);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the player who scored the most (with a valid move) at this turn.
|
|
|
|
* If several players have the same score, one is returned arbitrarily.
|
|
|
|
* If nobody played a valid move, the method returns a null pointer.
|
|
|
|
*/
|
|
|
|
Player * findBestPlayer() const;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/**
|
|
|
|
* This function really changes the turn, i.e. the best word is played,
|
|
|
|
* the game history is updated, a "solo" bonus is given if needed, and
|
|
|
|
* all racks are made equal to the one of the player who played the
|
|
|
|
* best move.
|
|
|
|
* We suppose here that all the players have finished to play for this
|
|
|
|
* turn (this should have been checked by tryEndturn())
|
|
|
|
*/
|
|
|
|
void endTurn();
|
|
|
|
|
2012-01-17 17:29:19 +01:00
|
|
|
/**
|
|
|
|
* Master move, i.e. the move that will be played on the board
|
|
|
|
* at this turn (even if no player actually played it).
|
|
|
|
* This is particularly useful for arbitration mode, but could also
|
|
|
|
* be used in normal Duplicate games.
|
|
|
|
*/
|
|
|
|
Move m_masterMove;
|
2005-02-05 12:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _DUPLICATE_H_ */
|
2006-01-01 20:49:35 +01:00
|
|
|
|