PyUGAT

Bowling

Aufgabenstellung

Die Aufgabe von der CodingDojo-Seite:

Create a program, which, given a valid sequence of rolls for one line of American Ten-Pin Bowling, produces the total score for the game. Here are some things that the program will not do:

  • We will not check for valid rolls.
  • We will not check for correct number of rolls and frames.
  • We will not provide scores for intermediate frames.

Depending on the application, this might or might not be a valid way to define a complete story, but we do it here for purposes of keeping the kata light. I think you'll see that improvements like those above would go in readily if they were needed for real. We can briefly summarize the scoring for this form of bowling:

  • Each game, or "line" of bowling, includes ten turns, or "frames" for the bowler.
  • In each frame, the bowler gets up to two tries to knock down all the pins.
  • If in two tries, he fails to knock them all down, his score for that frame is the total number of pins knocked down in his two tries.
  • If in two tries he knocks them all down, this is called a "spare" and his score for the frame is ten plus the number of pins knocked down on his next throw (in his next turn).
  • If on his first try in the frame he knocks down all the pins, this is called a "strike". His turn is over, and his score for the frame is ten plus the simple total of the pins knocked down in his next two rolls.
  • If he gets a spare or strike in the last (tenth) frame, the bowler gets to throw one or two more bonus balls, respectively. These bonus throws are taken as part of the same turn. If the bonus throws knock down all the pins, the process does not repeat: the bonus throws are only used to calculate the score of the final frame.
  • The game score is the total of all frame scores.

More info on the rules at: www.topendsports.com/sport/tenpin/scoring.htm

Das Ergebnis unserer gemeinsamen Bemühungen ist auf Bitbucket zu bewundern:

Für alle, die ihre eigene Lösung für das Problem entwickeln wollen: schreibt ein Python-Script, das einen String mit den Ergebnissen der einzelnen Würfe als Input nimmt und das Endergebnis als Zahl ausspuckt. Die Anzahl der umgefallen Pins wird durch eine Ziffer (0-9) angegeben, ein Miss mit einem Dash (-), ein Strike mit einem X und ein Spare mit einem Slash (/).

Testdaten

$ python bowling.py XXXXXXXXXXXX
300
$ python bowling.py 9-9-9-9-9-9-9-9-9-9-
90
$ python bowling.py 5/5/5/5/5/5/5/5/5/5/5
150
$ python bowling.py -1273/X5/7/3454--X7-
113
$ python bowling.py 'X7/9-X-88/-6XXX81
167

TEST_CASES = (
    ('XXXXXXXXXXXX', 300),
    ('9-9-9-9-9-9-9-9-9-9-', 90),
    ('5/5/5/5/5/5/5/5/5/5/5', 150),
    ('X7/729/XXX236/7/3', 168),
    ('--------------------', 0),
    ('-1273/X5/7/3454--X7-', 113),
    ('X7/9-X-88/-6XXX81', 167),
)

Diskussion

von David:

class Line

        data storage:
                a list that can hold 10 or 11 Frames

        method score()
                return the sum of score() from all Frames, taken in any order

        constructor( string rolls )
                do this 10 times
                        make a Frame(rolls)
                        append it to the list
                if rolls string is not empty now
                        make and append a BonusFrame( rolls )
                do this over the entire list in reverse order, skipping item [0]
                        make the next lower Frame's nextFrame refer to the current Frame

class Frame

        data storage:
                a Frame reference called nextFrame
                integers roll1 and roll2

        constructor( string rolls )
                extract and parse one number, store it in roll1
                if it's 10
                        set roll2 to zero
                else
                        extract and parse one number, store it in roll2

        method bonus1()
                return roll1

        method bonus2()
                if roll2 is 0
                        return roll1 plus bonus1() from nextFrame
                return roll1 plus roll2

        method score()
                if roll1 is 10
                        return roll1 plus bonus2() from nextFrame
                total equals roll1 plus roll2
                if total is not 10
                        return total
                return total plus bonus1() from nextFrame

class BonusFrame inherits from  Frame

        constructor( string rolls )
                pass rolls to base class constructor

        method score()
                return zero

PyUGAT: PythonDojo/KataBowling (last edited 2012-10-21 23:01:17 by hop)