Skip to content

Instantly share code, notes, and snippets.

@kenjis
Created June 18, 2016 12:55
Show Gist options
  • Save kenjis/536f2b1e86a82f94ead2adcc7586fcf3 to your computer and use it in GitHub Desktop.
Save kenjis/536f2b1e86a82f94ead2adcc7586fcf3 to your computer and use it in GitHub Desktop.
<?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();
}
}
@kenjis
Copy link
Author

kenjis commented Jun 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment