From c2d3a90bc91a14219a53b2e827b25cebea375d05 Mon Sep 17 00:00:00 2001 From: emilybache Date: Tue, 5 Feb 2013 08:08:45 +0100 Subject: [PATCH] C# version now available in cyber-dojo --- Tennis/README.md | 3 +- Tennis/csharp/TennisGame.cs | 10 +- Tennis/csharp/TennisGame1.cs | 156 ++++++++++---------- Tennis/csharp/TennisGame2.cs | 270 +++++++++++++++++------------------ Tennis/csharp/TennisGame3.cs | 64 ++++----- Tennis/csharp/TennisTest.cs | 200 +++++++++++++------------- 6 files changed, 352 insertions(+), 351 deletions(-) diff --git a/Tennis/README.md b/Tennis/README.md index 89acad8e..995c4d62 100644 --- a/Tennis/README.md +++ b/Tennis/README.md @@ -24,4 +24,5 @@ As an alternative to downloading the code, click one of the links below to creat - [Python](http://cyber-dojo.com/forker/fork/FFEB8EE18C?avatar=cheetah&tag=3) - [Ruby](http://cyber-dojo.com/forker/fork/9197D6B12C?avatar=cheetah&tag=3) - [Java](http://cyber-dojo.com/forker/fork/B22DCD17C3?avatar=buffalo&tag=11) -- [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45) \ No newline at end of file +- [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45) +- [C#](http://cyber-dojo.com/forker/fork/672E047F5D?avatar=buffalo&tag=7) \ No newline at end of file diff --git a/Tennis/csharp/TennisGame.cs b/Tennis/csharp/TennisGame.cs index 2131409a..32f99a74 100644 --- a/Tennis/csharp/TennisGame.cs +++ b/Tennis/csharp/TennisGame.cs @@ -2,11 +2,11 @@ using System; namespace Tennis { - public interface TennisGame - { - void WonPoint (string playerName); - string GetScore (); + public interface TennisGame + { + void WonPoint (string playerName); + string GetScore (); - } + } } diff --git a/Tennis/csharp/TennisGame1.cs b/Tennis/csharp/TennisGame1.cs index a6f35a25..4412ccd4 100644 --- a/Tennis/csharp/TennisGame1.cs +++ b/Tennis/csharp/TennisGame1.cs @@ -3,87 +3,87 @@ using NUnit.Framework; namespace Tennis { - class TennisGame1 : TennisGame - { - private int m_score1 = 0; - private int m_score2 = 0; - private string player1Name; - private string player2Name; + 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 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 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; - } - } + 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 index 5bd39e29..d09b6c67 100644 --- a/Tennis/csharp/TennisGame2.cs +++ b/Tennis/csharp/TennisGame2.cs @@ -2,143 +2,143 @@ 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 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 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(); - } + 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 index 9e9f793f..1f3e9ba4 100644 --- a/Tennis/csharp/TennisGame3.cs +++ b/Tennis/csharp/TennisGame3.cs @@ -2,40 +2,40 @@ using System; namespace Tennis { - public class TennisGame3 : TennisGame - { - private int p2; - private int p1; - private string p1N; - private string p2N; + 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 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; - } + 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 index 7e6a5caa..bd2c4dd6 100644 --- a/Tennis/csharp/TennisTest.cs +++ b/Tennis/csharp/TennisTest.cs @@ -3,113 +3,113 @@ 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; + [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; - } + 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 checkTennisGame1() { + TennisGame1 game = new TennisGame1("player1", "player2"); + checkAllScores(game); + } - [Test] - public void checkTennisGame2() { - TennisGame2 game = new TennisGame2("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); - } + [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()); - } + 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); - } - } + [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); + } + } }