Tennis Kata – My implementation in Java of the tennis kata

Intro

This Kata is about implementing a simple tennis game. For those not familiar with the concept of kata it’s:

An exercise in karate where you repeat a form many, many times, making little improvements in each. The intent behind code kata is similar. Each is a short exercise (perhaps 30 minutes to an hour long). Some involve programming, and can be coded in many different ways. Some are open ended, and involve thinking about the issues behind programming. These are unlikely to have a single correct answer.The point of the kata is not arriving at a correct answer. The point is the stuff you learn along the way. The goal is the practice, not the solution.

Tennis scoring rules

The scoring system for a Tennis game is kinda simple, also not really 😅, however, you will get a grasp at it easily:

  1. Points – Smallest unit of measurement. Points increment from 0-15-30-40-game.
  2. Games – Games consist of 4 points each, and are won when a player reaches 4 points with at least a 2 point advantage.
  3. Deuce – Occurs if a score of 40-40 is reached. In order to win the game, a player/team must win 2 consecutive points in order to take the game. If a player wins one point, they have an advantage, but if they lose the next point, the score returns to deuce.
  4. Sets – A set consists of 6 games and is won by the player/team who reaches 6 games first with at least a 2 point lead.
  5. Tie-break game – If a game score of 6-6 is reached and tie-break set rules are used, players must play a tie-break game in order to decide who wins the set. In a tie-break game, a player/team must reach 7 points with a two-point advantage to win.
  6. Matches – A match is played as best of 3 or best of 5 sets. Here will go with best-of-5 Grand Slam version.

Project structure

After reading the rules we can start visualizing the main classes that could be used by our project, in our case, they will be:

  • Player Class;
  • Match Class;
  • Set Class;
  • Game Class;
  • DisplayInformation Interface.

Where each Match will have multiple Sets, each Set will contain multiple Games and all will need 2 Players.

For example, we will create 2 Players that will create a new Match Object. A Match Object will contain multiple Set Objects that will create multiple Game objects.

Games will be played until a Set is won by one of the players. Sets will be played until one of the Players wins the Match.

Here’s a little schema just to visualize the points I mentioned above 😉

N.B: You notice here that each class manages only and fully its perimeter.

For example, a Game could be called from a Set (since a set is composed of multiple games), but also could be called alone from the Main method if we want to play one or multiple games independently.

Finally, DisplayInformation interface will specify the behavior that classes must implement in order to display information needed along the lifetime of a Game, a Set, or a Match.

Achieving abstraction here ensures what objects display instead of how they will display information (System.out.print, writing info into the file system, etc).

Project implementation

Since we have a clear idea of the objects that we need, all we gonna do is to make each object manage its perimeter.

The Game will manage everything related to how a Game works:

  • Initialize its fields through its constructor (at object creation);
  • Manage scores incrementations of each player (0, 15, 30, etc);
  • Apply the Deuce rule when the score gets to (40 – 40);
  • Designate a winner when one of the players meets the requirements to win.

The Set will manage everything related to how a set works:

  • Initialize its fields through its constructor (at object creation);
  • Manage scores incrementations of each player (1, 2, 3, etc);
  • Play a Tie-break game when the score of the Set gets to (6 – 6);
  • Designate a winner when one of the players meets the requirements to win.

The Match will manage everything related to how a match works:

  • Initialize its fields through its constructor (at object creation);
  • Manage scores incrementations of each player (1, 2, 3, etc);
  • Designate a winner when a player wins 3 sets out of 5.

DisplayInformationImpl will manage everything related to displaying information using System.out regarding a Tennis Game, Set, or Match, such as:

  • The current Game Score is: ( 40 – 15 )
  • Player1 has scored 1 point!
  • The winner of the Game is Player1
  • etc.

Finally, create a Main Class where we are going to create our objects and provide them with the required information (player’s names for example) so a Tennis Match could start.

N.B: Just to simplify our code, points will be attributed randomly to avoid having to manually choose which player scores along the lifetime of a Match, Set, or Game.

Let’s run this, shall we?

With a simple console user interface, we provide each player a username, then the application can run a Tennis match where Luck decides which player scores on each turn. Along the way, scores are displayed to show the progress of each player to win the Tennis match.

The code

You can take a look at my implementation of the kata on GitHub:

https://github.com/Ilagouilly/Tennis-Kata-Java-Project

Any feedback about the code, or ideas for different implementations are welcome!

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *