Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/practice/bowling/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"kytrinyx",
"lafent",
"petemcfarlane",
"tomasnorre"
"tomasnorre",
"lilyqin7"
],
"files": {
"solution": [
Expand Down
38 changes: 38 additions & 0 deletions exercises/practice/bowling/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,50 @@
class Game
{
private $rolls = [];
private $currentFrame = 1;
private $rollsInFrame = 0;
private $firstRollPins = 0;

public function roll($pins): void
{
if ($pins < 0 || $pins > 10) {
throw new Exception('Pins must be between 0 and 10');
}
// For frames 1-9
if ($this->currentFrame < 10) {
if ($this->rollsInFrame == 0) {
// First roll of frame
$this->firstRollPins = $pins;
$this->rollsInFrame = 1;

if ($pins == 10) { // Strike
$this->currentFrame++;
$this->rollsInFrame = 0;
}
} else {
// Second roll of frame
if ($this->firstRollPins + $pins > 10) {
throw new Exception("Pin count exceeds pins on the lane");
}
$this->currentFrame++;
$this->rollsInFrame = 0;
}
} else {
// Frame 10 special handling
if ($this->rollsInFrame == 0) {
$this->firstRollPins = $pins;
$this->rollsInFrame = 1;
} elseif ($this->rollsInFrame == 1) {
// Second roll in frame 10
if ($this->firstRollPins < 10 && $this->firstRollPins + $pins > 10) {
throw new Exception("Pin count exceeds pins on the lane");
}
$this->rollsInFrame = 2;
} else {
// Third roll in frame 10 (only valid after strike or spare)
$this->rollsInFrame = 3;
}
}
$this->rolls[] = $pins;
}

Expand Down
Loading
Loading