C# version now available in cyber-dojo

This commit is contained in:
emilybache 2013-02-05 08:08:45 +01:00
parent e2a9c5468d
commit c2d3a90bc9
6 changed files with 352 additions and 351 deletions

View File

@ -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) - [Python](http://cyber-dojo.com/forker/fork/FFEB8EE18C?avatar=cheetah&tag=3)
- [Ruby](http://cyber-dojo.com/forker/fork/9197D6B12C?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) - [Java](http://cyber-dojo.com/forker/fork/B22DCD17C3?avatar=buffalo&tag=11)
- [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45) - [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45)
- [C#](http://cyber-dojo.com/forker/fork/672E047F5D?avatar=buffalo&tag=7)

View File

@ -2,11 +2,11 @@ using System;
namespace Tennis namespace Tennis
{ {
public interface TennisGame public interface TennisGame
{ {
void WonPoint (string playerName); void WonPoint (string playerName);
string GetScore (); string GetScore ();
} }
} }

View File

@ -3,87 +3,87 @@ using NUnit.Framework;
namespace Tennis namespace Tennis
{ {
class TennisGame1 : TennisGame class TennisGame1 : TennisGame
{ {
private int m_score1 = 0; private int m_score1 = 0;
private int m_score2 = 0; private int m_score2 = 0;
private string player1Name; private string player1Name;
private string player2Name; private string player2Name;
public TennisGame1 (string player1Name, string player2Name) public TennisGame1 (string player1Name, string player2Name)
{ {
this.player1Name = player1Name; this.player1Name = player1Name;
this.player2Name = player2Name; this.player2Name = player2Name;
} }
public void WonPoint (string playerName) public void WonPoint (string playerName)
{ {
if (playerName == "player1") if (playerName == "player1")
m_score1 += 1; m_score1 += 1;
else else
m_score2 += 1; m_score2 += 1;
} }
public string GetScore () public string GetScore ()
{ {
String score = ""; String score = "";
int tempScore=0; int tempScore=0;
if (m_score1==m_score2) if (m_score1==m_score2)
{ {
switch (m_score1) switch (m_score1)
{ {
case 0: case 0:
score = "Love-All"; score = "Love-All";
break; break;
case 1: case 1:
score = "Fifteen-All"; score = "Fifteen-All";
break; break;
case 2: case 2:
score = "Thirty-All"; score = "Thirty-All";
break; break;
case 3: case 3:
score = "Forty-All"; score = "Forty-All";
break; break;
default: default:
score = "Deuce"; score = "Deuce";
break; break;
} }
} }
else if (m_score1>=4 || m_score2>=4) else if (m_score1>=4 || m_score2>=4)
{ {
int minusResult = m_score1-m_score2; int minusResult = m_score1-m_score2;
if (minusResult==1) score ="Advantage player1"; if (minusResult==1) score ="Advantage player1";
else if (minusResult ==-1) score ="Advantage player2"; else if (minusResult ==-1) score ="Advantage player2";
else if (minusResult>=2) score = "Win for player1"; else if (minusResult>=2) score = "Win for player1";
else score ="Win for player2"; else score ="Win for player2";
} }
else else
{ {
for (int i=1; i<3; i++) for (int i=1; i<3; i++)
{ {
if (i==1) tempScore = m_score1; if (i==1) tempScore = m_score1;
else { score+="-"; tempScore = m_score2;} else { score+="-"; tempScore = m_score2;}
switch(tempScore) switch(tempScore)
{ {
case 0: case 0:
score+="Love"; score+="Love";
break; break;
case 1: case 1:
score+="Fifteen"; score+="Fifteen";
break; break;
case 2: case 2:
score+="Thirty"; score+="Thirty";
break; break;
case 3: case 3:
score+="Forty"; score+="Forty";
break; break;
} }
} }
} }
return score; return score;
} }
} }
} }

View File

@ -2,143 +2,143 @@ using System;
namespace Tennis namespace Tennis
{ {
public class TennisGame2 : TennisGame public class TennisGame2 : TennisGame
{ {
public int P1point = 0; public int P1point = 0;
public int P2point = 0; public int P2point = 0;
public string P1res = ""; public string P1res = "";
public string P2res = ""; public string P2res = "";
private string player1Name; private string player1Name;
private string player2Name; private string player2Name;
public TennisGame2 (string player1Name, string player2Name) public TennisGame2 (string player1Name, string player2Name)
{ {
this.player1Name = player1Name; this.player1Name = player1Name;
this.player2Name = player2Name; this.player2Name = player2Name;
} }
public string GetScore(){ public string GetScore(){
string score = ""; string score = "";
if (P1point == P2point && P1point < 4) if (P1point == P2point && P1point < 4)
{ {
if (P1point==0) if (P1point==0)
score = "Love"; score = "Love";
if (P1point==1) if (P1point==1)
score = "Fifteen"; score = "Fifteen";
if (P1point==2) if (P1point==2)
score = "Thirty"; score = "Thirty";
if (P1point==3) if (P1point==3)
score = "Forty"; score = "Forty";
score += "-All"; score += "-All";
} }
if (P1point==P2point && P1point>3) if (P1point==P2point && P1point>3)
score = "Deuce"; score = "Deuce";
if (P1point > 0 && P2point==0) if (P1point > 0 && P2point==0)
{ {
if (P1point==1) if (P1point==1)
P1res = "Fifteen"; P1res = "Fifteen";
if (P1point==2) if (P1point==2)
P1res = "Thirty"; P1res = "Thirty";
if (P1point==3) if (P1point==3)
P1res = "Forty"; P1res = "Forty";
P2res = "Love"; P2res = "Love";
score = P1res + "-" + P2res; score = P1res + "-" + P2res;
} }
if (P2point > 0 && P1point==0) if (P2point > 0 && P1point==0)
{ {
if (P2point==1) if (P2point==1)
P2res = "Fifteen"; P2res = "Fifteen";
if (P2point==2) if (P2point==2)
P2res = "Thirty"; P2res = "Thirty";
if (P2point==3) if (P2point==3)
P2res = "Forty"; P2res = "Forty";
P1res = "Love"; P1res = "Love";
score = P1res + "-" + P2res; score = P1res + "-" + P2res;
} }
if (P1point>P2point && P1point < 4) if (P1point>P2point && P1point < 4)
{ {
if (P1point==2) if (P1point==2)
P1res="Thirty"; P1res="Thirty";
if (P1point==3) if (P1point==3)
P1res="Forty"; P1res="Forty";
if (P2point==1) if (P2point==1)
P2res="Fifteen"; P2res="Fifteen";
if (P2point==2) if (P2point==2)
P2res="Thirty"; P2res="Thirty";
score = P1res + "-" + P2res; score = P1res + "-" + P2res;
} }
if (P2point>P1point && P2point < 4) if (P2point>P1point && P2point < 4)
{ {
if (P2point==2) if (P2point==2)
P2res="Thirty"; P2res="Thirty";
if (P2point==3) if (P2point==3)
P2res="Forty"; P2res="Forty";
if (P1point==1) if (P1point==1)
P1res="Fifteen"; P1res="Fifteen";
if (P1point==2) if (P1point==2)
P1res="Thirty"; P1res="Thirty";
score = P1res + "-" + P2res; score = P1res + "-" + P2res;
} }
if (P1point > P2point && P2point >= 3) if (P1point > P2point && P2point >= 3)
{ {
score = "Advantage player1"; score = "Advantage player1";
} }
if (P2point > P1point && P1point >= 3) if (P2point > P1point && P1point >= 3)
{ {
score = "Advantage player2"; score = "Advantage player2";
} }
if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2) if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2)
{ {
score = "Win for player1"; score = "Win for player1";
} }
if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2) if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2)
{ {
score = "Win for player2"; score = "Win for player2";
} }
return score; return score;
} }
public void SetP1Score(int number){ public void SetP1Score(int number){
for (int i = 0; i < number; i++) for (int i = 0; i < number; i++)
{ {
P1Score(); P1Score();
} }
} }
public void SetP2Score(int number){ public void SetP2Score(int number){
for (int i = 0; i < number; i++) for (int i = 0; i < number; i++)
{ {
P2Score(); P2Score();
} }
} }
public void P1Score(){ public void P1Score(){
P1point++; P1point++;
} }
public void P2Score(){ public void P2Score(){
P2point++; P2point++;
} }
public void WonPoint(string player) { public void WonPoint(string player) {
if (player == "player1") if (player == "player1")
P1Score(); P1Score();
else else
P2Score(); P2Score();
} }
} }
} }

View File

@ -2,40 +2,40 @@ using System;
namespace Tennis namespace Tennis
{ {
public class TennisGame3 : TennisGame public class TennisGame3 : TennisGame
{ {
private int p2; private int p2;
private int p1; private int p1;
private string p1N; private string p1N;
private string p2N; private string p2N;
public TennisGame3 (string player1Name, string player2Name) public TennisGame3 (string player1Name, string player2Name)
{ {
this.p1N = player1Name; this.p1N = player1Name;
this.p2N = player2Name; this.p2N = player2Name;
} }
public string GetScore() { public string GetScore() {
string s; string s;
if (p1 < 4 && p2 < 4) { if (p1 < 4 && p2 < 4) {
string[] p = new String[]{"Love", "Fifteen", "Thirty", "Forty"}; string[] p = new String[]{"Love", "Fifteen", "Thirty", "Forty"};
s = p[p1]; s = p[p1];
return (p1 == p2) ? s + "-All" : s + "-" + p[p2]; return (p1 == p2) ? s + "-All" : s + "-" + p[p2];
} else { } else {
if (p1 == p2) if (p1 == p2)
return "Deuce"; return "Deuce";
s = p1 > p2 ? p1N : p2N; s = p1 > p2 ? p1N : p2N;
return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s; return ((p1-p2)*(p1-p2) == 1) ? "Advantage " + s : "Win for " + s;
} }
} }
public void WonPoint(string playerName) { public void WonPoint(string playerName) {
if (playerName == "player1") if (playerName == "player1")
this.p1 += 1; this.p1 += 1;
else else
this.p2 += 1; this.p2 += 1;
} }
} }
} }

View File

@ -3,113 +3,113 @@ using NUnit.Framework;
namespace Tennis namespace Tennis
{ {
[TestFixture(0, 0, "Love-All")] [TestFixture(0, 0, "Love-All")]
[TestFixture( 1, 1, "Fifteen-All" )] [TestFixture( 1, 1, "Fifteen-All" )]
[TestFixture( 2, 2, "Thirty-All")] [TestFixture( 2, 2, "Thirty-All")]
[TestFixture( 3, 3, "Forty-All")] [TestFixture( 3, 3, "Forty-All")]
[TestFixture( 4, 4, "Deuce")] [TestFixture( 4, 4, "Deuce")]
[TestFixture( 1, 0, "Fifteen-Love")] [TestFixture( 1, 0, "Fifteen-Love")]
[TestFixture( 0, 1, "Love-Fifteen")] [TestFixture( 0, 1, "Love-Fifteen")]
[TestFixture( 2, 0, "Thirty-Love")] [TestFixture( 2, 0, "Thirty-Love")]
[TestFixture( 0, 2, "Love-Thirty")] [TestFixture( 0, 2, "Love-Thirty")]
[TestFixture( 3, 0, "Forty-Love")] [TestFixture( 3, 0, "Forty-Love")]
[TestFixture( 0, 3, "Love-Forty")] [TestFixture( 0, 3, "Love-Forty")]
[TestFixture( 4, 0, "Win for player1")] [TestFixture( 4, 0, "Win for player1")]
[TestFixture( 0, 4, "Win for player2")] [TestFixture( 0, 4, "Win for player2")]
[TestFixture( 2, 1, "Thirty-Fifteen")] [TestFixture( 2, 1, "Thirty-Fifteen")]
[TestFixture( 1, 2, "Fifteen-Thirty")] [TestFixture( 1, 2, "Fifteen-Thirty")]
[TestFixture( 3, 1, "Forty-Fifteen")] [TestFixture( 3, 1, "Forty-Fifteen")]
[TestFixture( 1, 3, "Fifteen-Forty")] [TestFixture( 1, 3, "Fifteen-Forty")]
[TestFixture( 4, 1, "Win for player1")] [TestFixture( 4, 1, "Win for player1")]
[TestFixture( 1, 4, "Win for player2")] [TestFixture( 1, 4, "Win for player2")]
[TestFixture( 3, 2, "Forty-Thirty")] [TestFixture( 3, 2, "Forty-Thirty")]
[TestFixture( 2, 3, "Thirty-Forty")] [TestFixture( 2, 3, "Thirty-Forty")]
[TestFixture( 4, 2, "Win for player1")] [TestFixture( 4, 2, "Win for player1")]
[TestFixture( 2, 4, "Win for player2")] [TestFixture( 2, 4, "Win for player2")]
[TestFixture( 4, 3, "Advantage player1")] [TestFixture( 4, 3, "Advantage player1")]
[TestFixture( 3, 4, "Advantage player2")] [TestFixture( 3, 4, "Advantage player2")]
[TestFixture( 5, 4, "Advantage player1")] [TestFixture( 5, 4, "Advantage player1")]
[TestFixture( 4, 5, "Advantage player2")] [TestFixture( 4, 5, "Advantage player2")]
[TestFixture( 15, 14, "Advantage player1")] [TestFixture( 15, 14, "Advantage player1")]
[TestFixture( 14, 15, "Advantage player2")] [TestFixture( 14, 15, "Advantage player2")]
[TestFixture( 6, 4, "Win for player1")] [TestFixture( 6, 4, "Win for player1")]
[TestFixture( 4, 6, "Win for player2")] [TestFixture( 4, 6, "Win for player2")]
[TestFixture( 16, 14, "Win for player1")] [TestFixture( 16, 14, "Win for player1")]
[TestFixture( 14, 16, "Win for player2")] [TestFixture( 14, 16, "Win for player2")]
public class TennisTest public class TennisTest
{ {
private int player1Score; private int player1Score;
private int player2Score; private int player2Score;
private string expectedScore; private string expectedScore;
public TennisTest(int player1Score, int player2Score, string expectedScore) { public TennisTest(int player1Score, int player2Score, string expectedScore) {
this.player1Score = player1Score; this.player1Score = player1Score;
this.player2Score = player2Score; this.player2Score = player2Score;
this.expectedScore = expectedScore; this.expectedScore = expectedScore;
} }
[Test] [Test]
public void checkTennisGame1() { public void checkTennisGame1() {
TennisGame1 game = new TennisGame1("player1", "player2"); TennisGame1 game = new TennisGame1("player1", "player2");
checkAllScores(game); checkAllScores(game);
} }
[Test] [Test]
public void checkTennisGame2() { public void checkTennisGame2() {
TennisGame2 game = new TennisGame2("player1", "player2"); TennisGame2 game = new TennisGame2("player1", "player2");
checkAllScores(game); checkAllScores(game);
} }
[Test] [Test]
public void checkTennisGame3() { public void checkTennisGame3() {
TennisGame3 game = new TennisGame3("player1", "player2"); TennisGame3 game = new TennisGame3("player1", "player2");
checkAllScores(game); checkAllScores(game);
} }
public void checkAllScores(TennisGame game) { public void checkAllScores(TennisGame game) {
int highestScore = Math.Max(this.player1Score, this.player2Score); int highestScore = Math.Max(this.player1Score, this.player2Score);
for (int i = 0; i < highestScore; i++) { for (int i = 0; i < highestScore; i++) {
if (i < this.player1Score) if (i < this.player1Score)
game.WonPoint("player1"); game.WonPoint("player1");
if (i < this.player2Score) if (i < this.player2Score)
game.WonPoint("player2"); game.WonPoint("player2");
} }
Assert.AreEqual(this.expectedScore, game.GetScore()); Assert.AreEqual(this.expectedScore, game.GetScore());
} }
} }
[TestFixture()] [TestFixture()]
public class ExampleGameTennisTest public class ExampleGameTennisTest
{ {
public void RealisticTennisGame(TennisGame game) public void RealisticTennisGame(TennisGame game)
{ {
String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"}; String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"};
String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"}; String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"};
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
game.WonPoint(points[i]); game.WonPoint(points[i]);
Assert.AreEqual(expected_scores[i], game.GetScore()); Assert.AreEqual(expected_scores[i], game.GetScore());
} }
} }
[Test()] [Test()]
public void CheckGame1() public void CheckGame1()
{ {
TennisGame1 game = new TennisGame1("player1", "player2"); TennisGame1 game = new TennisGame1("player1", "player2");
RealisticTennisGame(game); RealisticTennisGame(game);
} }
[Test()] [Test()]
public void CheckGame2() public void CheckGame2()
{ {
TennisGame2 game = new TennisGame2("player1", "player2"); TennisGame2 game = new TennisGame2("player1", "player2");
RealisticTennisGame(game); RealisticTennisGame(game);
} }
[Test()] [Test()]
public void CheckGame3() public void CheckGame3()
{ {
TennisGame3 game = new TennisGame3("player1", "player2"); TennisGame3 game = new TennisGame3("player1", "player2");
RealisticTennisGame(game); RealisticTennisGame(game);
} }
} }
} }