Permalink
Browse files

Kata BowlingGame written in PHP

  • Loading branch information...
1 parent 828a33d commit 61ad73755a07204be6040116523ea9fb1027310d @pasku pasku committed with May 2, 2011
Showing with 138 additions and 0 deletions.
  1. +72 −0 pasku/BowlingGame.php
  2. +66 −0 pasku/BowlingGameTest.php
View
@@ -0,0 +1,72 @@
+<?php
+/**
+ * Kata 2011-04: Bowling Game
+ *
+ * Kata trying to resolve the BowlingGame problem using TDD.
+ *
+ * @category Kata
+ * @author Guillermo Pascual <@pasku1>
+ * @version 0.1
+ * @link http://github.com/12meses12katas/Abril-Bowling
+ */
+
+class BowlingGame
+{
+ protected $rolls = array();
+
+ public function roll($pins)
+ {
+ $this->rolls[] = $pins;
+ }
+
+ public function score()
+ {
+ $score = 0;
+ $frameIndex = 0;
+
+ for ($frame = 0; $frame < 10; $frame++) {
+ if ($this->isStrike($frameIndex)) {
+ $score += 10 + $this->strikeBonus($frameIndex);
+ $frameIndex++;
+ }
+
+ else if ($this->isSpare($frameIndex)) {
+ $score += 10 + $this->spareBonus($frameIndex);
+ $frameIndex += 2;
+ }
+
+ else {
+ $score += $this->sumOfPinsInFrame($frameIndex);
+ $frameIndex += 2;
+ }
+ }
+ return $score;
+ }
+
+ protected function isStrike($frameIndex)
+ {
+ return $this->rolls[$frameIndex] === 10;
+ }
+
+ protected function isSpare($frameIndex)
+ {
+ return $this->sumOfPinsInFrame($frameIndex) === 10;
+ }
+
+ protected function sumOfPinsInFrame($frameIndex)
+ {
+ return $this->rolls[$frameIndex] +
+ $this->rolls[$frameIndex + 1];
+ }
+
+ protected function strikeBonus($frameIndex)
+ {
+ return $this->rolls[$frameIndex + 1] +
+ $this->rolls[$frameIndex + 2];
+ }
+
+ protected function spareBonus($frameIndex)
+ {
+ return $this->rolls[$frameIndex + 2];
+ }
+}
@@ -0,0 +1,66 @@
+<?php
+
+require_once 'BowlingGame.php';
+
+class BowlingGameTest extends PHPUnit_Framework_TestCase
+{
+ protected $_game;
+
+ public function setUp()
+ {
+ $this->game = new BowlingGame();
+ }
+
+ public function testScoreForGutterGameIs0()
+ {
+ $this->rollMany(20, 0);
+ $this->assertEquals(0, $this->game->score());
+ }
+
+ public function testScoreForAllOnesIs20()
+ {
+ $this->rollMany(20, 1);
+ $this->assertEquals(20, $this->game->score());
+ }
+
+ public function testScoreForOneSpareAnd3Is16()
+ {
+ $this->rollSpare();
+ $this->rollMany(17, 0);
+ $this->assertEquals(16, $this->game->score());
+ }
+
+ public function testScoreForOneStrikeAnd3And4Is24()
+ {
+ $this->rollStrike();
+ $this->rollMany(17, 0);
+ $this->assertEquals(24, $this->game->score());
+ }
+
+ public function testScoreForPerfectGameIs300()
+ {
+ $this->rollMany(12, 10);
+ $this->assertEquals(300, $this->game->score());
+ }
+
+ protected function rollMany($n, $pins)
+ {
+ for ($i = 0; $i < $n; $i++) {
+ $this->game->roll($pins);
+ }
+ }
+
+ protected function rollSpare()
+ {
+ $this->game->roll(5);
+ $this->game->roll(5);
+ $this->game->roll(3);
+ }
+
+ private function rollStrike()
+ {
+ $this->game->roll(10);
+ $this->game->roll(3);
+ $this->game->roll(4);
+ }
+}

0 comments on commit 61ad737

Please sign in to comment.