New command to handle the player points, useful to handle the end of a free game

This commit is contained in:
Olivier Teulière 2008-11-30 21:10:25 +00:00
parent 687a31bee8
commit 5b1e228fb5
4 changed files with 112 additions and 2 deletions

View file

@ -33,6 +33,7 @@ libgame_a_SOURCES= \
turn.cpp turn.h \ turn.cpp turn.h \
history.cpp history.h \ history.cpp history.h \
player.cpp player.h \ player.cpp player.h \
player_points_cmd.cpp player_points_cmd.h \
player_move_cmd.cpp player_move_cmd.h \ player_move_cmd.cpp player_move_cmd.h \
player_rack_cmd.cpp player_rack_cmd.h \ player_rack_cmd.cpp player_rack_cmd.h \
ai_player.h \ ai_player.h \

View file

@ -33,6 +33,7 @@
#include "pldrack.h" #include "pldrack.h"
#include "results.h" #include "results.h"
#include "player.h" #include "player.h"
#include "player_points_cmd.h"
#include "player_move_cmd.h" #include "player_move_cmd.h"
#include "player_rack_cmd.h" #include "player_rack_cmd.h"
#include "game_move_cmd.h" #include "game_move_cmd.h"
@ -209,11 +210,17 @@ void FreeGame::endGame()
{ {
const PlayedRack &pld = m_players[i]->getCurrentRack(); const PlayedRack &pld = m_players[i]->getCurrentRack();
pld.getAllTiles(tiles); pld.getAllTiles(tiles);
int points = 0;
BOOST_FOREACH(const Tile &tile, tiles) BOOST_FOREACH(const Tile &tile, tiles)
{ {
m_players[i]->addPoints(- tile.getPoints()); points += tile.getPoints();
m_players[m_currPlayer]->addPoints(tile.getPoints());
} }
// Add the points to the current player...
Command *pCmd = new PlayerPointsCmd(*m_players[m_currPlayer], points);
accessNavigation().addAndExecute(pCmd);
// ... and remove them from the other player
Command *pCmd2 = new PlayerPointsCmd(*m_players[i], -points);
accessNavigation().addAndExecute(pCmd2);
} }
} }

View file

@ -0,0 +1,52 @@
/*******************************************************************
* Eliot
* Copyright (C) 2008 Olivier Teulière
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
#include <sstream>
#include "player_points_cmd.h"
#include "player.h"
PlayerPointsCmd::PlayerPointsCmd(Player &ioPlayer, int iPoints)
: m_player(ioPlayer), m_points(iPoints)
{
}
void PlayerPointsCmd::doExecute()
{
m_player.addPoints(m_points);
}
void PlayerPointsCmd::doUndo()
{
m_player.addPoints(-m_points);
}
wstring PlayerPointsCmd::toString() const
{
wostringstream oss;
oss << L"PlayerPointsCmd (player " << m_player.getId() << L"): "
<< "adding " << m_points << " points";
return oss.str();
}

50
game/player_points_cmd.h Normal file
View file

@ -0,0 +1,50 @@
/*******************************************************************
* Eliot
* Copyright (C) 2008 Olivier Teulière
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
#ifndef _PLAYER_POINTS_CMD_H
#define _PLAYER_POINTS_CMD_H
#include "command.h"
class Player;
/**
* This class implements the Command design pattern.
* It encapsulates the logic to add points to a player score.
*/
class PlayerPointsCmd: public Command
{
public:
PlayerPointsCmd(Player &ioPlayer, int iPoints);
virtual wstring toString() const;
protected:
virtual void doExecute();
virtual void doUndo();
private:
Player &m_player;
int m_points;
};
#endif