Tennis Score

Level: Intermediate 30–60 min

Concepts: State


Create a program to score a tennis match.

Requirements

  1. Implement tennis scoring rules:
    • 0 points = “Love”
    • 1 point = “15”
    • 2 points = “30”
    • 3 points = “40”
    • 4+ points = “Game” (if leading by 2)
  2. Handle deuce scenarios:
    • Both players at 40 = “Deuce”
    • Player ahead by 1 = “Advantage”
    • Player ahead by 2 = “Game”
  3. Support match scoring:
    • Games needed to win a set (typically 6, leading by 2)
    • Sets needed to win the match (typically 2 or 3)
    • Tiebreak rules (at 6-6)

Test Cases

ScenarioScoreExpected Output
Start of Game0-0”Love-Love”
First Point1-0”15-Love”
Two Points Each2-2”30-30”
Deuce3-3”Deuce”
Advantage4-3”Advantage Player 1”
Game Win4-2”Game Player 1”
Set Win6-4”Set Player 1”
Match Win6-4, 6-3”Match Player 1”

Edge Cases to Consider

  • Invalid score inputs
  • Negative scores
  • Impossible game states
  • Tiebreak scenarios
  • Match point situations

Tips

  • Start with basic point scoring
  • Add deuce handling next
  • Then implement game scoring
  • Finally add set and match scoring
  • Consider using enums for score states