# Bowling Score

Page
Description

This is my solution to a problem posted on Spoj.com called “Bowling (BOWLING1)”. The program first takes in a number N <= 1000, which is the number of test cases to follow. Then, the input will give a sequence of numbers between 0 and 10, representing the number of knocked down pins in a bowling game. Each test case is separated by a new line. The output should be the score obtained by the person who knocked down those exact pins.

Input/Output

```Sample Input
3
10 10 10 10 10 10 10 10 10 10 10 10
3 5 1 5 7 1 10 1 6 10 6 2 1 2 0 5 8 1
9 1 5 0 3 0 8 1 6 4 7 2 7 1 6 3 10 4 4
Sample Output

300
89
101```
Code Sample

```/******************************************************************************/
/*
Author:  Alejandro Hitti
Brief:   Bowling score calculator. Takes in the number of knocked down pins
and returns the score, according to Bowling rules.

All content (C) 2014-2015 Alejandro Hitti http://alejandrohitti.com.
*/
/******************************************************************************/

// Includes
//------------------------------------------------------------------------------
#include <iostream>
#include <vector>

const unsigned BufferSize = 256;

int main (void)
{
unsigned numTests;
char buffer[BufferSize];
std::vector<unsigned> pins;

// Reserve 21 slots, since it's the max number of rounds that can be played
pins.reserve(21);

std::cin >> numTests;

for (unsigned test = 0; test < numTests; ++test)
{
std::cin.getline(buffer, BufferSize);

// Checking for new lines in Windows, Linux and Mac
while ((std::cin.peek() != '\r\n') &&
(std::cin.peek() != '\n') &&
(std::cin.peek() != '\r'))
{
// Fill the container with the knocked down pins for the case
unsigned input;
std::cin >> input;
pins.push_back(input);
}

unsigned shotNum = 0;
unsigned score = 0;
unsigned size = pins.size();
for (unsigned i = 0; i < size && shotNum < 20; ++i)
{
// Add the current number of pins
score += pins[i];

// If it's a srike, add the current and next scores. Advance round.
if (pins[i] == 10)
{
++shotNum;
score += pins[i + 1] + pins[i + 2];
}
// If it's a spare, add the next score only.
else if ((shotNum % 2) == 0 && (pins[i] + pins[i + 1] == 10))
{
score += pins[i + 2];
}

++shotNum;
}

// Print the score and clear the container
std::cout << score << std::endl;
pins.clear();
}

return 0;
}
```

[top]