susanbuck/剪刀石头布

0.1.6 2021-11-01 01:49 UTC

This package is auto-updated.

Last update: 2024-09-29 05:22:11 UTC


README

这是一个示例包,用于演示在DGMD E-2 Web Programming for Beginners with PHP中使用的依赖关系。

安装

composer require susanbuck/rock-paper-scissors

使用

基本示例

require __DIR__.'/vendor/autoload.php';

use RPS\Game;

$game = new Game();

# Each invocation of the `play` method will play and track a new round of player (given move) vs. computer
$game->play('rock');

The Game类接受三个构造参数

  • bool $persistResults
    • 指示结果是否应持久化到SESSION
    • 默认为 false
  • int $maxResults
    • 指示在SESSION中记录的最大结果数
    • 默认为 5
  • string $timezone
    • 指示每轮应记录在哪个时区
    • 默认为 'America/New_York'

方法

play(String $move): array

接受字符串 'rock''paper''scissors'

如果给出无效动作,将终止程序执行。

返回结果数组

[
    'player' => player's move
    'computer' => computer's move
    'outcome' => outcome (won, lost, tie)
    'timestamp' => timestamp for when the round was played
];

getResults(): array

返回最后x个结果的数组,其中x是 $maxResults

如果 $persistResults 设置为 false,则返回 null。

clearResults(): void

清除会话中的结果