public
Created

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
class TennisGame(names : Array[String]) {
 
require(names.length == 2)
 
val player = Array[TennisPlayer] = names.map(n => new TennisPlayer(n))
 
def _score = player.map(p => p.point.count)
def score = {
scores = _score.sort
if scores[0] == scores[1] and scores[0] >= 3 "deuce"
else if scores[0] > 4 {
if scores[0] - scores[1] >= 2 describeVictory
else describeAdvantage
}
else simpleScore
 
 
 
def _winPoint(playerId) = player[playerId].winPoint
 
}
 
object PlayerScores(val player) {
 
 
 
}
 
class TennisPlayer(_name: String) {
 
name = _name
point = new TennisPoint()
 
def winPoint = point.increase
 
}
 
class TennisPoint {
 
count = 0
 
name = count match {
case 0 => "love"
case 1 => "fifteen"
case 2 => "thirty"
case 3 => "forty"
}
 
def increase = ++count
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.