|
9 | 9 | class Game |
10 | 10 | { |
11 | 11 | private $rolls = []; |
| 12 | + private $currentFrame = 1; |
| 13 | + private $rollsInFrame = 0; |
| 14 | + private $firstRollPins = 0; |
12 | 15 |
|
13 | 16 | public function roll($pins): void |
14 | 17 | { |
15 | 18 | if ($pins < 0 || $pins > 10) { |
16 | 19 | throw new Exception('Pins must be between 0 and 10'); |
17 | 20 | } |
| 21 | + // For frames 1-9 |
| 22 | + if ($this->currentFrame < 10) { |
| 23 | + if ($this->rollsInFrame == 0) { |
| 24 | + // First roll of frame |
| 25 | + $this->firstRollPins = $pins; |
| 26 | + $this->rollsInFrame = 1; |
| 27 | + |
| 28 | + if ($pins == 10) { // Strike |
| 29 | + $this->currentFrame++; |
| 30 | + $this->rollsInFrame = 0; |
| 31 | + } |
| 32 | + } else { |
| 33 | + // Second roll of frame |
| 34 | + if ($this->firstRollPins + $pins > 10) { |
| 35 | + throw new Exception("Pin count exceeds pins on the lane"); |
| 36 | + } |
| 37 | + $this->currentFrame++; |
| 38 | + $this->rollsInFrame = 0; |
| 39 | + } |
| 40 | + } else { |
| 41 | + // Frame 10 special handling |
| 42 | + if ($this->rollsInFrame == 0) { |
| 43 | + $this->firstRollPins = $pins; |
| 44 | + $this->rollsInFrame = 1; |
| 45 | + } elseif ($this->rollsInFrame == 1) { |
| 46 | + // Second roll in frame 10 |
| 47 | + if ($this->firstRollPins < 10 && $this->firstRollPins + $pins > 10) { |
| 48 | + throw new Exception("Pin count exceeds pins on the lane"); |
| 49 | + } |
| 50 | + $this->rollsInFrame = 2; |
| 51 | + } else { |
| 52 | + // Third roll in frame 10 (only valid after strike or spare) |
| 53 | + $this->rollsInFrame = 3; |
| 54 | + } |
| 55 | + } |
18 | 56 | $this->rolls[] = $pins; |
19 | 57 | } |
20 | 58 |
|
|
0 commit comments