forked from ClevPHP/code-kata-bowling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.php
More file actions
49 lines (33 loc) · 1.08 KB
/
Copy pathgame.php
File metadata and controls
49 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
class Game {
public $name;
public $rolls;
public function __construct( $name ) {
$this->name = $name;
}
public function bowl( array $num_pins ) {
// Log rolls to $this->rolls
$this->rolls[] = $num_pins[0];
$this->rolls[] = $num_pins[1];
}
public function score() {
$score = 0;
// Necessary info for loop
$length = count( $this->rolls ) - 1;
$rolls = $this->rolls;
// Calculate Score Here
for ( $i = 0; $i < $length; $i++ ) {
// Calculates score in event of strike
if ( $rolls[ $i ] == 10 ) {
$score = $score + $rolls[ $i ] + $rolls[ $i + 1 ] + $rolls[ $i + 2 ];
// Calculates score in event of spare
} elseif ( $rolls[ $i ] + $rolls[ $i + 1 ] == 10 ) {
$score = $score + $rolls[ $i ] + $rolls[ $i + 2 ];
// Calculates score normally
} else {
$score = $score + $rolls[ $i ];
}
}
return $score;
}
}