-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.php
65 lines (56 loc) · 2.01 KB
/
frame.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
class Frame {
public $rolls = array();
public $wait = false;
public $score = 0;
public $pins = 0;
public $strike = false;
public $spare = false;
public $completed = false;
public $tenth = false;
public function addRoll($pins) {
$this->rolls[] = $pins;
$this->pins += $pins;
if (count($this->rolls) == 1 && $this->pins == 10 && $this->tenth === false) {
$this->strike = true;
$this->wait = true;
$this->completed = true;
}
elseif (count($this->rolls) == 2) {
if ($this->pins > 10) {
if (!$this->tenth) throw new Exception('Invalid Frame');
elseif ($this->tenth && $this->rolls[0] < 10 && $this->pins > 10) throw new Exception('Invalid Frame');
}
if ($this->tenth === false) {
if ($this->pins == 10) {
$this->spare = true;
$this->wait = true;
}
$this->completed = true;
}
else {
if ($this->rolls[0] < 10) {
if ($this->pins == 10) $this->spare = true;
elseif ($this->pins < 10) $this->completed = true;
}
}
}
elseif (count($this->rolls) == 3) {
if ($this->rolls[0] == 10 && $this->rolls[1] < 10 && ($this->rolls[1] + $this->rolls[2]) > 10) throw new Exception('Invalid Frame');
$this->completed = true;
}
if ($this->completed && ($this->wait === false || $this->tenth)) $this->calcScore();
}
private function calcScore() {
foreach ($this->rolls as $roll) $this->score += $roll;
}
public function reset() {
$this->rolls = array();
$this->strike = false;
$this->spare = false;
$this->completed = false;
$this->wait = false;
$this->pins = 0;
$this->score = 0;
}
}