Skip to content

Instantly share code, notes, and snippets.

@mluukkai
Created June 6, 2013 10:40
Show Gist options
  • Save mluukkai/5720663 to your computer and use it in GitHub Desktop.
Save mluukkai/5720663 to your computer and use it in GitHub Desktop.
public interface TennisGame {
void wonPoint(String playerName);
String getScore();
}
public class TennisGame1 implements TennisGame {
private static final int LOVE = 0;
private static final int FIFTEEN = 1;
private static final int THIRTY = 2;
private static final int FORTY = 3;
private static final int OVER_FORTY = 4;
private static String nameFor(int score) {
if (score==FIFTEEN) {
return Point.Fifteen.name();
}
return Point.Love.name();
}
private static enum Point {
Love(LOVE), Fifteen(FIFTEEN), Thirty(THIRTY), Forty(FORTY), OverForty(OVER_FORTY);
private final int score;
private Point(int score) {
this.score = score;
}
public Point fromScore(int score) {
return null;
}
}
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() {
if (tie()) {
return scoreForTie();
} else if (possibleWin()) {
return scoreForPossibleWin();
}
return scoreForNoTieAndNoWin();
}
private String scoreForNoTieAndNoWin() {
String score = "";
int tempScore;
for (int i = 1; i < 3; i++) {
if (i == 1) {
tempScore = m_score1;
} else {
score += "-";
tempScore = m_score2;
}
switch (tempScore) {
case LOVE:
score += nameFor(tempScore);
break;
case FIFTEEN:
score += nameFor(tempScore);
break;
case THIRTY:
score += "Thirty";
break;
case FORTY:
score += "Forty";
break;
}
}
return score;
}
private String scoreForPossibleWin() {
String score;
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";
}
return score;
}
private String scoreForTie() {
switch (m_score1) {
case LOVE:
return "Love-All";
case FIFTEEN:
return "Fifteen-All";
case THIRTY:
return "Thirty-All";
case FORTY:
return "Forty-All";
default:
return "Deuce";
}
}
private boolean tie() {
return m_score1 == m_score2;
}
private boolean possibleWin() {
return m_score1 >= OVER_FORTY || m_score2 >= OVER_FORTY;
}
}
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"},
});
}
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");
}
assertEquals(this.expectedScore, game.getScore());
}
@Test
public void checkAllScoresTennisGame1() {
TennisGame1 game = new TennisGame1("player1", "player2");
checkAllScores(game);
}
@Test
public void checkAllScoresTennisGame2() {
TennisGame2 game = new TennisGame2("player1", "player2");
checkAllScores(game);
}
@Test
public void checkAllScoresTennisGame3() {
TennisGame3 game = new TennisGame3("player1", "player2");
checkAllScores(game);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment