Created
December 13, 2019 15:52
-
-
Save sk1418/f93bf3592965ec4a08f64f0de9d96691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
import static java.lang.Math.*; | |
@AllArgsConstructor | |
public class Game { | |
private Player p1; | |
private Player p2; | |
private static final int DEUCE_POINT = 3; | |
private static final String DEUCE = " Deuce "; | |
private static final String[] B4_DEUCE_RESULTS = { " Love ", " Fifteen ", " Thirty ", " Forty " }; | |
private static final String[] AFTER_DEUCE_RESULTS = { DEUCE, " Advantage ", " Win " }; | |
public String printResult() { | |
if (max(p1.getPoints(), p2.getPoints()) > DEUCE_POINT) { | |
return getAfterDeuce().trim(); | |
} | |
if (p1.getPoints() == p2.getPoints()) { | |
return getEqualResult(p1.getPoints(), p2.getPoints()).trim(); | |
} | |
return (B4_DEUCE_RESULTS[p1.getPoints()] + ":" + B4_DEUCE_RESULTS[p2.getPoints()]).trim(); | |
} | |
private String getAfterDeuce() { | |
String name = p1.getPoints() > p2.getPoints() ? p1.getName() : p2.getName(); | |
int diff = abs(p1.getPoints() - p2.getPoints()); | |
return (diff == 0 ? "" : name + " ") + AFTER_DEUCE_RESULTS[min(diff, AFTER_DEUCE_RESULTS.length - 1)]; | |
} | |
private String getEqualResult(int point1, int point2) { | |
assert point1 == point2; | |
return (point1 >= DEUCE_POINT) ? DEUCE : B4_DEUCE_RESULTS[p1.getPoints()] + " ALL"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.aviatar.condax.faulttelex.parser.tennis; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class GameTest { | |
private Player p1 = new Player("Kai"); | |
private Player p2 = new Player("Dirk"); | |
private Game game = new Game(p1, p2); | |
@Before | |
public void reset() { | |
p1.setPoints(0); | |
p2.setPoints(0); | |
} | |
@Test | |
public void test_0_0() { | |
String r = game.printResult(); | |
assertEquals("Love ALL", r); | |
} | |
@Test | |
public void test_0_15() { | |
p1.setPoints(0); | |
p2.setPoints(1); | |
String r = game.printResult(); | |
assertEquals("Love : Fifteen", r); | |
} | |
@Test | |
public void test_15_15() { | |
p1.setPoints(1); | |
p2.setPoints(1); | |
String r = game.printResult(); | |
assertEquals("Fifteen ALL", r); | |
} | |
@Test | |
public void test_15_30() { | |
p1.setPoints(1); | |
p2.setPoints(2); | |
String r = game.printResult(); | |
assertEquals("Fifteen : Thirty", r); | |
} | |
@Test | |
public void test_40_40() { | |
p1.setPoints(3); | |
p2.setPoints(3); | |
String r = game.printResult(); | |
assertEquals("Deuce", r); | |
} | |
@Test | |
public void test_40_45() { | |
p1.setPoints(3); | |
p2.setPoints(4); | |
String r = game.printResult(); | |
assertEquals(p2.getName() + " Advantage", r); | |
} | |
@Test | |
public void test_45_40() { | |
p1.setPoints(4); | |
p2.setPoints(3); | |
String r = game.printResult(); | |
assertEquals(p1.getName() + " Advantage", r); | |
} | |
@Test | |
public void test_x_x_deuce() { | |
p1.setPoints(30); | |
p2.setPoints(30); | |
String r = game.printResult(); | |
assertEquals("Deuce", r); | |
} | |
@Test | |
public void test_70_50() { | |
p1.setPoints(6); | |
p2.setPoints(4); | |
String r = game.printResult(); | |
assertEquals(p1.getName() + " Win", r); | |
} | |
@Test | |
public void test_0_50() { | |
p1.setPoints(0); | |
p2.setPoints(5); | |
String r = game.printResult(); | |
assertEquals(p2.getName() + " Win", r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment