From 5b1e228fb592989dce667dd4db12b023f4fc390b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 30 Nov 2008 21:10:25 +0000 Subject: [PATCH] New command to handle the player points, useful to handle the end of a free game --- game/Makefile.am | 1 + game/freegame.cpp | 11 ++++++-- game/player_points_cmd.cpp | 52 ++++++++++++++++++++++++++++++++++++++ game/player_points_cmd.h | 50 ++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 game/player_points_cmd.cpp create mode 100644 game/player_points_cmd.h diff --git a/game/Makefile.am b/game/Makefile.am index f3db483..017d044 100644 --- a/game/Makefile.am +++ b/game/Makefile.am @@ -33,6 +33,7 @@ libgame_a_SOURCES= \ turn.cpp turn.h \ history.cpp history.h \ player.cpp player.h \ + player_points_cmd.cpp player_points_cmd.h \ player_move_cmd.cpp player_move_cmd.h \ player_rack_cmd.cpp player_rack_cmd.h \ ai_player.h \ diff --git a/game/freegame.cpp b/game/freegame.cpp index d03ac5e..a130bbc 100644 --- a/game/freegame.cpp +++ b/game/freegame.cpp @@ -33,6 +33,7 @@ #include "pldrack.h" #include "results.h" #include "player.h" +#include "player_points_cmd.h" #include "player_move_cmd.h" #include "player_rack_cmd.h" #include "game_move_cmd.h" @@ -209,11 +210,17 @@ void FreeGame::endGame() { const PlayedRack &pld = m_players[i]->getCurrentRack(); pld.getAllTiles(tiles); + int points = 0; BOOST_FOREACH(const Tile &tile, tiles) { - m_players[i]->addPoints(- tile.getPoints()); - m_players[m_currPlayer]->addPoints(tile.getPoints()); + points += 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); } } diff --git a/game/player_points_cmd.cpp b/game/player_points_cmd.cpp new file mode 100644 index 0000000..fe0be21 --- /dev/null +++ b/game/player_points_cmd.cpp @@ -0,0 +1,52 @@ +/******************************************************************* + * Eliot + * Copyright (C) 2008 Olivier Teulière + * Authors: Olivier Teulière + * + * 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 + +#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(); +} + diff --git a/game/player_points_cmd.h b/game/player_points_cmd.h new file mode 100644 index 0000000..e96c966 --- /dev/null +++ b/game/player_points_cmd.h @@ -0,0 +1,50 @@ +/******************************************************************* + * Eliot + * Copyright (C) 2008 Olivier Teulière + * Authors: Olivier Teulière + * + * 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 +