alex-patterson-webdev / tennis-game
此包最新版本(1.1.0)没有可用的许可证信息。
简单的网球比分计算器
1.1.0
2020-07-04 21:26 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2024-09-05 06:30:32 UTC
README
Arp\TennisGame
关于
一个解决网球计分挑战的PHP解决方案。
安装
通过 composer 安装。
require alex-patterson-webdev/tennis-game ^1
使用方法
为了计算网球比分,首先创建一个 Arp\TennisGame\TennisGame
类的新实例。
use Arp\TennisGame\TennisGame;
$tennisGame = new TennisGame();
一旦创建,我们可以通过调用 playerOneWinsShot()
或 playerTwoWinsShot()
记录 PlayerOne
和 PlayerTwo
的得分。内部,TennisGame
类会记录每个玩家的得分,无论得分顺序如何。
$tennisGame->playerOneWinsShot(); // Increase PlayerOne score by 1 point
$tennisGame->playerTwoWinsShot(); // Increase PlayerOne score by 1 point
默认情况下,两位玩家的分数都从 love
(零)开始。在任何时候,我们都可以通过调用 renderScore()
方法来查看每位玩家的得分。两位玩家的分数将以字符串形式返回,格式为 x-y
,其中 x
代表第一位玩家的得分,y
代表第二位玩家的得分。
echo $tennisGame->renderScore(); // output string 'love-love'
$tennisGame->playerOneWinsShot();
echo $tennisGame->renderScore(); // output string 'fifteen-love'
$tennisGame->playerTwoWinsShot();
echo $tennisGame->renderScore(); // output string 'fifteen-fifteen'
根据网球规则,当两位选手得分相同且至少达到 forty
分时,调用 renderScore()
的结果将是 deuce
,表示得分相同。
$tennisGame->playerOneWinsShot();
$tennisGame->playerOneWinsShot();
$tennisGame->playerOneWinsShot();
$tennisGame->playerTwoWinsShot();
$tennisGame->playerTwoWinsShot();
$tennisGame->playerTwoWinsShot();
echo $tennisGame->renderScore(); // output string 'deuce'
如果比分在 deuce
时平局,那么选手们必须再得 1 分才能获得 advantage
。在这些情况下,调用 renderScore()
方法将显示当前具有优势的选手。
$tennisGame->playerOneWinsShot(); // 15
$tennisGame->playerOneWinsShot(); // 30
$tennisGame->playerOneWinsShot(); // 40
$tennisGame->playerTwoWinsShot(); // 15
$tennisGame->playerTwoWinsShot(); // 30
$tennisGame->playerTwoWinsShot(); // 40 (Deuce)
$tennisGame->playerTwoWinsShot(); // advantage PlayerOne
echo $tennisGame->renderScore(); // output string 'advantage PlayerTwo';
为了赢得比赛,选手们的得分必须超过 forty
分,并且比对手多至少两分。调用 renderScore()
将显示获胜选手的名字。
$tennisGame->playerOneWinsShot(); // 15
$tennisGame->playerTwoWinsShot(); // 15
$tennisGame->playerTwoWinsShot(); // 30
$tennisGame->playerTwoWinsShot(); // 40
$tennisGame->playerTwoWinsShot(); // win
echo $tennisGame->renderScore(); // output string 'win PlayerTwo';
单元测试
可以使用 PHPUnit 运行单元测试。
php vendor/bin/phpunit