public
Created

Koden til Mathias, Imtiaz og meg fra tennis-kataen på Bergen Codingdojo 11.09.2013 http://www.meetup.com/BergenCodingDojo/events/136912282/

  • Download Gist
Tennis.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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; }
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.