Scala Tutorial Through Katas: Tennis Game (Easy)

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

This article is part of the series “Scala Tutorial Through Katas”. Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.

For the complete list of Scala katas and solutions please visit the index page

The article assumes that the reader is familiar with the basic usage of ScalaTest asserts and knows how to run them from his favorite Scala IDE (ours is IntelliJ IDEA with the Scala plugin).

Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.

One possible solution is provided below the tests. Try to solve the kata by yourself first.

Tennis Game

Implement a simple tennis game

Rules:

  • Scores from zero to three points are described as “love”, “fifteen”, “thirty”, and “forty” respectively.
  • If at least three points have been scored by each side and a player has one more point than his opponent, the score of the game is “advantage” for the player in the lead.
  • If at least three points have been scored by each player, and the scores are equal, the score is “deuce”.
  • A game is won by the first player to have won at least four points in total and at least two points more than the opponent.

Following is a set of unit tests that can be used to solve this kata in the TDD fashion.

[UNIT TESTS]

class TennisGameUnitTest extends FlatSpec with Matchers {

  "Points" can "be added to each player" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    victor.winBall
    victor.winBall
    sarah.winBall
    victor.winBall
    victor.score should be (3)
    sarah.score should be (1)
  }

  "Love" should "be description for score 0" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    game.score should be ("love, love")
  }

  "Fifteen" should "be description for score 1" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    sarah.winBall
    game.score should be ("love, fifteen")
  }

  "Thirty" should "be description for score 2" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    victor.winBall
    victor.winBall
    sarah.winBall
    game.score should be ("thirty, fifteen")
  }

  "Forty" should "be description for score 3" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    (1 to 3).foreach(x => victor.winBall)
    game.score should be ("forty, love")
  }

  "Advantage" should "describe when least three points have been scored by each side and a player has one more point than his opponent" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    (1 to 3).foreach(x => victor.winBall)
    (1 to 4).foreach(x => sarah.winBall)
    game.score should be ("advantage Sarah")
  }

  "Deuce" should "be description when at least three points have been scored by each player and the scores are equal" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    (1 to 3).foreach(x => victor.winBall)
    (1 to 3).foreach(x => sarah.winBall)
    game.score should be ("deuce")
    victor.winBall
    game.score should not be "deuce"
    sarah.winBall
    game.score should be ("deuce")
  }

  "Game" should "be won by the first player to have won at least four points in total and with at least two points more than the opponent" in {
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)
    (1 to 4).foreach(x => victor.winBall)
    (1 to 3).foreach(x => sarah.winBall)
    game.score should not be "Victor won"
    game.score should not be "Sarah won"
    victor.winBall
    game.score should be ("Victor won")
  }

}

Following is the BDD scenario that should be used as acceptance criteria.

[BDD SCENARIO]

class TennisGameScenarioTest extends FeatureSpec with GivenWhenThen with Matchers {

  scenario("Simple tennis game") {

    Given("new game between Victor and Sarah starts")
    val victor = Player("Victor")
    val sarah = Player("Sarah")
    val game = new TennisGame(victor, sarah)

    Then("score is love, love")
    game.score should be ("love, love")

    When("Victor wins the ball")
    victor.winBall

    Then("score is fifteen, love")
    game.score should be ("fifteen, love")

    When("Victor wins the ball")
    victor.winBall

    Then("score is thirty, love")
    game.score should be ("thirty, love")

    When("Victor wins the ball")
    victor.winBall

    Then("score is forty, love")
    game.score should be ("forty, love")

    When("Sarah wins the ball")
    sarah.winBall

    Then("score is forty, fifteen")
    game.score should be ("forty, fifteen")

    When("Sarah wins the ball")
    sarah.winBall

    Then("score is forty, thirty")
    game.score should be ("forty, thirty")

    When("Sarah wins the ball")
    sarah.winBall

    Then("score is deuce")
    game.score should be ("deuce")

    When("Victor wins the ball")
    victor.winBall

    Then("score is advantage Victor")
    game.score should be ("advantage Victor")

    When("Sarah wins the ball")
    sarah.winBall

    Then("score is deuce")
    game.score should be ("deuce")

    When("Sarah wins the ball")
    sarah.winBall

    Then("score is advantage Sarah")
    game.score should be ("advantage Sarah")

    When("Sarah wins the ball")
    sarah.winBall

    Then("score is Sarah won")
    game.score should be ("Sarah won")

  }

}

Test code can be found in the GitHub TennisGame.scala.

[ONE POSSIBLE SOLUTION]

class TennisGame(player1: Player, player2: Player) {

  def score = {
    if (player1.score >= 3 && player2.score >= 3) {
      if (Math.abs(player2.score - player1.score) >= 2) leadPlayer.name + " won"
      else if (player1.score == player2.score) "deuce"
      else "advantage " + leadPlayer.name
    } else player1.scoreDescription + ", " + player2.scoreDescription
  }

  def leadPlayer = if (player1 > player2) player1 else player2

}

class Player(val name: String) extends Ordered[Player] {
  val pointsDescription = Array("love", "fifteen", "thirty", "forty")
  var score = 0
  def scoreDescription = pointsDescription(score)
  def compare(that: Player) = this.score - that.score
  def winBall { score += 1 }
}
object Player {
  def apply(name: String) = {new Player(name)}
}

The solution code can be found in TennisGame.scala solution.

What was your solution? Post it as a comment so that we can compare different ways to solve this kata.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s