|
1
|
|
|
<?php namespace Jarrett\RockPaperScissorsSpockLizard; |
|
2
|
|
|
|
|
3
|
|
|
use Jarrett\RockPaperScissorsSpockLizardException; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class RockPaperScissorsSpockLizard |
|
7
|
|
|
* |
|
8
|
|
|
* @author Jarrett Barnett <[email protected] |
|
9
|
|
|
* @see http://www.samkass.com/theories/RPSSL.html |
|
10
|
|
|
*/ |
|
11
|
|
|
class Player implements PlayerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Player ID - generated by game |
|
15
|
|
|
* @var $id |
|
16
|
|
|
*/ |
|
|
|
|
|
|
17
|
|
|
protected $id; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Player name |
|
21
|
|
|
* @var bool|Player |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Is player a bot? |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $is_bot = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var $moves |
|
33
|
|
|
*/ |
|
|
|
|
|
|
34
|
|
|
private $moves = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Player constructor. |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
*/ |
|
40
|
19 |
|
public function __construct($name = '') |
|
41
|
|
|
{ |
|
42
|
19 |
|
$this->setName($name); |
|
43
|
19 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Queue a play |
|
47
|
|
|
* @param $move |
|
48
|
|
|
* @return $this |
|
49
|
|
|
* @throws RockPaperScissorsSpockLizardException |
|
50
|
|
|
*/ |
|
51
|
14 |
|
public function move($move) |
|
52
|
|
|
{ |
|
53
|
14 |
|
$last_move = $this->getLastMoveIndex(); |
|
54
|
|
|
|
|
55
|
14 |
|
if (is_array($last_move) && array_shift($last_move) === false) |
|
56
|
|
|
{ |
|
57
|
1 |
|
throw new RockPaperScissorsSpockLizardException('Cannot set another move until the previous move has been played'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
14 |
|
if (empty($move)) { |
|
61
|
1 |
|
throw new RockPaperScissorsSpockLizardException('Move cannot be empty'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
13 |
|
$this->moves[] = [$move => false]; |
|
65
|
|
|
|
|
66
|
13 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set ID |
|
71
|
|
|
* @param $id |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
13 |
|
public function setId($id) |
|
75
|
|
|
{ |
|
76
|
13 |
|
$this->id = $id; |
|
77
|
|
|
|
|
78
|
13 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get ID |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
6 |
|
public function getId() |
|
86
|
|
|
{ |
|
87
|
6 |
|
return $this->id; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set player name |
|
92
|
|
|
* @param $name |
|
93
|
|
|
* @return $this|bool |
|
94
|
|
|
*/ |
|
95
|
19 |
|
public function setName($name) |
|
96
|
|
|
{ |
|
97
|
19 |
|
if (empty($name)) |
|
98
|
|
|
{ |
|
99
|
19 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
14 |
|
$this->name = $name; |
|
103
|
|
|
|
|
104
|
14 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get name |
|
109
|
|
|
* @return bool|Player |
|
110
|
|
|
*/ |
|
111
|
14 |
|
public function getName() |
|
112
|
|
|
{ |
|
113
|
14 |
|
return $this->name; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get Move History |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
14 |
|
public function getMoveHistory() |
|
121
|
|
|
{ |
|
122
|
14 |
|
return $this->moves; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Mark Last Move As Played |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
6 |
|
public function lastMoveIsPlayed() |
|
130
|
|
|
{ |
|
131
|
6 |
|
$moves = $this->getMoveHistory(); |
|
132
|
6 |
|
$offset = count($moves) - 1; |
|
133
|
6 |
|
$last_item = $this->moves[$offset]; |
|
134
|
6 |
|
$this->moves[$offset][key($last_item)] = true; |
|
135
|
6 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get Last Move |
|
140
|
|
|
* @return mixed |
|
141
|
|
|
*/ |
|
142
|
14 |
|
public function getLastMoveIndex() |
|
143
|
|
|
{ |
|
144
|
14 |
|
$move_history = $this->getMoveHistory(); |
|
145
|
|
|
|
|
146
|
|
|
// get last value |
|
147
|
14 |
|
$last_move = end($move_history); |
|
148
|
|
|
|
|
149
|
|
|
// reset pointer |
|
150
|
14 |
|
reset($this->moves); |
|
151
|
|
|
|
|
152
|
14 |
|
return $last_move; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get Last Move |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function getLastMove() |
|
160
|
|
|
{ |
|
161
|
1 |
|
$last_move_index = $this->getLastMoveIndex(); |
|
162
|
|
|
|
|
163
|
1 |
|
return key($last_move_index); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Is Bot? or set as a bot |
|
168
|
|
|
* @param bool $is_bot |
|
169
|
|
|
* @return $this if setting player to a bot, otherwise true/false depending on whether player is a bot |
|
170
|
|
|
*/ |
|
171
|
7 |
|
public function isBot($is_bot = null) |
|
172
|
|
|
{ |
|
173
|
|
|
// if no parameter provided, return boolean for whether player is a bot or not |
|
174
|
7 |
|
if ($is_bot === null) { |
|
175
|
6 |
|
return $this->is_bot; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
3 |
|
$this->is_bot = (bool) $is_bot; |
|
179
|
|
|
|
|
180
|
3 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|