trikitrok avatar trikitrok committed 22419ee Draft

Integrated Player with Game. Eliminated points map from Game. Renamed some methods. Extracted a whoWon() method

Comments (0)

Files changed (4)

 #include "Game.h"
 
 #include <iostream>
+#include "Player.h"
 using namespace std;
 
-Game::Game(const std::string & player1, const std::string & player2)
+Game::Game(Player * player1, Player * player2)
 {
 	this->player1 = player1;
 	this->player2 = player2;
 
-	points[player1] = 0;
-	points[player2] = 0;
-
 	scores.push_back("Love");
 	scores.push_back("Fifteen");
 	scores.push_back("Thirty");
 }
 
 Game::~Game()
-{}
+{
+	delete player1;
+	delete player2;
+}
 
-void Game::pointTo(const std::string & player)
+void Game::pointTo(Player * player)
 {
-	if(not isOver())
+	if(not gameStateIsGameOver())
 	{
-		points[player]++;
+		player->winPoint();
 
 		updateState();
 	}
 
 void Game::updateState()
 {
-	if(playersAreTightToFortyOrMore())
+	if(thereIsDeuce())
 	{
 		state = DEUCE;
 	}
-	else if(thereIsDeuce())
+	else if(gameStateIsDeuce())
 	{
 		state = ADVANTAGE;
 	}
-	else if(aPlayerHasWon())
+	else if(isGameOver())
 	{
 		state = GAME_OVER;
 	}
 
 std::string Game::score() const
 {
-	if(thereIsDeuce())
+	if(gameStateIsDeuce())
 	{
 		return "Deuce";
 	}
-	else if(isOver())
+	else if(gameStateIsGameOver())
 	{
-		return theWinner();
+		return victoryMessage();
 	}
-	else if(thereIsAdvantage())
+	else if(gameStateIsAdvantage())
 	{
-		return theOneWithAdvantage();
+		return advantageMessage();
 	}
 
 	return computeScore(player1) + "-" + computeScore(player2);
 }
 
-std::string Game::theWinner() const
+std::string Game::victoryMessage() const
+{
+	return whoWon()->getName() + " won!";
+}
+
+Player * Game::whoWon() const
 {
 	if(playerWon(player1))
-		return player1 + " won!";
-	else
-		return player2 + " won!";
+		return player1;
+
+	return player2;
 }
 
-std::string Game::theOneWithAdvantage() const
+std::string Game::advantageMessage() const
 {
-	std::string score = "Advantage for ";
+	std::string message = "Advantage for ";
 
-	if(getPlayerPoints(player1) > getPlayerPoints(player2))
-	{
-		return score + player1;
-	}
-	else
-	{
-		return score + player2;
-	}
+	if(player1->getPoints() > player2->getPoints())
+		return message + player1->getName();
+
+	return message + player2->getName();
 }
 
-std::string Game::computeScore(const std::string & player) const
+std::string Game::computeScore(Player * player) const
 {
-	return scores[getPlayerPoints(player)];
+	return scores[player->getPoints()];
 }
 
-int Game::getPlayerPoints(const std::string & player) const
+bool Game::thereIsDeuce() const
 {
-	std::map<std::string, int>::const_iterator it = points.find(player);
-	return it->second;
-}
-
-bool Game::playersAreTightToFortyOrMore() const
-{
-	return playersAreTight() and getPlayerPoints(player1) >= 3;
+	return playersAreTight() and player1->getPoints() >= 3;
 }
 
 bool Game::playersAreTight() const
 {
-	return getPlayerPoints(player1) == getPlayerPoints(player2);
+	return player1->getPoints() == player2->getPoints();
 }
 
-bool Game::isOver() const
+bool Game::gameStateIsGameOver() const
 {
 	return state == GAME_OVER;
 }
 
-bool Game::aPlayerHasWon() const
+bool Game::isGameOver() const
 {
 	return playerWon(player1) or playerWon(player2);
 }
 
-bool Game::playerWon(const std::string & player) const
+bool Game::playerWon(Player * player) const
 {
 	if(state == INITIAL)
-		return playerOverForty(player);
-	else
-		return getPlayerPoints(player) > getPlayerPoints(getOtherPlayer(player));
+		return player->wonMoreThanThreePoints();
+
+	return player->getPoints() > getOtherPlayer(player)->getPoints();
 }
 
-std::string Game::getOtherPlayer(const std::string & player) const
+Player * Game::getOtherPlayer(Player * player) const
 {
 	if(player == player1)
 		return player2;
 	return player1;
 }
 
-bool Game::playerOverForty(const std::string & player) const
-{
-	return getPlayerPoints(player) > 3;
-}
-
-bool Game::thereIsDeuce() const
+bool Game::gameStateIsDeuce() const
 {
 	return state == DEUCE;
 }
 
-bool Game::thereIsAdvantage() const
+bool Game::gameStateIsAdvantage() const
 {
 	return state == ADVANTAGE;
 }
 #include<map>
 #include<vector>
 
+class Player;
+
 enum GameState {
 	INITIAL = 1,
 	DEUCE,
 
 class Game {
 	public:
-		Game(const std::string & player1, const std::string & player2);
+		Game(Player * player1, Player * player2);
 		virtual ~Game();
 
-		void pointTo(const std::string & player);
+		void pointTo(Player * player);
 
 		std::string score() const;
 
-		bool isOver() const;
+		bool gameStateIsGameOver() const;
 
 	private:
-		std::string player1;
-		std::string player2;
-		std::map< std::string, int > points;
+		Player * player1;
+		Player * player2;
 		std::vector<std::string> scores;
 
 		GameState state;
 
 		void updateState();
 
-		bool playersAreTightToFortyOrMore() const;
+		bool thereIsDeuce() const;
 		bool playersAreTight() const;
-		bool aPlayerHasWon() const;
+		bool isGameOver() const;
 
-		bool thereIsDeuce() const;
-		bool thereIsAdvantage() const;
+		bool gameStateIsDeuce() const;
+		bool gameStateIsAdvantage() const;
 
-		std::string theWinner() const;
-		std::string theOneWithAdvantage() const;
+		Player * whoWon() const;
 
-		std::string computeScore(const std::string & player) const;
+		std::string victoryMessage() const;
+		std::string advantageMessage() const;
 
-		int getPlayerPoints(const std::string & player) const;
+		std::string computeScore(Player * player) const;
 
-		bool playerOverForty(const std::string & player) const;
+		Player * getOtherPlayer(Player * player) const;
 
-		std::string getOtherPlayer(const std::string & player) const;
-
-		bool playerWon(const std::string & player) const;
+		bool playerWon(Player * player) const;
 };
 
 #endif /* GAME_H_ */

src/test/GameTests.cpp

 #include <CppUTest/TestHarness.h>
 
 #include "../Game.h"
+#include "../Player.h"
 
-namespace Players
+namespace PlayersNames
 {
-	const std::string PLAYER_ONE = "koko";
-	const std::string PLAYER_TWO = "triki";
+	const std::string PLAYER_ONE_NAME = "koko";
+	const std::string PLAYER_TWO_NAME = "triki";
 }
 
 TEST_GROUP(GameTests)
 {
 	Game * game;
+	Player * playerOne;
+	Player * playerTwo;
 
 	void setup() {
-		game = new Game(Players::PLAYER_ONE, Players::PLAYER_TWO);
+
+		playerOne = new Player(PlayersNames::PLAYER_ONE_NAME);
+		playerTwo = new Player(PlayersNames::PLAYER_TWO_NAME);
+		game = new Game(playerOne, playerTwo);
 	}
 
 	void teardown() {
 		delete game;
 	}
 
-	void makePlayerWinSeveralPoints(const std::string & player, unsigned int numPoints)
+	void makePlayerWinSeveralPoints(Player * player, unsigned int numPoints)
 	{
 		for(unsigned int i=0; i < numPoints; ++i)
 		{
 
 	void gameGetToDeuce()
 	{
-		makePlayerWinSeveralPoints(Players::PLAYER_ONE, 3);
-		makePlayerWinSeveralPoints(Players::PLAYER_TWO, 3);
-
+		makePlayerWinSeveralPoints(playerOne, 3);
+		makePlayerWinSeveralPoints(playerTwo, 3);
 	}
 
-	void makePlayerWin(const std::string & player)
+	void makePlayerWin(Player * player)
 	{
 		makePlayerWinSeveralPoints(player, 4);
 	}
 
 TEST(GameTests, playersCanWinSeveralPoints)
 {
-	game->pointTo(Players::PLAYER_ONE);
+	game->pointTo(playerOne);
 	CHECK("Fifteen-Love" == game->score());
 
-	game->pointTo(Players::PLAYER_ONE);
+	game->pointTo(playerOne);
 	CHECK("Thirty-Love" == game->score());
 
-	game->pointTo(Players::PLAYER_ONE);
+	game->pointTo(playerOne);
 	CHECK("Forty-Love" == game->score());
 
-	game->pointTo(Players::PLAYER_TWO);
+	game->pointTo(playerTwo);
 	CHECK("Forty-Fifteen" == game->score());
 
-	game->pointTo(Players::PLAYER_TWO);
+	game->pointTo(playerTwo);
 	CHECK("Forty-Thirty" == game->score());
 }
 
 TEST(GameTests, gameIsOverAfterPlayerOneWinsFirstForPoints)
 {
-	makePlayerWin(Players::PLAYER_ONE);
-	CHECK(game->isOver());
+	makePlayerWin(playerOne);
+	CHECK(game->gameStateIsGameOver());
 }
 
 TEST(GameTests, gameIsOverAfterPlayerTwoWinsFirstForPoints)
 {
-	makePlayerWin(Players::PLAYER_TWO);
-	CHECK(game->isOver());
+	makePlayerWin(playerTwo);
+	CHECK(game->gameStateIsGameOver());
 }
 
 TEST(GameTests, scoreDoesNotChangeAfterAPlayerWins)
 {
-	makePlayerWin(Players::PLAYER_ONE);
+	makePlayerWin(playerOne);
 
-	std::string PLAYER_ONE_WON = Players::PLAYER_ONE + " won!";
+	std::string PLAYER_ONE_WON = PlayersNames::PLAYER_ONE_NAME + " won!";
 
-	game->pointTo(Players::PLAYER_ONE);
+	game->pointTo(playerOne);
 	CHECK(PLAYER_ONE_WON == game->score());
 
-	game->pointTo(Players::PLAYER_TWO);
+	game->pointTo(playerTwo);
 	CHECK(PLAYER_ONE_WON == game->score());
 }
 
 TEST(GameTests, gameIsNotOverIfNoOneGetsFirstToForty)
 {
-	CHECK(not game->isOver());
-	makePlayerWinSeveralPoints(Players::PLAYER_ONE, 3);
-	CHECK(not game->isOver());
+	CHECK(not game->gameStateIsGameOver());
+	makePlayerWinSeveralPoints(playerOne, 3);
+	makePlayerWinSeveralPoints(playerTwo, 3);
+	CHECK(not game->gameStateIsGameOver());
 }
 
 TEST(GameTests, thereIsDeuce)
 TEST(GameTests, gameIsNotOverWhenThereIsDeuce)
 {
 	gameGetToDeuce();
-	CHECK(not game->isOver());
+	CHECK(not game->gameStateIsGameOver());
 }
 
 TEST(GameTests, thereIsAdvantage)
 {
 	gameGetToDeuce();
-	game->pointTo(Players::PLAYER_ONE);
+	game->pointTo(playerOne);
 
 	CHECK("Advantage for koko" == game->score());
 }
 TEST(GameTests, backToDeuce)
 {
 	gameGetToDeuce();
-	game->pointTo(Players::PLAYER_ONE);
-	game->pointTo(Players::PLAYER_TWO);
+	game->pointTo(playerOne);
+	game->pointTo(playerTwo);
 	CHECK("Deuce" == game->score());
 }
 
 TEST(GameTests, thePlayerWithAdvantageWins)
 {
 	gameGetToDeuce();
-	game->pointTo(Players::PLAYER_ONE); // Advantage for player one
-	game->pointTo(Players::PLAYER_TWO); // Deuce again
-	game->pointTo(Players::PLAYER_TWO); // Advantage for player two
-	game->pointTo(Players::PLAYER_TWO);
+	game->pointTo(playerOne); // Advantage for player one
+	game->pointTo(playerTwo); // Deuce again
+	game->pointTo(playerTwo); // Advantage for player two
+	game->pointTo(playerTwo);
 
-	std::string PLAYER_TWO_WON = Players::PLAYER_TWO + " won!";
+	std::string PLAYER_TWO_WON = PlayersNames::PLAYER_TWO_NAME + " won!";
 
 	CHECK(PLAYER_TWO_WON == game->score());
 }

src/test/PlayerTests.cpp

 
 	CHECK(player->wonMoreThanThreePoints());
 }
-
-
-
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.