Skip to content

Commit cb1b361

Browse files
authored
Bowling - inline test helper functions setUp and rollMany (#944)
[no important files changed]
1 parent dac00e3 commit cb1b361

File tree

3 files changed

+1195
-173
lines changed

3 files changed

+1195
-173
lines changed

exercises/practice/bowling/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"kytrinyx",
99
"lafent",
1010
"petemcfarlane",
11-
"tomasnorre"
11+
"tomasnorre",
12+
"lilyqin7"
1213
],
1314
"files": {
1415
"solution": [

exercises/practice/bowling/.meta/example.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,50 @@
99
class Game
1010
{
1111
private $rolls = [];
12+
private $currentFrame = 1;
13+
private $rollsInFrame = 0;
14+
private $firstRollPins = 0;
1215

1316
public function roll($pins): void
1417
{
1518
if ($pins < 0 || $pins > 10) {
1619
throw new Exception('Pins must be between 0 and 10');
1720
}
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+
}
1856
$this->rolls[] = $pins;
1957
}
2058

0 commit comments

Comments
 (0)