Skip to content

Instantly share code, notes, and snippets.

@snasphysicist
Created August 28, 2020 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snasphysicist/a6ea34942f9658b2cac547c6ff63d028 to your computer and use it in GitHub Desktop.
Save snasphysicist/a6ea34942f9658b2cac547c6ff63d028 to your computer and use it in GitHub Desktop.
Diffblue Cover Tennis Kata 2
// Original: https://github.com/emilybache/Tennis-Refactoring-Kata/blob/master/java/src/main/java/TennisGame1.java
package com.example.tennis;
public class TennisScore {
public static class GamesWon {
final int player1;
final int player2;
public GamesWon(final int player1Won, final int player2Won) {
player1 = player1Won;
player2 = player2Won;
}
}
public String announceScore(final GamesWon gamesWon) {
if (gamesWon.player1 < 0) {
throw new IllegalArgumentException(
String.format(
"Number of games won cannot be negative, got %d for player 1", gamesWon.player1
)
);
}
if (gamesWon.player2 < 0) {
throw new IllegalArgumentException(
String.format(
"Number of games won cannot be negative, got %d for player 2", gamesWon.player2
)
);
}
String score = "";
int tempScore = 0;
if (gamesWon.player1 == gamesWon.player2) {
switch (gamesWon.player1) {
case 0:
score = "Love-All";
break;
case 1:
score = "Fifteen-All";
break;
case 2:
score = "Thirty-All";
break;
default:
score = "Deuce";
break;
}
}
else if (gamesWon.player1 >= 4 || gamesWon.player2 >= 4) {
int minusResult = gamesWon.player1 - gamesWon.player2;
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 = gamesWon.player1;
else { score += "-"; tempScore = gamesWon.player2;}
switch(tempScore) {
case 0:
score += "Love";
break;
case 1:
score += "Fifteen";
break;
case 2:
score += "Thirty";
break;
case 3:
score += "Forty";
break;
}
}
}
return score;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment