Skip to content

Instantly share code, notes, and snippets.

@rchatley
Created July 6, 2016 12:57
Show Gist options
  • Save rchatley/2832044198c38e53e7b799c51854eab3 to your computer and use it in GitHub Desktop.
Save rchatley/2832044198c38e53e7b799c51854eab3 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.fail;
public class WimbledonScoreCalcTest {
private WimbledonScoreCalc scoreCalc= new WimbledonScoreCalc();
@Test
public void scoresUpdateWhenPlayerOneScores() {
assertThat(scoreCalc.score(), is("Love all"));
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("15 - Love"));
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("30 - Love"));
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("40 - Love"));
}
@Test
public void scoreUpdatesWhenPlayerOneScoresFourPointsThenTheyWinTheGame() {
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("Game Player 1"));
}
@Test
public void scoreUpdatesWhenPlayerTwoScoresFourPointsThenTheyWinTheGame() {
scoreCalc.playerTwoScores();
scoreCalc.playerTwoScores();
scoreCalc.playerTwoScores();
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Game Player 2"));
}
@Test
public void scoreUpdatesWhenPlayerTwoScores() {
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Love - 15"));
}
@Test
public void scoreUpdatesWhenPlayersBothScore() {
scoreCalc.playerOneScores();
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("15 all"));
}
@Test
public void scoreUpdatesWhenPlayersBothScoreThrice() {
gameReachesDeuce();
assertThat(scoreCalc.score(), is("Deuce"));
}
private void gameReachesDeuce() {
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
scoreCalc.playerTwoScores();
scoreCalc.playerTwoScores();
scoreCalc.playerTwoScores();
}
@Test
public void scoreUpdatesWhenPlayerOneHasAdvantage() {
gameReachesDeuce();
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("Adv Player 1"));
}
@Test
public void scoreUpdatesWhenPlayerTwoHasAdvantageAndScoresAgain() {
gameReachesDeuce();
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Adv Player 2"));
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Game Player 2"));
}
@Test
public void scoreUpdatesWhenPlayerOneHasAdvantageAndScoresAgain() {
gameReachesDeuce();
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("Adv Player 1"));
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("Game Player 1"));
}
@Test
public void scoreGoesBackToDeuceIfAdvantageIsLost(){
gameReachesDeuce();
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("Adv Player 1"));
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Deuce"));
}
@Test
public void playerTwoWinsAfterAdvantageIsLost(){
gameReachesDeuce();
scoreCalc.playerOneScores();
assertThat(scoreCalc.score(), is("Adv Player 1"));
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Deuce"));
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Adv Player 2"));
scoreCalc.playerTwoScores();
assertThat(scoreCalc.score(), is("Game Player 2"));
}
@Test
public void gamePlayCannotContinueAfterWin(){
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
scoreCalc.playerOneScores();
try {
scoreCalc.playerOneScores();
fail("Should have thrown exception");
} catch (IllegalStateException iae) {
assertThat(iae.getMessage(), is("Player cannot score after game has ended"));
}
}
}
public class WimbledonScoreCalc {
private int playerOneScore = 0;
private int playerTwoScore = 0;
private String[] scoreNames = {"Love", "15", "30", "40"};
public String score() {
if (playerOneScore > 2 && playerTwoScore > 2) {
int difference = playerOneScore - playerTwoScore;
switch (difference) {
case 0:
return "Deuce";
case 1:
return "Adv Player 1";
case -1:
return "Adv Player 2";
case 2:
return "Game Player 1";
case -2:
return "Game Player 2";
}
}
if (playerOneScore > 3) {
return "Game Player 1";
}
if (playerTwoScore > 3) {
return "Game Player 2";
}
if (playerOneScore == playerTwoScore) {
return scoreNames[playerOneScore] + " all";
}
return scoreNames[playerOneScore] + " - " + scoreNames[playerTwoScore];
}
public void playerOneScores() {
if (gameHasEnded()) {
throw new IllegalStateException("Player cannot score after game has ended");
}
playerOneScore++;
}
private boolean gameHasEnded() {
return score().contains("Game");
}
public void playerTwoScores() {
if (gameHasEnded()) {
throw new IllegalStateException("Player cannot score after game has ended");
}
playerTwoScore++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment