/js-kata-bowling-open-closed

Implement bowling scoring applying open closed principles.

Primary LanguageJavaScript

Bowling Game Open Closed

End date: Friday 31st July (2 weeks)

Rules

  1. Write the first failing test. It should use a factory method to obtain the object(s) under test.
  2. Make the test pass.
  3. Write the next failing test.
  4. Try to make this test pass by only changing the factory and/or adding new classes.
    If you find yourself modifying or adding functionality in existing non-factory classes
  5. Refactor the code so that you could proceed without changing these classes. The refactoring itself should provide no functional changes and the current test should still fail after the refactoring.
  6. Now make the test pass.
  7. Go to step 2.

The factory should be limited to creating objects and linking them together. No conditionals allowed.

Scoring

You get 1 points per "game" that passes the tests. Each if, else, else if you use costs you 1 point. Cheeky short-circuit evaluations such as doSomething() || doSomethingElse() or ifSomethingsTrue() && thenDoThis() will also cost you 1 point. Ternary operations cost 2 points. Winner is whoever has the most points! (there's probably no prize).

End goal

Frame 12345 678910
Rolls 1,44,56,45,510 0,17,36,4102,8,6
Total 514294960 617797117133

The game consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares.

A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll. So in frame 3 above, the score is 10 (the total number knocked down) plus a bonus of 5 (the number of pins knocked down on the next roll.)

A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled.

In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame. However no more than three balls can be rolled in tenth frame.

You can read more about bowling scoring here: http://bowling.about.com/od/rulesofthegame/a/bowlingscoring.htm

<tr>
	<td>Gutter game</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td>
	<td>0</td>
</tr>

<tr>
	<td>All ones</td>
	<td>1,1</td><td>1,1</td><td>1,1</td><td>1,1</td><td>1,1</td>
	<td>1,1</td><td>1,1</td><td>1,1</td><td>1,1</td><td>1,1</td>
	<td>20</td>
</tr>

<tr>
	<td>No spares</td>
	<td>0,0</td><td>3,0</td><td>0,0</td><td>6,2</td><td>0,2</td>
	<td>0,0</td><td>7,1</td><td>0,5</td><td>0,0</td><td>0,0</td>
	<td>26</td>
</tr>

<tr>
	<td>A spare</td>
	<td>0,1</td><td>7,2</td><td>3,7</td><td>7,1</td><td>0,0</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td>
	<td>35</td>
</tr>

<tr>
	<td>Final spare</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>9,1,7</td>
	<td>17</td>
</tr>

<tr>
	<td>A strike</td>
	<td>0,0</td><td>0,0</td><td>10</td><td>2,3</td><td>0,0</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td>
	<td>20</td>
</tr>

<tr>
	<td>Final strike</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td>
	<td>0,0</td><td>0,0</td><td>0,0</td><td>0,0</td><td>10,3,6</td>
	<td>19</td>
</tr>

<tr>
	<td>Perfect game</td>
	<td>10</td><td>10</td><td>10</td><td>10</td><td>10</td>
	<td>10</td><td>10</td><td>10</td><td>10</td><td>10,10,10</td>
	<td>300</td>
</tr>
Standard Bowling Frames (x10) Score

Additional Challenge

The same rules as standard 10 pin bowling with the following changes:

  • Game - now 5 frames per game.
  • Frame - now 3 rolls per frame.
  • Spare - now counts the next 2 rolls as bonuses and awards 2 bonus rolls in the final frame.
  • Strike - now counts the next 3 rolls as bonuses and awards 2 bonus rolls in the final frame.
  • Final Frame - no more than 5 rolls can be made in the final frame.
<tr>
	<td>Gutter game</td>
	<td>0,0,0</td><td>0,0,0</td><td>0,0,0</td><td>0,0,0</td><td>0,0,0</td>
	<td>0</td>
</tr>

<tr>
	<td>All ones</td>
	<td>1,1,1</td><td>1,1,1</td><td>1,1,1</td><td>1,1,1</td><td>1,1,1</td>
	<td>15</td>
</tr>

<tr>
	<td>No spares</td>
	<td>0,0,0</td><td>3,0,1</td><td>0,0,0</td><td>5,2,1</td><td>0,2,0</td>
	<td>14</td>
</tr>

<tr>
	<td>A spare</td>
	<td>0,0,0</td><td>7,2,1</td><td>3,2,1</td><td>0,0,0</td><td>0,0,0</td>
	<td>21</td>
</tr>

<tr>
	<td>Final spare</td>
	<td>0,0,0</td><td>0,0,0</td><td>0,0,0</td><td>0,0,0</td><td>7,2,1,7,1</td>
	<td>18</td>
</tr>

<tr>
	<td>A strike</td>
	<td>0,0,0</td><td>10</td><td>3,5,1</td><td>0,0,0</td><td>0,0,0</td>
	<td>28</td>
</tr>

<tr>
	<td>A strike + spare</td>
	<td>0,0,0</td><td>10</td><td>8,2</td><td>3,2,1</td><td>0,0,0</td>
	<td>44</td>
</tr>

<tr>
	<td>Final spare</td>
	<td>10</td><td>10</td><td>10</td><td>10</td><td>10,10,10,10</td>
	<td>200</td>
</tr>
Rapid bowling Frames (x5) Score

Setup/Running Tests

Install dependencies

npm install

Run tests

npm test

The test framework is mocha and the assertion library is chai, sinon and sinon-chai have also been included for you. There is a helper.js file that includes mocha, chai, sinon and sinon-chai and sets up chai with the should style of assertions to save you having to do it in each test file.

There's some nice cheat sheets here:

http://ricostacruz.com/cheatsheets/mocha.html
http://ricostacruz.com/cheatsheets/chai.html
http://ricostacruz.com/cheatsheets/sinon-chai.html

References/Inspiration

http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata http://www.red-gate.com/blog/code-kata-6-classic-bowling-game-scorer-open-closed http://matteo.vaccari.name/blog/archives/293 http://www.slideshare.net/kevinrutherford/ocp-kata-24027400