mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 12:22:12 +00:00
C sharp translation of Tennis kata
This commit is contained in:
parent
6654875bc0
commit
e2a9c5468d
27
Tennis/csharp/AssemblyInfo.cs
Normal file
27
Tennis/csharp/AssemblyInfo.cs
Normal file
@ -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("")]
|
||||
|
||||
12
Tennis/csharp/Main.cs
Normal file
12
Tennis/csharp/Main.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Tennis
|
||||
{
|
||||
class MainClass
|
||||
{
|
||||
public static void Main (string[] args)
|
||||
{
|
||||
Console.WriteLine ("Hello World!");
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Tennis/csharp/Tennis.csproj
Normal file
48
Tennis/csharp/Tennis.csproj
Normal file
@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||
<ProductVersion>9.0.21022</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{F059601E-EF1A-4148-A2A4-1E1697CC69EA}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>Tennis</RootNamespace>
|
||||
<AssemblyName>Tennis</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<DebugSymbols>True</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>False</Optimize>
|
||||
<OutputPath>bin\Debug</OutputPath>
|
||||
<DefineConstants>DEBUG;</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<Externalconsole>True</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>True</Optimize>
|
||||
<OutputPath>bin\Release</OutputPath>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<Externalconsole>True</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="nunit.framework" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="TennisTest.cs" />
|
||||
<Compile Include="TennisGame1.cs" />
|
||||
<Compile Include="TennisGame2.cs" />
|
||||
<Compile Include="TennisGame3.cs" />
|
||||
<Compile Include="TennisGame.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
12
Tennis/csharp/TennisGame.cs
Normal file
12
Tennis/csharp/TennisGame.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Tennis
|
||||
{
|
||||
public interface TennisGame
|
||||
{
|
||||
void WonPoint (string playerName);
|
||||
string GetScore ();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
89
Tennis/csharp/TennisGame1.cs
Normal file
89
Tennis/csharp/TennisGame1.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
144
Tennis/csharp/TennisGame2.cs
Normal file
144
Tennis/csharp/TennisGame2.cs
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
41
Tennis/csharp/TennisGame3.cs
Normal file
41
Tennis/csharp/TennisGame3.cs
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
115
Tennis/csharp/TennisTest.cs
Normal file
115
Tennis/csharp/TennisTest.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user