mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 04:12:13 +00:00
converted tabs to spaces
This commit is contained in:
parent
afc37306a8
commit
8e3bd6076f
@ -1,80 +1,80 @@
|
|||||||
package defactored1;
|
package defactored1;
|
||||||
|
|
||||||
public class TennisGame {
|
public class 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 TennisGame(String player1Name, String player2Name) {
|
public TennisGame(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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,84 +14,84 @@ import org.junit.runners.Parameterized.Parameters;
|
|||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Parameters
|
@Parameters
|
||||||
public static Collection<Object[]> getAllScores() {
|
public static Collection<Object[]> getAllScores() {
|
||||||
return Arrays.asList(new Object[][] {
|
return Arrays.asList(new Object[][] {
|
||||||
{ 0, 0, "Love-All" },
|
{ 0, 0, "Love-All" },
|
||||||
{ 1, 1, "Fifteen-All" },
|
{ 1, 1, "Fifteen-All" },
|
||||||
{ 2, 2, "Thirty-All"},
|
{ 2, 2, "Thirty-All"},
|
||||||
{ 3, 3, "Forty-All"},
|
{ 3, 3, "Forty-All"},
|
||||||
{ 4, 4, "Deuce"},
|
{ 4, 4, "Deuce"},
|
||||||
|
|
||||||
{ 1, 0, "Fifteen-Love"},
|
{ 1, 0, "Fifteen-Love"},
|
||||||
{ 0, 1, "Love-Fifteen"},
|
{ 0, 1, "Love-Fifteen"},
|
||||||
{ 2, 0, "Thirty-Love"},
|
{ 2, 0, "Thirty-Love"},
|
||||||
{ 0, 2, "Love-Thirty"},
|
{ 0, 2, "Love-Thirty"},
|
||||||
{ 3, 0, "Forty-Love"},
|
{ 3, 0, "Forty-Love"},
|
||||||
{ 0, 3, "Love-Forty"},
|
{ 0, 3, "Love-Forty"},
|
||||||
{ 4, 0, "Win for player1"},
|
{ 4, 0, "Win for player1"},
|
||||||
{ 0, 4, "Win for player2"},
|
{ 0, 4, "Win for player2"},
|
||||||
|
|
||||||
{ 2, 1, "Thirty-Fifteen"},
|
{ 2, 1, "Thirty-Fifteen"},
|
||||||
{ 1, 2, "Fifteen-Thirty"},
|
{ 1, 2, "Fifteen-Thirty"},
|
||||||
{ 3, 1, "Forty-Fifteen"},
|
{ 3, 1, "Forty-Fifteen"},
|
||||||
{ 1, 3, "Fifteen-Forty"},
|
{ 1, 3, "Fifteen-Forty"},
|
||||||
{ 4, 1, "Win for player1"},
|
{ 4, 1, "Win for player1"},
|
||||||
{ 1, 4, "Win for player2"},
|
{ 1, 4, "Win for player2"},
|
||||||
|
|
||||||
{ 3, 2, "Forty-Thirty"},
|
{ 3, 2, "Forty-Thirty"},
|
||||||
{ 2, 3, "Thirty-Forty"},
|
{ 2, 3, "Thirty-Forty"},
|
||||||
{ 4, 2, "Win for player1"},
|
{ 4, 2, "Win for player1"},
|
||||||
{ 2, 4, "Win for player2"},
|
{ 2, 4, "Win for player2"},
|
||||||
|
|
||||||
{ 4, 3, "Advantage player1"},
|
{ 4, 3, "Advantage player1"},
|
||||||
{ 3, 4, "Advantage player2"},
|
{ 3, 4, "Advantage player2"},
|
||||||
{ 5, 4, "Advantage player1"},
|
{ 5, 4, "Advantage player1"},
|
||||||
{ 4, 5, "Advantage player2"},
|
{ 4, 5, "Advantage player2"},
|
||||||
{ 15, 14, "Advantage player1"},
|
{ 15, 14, "Advantage player1"},
|
||||||
{ 14, 15, "Advantage player2"},
|
{ 14, 15, "Advantage player2"},
|
||||||
|
|
||||||
{ 6, 4, "Win for player1"},
|
{ 6, 4, "Win for player1"},
|
||||||
{ 4, 6, "Win for player2"},
|
{ 4, 6, "Win for player2"},
|
||||||
{ 16, 14, "Win for player1"},
|
{ 16, 14, "Win for player1"},
|
||||||
{ 14, 16, "Win for player2"},
|
{ 14, 16, "Win for player2"},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkAllScores() {
|
public void checkAllScores() {
|
||||||
TennisGame game = new TennisGame("player1", "player2");
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
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");
|
||||||
}
|
}
|
||||||
assertEquals(this.expectedScore, game.getScore());
|
assertEquals(this.expectedScore, game.getScore());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void realisticGame() {
|
public void realisticGame() {
|
||||||
TennisGame game = new TennisGame("player1", "player2");
|
TennisGame game = new TennisGame("player1", "player2");
|
||||||
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]);
|
||||||
assertEquals(expected_scores[i], game.getScore());
|
assertEquals(expected_scores[i], game.getScore());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,137 +2,137 @@ package defactored2;
|
|||||||
|
|
||||||
public class TennisGame
|
public class 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 TennisGame(String player1Name, String player2Name) {
|
public TennisGame(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==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++)
|
if (P1point > 0 && P2point==0)
|
||||||
{
|
{
|
||||||
P1Score();
|
if (P1point==1)
|
||||||
}
|
P1res = "Fifteen";
|
||||||
|
if (P1point==2)
|
||||||
}
|
P1res = "Thirty";
|
||||||
|
if (P1point==3)
|
||||||
public void SetP2Score(int number){
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < number; i++)
|
if (P1point>P2point && P1point < 4)
|
||||||
{
|
{
|
||||||
P2Score();
|
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 P1Score(){
|
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++;
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,37 +1,37 @@
|
|||||||
package defactored3;
|
package defactored3;
|
||||||
|
|
||||||
public class TennisGame {
|
public class 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 TennisGame(String p1N, String p2N) {
|
public TennisGame(String p1N, String p2N) {
|
||||||
this.p1N = p1N;
|
this.p1N = p1N;
|
||||||
this.p2N = p2N;
|
this.p2N = p2N;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user