Skip to content

Instantly share code, notes, and snippets.

@hatelove
Last active December 9, 2017 12:57
Show Gist options
  • Save hatelove/c3d1010fe644a8daf69ef71eee55d574 to your computer and use it in GitHub Desktop.
Save hatelove/c3d1010fe644a8daf69ef71eee55d574 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: JoeyChen
* Date: 2017/12/2
* Time: 下午 01:28
*/
namespace JoeyDojo;
class TennisGame
{
private $_firstPlayerScoreTimes = 0;
private $_secondPlayerScoreTimes = 0;
private $lookup = [
0 => "Love",
1 => "Fifteen",
2 => "Thirty",
3 => "Forty",
];
private $_firstPlayerName;
private $_secondPlayerName;
/**
* TennisGame constructor.
* @param $firstPlayerName
* @param $secondPlayerName
*/
public function __construct($firstPlayerName, $secondPlayerName)
{
$this->_firstPlayerName = $firstPlayerName;
$this->_secondPlayerName = $secondPlayerName;
}
public function score()
{
return $this->isScoreDifferent()
? ($this->isReadyForWin() ? $this->advState() : $this->normalScore())
: ($this->isDeuce() ? $this->deuce() : $this->sameScore());
}
/**
* @return bool
*/
private function isScoreDifferent(): bool
{
return $this->_firstPlayerScoreTimes != $this->_secondPlayerScoreTimes;
}
/**
* @return bool
*/
private function isReadyForWin(): bool
{
return $this->_firstPlayerScoreTimes > 3 || $this->_secondPlayerScoreTimes > 3;
}
/**
* @return string
*/
private function advState(): string
{
return $this->advPlayer() . ($this->isAdv() ? " Adv" : " Win");
}
/**
* @return mixed
*/
private function advPlayer()
{
$advPlayer = $this->_firstPlayerScoreTimes > $this->_secondPlayerScoreTimes ? $this->_firstPlayerName : $this->_secondPlayerName;
return $advPlayer;
}
/**
* @return bool
*/
private function isAdv(): bool
{
return abs($this->_firstPlayerScoreTimes - $this->_secondPlayerScoreTimes) == 1;
}
/**
* @return string
*/
private function normalScore(): string
{
return $this->lookup[$this->_firstPlayerScoreTimes] . " " . $this->lookup[$this->_secondPlayerScoreTimes];
}
/**
* @return bool
*/
private function isDeuce(): bool
{
return $this->_firstPlayerScoreTimes >= 3;
}
/**
* @return string
*/
private function deuce(): string
{
return "Deuce";
}
/**
* @return string
*/
private function sameScore(): string
{
return $this->lookup[$this->_firstPlayerScoreTimes] . " All";
}
public function firstPlayerScore()
{
$this->_firstPlayerScoreTimes++;
}
public function secondPlayerScore()
{
$this->_secondPlayerScoreTimes++;
}
}
<?php
/**
* Created by PhpStorm.
* User: JoeyChen
* Date: 2017/12/2
* Time: 下午 01:25
*/
namespace Tests;
use JoeyDojo\TennisGame;
use PHPUnit\Framework\TestCase;
class TennisGameTest extends TestCase
{
/**
* @var TennisGame
*/
private $tennisGame;
public function test_score_Love_Fifteen()
{
$this->tennisGame->secondPlayerScore();
$this->scoreShouldBe("Love Fifteen");
}
private function scoreShouldBe($expected): void
{
$this->assertEquals($expected, $this->tennisGame->score());
}
public function test_score_Love_Thirty()
{
$this->givenSecondPlayerScoreTimes(2);
$this->scoreShouldBe("Love Thirty");
}
private function givenSecondPlayerScoreTimes($times)
{
for ($i = 0; $i < $times; $i++) {
$this->tennisGame->secondPlayerScore();
}
}
public function test_score_Love_All()
{
$this->scoreShouldBe("Love All");
}
public function test_score_Fifteen_Love()
{
$this->tennisGame->firstPlayerScore();
$this->scoreShouldBe("Fifteen Love");
}
public function test_score_Thirty_Love()
{
$this->givenFirstPlayerScoreTimes(2);
$this->scoreShouldBe("Thirty Love");
}
private function givenFirstPlayerScoreTimes($times)
{
for ($i = 0; $i < $times; $i++) {
$this->tennisGame->firstPlayerScore();
}
}
public function test_score_Forty_Love()
{
$this->givenFirstPlayerScoreTimes(3);
$this->scoreShouldBe("Forty Love");
}
public function test_score_Fifteen_All()
{
$this->tennisGame->firstPlayerScore();
$this->tennisGame->secondPlayerScore();
$this->scoreShouldBe("Fifteen All");
}
public function test_score_Deuce()
{
$this->givenFirstPlayerScoreTimes(3);
$this->givenSecondPlayerScoreTimes(3);
$this->scoreShouldBe("Deuce");
}
public function test_score_FirstPlayer_Adv()
{
$this->givenFirstPlayerScoreTimes(4);
$this->givenSecondPlayerScoreTimes(3);
$this->scoreShouldBe("Joey Adv");
}
public function test_score_SecondPlayer_Adv()
{
$this->givenFirstPlayerScoreTimes(3);
$this->givenSecondPlayerScoreTimes(4);
$this->scoreShouldBe("May Adv");
}
public function test_score_FirstPlayer_Win()
{
$this->givenFirstPlayerScoreTimes(5);
$this->givenSecondPlayerScoreTimes(3);
$this->scoreShouldBe("Joey Win");
}
public function test_score_SecondPlayer_Win()
{
$this->givenFirstPlayerScoreTimes(3);
$this->givenSecondPlayerScoreTimes(5);
$this->scoreShouldBe("May Win");
}
protected function setUp()
{
$this->tennisGame = new TennisGame("Joey", "May");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment