From e2a9c5468d2eab04e0b88ae5dd06f4497da68c02 Mon Sep 17 00:00:00 2001 From: emilybache Date: Fri, 1 Feb 2013 15:38:54 +0100 Subject: [PATCH] C sharp translation of Tennis kata --- Tennis/csharp/AssemblyInfo.cs | 27 +++++++ Tennis/csharp/Main.cs | 12 +++ Tennis/csharp/Tennis.csproj | 48 ++++++++++++ Tennis/csharp/TennisGame.cs | 12 +++ Tennis/csharp/TennisGame1.cs | 89 +++++++++++++++++++++ Tennis/csharp/TennisGame2.cs | 144 ++++++++++++++++++++++++++++++++++ Tennis/csharp/TennisGame3.cs | 41 ++++++++++ Tennis/csharp/TennisTest.cs | 115 +++++++++++++++++++++++++++ 8 files changed, 488 insertions(+) create mode 100644 Tennis/csharp/AssemblyInfo.cs create mode 100644 Tennis/csharp/Main.cs create mode 100644 Tennis/csharp/Tennis.csproj create mode 100644 Tennis/csharp/TennisGame.cs create mode 100644 Tennis/csharp/TennisGame1.cs create mode 100644 Tennis/csharp/TennisGame2.cs create mode 100644 Tennis/csharp/TennisGame3.cs create mode 100644 Tennis/csharp/TennisTest.cs diff --git a/Tennis/csharp/AssemblyInfo.cs b/Tennis/csharp/AssemblyInfo.cs new file mode 100644 index 00000000..115d92f5 --- /dev/null +++ b/Tennis/csharp/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("Tennis")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("emily")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Tennis/csharp/Main.cs b/Tennis/csharp/Main.cs new file mode 100644 index 00000000..896d9de0 --- /dev/null +++ b/Tennis/csharp/Main.cs @@ -0,0 +1,12 @@ +using System; + +namespace Tennis +{ + class MainClass + { + public static void Main (string[] args) + { + Console.WriteLine ("Hello World!"); + } + } +} diff --git a/Tennis/csharp/Tennis.csproj b/Tennis/csharp/Tennis.csproj new file mode 100644 index 00000000..34482ba2 --- /dev/null +++ b/Tennis/csharp/Tennis.csproj @@ -0,0 +1,48 @@ + + + + Debug + x86 + 9.0.21022 + 2.0 + {F059601E-EF1A-4148-A2A4-1E1697CC69EA} + Exe + Tennis + Tennis + v3.5 + + + True + full + False + bin\Debug + DEBUG; + prompt + 4 + x86 + True + + + none + True + bin\Release + prompt + 4 + x86 + True + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tennis/csharp/TennisGame.cs b/Tennis/csharp/TennisGame.cs new file mode 100644 index 00000000..2131409a --- /dev/null +++ b/Tennis/csharp/TennisGame.cs @@ -0,0 +1,12 @@ +using System; + +namespace Tennis +{ + public interface TennisGame + { + void WonPoint (string playerName); + string GetScore (); + + } +} + diff --git a/Tennis/csharp/TennisGame1.cs b/Tennis/csharp/TennisGame1.cs new file mode 100644 index 00000000..a6f35a25 --- /dev/null +++ b/Tennis/csharp/TennisGame1.cs @@ -0,0 +1,89 @@ +using System; +using NUnit.Framework; + +namespace Tennis +{ + class TennisGame1 : TennisGame + { + private int m_score1 = 0; + private int m_score2 = 0; + private string player1Name; + private string player2Name; + + public TennisGame1 (string player1Name, string player2Name) + { + this.player1Name = player1Name; + this.player2Name = player2Name; + } + + public void WonPoint (string playerName) + { + if (playerName == "player1") + m_score1 += 1; + else + m_score2 += 1; + } + + public string GetScore () + { + String score = ""; + int tempScore=0; + if (m_score1==m_score2) + { + switch (m_score1) + { + case 0: + score = "Love-All"; + break; + case 1: + score = "Fifteen-All"; + break; + case 2: + score = "Thirty-All"; + break; + case 3: + score = "Forty-All"; + break; + default: + score = "Deuce"; + break; + + } + } + else if (m_score1>=4 || m_score2>=4) + { + int 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 (int i=1; i<3; i++) + { + if (i==1) tempScore = m_score1; + else { score+="-"; tempScore = m_score2;} + switch(tempScore) + { + case 0: + score+="Love"; + break; + case 1: + score+="Fifteen"; + break; + case 2: + score+="Thirty"; + break; + case 3: + score+="Forty"; + break; + } + } + } + return score; + } + } + +} + diff --git a/Tennis/csharp/TennisGame2.cs b/Tennis/csharp/TennisGame2.cs new file mode 100644 index 00000000..5bd39e29 --- /dev/null +++ b/Tennis/csharp/TennisGame2.cs @@ -0,0 +1,144 @@ +using System; + +namespace Tennis +{ + public class TennisGame2 : TennisGame + { + public int P1point = 0; + public int P2point = 0; + + public string P1res = ""; + public string P2res = ""; + private string player1Name; + private string player2Name; + + public TennisGame2 (string player1Name, string player2Name) + { + this.player1Name = player1Name; + this.player2Name = player2Name; + } + + public string GetScore(){ + string score = ""; + if (P1point == P2point && P1point < 4) + { + if (P1point==0) + score = "Love"; + if (P1point==1) + score = "Fifteen"; + if (P1point==2) + score = "Thirty"; + if (P1point==3) + score = "Forty"; + score += "-All"; + } + if (P1point==P2point && P1point>3) + score = "Deuce"; + + if (P1point > 0 && P2point==0) + { + if (P1point==1) + P1res = "Fifteen"; + if (P1point==2) + P1res = "Thirty"; + if (P1point==3) + P1res = "Forty"; + + P2res = "Love"; + score = P1res + "-" + P2res; + } + if (P2point > 0 && P1point==0) + { + if (P2point==1) + P2res = "Fifteen"; + if (P2point==2) + P2res = "Thirty"; + if (P2point==3) + P2res = "Forty"; + + P1res = "Love"; + score = P1res + "-" + P2res; + } + + if (P1point>P2point && P1point < 4) + { + if (P1point==2) + P1res="Thirty"; + if (P1point==3) + P1res="Forty"; + if (P2point==1) + P2res="Fifteen"; + if (P2point==2) + P2res="Thirty"; + score = P1res + "-" + P2res; + } + if (P2point>P1point && P2point < 4) + { + if (P2point==2) + P2res="Thirty"; + if (P2point==3) + P2res="Forty"; + if (P1point==1) + P1res="Fifteen"; + if (P1point==2) + P1res="Thirty"; + score = P1res + "-" + P2res; + } + + if (P1point > P2point && P2point >= 3) + { + score = "Advantage player1"; + } + + if (P2point > P1point && P1point >= 3) + { + score = "Advantage player2"; + } + + if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2) + { + score = "Win for player1"; + } + if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2) + { + score = "Win for player2"; + } + return score; + } + + public void SetP1Score(int number){ + + for (int i = 0; i < number; i++) + { + P1Score(); + } + + } + + public void SetP2Score(int number){ + + for (int i = 0; i < number; i++) + { + P2Score(); + } + + } + + public void P1Score(){ + P1point++; + } + + public void P2Score(){ + P2point++; + } + + public void WonPoint(string player) { + if (player == "player1") + P1Score(); + else + P2Score(); + } + + } +} + diff --git a/Tennis/csharp/TennisGame3.cs b/Tennis/csharp/TennisGame3.cs new file mode 100644 index 00000000..9e9f793f --- /dev/null +++ b/Tennis/csharp/TennisGame3.cs @@ -0,0 +1,41 @@ +using System; + +namespace Tennis +{ + public class TennisGame3 : TennisGame + { + private int p2; + private int p1; + private string p1N; + private string p2N; + + public TennisGame3 (string player1Name, string player2Name) + { + this.p1N = player1Name; + this.p2N = player2Name; + } + + public string GetScore() { + string s; + if (p1 < 4 && p2 < 4) { + string[] p = new String[]{"Love", "Fifteen", "Thirty", "Forty"}; + s = p[p1]; + return (p1 == p2) ? s + "-All" : s + "-" + p[p2]; + } else { + if (p1 == p2) + return "Deuce"; + s = p1 > p2 ? p1N : p2N; + return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s; + } + } + + public void WonPoint(string playerName) { + if (playerName == "player1") + this.p1 += 1; + else + this.p2 += 1; + } + + } +} + diff --git a/Tennis/csharp/TennisTest.cs b/Tennis/csharp/TennisTest.cs new file mode 100644 index 00000000..7e6a5caa --- /dev/null +++ b/Tennis/csharp/TennisTest.cs @@ -0,0 +1,115 @@ +using System; +using NUnit.Framework; + +namespace Tennis +{ + [TestFixture(0, 0, "Love-All")] + [TestFixture( 1, 1, "Fifteen-All" )] + [TestFixture( 2, 2, "Thirty-All")] + [TestFixture( 3, 3, "Forty-All")] + [TestFixture( 4, 4, "Deuce")] + [TestFixture( 1, 0, "Fifteen-Love")] + [TestFixture( 0, 1, "Love-Fifteen")] + [TestFixture( 2, 0, "Thirty-Love")] + [TestFixture( 0, 2, "Love-Thirty")] + [TestFixture( 3, 0, "Forty-Love")] + [TestFixture( 0, 3, "Love-Forty")] + [TestFixture( 4, 0, "Win for player1")] + [TestFixture( 0, 4, "Win for player2")] + [TestFixture( 2, 1, "Thirty-Fifteen")] + [TestFixture( 1, 2, "Fifteen-Thirty")] + [TestFixture( 3, 1, "Forty-Fifteen")] + [TestFixture( 1, 3, "Fifteen-Forty")] + [TestFixture( 4, 1, "Win for player1")] + [TestFixture( 1, 4, "Win for player2")] + [TestFixture( 3, 2, "Forty-Thirty")] + [TestFixture( 2, 3, "Thirty-Forty")] + [TestFixture( 4, 2, "Win for player1")] + [TestFixture( 2, 4, "Win for player2")] + [TestFixture( 4, 3, "Advantage player1")] + [TestFixture( 3, 4, "Advantage player2")] + [TestFixture( 5, 4, "Advantage player1")] + [TestFixture( 4, 5, "Advantage player2")] + [TestFixture( 15, 14, "Advantage player1")] + [TestFixture( 14, 15, "Advantage player2")] + [TestFixture( 6, 4, "Win for player1")] + [TestFixture( 4, 6, "Win for player2")] + [TestFixture( 16, 14, "Win for player1")] + [TestFixture( 14, 16, "Win for player2")] + public class TennisTest + { + private int player1Score; + private int player2Score; + private string expectedScore; + + public TennisTest(int player1Score, int player2Score, string expectedScore) { + this.player1Score = player1Score; + this.player2Score = player2Score; + this.expectedScore = expectedScore; + } + + [Test] + public void checkTennisGame1() { + TennisGame1 game = new TennisGame1("player1", "player2"); + checkAllScores(game); + } + + [Test] + public void checkTennisGame2() { + TennisGame2 game = new TennisGame2("player1", "player2"); + checkAllScores(game); + } + + [Test] + public void checkTennisGame3() { + TennisGame3 game = new TennisGame3("player1", "player2"); + checkAllScores(game); + } + + public void checkAllScores(TennisGame game) { + int highestScore = Math.Max(this.player1Score, this.player2Score); + for (int i = 0; i < highestScore; i++) { + if (i < this.player1Score) + game.WonPoint("player1"); + if (i < this.player2Score) + game.WonPoint("player2"); + } + Assert.AreEqual(this.expectedScore, game.GetScore()); + } + + } + + [TestFixture()] + public class ExampleGameTennisTest + { + public void RealisticTennisGame(TennisGame game) + { + String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"}; + String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"}; + for (int i = 0; i < 6; i++) { + game.WonPoint(points[i]); + Assert.AreEqual(expected_scores[i], game.GetScore()); + } + } + [Test()] + public void CheckGame1() + { + TennisGame1 game = new TennisGame1("player1", "player2"); + RealisticTennisGame(game); + } + [Test()] + public void CheckGame2() + { + TennisGame2 game = new TennisGame2("player1", "player2"); + RealisticTennisGame(game); + } + [Test()] + public void CheckGame3() + { + TennisGame3 game = new TennisGame3("player1", "player2"); + RealisticTennisGame(game); + } + } + +} +