r/dailyprogrammer • u/jnazario 2 0 • Oct 07 '15
[2015-10-07] Challenge #235 [Intermediate] Scoring a Bowling Game
Description
The game of bowling is pretty simple: you have ten pins arranged in a triangle, and you roll a ball down a slick alley towards them and try and knock as many down as possible. In most frames (see below about the tenth frame) you get two attempts per "frame" before the remaining pins are cleared.
The bowler is allowed 10 frames in which to knock down pins, with frames one (1) through nine (9) being composed of up to two rolls. The tenth frame may be composed of up to three rolls: the bonus roll(s) following a strike or spare in the tenth (sometimes referred to as the eleventh and twelfth frames) are fill ball(s) used only to calculate the score of the mark rolled in the tenth.
Bowing scoring is a bit tricky (which is why this is an intermediate challenge). In addition to a gutter ball (which is 0 pins), you have strikes and spares as well as 1 to 9 pins being knocked down. Strikes and spares affect the next balls in different ways.
When all ten pins are knocked down with the first ball of a frame (called a strike and typically rendered as an "X" on a scoresheet), a player is awarded ten points, plus a bonus of whatever is scored with the next two balls. In this way, the points scored for the two balls after the strike are counted twice.
A "spare" is awarded when no pins are left standing after the second ball of a frame; i.e., a player uses both balls of a frame to clear all ten pins. A player achieving a spare is awarded ten points, plus a bonus of whatever is scored with the next ball (only the first ball is counted). It is typically rendered as a slash on scoresheets in place of the second pin count for a frame.
Your challenge today is to determine a bowling score from a score sheet.
Input Description
You'll be given a bowling sheet for a single person on a line, with the ten frames separated by a space. All scores are single characters: - for zero (aka a gutter ball), 1-9 for 1-9 pins knocked down, / for a spare, and X for a strike. If you see any two digit numbers (e.g. "63") that's actually the first and second ball scores (a six then a three). Example:
X X X X X X X X X XXX Output Description
Your program should calculate the score for the sheet and report it. From our example:
300Aka a perfect game.
Challenge Input
X -/ X 5- 8/ 9- X 81 1- 4/X
62 71 X 9- 8/ X X 35 72 5/8Challenge Output
137
140Bonus ASCII Art
! ! ! !
." ! ! ! /
." ! ! /
." ! /
." /
." o /
." /
." /
." /
." . '. /
." ' ' /
." / grh
." 0 | /
|/
/|
/ |Credit
This challenge was suggested by /u/firebolt0777. If you have an idea for a challenge please share it over at /r/dailyprogrammer_ideas and we may wind up using it!
31
u/fvandepitte 0 0 Oct 07 '15
+1 for ASCII art alone.
8
u/vesche Oct 07 '15 edited Oct 07 '15
Python 2.7 (assumes valid input)
TIL that I've gone 23 years of my life without knowing how bowling was scored.
Edit: Cleaned up solution.
8
u/lukz 2 0 Oct 08 '15
Z80 assembly
Program runs on Sharp MZ-800 computer. Go into monitor, run it using G1200 command and put the input on the input line right after the G1200 command. The program prints output on the next line and returns. Assembled program is 110 bytes long.
Screenshot
1
u/jnazario 2 0 Oct 07 '15
This is a great teardown of a Ruby solution to a very similar challenge. http://rubyquiz.strd6.com/quizzes/181-bowling-scores
5
u/casualfrog Oct 07 '15 edited Oct 07 '15
JavaScript (feedback welcome):
Output:
5
u/NeuroXc Oct 07 '15
A crude solution in Rust. Apparently I never knew how the last frame in bowling is actually scored until today.
5
u/[deleted] Oct 08 '15
Python 3.4. First submission:
4
u/hooksfordays Oct 07 '15
I've actually done this before! But for 5 pin bowling, which is a Canadian version of "regular" bowling (called 10 pin bowling here - we have both).
In 5 pin bowling though, each of the 5 pins have different values, rather than them all being worth 1. In order from left to right, the values are 2, 3, 5, 3, 2. You also get 3 balls per frame, instead of just 2, and the highest score possible is 450 instead of 300. And finally, the balls are way smaller. They're are no thumb holes, you just grip them in your hand. They're a bit bigger than a softball.
So my solution was written for an Android app I made to keep track of your bowling statistics over the year, like how many times you get a strike, or which pins you knock down / miss the most. I'm not going to post my solution, however, because it's horrendous code :P but I look forward to seeing how much better I could have done it!
4
u/Godspiral 3 3 Oct 07 '15 edited Oct 07 '15
In J,
as frame scores
8 8 19 9 20 23 18 8 9 18
20 20 15 5 19 9 19 9 1 20
pins function was originally written just to get 10th frame, but its useful for bonuses to frames. Mess on the left of it is to deal with single strike throw. It initially converts 'X-/' to 10 0 _1 (and other tokens to their digit) and then processes _1s (spares) to have the pin count from that throw.
framval was more descriptive than I needed it to be. Its used for first 9 frames to get total and number of balls thrown. I could have simply gotten the total. The total for a frame is take the pins (ball) list of that frame and next 3 frames, and if the frame is a strike or spare (10 total), it is the total of the first 3 pins in that list. Else, its the frame total (less than 10). Frame 10 is just the frame pinlist total.
4
u/Relayerduos Oct 08 '15
My first solution here! Very excited. This is my first program in python, too!
3
u/curtmack Oct 07 '15
Clojure
The story of how this code was written:
The actual code:
I actually had an idea for how to implement this that would have used the
trampolinefunction, but I forgot what it was, which kind of makes me sad becausetrampolineis my favorite useless function.3
u/a_Happy_Tiny_Bunny Oct 08 '15 edited Oct 08 '15
Haskell
The case chain grew a little too unwieldy for my tastes. I should have probably coded a solution with more data type—similar to /u/fvandepitte's—in order to avoid this issue and improve clarity.
EDIT: Removed
calculateScoreredundancy, altered the indentation ofcalculateScore's case-chain, correctedbreakUp10thto handle 10th frames types not exemplified in the provided inputs, and greatly simplifiedframeToPointsby getting rid of silly pattern matching.3
u/gabyjunior 1 2 Oct 10 '15
C with syntax error detection, score = -1 for each invalid line
Source code
Input including challenge and other test data found on the net
Output
2
u/Hypersapien Oct 07 '15 edited Oct 07 '15
C#
Assumes the input is in the correct format. Also corrects for extra spaces between frames.
I'm new to this subreddit so I appreciate any comments.
2
u/fjom Oct 07 '15 edited Oct 09 '15
C#
I think there should be a test case where the 10th frame only has 2 throws (ie neither a strike nor a spare was acheived):
For example, replacing the second challenge input´s next to last throw with 3 pins, there would not be an extra throw, so the full line would be
62 71 X 9- 8/ X X 35 72 53
And the score should be 130
*Edit: Now with 100% more input validity checks
2
u/gju_ Oct 08 '15
Python3
Hello, this is my solution using Python3 and some index 'magic'. It's probably a pretty messy solution. Any improvement proposals or the like are very welcome.
2
u/mn-haskell-guy 1 0 Oct 08 '15 edited Oct 08 '15
This is what I call "classical Haskell".
The logic in
parseFramescould be factored a bit to avoid repeated calls toisDigit.Updated version - uses mutual recursion for parsing and handles incomplete games gracefully.
2
u/Nielsio Oct 09 '15 edited Oct 09 '15
Python 3.4.3
First time submitting here, hope the formatting comes out okay. Didn't use any fancy tricks, just plainly adding the regular scores, bonus scores, and to make a slight adjustment for the final bonus throw.
The problem became easier once I realized I could just treat the entire sequence as one, instead of caring about frames.
2
u/Vakz Oct 21 '15
C++
1
u/glenbolake 2 0 Oct 07 '15 edited Oct 07 '15
Python 3. Does not produce ASCII art.
EDIT: Added input validation (but didn't test on invalid games)
1
u/JoeOfDestiny Oct 07 '15
Shouldn't a perfect game be 330, not 300? The first 9 rolls are worth 30 points each (10 plus 20 for the next two rolls), so they add up to 270 total. Then, in the 10th frame, the first roll is worth 30 (10 plus 20 for the next two rolls), the second roll is worth 20 (10 plus 10 for the next roll), and the final roll is worth 10. That adds up to 330, not 300. Does the rule about adding extra points to a strike not apply in the 10th frame?
1
u/Jambecca Oct 07 '15
Spacing looks a little wonky on the second challenge input. Is that on purpose? Anyway, C#, assumes a single space between frames.
1
u/fvandepitte 0 0 Oct 07 '15
I think I have a problem in my logic
It's faulty, I'll look into it tomorow
1
u/Eggbert345 Oct 07 '15
Golang solution. Really took me awhile to wrap my head around that last frame. Had to go and read up on other websites to figure it out.
1
u/PMMeYourPJs Oct 07 '15
Java. I didn't know how to score bowling until just now so I'm sorry if my output is a bit unorthodox.
Outputs:
1
u/alisterr Oct 07 '15
Naive solution in Rust. I'm surprised to learn the scoring model of bowling, and at the same time disappointed how ugly my logic is :/
1
u/sbditto85 Oct 07 '15
In Rust, instead of reading input from a file i just feed the function a vector or "throws" which is basically the value of each throw as a char. I'm sure I could improve this, but meh it works.
The points returned in the main function is also my highest score bowling :)
1
u/christopherjwang Oct 07 '15
Python
Iterating through an unmodified input once.
1
u/thursday42 Oct 08 '15 edited Oct 08 '15
Java:
Edit: Added conditions for possibilities I missed for the 10th frame, and condensed code slightly.
Output:
1
u/[deleted] Oct 08 '15
Java
Trying to do the extensible/maintainable approach over pure performance, since I am in Java. Feel free to comment on it if you see anything!
GitHub Repo
1
u/shlomocomputer Oct 08 '15
Lua 5.2. Does Lua strongly remind anyone besides me of Javascript?
1
u/firebolt0777 1 0 Oct 08 '15
Yay, I does well?
1
u/minikomi Oct 08 '15 edited Oct 08 '15
Racket:
1
u/shlomocomputer Oct 08 '15
C
1
u/ikkeflikkeri Oct 08 '15 edited Oct 08 '15
C#
Hi, this is my second submissing! I'm starting to like this sub.
I think i made it a bit too complicated, but it isn't that bad.
Feedback is appreciated! Have a nice day.
Input:
Output:
1
u/grangelov Oct 08 '15
Perl
1
u/banProsper Oct 08 '15
C#
1
u/veevax Oct 08 '15 edited Oct 08 '15
Javascript, with no I/O. Assuming only one space between frames.
1
u/Quistic Oct 08 '15 edited Oct 09 '15
C# I am a novice at C# so I wanted to try this challenge with C#. Might not be so elegant.
1
u/YonkeMuffinMan Oct 08 '15
Python 2.7
1
u/carbonetc Oct 08 '15
JavaScript
https://jsfiddle.net/pendensproditor/m8mrh84r/
The final frame took some thinking. I realized I had a faulty memory of how bowling is scored and couldn't figure out why my numbers didn't match up.
1
u/mhuiwang Oct 08 '15 edited Oct 08 '15
With scala, instead of calculating a score per frame, I calculate a score per ball. The score is divided into two parts : oneToNine and the tenth frame.
With Unit tests
1
u/Kobel Oct 08 '15 edited Oct 09 '15
Using ES6 flavoured JavaScript, as I want to start getting the hang of it.
Calculator module:
To run it:
1
u/DStekel Oct 09 '15
My C# solution. Probably terrible, because of the length. But it works :P
1
u/Rashnok Oct 09 '15 edited Oct 09 '15
python 3, got lazy with the input
1
u/chrissou Oct 09 '15
Haskell solution Really ugly strike and spare scores calculations ... My model really isn't optimal In the end it's really harder than you think!
1
u/WTFalreadytaken Oct 10 '15
First submission :) Just starting out with Scala. Open for any feedback. Just a bunch of pattern matching and I did go with scoring each frame individually.
Scala
**Output **
1
u/zengargoyle Oct 11 '15
Trying not to think about it too hard and just doing what a person would do. Here's some Perl...
Output:
1
u/spx88 Oct 13 '15
Java 1.7 (Feedback very welcome)
1
u/NihilCredo Oct 13 '15 edited Oct 13 '15
F#
I'd love feedback!
Note: I intentionally avoided checking for the total number of frames so the function can be used just as well for a running score.
1
u/orsonreads Oct 14 '15 edited Oct 14 '15
first time poster, dirty code, Im digging this sub~ python 2.7
1
u/[deleted] Oct 15 '15
C - Another attempt at solving a problem in C99. Any pointers (pun not intended!), optimizations, comments, etc, greatly appreciated!
1
u/aaargha Oct 16 '15
C++
Using a bit of a different approach from other posts I've looked at. Calculates the score by traversing the line in reverse. Keeps track of balls already passed instead of looking forward.
Feedback and suggestions appreciated.
1
u/ken__mongolia Oct 25 '15 edited Oct 26 '15
Taking advantage of awk and having each record field correspond to a roll.
1
u/DevNamedJosh Nov 05 '15 edited Nov 05 '15
Java. I had to revisit this a few times to find a good approach. It's all explained in the javadoc comment.
Feedback welcome.
1
u/j7b Dec 03 '15
Go (golang) https://play.golang.org/p/k8IYM2qvYI
1
u/stewartj95 Dec 05 '15
1
u/Godd2 Dec 17 '15
A bit later than usual, but here's mine in Ruby:
And for output:
1
u/rorstrand Jan 28 '16
Python (Feedbacks are welcome) My first code being a new bee to scripting and really exited to get positive criticism :) that can help me do justice with scripting.