LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 June 19 2013

Joe
Member

[Exercise] Let's go bowling

In this exercise, let's explore a common and fun game: Bowling!
In particular, we'll take a look at the not so conventional way of keeping score in this game.

Scoring in bowling

In a standard game of bowling, a player gets 10 turns. In each turn, the goal is to knock down the 10 pins located at the end of the lane, by throwing a large ball at them. And the player gets 2 shots per turn.

For example, on her first turn Alice takes 2 shots:

  • The first shot knocks down 6 pins

  • The second shot knocks down 2 of the remaining 4 pins

As a result Alice scored 8 in the first round.
In a simple world, that's all there is to it. At the end of the 10 rounds, you sum the scores and get the final score for the game. And it would make for a very boring exercise.

The scoring rules get more complicated by introducing multipliers. There are 2 kinds of multipliers one can get, and they both involve knocking down the 10 pins.

The Spare

A spare happens when a player knocks down the 10 pins in one round and 2 shots. Any combination that adds up to 10 can be a spare:

  • 6-4

  • 9-1

  • 0-10

Bonus: If a player gets a spare, the first shot (and only the first shot) of her next round gets added to the current round shot.

Example: If a player scores:

  • 6 - 4 - 7 - 2

The first round (the spare) scores 17 points and the second round 9 points for a total of 26 in 2 rounds.

The Strike

A strike happens when a player knocks down the 10 pins in the first shot of any round.

Bonus: If a player scores:

  • 10 - 3 - 2

The first round (the strike) scores 15 points and the second round 5 points for a total of 20 points in 2 rounds.

Important note: The final score of a strike round is determine only once the final score of the succeeding round is determined. This can be tricky if you have 2 or more successive strikes.

  • 10 - 10 - 4 - 2

In this case the player has played 3 rounds (2 strikes than 6). Starting from last:

  • The 3rd round: 6 points

  • The 2nd round: 10 + 6 = 16 points

  • The 1st round: 10 + 16 = 26 points

  • Total: 26 + 16 + 6 = 48 points in 3 rounds

As you can see, successive strikes can be a powerful way to raise your score.

Reminder: 0 - 10 is not a strike. It's a spare. A strike has to be done in the first shot of the round.

Final round multipliers

What happens if you knock down the 10 pins in the last (10th) round? Well, the rules say you can get more shots with a maximum of 3 shots in the last round. So you can get a spare + 1 shot, or 2 strikes + 1 shots.

If your extra shot is a strike, it's worth only 10 points.

Exercise

Given a space separated series of scores, your goal is to write a program that calculates the total score of a player in a game. For instance:

  • 2 3 4 4 6 2 1 0 2 4 5 2 9 0 1 1 3 4 2 2. Total = 57

  • 2 3 4 6 6 2 1 0 2 4 5 2 9 0 1 1 3 4 2 2. Total = 65

  • 2 3 4 6 10 1 0 2 4 5 2 9 0 1 1 3 4 2 2. Total = 72

  • 2 3 4 6 0 10 1 0 2 4 5 2 9 0 1 1 3 4 2 2. Total = 62

Note that you may not have a total of 20 shots, since a strike will only count for one shot for the whole round. This format was specifically chosen to be hard(er) to parse. There are ways to make things easier, notably by marking spares as "/" and strikes as "X" (which is the official notation).

Your program should be tested against the above examples.

Bonus question

What's the maximal score one can get in Bowling?
10 10 10 10 10 10 10 10 10 10 10 10

Offline

#2 June 20 2013

Ra8
Member

Re: [Exercise] Let's go bowling

code:

def calculateScore(scoreList):
	scoreList=scoreList
	i=0
	total=0
	while i<len(scoreList):
		total+=scoreList[i]
		if i>=len(scoreList)-3:
			i+=1
			continue
		if scoreList[i]==10:
			total+=scoreList[i+1]+scoreList[i+2]
			i+=1
			continue
		i+=1
		total+=scoreList[i]
		if scoreList[i]+scoreList[i-1]==10:
			total+=scoreList[i+1]
		i+=1
		
	return total

scoreLists = [
	[2,3,4,4,6,2,1,0,2,4,5,2,9,0,1,1,3,4,2,2],
	[2,3,4,6,6,2,1,0,2,4,5,2,9,0,1,1,3,4,2,2],
	[2,3,4,6,10,1,0,2,4,5,2,9,0,1,1,3,4,2,2],
	[2,3,4,6,0,10,1,0,2,4,5,2,9,0,1,1,3,4,2,2],
	[10,10,10,10,10,10,10,10,10,10,10,10]
	]
for x in scoreLists:
	print(calculateScore(x))

output:

57
65
72
62
300

I think your output is wrong, I calculated it by hand to make sure too.

Last edited by Ra8 (June 20 2013)

Offline

#3 June 20 2013

yasamoka
Member

Re: [Exercise] Let's go bowling

rahmu wrote:
The Strike

A strike happens when a player knocks down the 10 pins in the first shot of any round.

Bonus: If a player scores:

  • 10 - 3 - 2

The first round (the strike) scores 15 points and the second round 5 points for a total of 20 points in 2 rounds.

Important note: The final score of a strike round is determine only once the final score of the succeeding round is determined. This can be tricky if you have 2 or more successive strikes.

  • 10 - 10 - 4 - 2

In this case the player has played 3 rounds (2 strikes than 6). Starting from last:

  • The 3rd round: 6 points

  • The 2nd round: 10 + 6 = 16 points

  • The 1st round: 10 + 16 = 26 points

  • Total: 26 + 16 + 6 = 48 points in 3 rounds

Are you sure about this?
From wikipedia:

When all ten pins are knocked down with the first ball (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.

Balls, not frames. Check out this example:

A turkey's pinfall is:
Frame 1, ball 1: 10 pins (Strike)
Frame 2, ball 1: 10 pins (Strike)
Frame 3, ball 1: 10 pins (Strike)
Frame 4, ball 1: 0 pins (Gutterball)
Frame 4, ball 2: 9 pins
The total score from these throws is:
Frame one: 10 + (10 + 10) = 30
Frame two: 10 + (10 + 0) = 20
Frame three: 10 + (0 + 9) = 19
Frame four: 0 + 9 = 9
TOTAL = 78

With your method:
Frame four: 0 + 9 = 9
Frame three: 10 + 9 = 19
Frame two: 10 + 19 = 29 (should be 10 + (10 + 0))
Frame one: 10 + 29 = 39 (should be 10 + (10 + 10)

Limitation: A frame cannot score more than 30.

Also:

What happens if you knock down the 10 pins in the last (10th) round? Well, the rules say you can get more shots with a maximum of 3 shots in the last round. So you can get a spare + 1 shot, or 2 strikes + 1 shots.
If your extra shot is a strike, it's worth only 10 points.

Aren't the possible scenarios three?
1) Spare + you are awarded one shot (spare + 1 shot)
2) Strike + you are awarded two shots (strike - shot - shot where the shows can be strikes or not)
The first strike you might get in the last frame is also worth only 10 points. Else the last frame would have a maximum score of 50.

I think the spare also should not be worth more than 10 points, this is why:
You get a spare then a strike. +10 for spare, +10 for the next ball (a strike) + 10 for the strike itself = 30 (= 3 strikes in the last frame!)

So I believe that for the last frame, no multipliers apply if you get a spare or strike.
Or you apply the multipliers but you don't add the values of the next 1 or 2 shots again. This would explain why they included an extra one shot (after spare) and an extra two shots (after strike), so they can calculate the spare and strike scores.

In your examples, you did not give an example that contains consecutive strikes, so both methods of calculating strikes will return the same results.

Anyways, here is my code (C++):

#include <iostream>
using namespace std;

int main()
{
	int *shot = new int[21];
	int j=0;
	char c=NULL;
	while(c != '\n') {
		cin >> shot[j];
		j++;
		cin.get(c);
	}

	int rounds=0, i=0, score=0;
	while(rounds < 9) {
		if(shot[i] == 10) { //strike
			score += 10 + shot[i+1] + shot[i+2];
			i++;
		}
		else { //not strike
			if(shot[i] + shot[i+1] == 10) //spare
				score += 10 + shot[i+2];
			else //not spare
				score += shot[i] + shot[i+1];
			
			i = i+2;
		}
		
		rounds++;
	}

	if(shot[i] == 10) {
		score = score + 10 + shot[i+1]+shot[i+2];
	}
	else if(shot[i] + shot[i+1] == 10) {
		score = score + 10 + shot[i+1];
	}
	else
		score = score + shot[i] + shot[i+1];
	
	cout << score << endl;
}

I was using recursion with your method of calculating scores for consecutive strikes. It was nice.

Last edited by yasamoka (June 20 2013)

Offline

#4 June 20 2013

Joe
Member

Re: [Exercise] Let's go bowling

@yasamoka: you're right, I had misunderstood the rules. I'll correct this tonight after work. Thanks.

Offline

Board footer