commit 0: 9683ed052622
branch: default
tags: tip
adding groovy stuff from dojo
mne...@markneedham.local
8 months ago
bowling-game-groovy / src / BowlingGame.groovy
r0:9683ed052622 55 loc 874 bytes embed / history / annotate / raw /
public class Frame {
  def Frame (firstGo,  secondGo) {
    this.firstGo = firstGo
    this.secondGo = secondGo
    this.total = firstGo + secondGo
  }

  def firstGo
  def secondGo
  def total

  def isAStrike () {
    firstGo == 10  
  }

  boolean isASpare() {
    !isAStrike() && total == 10
  }


  def static STRIKE = new Frame(10,0)
}

public class BowlingGame {

  def frames = []

  def add(frame) {
    frames << frame
  }


  def totalScore() {
    def total = 0

    def reversedList = frames.reverse()
    reversedList.eachWithIndex {frame, index ->
      total += frame.total


      if(index + 1 < reversedList.size()) {
        def nextFrame = reversedList[index + 1]

        if (nextFrame.isASpare()) {
          total += frame.firstGo
        }
        if (nextFrame.isAStrike()) {
          total +=  frame.total
        }
      }
    }

    total
  }

}