Giter Club home page Giter Club logo

tenpin's Introduction

tenpin

Matt Stewart

[email protected]

Installation

Easiest to run via leiningen or load core.clj in the repl.

API

create-scorecard :: [[Int]]

the scorecard is represented by a 2d vector containing 12 frames [[-1 -1] [-1 -1] ...].

The raw data structure uses 12 frames as it simplifies handling the 2 fill frames should a spare or strike occur on the 10th frame.

Frame type representations:

strike

[10 -1]

spare

both rolls must be 0 - 10 inclusive and sum to 10

[7 3]

open

both rolls must be 0 - 10 inclusive and sum to less than 10

[1 3]

not used:

No rolls yet for this frame

[-1 -1]

special 10th frame case

If a spare is rolled on the 10th frame, 1 more roll needs to be stored in the 11th frame. The vector must have its second roll fixed at zero, eg:

[5 0]

calculate-scorecard :: [[Int]] -> CalculatedScorecardResult

transforms the raw scorecard into a calculated result. The game is complete when the total is not -1 as it implies every frame has sucessfully resolved its computed score. Should the game be in a pending state, all frames that can resolve their scores will do so, however the total will still be -1 until all frames have resolved scores.

where CalculatedScorecardResult is a map -> { :calculated-scorecard [{:score Int :symbol String :rolls [Int]}] :total Int }

Example:

(calculate-scorecard [[10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1] [10 -1]])

=>
{
 :calculated-scorecard [
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X", :rolls [10 10 10]} 
    {:score 30, :symbol "X X X", :rolls [10 10 10]}
    ], 
:total 300
}

finished? :: CalculatedScorecardResult -> Bool

Simply just returns the result of total != -1

score-frame :: [[Int]] -> Int -> [Int] -> [[Int]]

Updates a frames score by taking in the scorecard, index position of the frame and the new frame and returns back the updated scorecard.

For example, to update the scorecards first frame with a strike

(score-frame [[-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1]] 0 [10 -1])

=>

[[10 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1] [-1 -1]] 

Validation

clojure spec is used to validate at the edges only. This simplifies internal helper functions as they can guarantee the scoreboard vector is in a valid state. Its assumed the create-scoreboard is used to create the initial board, then the score-frame receives most of the validation to ensure the scorecard remains valid throughout the games lifetime.

tenpin's People

Contributors

mjstewart avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.