Commits

trikitrok  committed d5d5067 Draft

Refactored code to remove some duplication

  • Participants
  • Parent commits 3b530be

Comments (0)

Files changed (2)

File src/Game.cpp

 	}
 }
 
-std::string Game::score()
+std::string Game::score() const
 {
 	if(thereIsDeuce())
 	{
 	return computeScore(player1) + "-" + computeScore(player2);
 }
 
-std::string Game::theWinner()
+std::string Game::theWinner() const
 {
-	if(playerOneHasWon())
+	if(playerWon(player1))
 		return player1 + " won!";
 	else
 		return player2 + " won!";
 }
 
-std::string Game::theOneWithAdvantage()
+std::string Game::theOneWithAdvantage() const
 {
 	std::string score = "Advantage for ";
 
-	if(points[player1] > points[player2])
+	if(getPlayerPoints(player1) > getPlayerPoints(player2))
 	{
 		return score + player1;
 	}
 	}
 }
 
-std::string Game::computeScore(const std::string & player)
+std::string Game::computeScore(const std::string & player) const
 {
-	return scores[points[player]];
+	return scores[getPlayerPoints(player)];
 }
 
-bool Game::playersAreTightToFortyOrMore()
+int Game::getPlayerPoints(const std::string & player) const
 {
-	return playersAreTight() and points[player1] >= 3;
+	std::map<std::string, int>::const_iterator it = points.find(player);
+	return it->second;
 }
 
-bool Game::playersAreTight()
+bool Game::playersAreTightToFortyOrMore() const
 {
-	return points[player1] == points[player2];
+	return playersAreTight() and getPlayerPoints(player1) >= 3;
 }
 
-bool Game::isOver()
+bool Game::playersAreTight() const
+{
+	return getPlayerPoints(player1) == getPlayerPoints(player2);
+}
+
+bool Game::isOver() const
 {
 	return state == GAME_OVER;
 }
 
-bool Game::aPlayerHasWon()
+bool Game::aPlayerHasWon() const
 {
-	return playerOneHasWon() or playerTwoHasWon();
+	return playerWon(player1) or playerWon(player2);
 }
 
-bool Game::playerOneHasWon()
+bool Game::playerWon(const std::string & player) const
 {
 	if(state == INITIAL)
-		return playerOneIsOverForty();
+		return playerOverForty(player);
 	else
-		return points[player1] > points[player2];
+		return getPlayerPoints(player) > getPlayerPoints(getOtherPlayer(player));
 }
 
-bool Game::playerOneIsOverForty()
+std::string Game::getOtherPlayer(const std::string & player) const
 {
-	return points[player1] > 3;
+	if(player == player1)
+		return player2;
+
+	return player1;
 }
 
-bool Game::playerTwoIsOverForty()
+bool Game::playerOverForty(const std::string & player) const
 {
-	return points[player2] > 3;
+	return getPlayerPoints(player) > 3;
 }
 
-bool Game::playerTwoHasWon()
-{
-	if(state == INITIAL)
-		return playerTwoIsOverForty();
-	else
-		return points[player1] < points[player2];
-}
-
-bool Game::thereIsDeuce()
+bool Game::thereIsDeuce() const
 {
 	return state == DEUCE;
 }
 
-bool Game::thereIsAdvantage()
+bool Game::thereIsAdvantage() const
 {
 	return state == ADVANTAGE;
 }
 
 		void pointTo(const std::string & player);
 
-		std::string score();
+		std::string score() const;
 
-		bool isOver();
+		bool isOver() const;
 
 	private:
 		std::string player1;
 
 		void updateState();
 
-		bool playersAreTightToFortyOrMore();
-		bool playersAreTight();
-		bool aPlayerHasWon();
+		bool playersAreTightToFortyOrMore() const;
+		bool playersAreTight() const;
+		bool aPlayerHasWon() const;
 
-		bool playerOneHasWon();
-		bool playerTwoHasWon();
+		bool thereIsDeuce() const;
+		bool thereIsAdvantage() const;
 
-		bool playerOneIsOverForty();
-		bool playerTwoIsOverForty();
+		std::string theWinner() const;
+		std::string theOneWithAdvantage() const;
 
-		bool thereIsDeuce();
-		bool thereIsAdvantage();
+		std::string computeScore(const std::string & player) const;
 
-		std::string theWinner();
-		std::string theOneWithAdvantage();
+		int getPlayerPoints(const std::string & player) const;
 
-		std::string computeScore(const std::string & player);
+		bool playerOverForty(const std::string & player) const;
+
+		std::string getOtherPlayer(const std::string & player) const;
+
+		bool playerWon(const std::string & player) const;
 };
 
 #endif /* GAME_H_ */