-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frames.php
132 lines (102 loc) · 3.26 KB
/
Frames.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
interface FrameInterface {
public function getScore();
public function getFirst();
public function getSecond();
public function calculateScore(FrameInterface $future_frame_score);
}
abstract class AbstractFrame implements FrameInterface {
protected $previous_frame_score;
protected $first_throw;
protected $second_throw;
public function __construct(FrameInterface $previous_frame_score, $first_throw = null, $second_throw = null){
$this->previous_frame_score = $previous_frame_score;
$this->first_throw = $first_throw;
$this->second_throw = $second_throw;
}
public function getFirst(){
return $this->first_throw;
}
public function getSecond(){
return $this->second_throw;
}
public function getPreviousFrame(){
return $this->previous_frame_score;
}
public function calculateScore(FrameInterface $future_frame_score){
//Override if class needs to do special score calculation
}
}
class FrameFactory {
public static function factory($sequence, FrameInterface $previous_frame_score) {
//$sequence is one or two chars long
$first_throw = substr($sequence,0,1);
if (strlen($sequence) > 1){
$second_throw = substr($sequence,1,1);
}else{
$second_throw = 0;
}
if ($first_throw == "X"){
return new FrameStrike($previous_frame_score);
}elseif($first_throw == "-"){
$first_throw = 0;
}
if ($second_throw == "/"){
return new FrameSpare($previous_frame_score, $first_throw);
}
if ($second_throw == "-"){
$second_throw = 0;
}
return new FrameNormal($previous_frame_score, $first_throw, $second_throw);
}
}
class FrameNormal extends AbstractFrame {
public function getScore(){
return $this->first_throw + $this->second_throw;
}
}
class FrameSpare extends AbstractFrame {
protected $score = 10;
public function getScore(){
return $this->score;
}
public function calculateScore(FrameInterface $future_frame_score){
if ($future_frame_score instanceof FrameStrike){
$score = 10;
}elseif($future_frame_score instanceof FrameSpare){
$score = $future_frame_score->getFirst();
}else{
$score = $future_frame_score->getFirst();
}
$this->score = $this->score + $score;
}
}
class FrameStrike extends AbstractFrame {
protected $score = 10;
protected $first_throw = 10;
protected $second_throw = 10;
public function getScore(){
return $this->score;
}
public function calculateScore(FrameInterface $future_frame_score){
if ($future_frame_score instanceof FrameStrike){
$score = 20;
}elseif($future_frame_score instanceof FrameSpare){
$score = $future_frame_score->getFirst() + 10;
}else{
$score = $future_frame_score->getScore();
}
$this->score = $this->score + $score;
}
}
/**
* Class FrameNull
* Start and ending frame. Does not count on score
*/
class FrameNull extends AbstractFrame {
public function __construct(){
}
public function getScore(){
return 0;
}
}