views:

468

answers:

3

I am programming a simple C# console application.

The spec is:

A game consists of ten frames, which start with a full rack of ten pins. In each frame, you have two deliveries of your ball, in which to knock down as many of the ten pins as you can. If you knock down all the pins on your first ball, it is called a strike. The score doesn't get added on straight away because for a strike you get the values of your next two balls as a bonus. For example, if you score a strike in the first frame, then a 7 and 1 in the second frame, you would score 18 (10+7+1) for the first frame, and 8 for the second frame, making a total of 26 after two frames. If you knock down some of the pins on the first ball, and knock down the remainder of the pins in the second ball, it is known as a spare. Again, the score doesn't get added on straight away because for a spare, you get the values of your next ball as a bonus. For example, if you score a spare in the first frame, say a 6 and a 4, then got an 8 and a 1 in the second frame, you would score 18 (6+4+8) for the first frame, and 9 for the second frame, making a total of 27 after two frames.

I understand how to write the code etc. etc. However, I just cannot get my head around the best way of doing this scoring system and I need some advice. Obviously a new object will be created for each player, however I am not sure of how to get round what seemed like a simple programming problem initially.

Any help would be greatly appreciated.

Regards

+4  A: 

Well, if you want a real in-depth discussion that deals with OOP and TDD for a bowling game, I would suggest reading this entire article:

http://www.objectmentor.com/resources/articles/xpepisode.htm

Otherwise, you can just scroll to the bottom and see how they implemented it.

John Rasch
+5  A: 

In bowling, strikes and spares are called "marks." Use this!

enum Marks { Open, Spare, Strike };

This way, you can determine what type of mark you have per frame. After a ball is thrown, check the last two frames, and update their scores. Then add up the scores.

Another note: If you're showing frame-by-frame scoring, do not show the frame score in a string of strikes (e.g.-four strikes in a row should not result in "30" being shown in frame 1). Wait for a spare or open to display the totals. It's improper scoring to do anything different, and it bugs the hell out of me when video games don't recognize that.

Eric
Don't forget as well that a throw doesn't knock any pins, it is marked with a '-' not a zero. Also, if the player got a split in the first throw of a frame, the score is circled (yes, it's a console app, but the rules are the rules) and some people annotate with the pins in the split.
plinth
A: 

When working with problems like these, I've found using flags to be useful. By flags, I mean small boolean or int variables that just tell you if something is true or false. As in:

bool isStrike;
bool isSpare;

Put these variables in your object, and when the person gets a strike set isStrike to true.

When it comes time to calculate the scores, you can then change your calculation system by checking these flags.

if(isStrike)
    // do not calculate the score until the other two bowls are made.
if(isSpare)
    // do not calculate the score until one more bowl is made

Then you update your score keeping variables to reflect these after those bowls.

note: I don't know how score keeping works in bowling, but I think the information here is general enough.

chustar