mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 04:12:13 +00:00
first commit
This commit is contained in:
commit
d70ce44c5f
80
Tennis/java/defactored1/TennisGame.java
Normal file
80
Tennis/java/defactored1/TennisGame.java
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package defactored1;
|
||||||
|
|
||||||
|
public class TennisGame {
|
||||||
|
|
||||||
|
private int m_score1 = 0;
|
||||||
|
private int m_score2 = 0;
|
||||||
|
private String player1Name;
|
||||||
|
private String player2Name;
|
||||||
|
|
||||||
|
public TennisGame(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
94
Tennis/java/defactored1/TennisTest.java
Normal file
94
Tennis/java/defactored1/TennisTest.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package defactored1;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> getAllScores() {
|
||||||
|
return Arrays.asList(new Object[][] {
|
||||||
|
{ 0, 0, "Love-All" },
|
||||||
|
{ 1, 1, "Fifteen-All" },
|
||||||
|
{ 2, 2, "Thirty-All"},
|
||||||
|
{ 3, 3, "Forty-All"},
|
||||||
|
{ 4, 4, "Deuce"},
|
||||||
|
|
||||||
|
{ 1, 0, "Fifteen-Love"},
|
||||||
|
{ 0, 1, "Love-Fifteen"},
|
||||||
|
{ 2, 0, "Thirty-Love"},
|
||||||
|
{ 0, 2, "Love-Thirty"},
|
||||||
|
{ 3, 0, "Forty-Love"},
|
||||||
|
{ 0, 3, "Love-Forty"},
|
||||||
|
{ 4, 0, "Win for player1"},
|
||||||
|
{ 0, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 2, 1, "Thirty-Fifteen"},
|
||||||
|
{ 1, 2, "Fifteen-Thirty"},
|
||||||
|
{ 3, 1, "Forty-Fifteen"},
|
||||||
|
{ 1, 3, "Fifteen-Forty"},
|
||||||
|
{ 4, 1, "Win for player1"},
|
||||||
|
{ 1, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 3, 2, "Forty-Thirty"},
|
||||||
|
{ 2, 3, "Thirty-Forty"},
|
||||||
|
{ 4, 2, "Win for player1"},
|
||||||
|
{ 2, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 4, 3, "Advantage player1"},
|
||||||
|
{ 3, 4, "Advantage player2"},
|
||||||
|
{ 5, 4, "Advantage player1"},
|
||||||
|
{ 4, 5, "Advantage player2"},
|
||||||
|
{ 15, 14, "Advantage player1"},
|
||||||
|
{ 14, 15, "Advantage player2"},
|
||||||
|
|
||||||
|
{ 6, 4, "Win for player1"},
|
||||||
|
{ 4, 6, "Win for player2"},
|
||||||
|
{ 16, 14, "Win for player1"},
|
||||||
|
{ 14, 16, "Win for player2"},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkAllScores() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
for (int i = 0; i < this.player1Score; i++)
|
||||||
|
game.wonPoint("player1");
|
||||||
|
for (int i = 0; i < this.player2Score; i++)
|
||||||
|
game.wonPoint("player2");
|
||||||
|
assertEquals(this.expectedScore, game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void realisticGame() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
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]);
|
||||||
|
assertEquals(expected_scores[i], game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
138
Tennis/java/defactored2/TennisGame.java
Normal file
138
Tennis/java/defactored2/TennisGame.java
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
package defactored2;
|
||||||
|
|
||||||
|
public class TennisGame
|
||||||
|
{
|
||||||
|
public int P1point = 0;
|
||||||
|
public int P2point = 0;
|
||||||
|
|
||||||
|
public String P1res = "";
|
||||||
|
public String P2res = "";
|
||||||
|
private String player1Name;
|
||||||
|
private String player2Name;
|
||||||
|
|
||||||
|
public TennisGame(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
94
Tennis/java/defactored2/TennisTest.java
Normal file
94
Tennis/java/defactored2/TennisTest.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package defactored2;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> getAllScores() {
|
||||||
|
return Arrays.asList(new Object[][] {
|
||||||
|
{ 0, 0, "Love-All" },
|
||||||
|
{ 1, 1, "Fifteen-All" },
|
||||||
|
{ 2, 2, "Thirty-All"},
|
||||||
|
{ 3, 3, "Forty-All"},
|
||||||
|
{ 4, 4, "Deuce"},
|
||||||
|
|
||||||
|
{ 1, 0, "Fifteen-Love"},
|
||||||
|
{ 0, 1, "Love-Fifteen"},
|
||||||
|
{ 2, 0, "Thirty-Love"},
|
||||||
|
{ 0, 2, "Love-Thirty"},
|
||||||
|
{ 3, 0, "Forty-Love"},
|
||||||
|
{ 0, 3, "Love-Forty"},
|
||||||
|
{ 4, 0, "Win for player1"},
|
||||||
|
{ 0, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 2, 1, "Thirty-Fifteen"},
|
||||||
|
{ 1, 2, "Fifteen-Thirty"},
|
||||||
|
{ 3, 1, "Forty-Fifteen"},
|
||||||
|
{ 1, 3, "Fifteen-Forty"},
|
||||||
|
{ 4, 1, "Win for player1"},
|
||||||
|
{ 1, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 3, 2, "Forty-Thirty"},
|
||||||
|
{ 2, 3, "Thirty-Forty"},
|
||||||
|
{ 4, 2, "Win for player1"},
|
||||||
|
{ 2, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 4, 3, "Advantage player1"},
|
||||||
|
{ 3, 4, "Advantage player2"},
|
||||||
|
{ 5, 4, "Advantage player1"},
|
||||||
|
{ 4, 5, "Advantage player2"},
|
||||||
|
{ 15, 14, "Advantage player1"},
|
||||||
|
{ 14, 15, "Advantage player2"},
|
||||||
|
|
||||||
|
{ 6, 4, "Win for player1"},
|
||||||
|
{ 4, 6, "Win for player2"},
|
||||||
|
{ 16, 14, "Win for player1"},
|
||||||
|
{ 14, 16, "Win for player2"},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkAllScores() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
for (int i = 0; i < this.player1Score; i++)
|
||||||
|
game.wonPoint("player1");
|
||||||
|
for (int i = 0; i < this.player2Score; i++)
|
||||||
|
game.wonPoint("player2");
|
||||||
|
assertEquals(this.expectedScore, game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void realisticGame() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
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]);
|
||||||
|
assertEquals(expected_scores[i], game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
37
Tennis/java/defactored3/TennisGame.java
Normal file
37
Tennis/java/defactored3/TennisGame.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package defactored3;
|
||||||
|
|
||||||
|
public class TennisGame {
|
||||||
|
|
||||||
|
private int p2;
|
||||||
|
private int p1;
|
||||||
|
private String p1N;
|
||||||
|
private String p2N;
|
||||||
|
|
||||||
|
public TennisGame(String p1N, String p2N) {
|
||||||
|
this.p1N = p1N;
|
||||||
|
this.p2N = p2N;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
94
Tennis/java/defactored3/TennisTest.java
Normal file
94
Tennis/java/defactored3/TennisTest.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package defactored3;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> getAllScores() {
|
||||||
|
return Arrays.asList(new Object[][] {
|
||||||
|
{ 0, 0, "Love-All" },
|
||||||
|
{ 1, 1, "Fifteen-All" },
|
||||||
|
{ 2, 2, "Thirty-All"},
|
||||||
|
{ 3, 3, "Forty-All"},
|
||||||
|
{ 4, 4, "Deuce"},
|
||||||
|
|
||||||
|
{ 1, 0, "Fifteen-Love"},
|
||||||
|
{ 0, 1, "Love-Fifteen"},
|
||||||
|
{ 2, 0, "Thirty-Love"},
|
||||||
|
{ 0, 2, "Love-Thirty"},
|
||||||
|
{ 3, 0, "Forty-Love"},
|
||||||
|
{ 0, 3, "Love-Forty"},
|
||||||
|
{ 4, 0, "Win for player1"},
|
||||||
|
{ 0, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 2, 1, "Thirty-Fifteen"},
|
||||||
|
{ 1, 2, "Fifteen-Thirty"},
|
||||||
|
{ 3, 1, "Forty-Fifteen"},
|
||||||
|
{ 1, 3, "Fifteen-Forty"},
|
||||||
|
{ 4, 1, "Win for player1"},
|
||||||
|
{ 1, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 3, 2, "Forty-Thirty"},
|
||||||
|
{ 2, 3, "Thirty-Forty"},
|
||||||
|
{ 4, 2, "Win for player1"},
|
||||||
|
{ 2, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 4, 3, "Advantage player1"},
|
||||||
|
{ 3, 4, "Advantage player2"},
|
||||||
|
{ 5, 4, "Advantage player1"},
|
||||||
|
{ 4, 5, "Advantage player2"},
|
||||||
|
{ 15, 14, "Advantage player1"},
|
||||||
|
{ 14, 15, "Advantage player2"},
|
||||||
|
|
||||||
|
{ 6, 4, "Win for player1"},
|
||||||
|
{ 4, 6, "Win for player2"},
|
||||||
|
{ 16, 14, "Win for player1"},
|
||||||
|
{ 14, 16, "Win for player2"},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkAllScores() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
for (int i = 0; i < this.player1Score; i++)
|
||||||
|
game.wonPoint("player1");
|
||||||
|
for (int i = 0; i < this.player2Score; i++)
|
||||||
|
game.wonPoint("player2");
|
||||||
|
assertEquals(this.expectedScore, game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void realisticGame() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
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]);
|
||||||
|
assertEquals(expected_scores[i], game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
63
Tennis/java/factored1/TennisGame.java
Normal file
63
Tennis/java/factored1/TennisGame.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package factored1;
|
||||||
|
|
||||||
|
public class TennisGame {
|
||||||
|
|
||||||
|
public static final String[] POINTS = new String[]{"Love", "Fifteen", "Thirty", "Forty"};
|
||||||
|
|
||||||
|
private int player2Score;
|
||||||
|
private int player1Score;
|
||||||
|
|
||||||
|
private String player1Name;
|
||||||
|
private String player2Name;
|
||||||
|
|
||||||
|
public TennisGame(String player1Name, String player2Name) {
|
||||||
|
this.player1Name = player1Name;
|
||||||
|
this.player2Name = player2Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getScore() {
|
||||||
|
if (someoneHasWon())
|
||||||
|
return "Win for " + winningPlayerName();
|
||||||
|
|
||||||
|
if (isEndgame()) {
|
||||||
|
if (pointsAreEven())
|
||||||
|
return "Deuce";
|
||||||
|
else
|
||||||
|
return "Advantage " + winningPlayerName();
|
||||||
|
} else {
|
||||||
|
if (pointsAreEven())
|
||||||
|
return POINTS[player1Score] + "-All";
|
||||||
|
else {
|
||||||
|
return POINTS[player1Score] + "-" + POINTS[player2Score];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean someoneHasWon() {
|
||||||
|
return isEndgame() && (player1Score-player2Score >= 2 || player2Score-player1Score >= 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean pointsAreEven() {
|
||||||
|
return player1Score == player2Score;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isEndgame() {
|
||||||
|
return player1Score > 3 || player2Score > 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String winningPlayerName() {
|
||||||
|
if (player1Score > player2Score)
|
||||||
|
return player1Name;
|
||||||
|
else
|
||||||
|
return player2Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void wonPoint(String playerName) {
|
||||||
|
if (playerName == player1Name)
|
||||||
|
this.player1Score += 1;
|
||||||
|
else
|
||||||
|
this.player2Score += 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
93
Tennis/java/factored1/TennisTest.java
Normal file
93
Tennis/java/factored1/TennisTest.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package factored1;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> getAllScores() {
|
||||||
|
return Arrays.asList(new Object[][] {
|
||||||
|
{ 0, 0, "Love-All" },
|
||||||
|
{ 1, 1, "Fifteen-All" },
|
||||||
|
{ 2, 2, "Thirty-All"},
|
||||||
|
{ 3, 3, "Forty-All"},
|
||||||
|
{ 4, 4, "Deuce"},
|
||||||
|
|
||||||
|
{ 1, 0, "Fifteen-Love"},
|
||||||
|
{ 0, 1, "Love-Fifteen"},
|
||||||
|
{ 2, 0, "Thirty-Love"},
|
||||||
|
{ 0, 2, "Love-Thirty"},
|
||||||
|
{ 3, 0, "Forty-Love"},
|
||||||
|
{ 0, 3, "Love-Forty"},
|
||||||
|
{ 4, 0, "Win for player1"},
|
||||||
|
{ 0, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 2, 1, "Thirty-Fifteen"},
|
||||||
|
{ 1, 2, "Fifteen-Thirty"},
|
||||||
|
{ 3, 1, "Forty-Fifteen"},
|
||||||
|
{ 1, 3, "Fifteen-Forty"},
|
||||||
|
{ 4, 1, "Win for player1"},
|
||||||
|
{ 1, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 3, 2, "Forty-Thirty"},
|
||||||
|
{ 2, 3, "Thirty-Forty"},
|
||||||
|
{ 4, 2, "Win for player1"},
|
||||||
|
{ 2, 4, "Win for player2"},
|
||||||
|
|
||||||
|
{ 4, 3, "Advantage player1"},
|
||||||
|
{ 3, 4, "Advantage player2"},
|
||||||
|
{ 5, 4, "Advantage player1"},
|
||||||
|
{ 4, 5, "Advantage player2"},
|
||||||
|
{ 15, 14, "Advantage player1"},
|
||||||
|
{ 14, 15, "Advantage player2"},
|
||||||
|
|
||||||
|
{ 6, 4, "Win for player1"},
|
||||||
|
{ 4, 6, "Win for player2"},
|
||||||
|
{ 16, 14, "Win for player1"},
|
||||||
|
{ 14, 16, "Win for player2"},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkAllScores() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
for (int i = 0; i < this.player1Score; i++)
|
||||||
|
game.wonPoint("player1");
|
||||||
|
for (int i = 0; i < this.player2Score; i++)
|
||||||
|
game.wonPoint("player2");
|
||||||
|
assertEquals(this.expectedScore, game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void realisticGame() {
|
||||||
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
|
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]);
|
||||||
|
assertEquals(expected_scores[i], game.getScore());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
176
Tennis/python/tennis.py
Normal file
176
Tennis/python/tennis.py
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class TennisGameDefactored1:
|
||||||
|
|
||||||
|
def __init__(self, player1Name, player2Name):
|
||||||
|
self.player1Name = player1Name
|
||||||
|
self.player2Name = player2Name
|
||||||
|
self.p1points = 0
|
||||||
|
self.p2points = 0
|
||||||
|
|
||||||
|
def won_point(self, playerName):
|
||||||
|
if playerName == self.player1Name:
|
||||||
|
self.p1points += 1
|
||||||
|
else:
|
||||||
|
self.p2points += 1
|
||||||
|
|
||||||
|
def score(self):
|
||||||
|
result = ""
|
||||||
|
tempScore=0
|
||||||
|
if (self.p1points==self.p2points):
|
||||||
|
result = {
|
||||||
|
0 : "Love-All",
|
||||||
|
1 : "Fifteen-All",
|
||||||
|
2 : "Thirty-All",
|
||||||
|
3 : "Forty-All",
|
||||||
|
}.get(self.p1points, "Deuce")
|
||||||
|
elif (self.p1points>=4 or self.p2points>=4):
|
||||||
|
minusResult = self.p1points-self.p2points
|
||||||
|
if (minusResult==1):
|
||||||
|
result ="Advantage player1"
|
||||||
|
elif (minusResult ==-1):
|
||||||
|
result ="Advantage player2"
|
||||||
|
elif (minusResult>=2):
|
||||||
|
result = "Win for player1"
|
||||||
|
else:
|
||||||
|
result ="Win for player2"
|
||||||
|
else:
|
||||||
|
for i in range(1,3):
|
||||||
|
if (i==1):
|
||||||
|
tempScore = self.p1points
|
||||||
|
else:
|
||||||
|
result+="-"
|
||||||
|
tempScore = self.p2points
|
||||||
|
result += {
|
||||||
|
0 : "Love",
|
||||||
|
1 : "Fifteen",
|
||||||
|
2 : "Thirty",
|
||||||
|
3 : "Forty",
|
||||||
|
}[tempScore]
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
class TennisGameDefactored2:
|
||||||
|
def __init__(self, player1Name, player2Name):
|
||||||
|
self.player1Name = player1Name
|
||||||
|
self.player2Name = player2Name
|
||||||
|
self.p1points = 0
|
||||||
|
self.p2points = 0
|
||||||
|
|
||||||
|
def won_point(self, playerName):
|
||||||
|
if playerName == self.player1Name:
|
||||||
|
self.P1Score()
|
||||||
|
else:
|
||||||
|
self.P2Score()
|
||||||
|
|
||||||
|
def score(self):
|
||||||
|
result = ""
|
||||||
|
if (self.p1points == self.p2points and self.p1points < 4):
|
||||||
|
if (self.p1points==0):
|
||||||
|
result = "Love"
|
||||||
|
if (self.p1points==1):
|
||||||
|
result = "Fifteen"
|
||||||
|
if (self.p1points==2):
|
||||||
|
result = "Thirty"
|
||||||
|
if (self.p1points==3):
|
||||||
|
result = "Forty"
|
||||||
|
result += "-All"
|
||||||
|
if (self.p1points==self.p2points and self.p1points>3):
|
||||||
|
result = "Deuce"
|
||||||
|
|
||||||
|
P1res = ""
|
||||||
|
P2res = ""
|
||||||
|
if (self.p1points > 0 and self.p2points==0):
|
||||||
|
if (self.p1points==1):
|
||||||
|
P1res = "Fifteen"
|
||||||
|
if (self.p1points==2):
|
||||||
|
P1res = "Thirty"
|
||||||
|
if (self.p1points==3):
|
||||||
|
P1res = "Forty"
|
||||||
|
|
||||||
|
P2res = "Love"
|
||||||
|
result = P1res + "-" + P2res
|
||||||
|
if (self.p2points > 0 and self.p1points==0):
|
||||||
|
if (self.p2points==1):
|
||||||
|
P2res = "Fifteen"
|
||||||
|
if (self.p2points==2):
|
||||||
|
P2res = "Thirty"
|
||||||
|
if (self.p2points==3):
|
||||||
|
P2res = "Forty"
|
||||||
|
|
||||||
|
P1res = "Love"
|
||||||
|
result = P1res + "-" + P2res
|
||||||
|
|
||||||
|
|
||||||
|
if (self.p1points>self.p2points and self.p1points < 4):
|
||||||
|
if (self.p1points==2):
|
||||||
|
P1res="Thirty"
|
||||||
|
if (self.p1points==3):
|
||||||
|
P1res="Forty"
|
||||||
|
if (self.p2points==1):
|
||||||
|
P2res="Fifteen"
|
||||||
|
if (self.p2points==2):
|
||||||
|
P2res="Thirty"
|
||||||
|
result = P1res + "-" + P2res
|
||||||
|
if (self.p2points>self.p1points and self.p2points < 4):
|
||||||
|
if (self.p2points==2):
|
||||||
|
P2res="Thirty"
|
||||||
|
if (self.p2points==3):
|
||||||
|
P2res="Forty"
|
||||||
|
if (self.p1points==1):
|
||||||
|
P1res="Fifteen"
|
||||||
|
if (self.p1points==2):
|
||||||
|
P1res="Thirty"
|
||||||
|
result = P1res + "-" + P2res
|
||||||
|
|
||||||
|
if (self.p1points > self.p2points and self.p2points >= 3):
|
||||||
|
result = "Advantage player1"
|
||||||
|
|
||||||
|
if (self.p2points > self.p1points and self.p1points >= 3):
|
||||||
|
result = "Advantage player2"
|
||||||
|
|
||||||
|
if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2):
|
||||||
|
result = "Win for player1"
|
||||||
|
if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2):
|
||||||
|
result = "Win for player2"
|
||||||
|
return result
|
||||||
|
|
||||||
|
def SetP1Score(self, number):
|
||||||
|
for i in range(number):
|
||||||
|
self.P1Score()
|
||||||
|
|
||||||
|
def SetP2Score(self, number):
|
||||||
|
for i in range(number):
|
||||||
|
self.P2Score()
|
||||||
|
|
||||||
|
def P1Score(self):
|
||||||
|
self.p1points +=1
|
||||||
|
|
||||||
|
|
||||||
|
def P2Score(self):
|
||||||
|
self.p2points +=1
|
||||||
|
|
||||||
|
class TennisGameDefactored3:
|
||||||
|
def __init__(self, player1Name, player2Name):
|
||||||
|
self.p1N = player1Name
|
||||||
|
self.p2N = player2Name
|
||||||
|
self.p1 = 0
|
||||||
|
self.p2 = 0
|
||||||
|
|
||||||
|
def won_point(self, n):
|
||||||
|
if n == self.p1N:
|
||||||
|
self.p1 += 1
|
||||||
|
else:
|
||||||
|
self.p2 += 1
|
||||||
|
|
||||||
|
def score(self):
|
||||||
|
if (self.p1 < 4 and self.p2 < 4):
|
||||||
|
p = ["Love", "Fifteen", "Thirty", "Forty"]
|
||||||
|
s = p[self.p1]
|
||||||
|
return s + "-All" if (self.p1 == self.p2) else s + "-" + p[self.p2]
|
||||||
|
else:
|
||||||
|
if (self.p1 == self.p2):
|
||||||
|
return "Deuce"
|
||||||
|
s = self.p1N if self.p1 > self.p2 else self.p2N
|
||||||
|
return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s
|
||||||
|
|
||||||
BIN
Tennis/python/tennis.pyc
Normal file
BIN
Tennis/python/tennis.pyc
Normal file
Binary file not shown.
64
Tennis/python/tennis_test.py
Normal file
64
Tennis/python/tennis_test.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
from tennis import TennisGame
|
||||||
|
|
||||||
|
# test support code
|
||||||
|
def params(funcarglist):
|
||||||
|
def wrapper(function):
|
||||||
|
function.funcarglist = funcarglist
|
||||||
|
return function
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
def pytest_generate_tests(metafunc):
|
||||||
|
for funcargs in getattr(metafunc.function, 'funcarglist', ()):
|
||||||
|
metafunc.addcall(funcargs=funcargs)
|
||||||
|
|
||||||
|
# actual test code
|
||||||
|
class TestTennis:
|
||||||
|
|
||||||
|
@params([dict(p1Points=0, p2Points=0, score="Love-All"),
|
||||||
|
dict(p1Points=1, p2Points=1, score="Fifteen-All"),
|
||||||
|
dict(p1Points=2, p2Points=2, score="Thirty-All"),
|
||||||
|
dict(p1Points=3, p2Points=3, score="Forty-All"),
|
||||||
|
dict(p1Points=4, p2Points=4, score="Deuce"),
|
||||||
|
|
||||||
|
dict(p1Points=1, p2Points=0, score="Fifteen-Love"),
|
||||||
|
dict(p1Points=0, p2Points=1, score="Love-Fifteen"),
|
||||||
|
dict(p1Points=2, p2Points=0, score="Thirty-Love"),
|
||||||
|
dict(p1Points=0, p2Points=2, score="Love-Thirty"),
|
||||||
|
dict(p1Points=3, p2Points=0, score="Forty-Love"),
|
||||||
|
dict(p1Points=0, p2Points=3, score="Love-Forty"),
|
||||||
|
dict(p1Points=4, p2Points=0, score="Win for player1"),
|
||||||
|
dict(p1Points=0, p2Points=4, score="Win for player2"),
|
||||||
|
|
||||||
|
dict(p1Points=2, p2Points=1, score="Thirty-Fifteen"),
|
||||||
|
dict(p1Points=1, p2Points=2, score="Fifteen-Thirty"),
|
||||||
|
dict(p1Points=3, p2Points=1, score="Forty-Fifteen"),
|
||||||
|
dict(p1Points=1, p2Points=3, score="Fifteen-Forty"),
|
||||||
|
dict(p1Points=4, p2Points=1, score="Win for player1"),
|
||||||
|
dict(p1Points=1, p2Points=4, score="Win for player2"),
|
||||||
|
|
||||||
|
dict(p1Points=3, p2Points=2, score="Forty-Thirty"),
|
||||||
|
dict(p1Points=2, p2Points=3, score="Thirty-Forty"),
|
||||||
|
dict(p1Points=4, p2Points=2, score="Win for player1"),
|
||||||
|
dict(p1Points=2, p2Points=4, score="Win for player2"),
|
||||||
|
|
||||||
|
dict(p1Points=4, p2Points=3, score="Advantage player1"),
|
||||||
|
dict(p1Points=3, p2Points=4, score="Advantage player2"),
|
||||||
|
dict(p1Points=5, p2Points=4, score="Advantage player1"),
|
||||||
|
dict(p1Points=4, p2Points=5, score="Advantage player2"),
|
||||||
|
dict(p1Points=15, p2Points=14, score="Advantage player1"),
|
||||||
|
dict(p1Points=14, p2Points=15, score="Advantage player2"),
|
||||||
|
|
||||||
|
dict(p1Points=6, p2Points=4, score="Win for player1"),
|
||||||
|
dict(p1Points=4, p2Points=6, score="Win for player2"),
|
||||||
|
dict(p1Points=16, p2Points=14, score="Win for player1"),
|
||||||
|
dict(p1Points=14, p2Points=16, score="Win for player2"),
|
||||||
|
])
|
||||||
|
def test_get_score(self, p1Points, p2Points, score):
|
||||||
|
game = TennisGame("player1", "player2")
|
||||||
|
for i in range(p1Points):
|
||||||
|
game.won_point("player1")
|
||||||
|
for i in range(p2Points):
|
||||||
|
game.won_point("player2")
|
||||||
|
assert score == game.score()
|
||||||
|
|
||||||
BIN
Tennis/python/tennis_test.pyc
Normal file
BIN
Tennis/python/tennis_test.pyc
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user