diff --git a/csharp/Tennis/TennisGame.cs b/csharp/Tennis/TennisGame.cs
index 8f24eefe2..6054f1e37 100644
--- a/csharp/Tennis/TennisGame.cs
+++ b/csharp/Tennis/TennisGame.cs
@@ -1,81 +1,158 @@
+using System.Collections.Generic;
+using System.Linq;
+
namespace Tennis
{
+ class TennisPlayers
+ {
+ public string player1Name;
+ public string player2Name;
+ public int player1Score;
+ public int player2Score;
+
+ ///
+ /// Constructor for the TennisPlayers object.
+ ///
+ /// Name of tennis player 1.
+ /// Name of tennis player 2.
+ public TennisPlayers(string tp1Name, string tp2Name)
+ {
+ player1Name = tp1Name;
+ player2Name = tp2Name;
+ }
+ }
+
class TennisGame : ITennisGame
{
- private int m_score1 = 0;
- private int m_score2 = 0;
- private string player1Name;
- private string player2Name;
+ private TennisPlayers players;
+ ///
+ /// Begins a game of tennis by instantiating the TennisPlayers object.
+ ///
+ /// Name of player 1.
+ /// Name of player 2.
public TennisGame(string player1Name, string player2Name)
{
- this.player1Name = player1Name;
- this.player2Name = player2Name;
+ players = new TennisPlayers(player1Name, player2Name);
}
+ ///
+ /// Updates the score using the name of the player who scored.
+ ///
+ /// Name of scoring player.
public void WonPoint(string playerName)
{
- if (playerName == "player1")
- m_score1 += 1;
+ if(playerName == players.player1Name)
+ {
+ players.player1Score += 1;
+ }
else
- m_score2 += 1;
+ {
+ players.player2Score += 1;
+ }
}
+ ///
+ /// Gets the score of the Tennis Game.
+ ///
+ /// The current score of the tennis game.
public string GetScore()
{
- string score = "";
- var tempScore = 0;
- if (m_score1 == m_score2)
+ switch (players.player1Score)
{
- switch (m_score1)
- {
- case 0:
- score = "Love-All";
- break;
- case 1:
- score = "Fifteen-All";
- break;
- case 2:
- score = "Thirty-All";
- break;
- default:
- score = "Deuce";
- break;
-
- }
- }
- else if (m_score1 >= 4 || m_score2 >= 4)
- {
- var minusResult = m_score1 - m_score2;
- if (minusResult == 1) score = "Advantage player1";
- else if (minusResult == -1) score = "Advantage player2";
- else if (minusResult >= 2) score = "Win for player1";
- else score = "Win for player2";
- }
- else
- {
- for (var i = 1; i < 3; i++)
- {
- if (i == 1) tempScore = m_score1;
- else { score += "-"; tempScore = m_score2; }
- switch (tempScore)
+ case 0:
+ switch (players.player2Score)
+ {
+ case 0:
+ return "Love-All"; //Player 1: 0 Player 2: 0
+ case 1:
+ return "Love-Fifteen"; //Player 1: 0 Player 2: 1
+ case 2:
+ return "Love-Thirty"; //Player 1: 0 Player 2: 2
+ case 3:
+ return "Love-Forty"; //Player 1: 0 Player 2: 3
+ default:
+ return "Win for " + players.player2Name; //Player 1: 0 Player 2: >3
+ }
+ case 1:
+ switch (players.player2Score)
+ {
+ case 0:
+ return "Fifteen-Love"; //Player 1: 1 Player 2: 0
+ case 1:
+ return "Fifteen-All"; //Player 1: 1 Player 2: 1
+ case 2:
+ return "Fifteen-Thirty"; //Player 1: 1 Player 2: 2
+ case 3:
+ return "Fifteen-Forty"; //Player 1: 1 Player 2: 3
+ default:
+ return "Win for " + players.player2Name; //Player 1: 1 Player 2: >3
+ }
+ case 2:
+ switch (players.player2Score)
+ {
+ case 0:
+ return "Thirty-Love"; //Player 1: 2 Player 2: 0
+ case 1:
+ return "Thirty-Fifteen"; //Player 1: 2 Player 2: 1
+ case 2:
+ return "Thirty-All"; //Player 1: 2 Player 2: 2
+ case 3:
+ return "Thirty-Forty"; //Player 1: 2 Player 2: 3
+ default:
+ return "Win for " + players.player2Name; //Player 1: 2 Player 2: >3
+ }
+ case 3:
+ switch (players.player2Score)
{
case 0:
- score += "Love";
- break;
+ return "Forty-Love"; //Player 1: 3 Player 2: 0
case 1:
- score += "Fifteen";
- break;
+ return "Forty-Fifteen"; //Player 1: 3 Player 2: 1
case 2:
- score += "Thirty";
- break;
+ return "Forty-Thirty"; //Player 1: 3 Player 2: 2
+ case 3:
+ return "Deuce"; //Player 1: 3 Player 2: 3
+ case 4:
+ return "Advantage " + players.player2Name; //Player 1: 3 Player 2: 4
+ default:
+ return "Win for " + players.player2Name; //Player 1: 3 Player 2: >4
+ }
+ case 4:
+ switch (players.player2Score)
+ {
+ case 0: case 1: case 2:
+ return "Win for " + players.player1Name; //Player 1: 4 Player 2: 0/1/2
case 3:
- score += "Forty";
- break;
+ return "Advantage " + players.player1Name; //Player 1: 4 Player 2: 3
+ case 4:
+ return "Deuce"; //Player 1: 4 Player 2: 4
+ case 5:
+ return "Advantage " + players.player2Name; //Player 1: 4 Player 2: 5
+ default:
+ return "Win for " + players.player2Name; //Player 1: 4 Player 2: >5
+ }
+ default:
+ int difference = players.player1Score - players.player2Score;
+ if(difference > 1)
+ {
+ return "Win for " + players.player1Name; //Player 1 has at least 2 more points than player 2 and Player 1's score is higher than 4
+ }
+ else if (difference < -1)
+ {
+ return "Win for " + players.player2Name; //Player 1 has scored 5 or more points but Player 2 has scored at least 2 more
+ }
+ else if (difference == 1)
+ {
+ return "Advantage " + players.player1Name; //Player 1 has scored 5 or more points and Player 2 has scored 1 less than that
+ }
+ else if (difference == -1)
+ {
+ return "Advantage " + players.player2Name; //Player 1 has scored 5 or more points and Player 2 has scored 1 more than that
}
- }
+ break;
}
- return score;
+ return "error";
}
}
}
diff --git a/csharp/Tennis/TennisTests.cs b/csharp/Tennis/TennisTests.cs
index f31d06901..ccde8f7e1 100644
--- a/csharp/Tennis/TennisTests.cs
+++ b/csharp/Tennis/TennisTests.cs
@@ -30,15 +30,19 @@ public class TennisGameTest
[TestCase( 4, 2, "Win for player1")]
[TestCase( 2, 4, "Win for player2")]
[TestCase( 4, 3, "Advantage player1")]
+ [TestCase( 688, 687, "Advantage player1")]
[TestCase( 3, 4, "Advantage player2")]
[TestCase( 5, 4, "Advantage player1")]
[TestCase( 4, 5, "Advantage player2")]
[TestCase(15, 14, "Advantage player1")]
[TestCase(14, 15, "Advantage player2")]
[TestCase( 6, 4, "Win for player1")]
+ [TestCase( 16, 0, "Win for player1")]
[TestCase( 4, 6, "Win for player2")]
[TestCase(16, 14, "Win for player1")]
[TestCase(14, 16, "Win for player2")]
+ [TestCase(0, 16, "Win for player2")]
+ [TestCase(0, 4, "Win for player2")]
public void CheckTennisGame(int player1Score, int player2Score, string expectedScore)
{
var game = new TennisGame("player1", "player2");