Sha256: 1a343c19365a4547d6f5769e84583672cddff7e08343b29b59d23ce8b9b263dd

Contents?: true

Size: 1.34 KB

Versions: 384

Compression:

Stored size: 1.34 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
        assert(current[2] == 10 or current[2] + current[3] <= 10, 'illegal roll')
        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
        local total = current[1] + current[2]
        assert(total <= 10, 'illegal roll')
        table.insert(frames, total)
        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

384 entries across 384 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.179 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.178 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.177 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.176 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.175 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.174 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.173 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.172 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.171 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.170 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.169 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.167 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.166 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.165 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.164 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.163 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.162 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.161 tracks/lua/exercises/bowling/example.lua
trackler-2.2.1.160 tracks/lua/exercises/bowling/example.lua