/tennis-score-logic

Implementation of the tennis score rules logic

Primary LanguageJava

Tennis score logic

Description

Write a program that can track the score of a tennis match:

  • The main class should have three accessible methods playerAWonPoint(), playerBWonPoint(), currentScore()
  • currentScore() should return a String of the form

<player A sets>/<player B sets> <player A games>/<player B games> <player A roundPoints>/<player B roundPoints>_

  • For example, "2/1 3/2 AD/40" indicating that A is 2 sets to 1 up, A is 3 games to 2 up in the fourth set, and has Advantage in the current game

  • Another example, "2/1 6/6 5/3" indicating that A is 2 sets to 1 up, it is 6 all in the fourth set and player A is leading the tie break 5-3

  • It should use the rules as per Wimbledon Men’s Singles, i.e. 5 sets and a tie-break if 12-12 is reached in the fifth set

See https://en.wikipedia.org/wiki/Tennis_scoring_system for reference.

Prerequisites and Solutions

  • Code: Java 8, Gradle.
  • Application is divided into sub apps (here domain model is presented only).
  • Functional tests: Spock, Groovy.
  • Application architecture - simple due to complex logic and short time to implement, written according to TDD and DDD approach.
  • Every event (point) is giving new object - this allows to keep the track of the score history, is thread safe and could be a base for event sourcing architecture.

The pure functional approach

The functional approach is desired, but it has some disadvantages too.

  • it's hard to debug lambda functions
  • it's easy to make mistake
  • it's very hard to follow the logic of the code

At some point I decided to rewrite everything to structural constructions (v2,v3) - bug has magically fixed itself. Decided to focus more on logic and tests (TDD). In the next step, when I fully understood the domain logic, I've refactored the code to make it easier to read and extend. Could go deeper into functional style, but as mentioned above I am not a fun of the over-engineering, I've rather focused on DDD approach.

Testing the logic

  • use gradle:

./gradlew clean build

Test coverage report (100%)

Code coverage

TODO:
  • write more tests - cover corner cases for set rules
  • finish set rules logic (now it is borrowed from games rules)
  • rewrite to functional structure - almost done
  • rewrite to Java 11 - yeah...