Sha256: f94d6300b95b70c304185f5787212def2dffa8771edf83afecea5ed6629fc362

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

return function()
  local current = {}
  local frames = {}

  local function strike()
    return current[1] == 10
  end

  local function spare()
    return current[1] + current[2] == 10
  end

  local function complete()
    return #frames == 10
  end

  return {
    roll = function(pins)
      assert(not complete(), 'the game is already over')
      assert(pins >= 0 and pins <= 10, 'illegal roll')

      table.insert(current, pins)

      if #current == 3 and strike() then
        table.insert(frames, current[1] + current[2] + current[3])
        table.remove(current, 1)
      end

      if #current == 3 and spare() then
        table.insert(frames, current[1] + current[2] + current[3])
        table.remove(current, 1)
        table.remove(current, 1)
      end

      if #frames < 10 and #current == 2 and not spare() and not strike() then
        table.insert(frames, current[1] + current[2])
        table.remove(current, 1)
        table.remove(current, 1)
      end
    end,

    score = function()
      assert(complete(), 'the game is not yet over')

      local score = 0
      for _, frame_score in ipairs(frames) do
        score = score + frame_score
      end
      return score
    end
  }
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
trackler-2.0.0.1 tracks/lua/exercises/bowling/example.lua
trackler-2.0.0.0 tracks/lua/exercises/bowling/example.lua
trackler-1.0.4.1 tracks/lua/exercises/bowling/example.lua
trackler-1.0.4.0 tracks/lua/exercises/bowling/example.lua
trackler-1.0.3.0 tracks/lua/exercises/bowling/example.lua
trackler-1.0.2.1 tracks/lua/exercises/bowling/example.lua
trackler-1.0.2.0 tracks/lua/exercises/bowling/example.lua
trackler-1.0.1.2 tracks/lua/exercises/bowling/example.lua
trackler-1.0.1.1 tracks/lua/exercises/bowling/example.lua
trackler-1.0.1.0 tracks/lua/exercises/bowling/example.lua
trackler-1.0.0.1 tracks/lua/exercises/bowling/example.lua
trackler-1.0.0 tracks/lua/exercises/bowling/example.lua