[CodeKata][Java] Tennis Game - Pajace/CodeKata GitHub Wiki
Production Code
import java.util.HashMap;
import java.util.Map;
public class TennisGame {
private final String player1Name;
private final String player2Name;
private int player1ScoredTimes;
private int player2ScoredTimes;
public TennisGame(String player1, String player2) {
player1Name = player1;
player2Name = player2;
}
public String getScore() {
return isDifferenceScore()
? (isReadyForWin() ? getAdvantageState() : getNormalScore())
: (isDeuce() ? getDeuce() : getSameScore());
}
private String getAdvantageState() {
return getAdvantagePlayerName() + (hasWinner() ? " Win" : " Adv");
}
private String getSameScore() {
return getScoreName(player1ScoredTimes) + " All";
}
private boolean isDeuce() {
return player1ScoredTimes >= 3;
}
private String getDeuce() {
return "Deuce";
}
private boolean isDifferenceScore() {
return player1ScoredTimes != player2ScoredTimes;
}
private boolean hasWinner() {
return Math.abs(player1ScoredTimes - player2ScoredTimes) == 2;
}
private String getAdvantagePlayerName() {
return player1ScoredTimes > player2ScoredTimes ? player1Name : player2Name;
}
private String getNormalScore() {
return getScoreName(player1ScoredTimes) + " " + getScoreName(player2ScoredTimes);
}
private boolean isReadyForWin() {
return player1ScoredTimes > 3 || player2ScoredTimes > 3;
}
private String getScoreName(int score) {
Map<Integer, String> scoreLabel = new HashMap<>();
scoreLabel.put(0, "Love");
scoreLabel.put(1, "Fifteen");
scoreLabel.put(2, "Thirty");
scoreLabel.put(3, "Forty");
return scoreLabel.get(score);
}
public void player1Scored() {
player1ScoredTimes += 1;
}
public void player2Scored() {
player2ScoredTimes += 1;
}
public String getPlayer1Name() {
return player1Name;
}
public String getPlayer2Name() {
return player2Name;
}
}
Test Case
import org.junit.Before;
import org.junit.Test;
import static com.google.common.truth.Truth.assertThat;
public class TennisGameTest {
private TennisGame tennisGame;
@Before
public void setUp() throws Exception {
tennisGame = new TennisGame("Pajace", "Tom");
}
@Test
public void newGameScoreShouldBeLoveAll() {
assertScoreShouldBe("Love All");
}
@Test
public void testFifteenLove() {
player1GetScore(1);
assertScoreShouldBe("Fifteen Love");
}
@Test
public void testThirtyLove() {
player1GetScore(2);
assertScoreShouldBe("Thirty Love");
}
@Test
public void testFortyLove() {
player1GetScore(3);
assertScoreShouldBe("Forty Love");
}
@Test
public void testLoveFifteen() {
player2GetScore(1);
assertScoreShouldBe("Love Fifteen");
}
@Test
public void testLoveThrity() {
player2GetScore(2);
assertScoreShouldBe("Love Thirty");
}
@Test
public void testLoveForty() {
player2GetScore(3);
assertScoreShouldBe("Love Forty");
}
@Test
public void testDeuce() {
givenTennisGameScore(3, 3);
assertScoreShouldBe("Deuce");
}
@Test
public void testDeuceWhen4_4() {
givenTennisGameScore(4, 4);
assertScoreShouldBe("Deuce");
}
@Test
public void testPlayer1Win() {
givenTennisGameScore(5, 3);
assertScoreShouldBe(tennisGame.getPlayer1Name() + " Win");
}
@Test
public void testPlayer2Win() {
givenTennisGameScore(3, 5);
assertScoreShouldBe(tennisGame.getPlayer2Name() + " Win");
}
@Test
public void testPlayer1Adv() {
givenTennisGameScore(4, 3);
assertScoreShouldBe(tennisGame.getPlayer1Name() + " Adv");
}
@Test
public void testPlayer2Adv() {
givenTennisGameScore(3, 4);
assertScoreShouldBe(tennisGame.getPlayer2Name() + " Adv");
}
@Test
public void testPlayer1winWithoutDeuce() {
givenTennisGameScore(4, 2);
assertScoreShouldBe(tennisGame.getPlayer1Name() + " Win");
}
@Test
public void testFifteenAll() {
givenTennisGameScore(1, 1);
assertScoreShouldBe("Fifteen All");
}
@Test
public void testThirtyAll() {
givenTennisGameScore(2, 2);
assertScoreShouldBe("Thirty All");
}
private void givenTennisGameScore(int player1Score, int player2Score) {
player1GetScore(player1Score);
player2GetScore(player2Score);
}
private void player2GetScore(int times) {
for (int i = 0; i < times; i++) {
tennisGame.player2Scored();
}
}
private void player1GetScore(int times) {
for (int i = 0; i < times; i++) {
tennisGame.player1Scored();
}
}
private void assertScoreShouldBe(String expected) {
String actual = tennisGame.getScore();
assertThat(actual).isEqualTo(expected);
}
}