Created
June 18, 2016 12:55
-
-
Save kenjis/536f2b1e86a82f94ead2adcc7586fcf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TennisGame1 implements TennisGame | |
{ | |
private $m_score1 = 0; | |
private $m_score2 = 0; | |
private $player1Name = ''; | |
private $player2Name = ''; | |
private $score_map = [ | |
0 => 'Love', | |
1 => 'Fifteen', | |
2 => 'Thirty', | |
3 => 'Forty', | |
]; | |
public function __construct($player1Name, $player2Name) | |
{ | |
$this->player1Name = $player1Name; | |
$this->player2Name = $player2Name; | |
} | |
public function wonPoint($playerName) | |
{ | |
if ('player1' === $playerName) { | |
$this->m_score1++; | |
} else { | |
$this->m_score2++; | |
} | |
} | |
private function isEven() | |
{ | |
if ($this->m_score1 === $this->m_score2) { | |
return true; | |
} | |
return false; | |
} | |
private function getEvenScore() | |
{ | |
if ($this->m_score1 < 3) { | |
return $this->score_map[$this->m_score1] . '-All'; | |
} | |
return 'Deuce'; | |
} | |
private function isAdvantageOrWin() | |
{ | |
if ($this->m_score1 >= 4 || $this->m_score2 >= 4) { | |
return true; | |
} | |
return false; | |
} | |
private function getAdvantageOrWin() | |
{ | |
$diff = abs($this->m_score1 - $this->m_score2); | |
if ($this->m_score1 > $this->m_score2) { | |
if ($diff === 1) { | |
return 'Advantage player1'; | |
} | |
return 'Win for player1'; | |
} | |
if ($diff === 1) { | |
return 'Advantage player2'; | |
} | |
return 'Win for player2'; | |
} | |
private function getOtherScore() | |
{ | |
return $this->score_map[$this->m_score1] . '-' . $this->score_map[$this->m_score2]; | |
} | |
public function getScore() | |
{ | |
if ($this->isEven()) { | |
return $this->getEvenScore(); | |
} | |
if ($this->isAdvantageOrWin()) { | |
return $this->getAdvantageOrWin(); | |
} | |
return $this->getOtherScore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Refactored https://github.com/emilybache/Tennis-Refactoring-Kata/blob/master/php/TennisGame1.php.