Skip to content

Instantly share code, notes, and snippets.

@joshpeterson
Created October 25, 2011 01:10
Show Gist options
  • Save joshpeterson/1311007 to your computer and use it in GitHub Desktop.
Save joshpeterson/1311007 to your computer and use it in GitHub Desktop.
using NUnit.Framework;
namespace TennisScoringKata
{
[TestFixture]
public class DefineTennisGame
{
private TennisGame game;
[SetUp]
public void Setup()
{
game = new TennisGame("Thing 1", "Thing 2");
}
[Test]
public void ReturnsLoveLoveForANewGame()
{
Assert.That(game.Score, Is.EqualTo("love-love"));
}
[Test]
public void Returns15LoveWhenTheServerScoresOnce()
{
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("15-love"));
}
[Test]
public void Returns30LoveWhenTheServerScoresTwice()
{
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("30-love"));
}
[Test]
public void Returns40LoveWhenTheServerScoresThreeTimes()
{
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("40-love"));
}
[Test]
public void ReturnGameWhenTheServerScoresFourTimes()
{
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("game Thing 1"));
}
[Test]
public void ReturnsLove15WhenTheNonServerScoresOnce()
{
game.Volley("Thing 2");
Assert.That(game.Score, Is.EqualTo("love-15"));
}
[Test]
public void ReturnsLove30WhenTheNonServerScoresTwice()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
Assert.That(game.Score, Is.EqualTo("love-30"));
}
[Test]
public void ReturnsLove40WhenTheNonServerScoresThreeTimes()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
Assert.That(game.Score, Is.EqualTo("love-40"));
}
[Test]
public void ReturnsGameWhenTheNonServerScoresFourTimes()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
Assert.That(game.Score, Is.EqualTo("game Thing 2"));
}
[Test]
public void ReturnsDeuceWhenBothPlayersScoreThreeTimes()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("deuce"));
}
[Test]
public void ReturnsDeuceWhenBothPlayersScoreFourTimes()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("deuce"));
}
[Test]
public void ReturnsAdvantageServerWhenServerScoresAfterDeuce()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("advantage Thing 1"));
}
[Test]
public void ReturnsAdvantageNonServerWhenNonServerScoresAfterDeuce()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 2");
Assert.That(game.Score, Is.EqualTo("advantage Thing 2"));
}
[Test]
public void ReturnsGameWhenServerScoresTwiceAfterDeuce()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
Assert.That(game.Score, Is.EqualTo("game Thing 1"));
}
[Test]
public void ReturnsGameWhenNonServerScoresTwiceAfterDeuce()
{
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 2");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 1");
game.Volley("Thing 2");
game.Volley("Thing 2");
Assert.That(game.Score, Is.EqualTo("game Thing 2"));
}
}
}
using System;
namespace TennisScoringKata
{
class TennisGame
{
private int[] points = new int[2];
private string server;
private string nonServer;
public TennisGame(string server, string nonServer)
{
this.server = server;
this.nonServer = nonServer;
}
public string Score
{
get
{
if (points[0] >= 3 && points[1] >= 3 && Math.Abs(points[0] - points[1]) < 2)
if (points[0] == points[1])
return "deuce";
else if (points[0] > points[1])
return "advantage " + server;
else
return "advantage " + nonServer;
if (points[0] < 4 && points[1] < 4)
return TranslateScore(points[0]) + "-" + TranslateScore(points[1]);
if (points[0] >= 4)
return TranslateScore(points[0]) + " " + server;
return TranslateScore(points[1]) + " " + nonServer;
}
}
internal void Volley(string scoringPlayer)
{
if (scoringPlayer == server)
points[0]++;
else
points[1]++;
}
static string TranslateScore(int points)
{
switch (points)
{
case 3:
return "40";
case 2:
return "30";
case 1:
return "15";
case 0:
return "love";
default:
return "game";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment