Skip to content

@Olsenius /Tennis.cs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Koden til Mathias, Imtiaz og meg fra tennis-kataen på Bergen Codingdojo 11.09.2013 http://www.meetup.com/BergenCodingDojo/events/136912282/
using System;
using System.Linq;
using NUnit.Framework;
using Should;
namespace Tennis
{
[TestFixture]
public class TennisTests
{
[Test]
public void should_score_points()
{
Scorer.Score("A").Points.A.ShouldEqual(1);
Scorer.Score("AAAABB").Points.A.ShouldEqual(4);
Scorer.Score("AAAABB").Points.B.ShouldEqual(2);
}
[Test]
public void should_game()
{
var result = Scorer.Score("AAAAA AAAABB");
result.Sets.A.ShouldEqual(0);
result.Sets.B.ShouldEqual(0);
result.Games.A.ShouldEqual(1);
result.Games.B.ShouldEqual(0);
result.Points.A.ShouldEqual(4);
result.Points.B.ShouldEqual(2);
}
[Test]
public void should_score_sets()
{
var result = Scorer.Score("AAAAA AAAAA AAAAA AAAAA AAAAA AAAAA");
result.Sets.A.ShouldEqual(1);
result.Sets.B.ShouldEqual(0);
result.Games.A.ShouldEqual(0);
result.Games.B.ShouldEqual(0);
result.Points.A.ShouldEqual(0);
result.Points.B.ShouldEqual(0);
}
}
public class Scorer
{
public static Result Score(string input)
{
var result = new Result();
var winners = input.ToCharArray();
foreach (var winner in winners.Where(x => x != ' '))
{
var pointDiff = Math.Abs(result.Points.A - result.Points.B);
var gameDiff = Math.Abs(result.Games.A - result.Games.B);
if (winner == 'A')
{
if (result.Points.A >= 4 && pointDiff >= 2)
{
if (result.Games.A >= 5 && gameDiff >= 2)
{
result.Games.A = 0;
result.Games.B = 0;
result.Points.A = 0;
result.Points.B = 0;
result.Sets.A++;
continue;
}
result.Points.A = 0;
result.Points.B = 0;
result.Games.A++;
continue;
}
result.Points.A++;
}
else
{
if (result.Points.B >= 4 && pointDiff >= 2)
{
if (result.Games.B >= 6 && gameDiff >= 2)
{
result.Games.A = 0;
result.Games.B = 0;
result.Points.A = 0;
result.Points.B = 0;
result.Sets.B++;
continue;
}
result.Points.A = 0;
result.Points.B = 0;
result.Games.B++;
continue;
}
result.Points.B++;
}
}
return result;
}
}
public class Result
{
public Result()
{
Sets = new Monkey();
Games = new Monkey();
Points = new Monkey();
}
public Monkey Sets { get; set; }
public Monkey Games { get; set; }
public Monkey Points { get; set; }
}
public class Monkey
{
public int A { get; set; }
public int B { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.