Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 131 additions & 54 deletions csharp/Tennis/TennisGame.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Constructor for the TennisPlayers object.
/// </summary>
/// <param name="tp1Name">Name of tennis player 1.</param>
/// <param name="tp2Name">Name of tennis player 2.</param>
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;

/// <summary>
/// Begins a game of tennis by instantiating the TennisPlayers object.
/// </summary>
/// <param name="player1Name">Name of player 1.</param>
/// <param name="player2Name">Name of player 2.</param>
public TennisGame(string player1Name, string player2Name)
{
this.player1Name = player1Name;
this.player2Name = player2Name;
players = new TennisPlayers(player1Name, player2Name);
}

/// <summary>
/// Updates the score using the name of the player who scored.
/// </summary>
/// <param name="playerName">Name of scoring player.</param>
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;
}
}

/// <summary>
/// Gets the score of the Tennis Game.
/// </summary>
/// <returns>The current score of the tennis game.</returns>
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";
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions csharp/Tennis/TennisTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down