Skip to content

Commit

Permalink
Better docs and result tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
susanBuck committed Oct 22, 2019
1 parent ca2897a commit ce40aed
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 14 deletions.
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,54 @@ composer require susanbuck/rock-paper-scissors


## Usage
Basic example:

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

use RPS\Game;

$game = new Game();

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

The Game class accepts three constructor parameters:

+ `bool persistResults`
+ Indicates whether or not results should be persisted to the SESSION
+ Defaults to `false`
+ `int $maxResults`
+ Indicates the max # of results to record in the SESSION
+ Defaults to `5`
+ `string $timezone`
+ Indicates what timezone each round should be recorded in
+ Defaults to `'America/New_York'`


## Methods

## `play(String $move): array`
Accepts a string of either `'rock'`, `'paper'`, or `'scissors'`.

Will halt the execution of the program if an invalid move is given.

Returns an array of results:
```
[
'player' => player's move
'computer' => computer's move
'outcome' => outcome (won, lost, tie)
'timestamp' => timestamp for when the round was played
];
```

## `getResults(): array`
Returns an array of the last x results, where x is `$maxResults`.

Returns null if `$persistResults` is set to `false`.


# An array of the results for each round (player move, computer move, whether player won) can be accessed from the `getResults` method
var_dump($game->getResults());
```
## `clearResults(): void`
Clears the session of results
106 changes: 98 additions & 8 deletions src/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,42 @@

class Game
{
private $results;
private $persistResults;
private $maxResults;
private $sessionKey = 'RPS_Results';


/**
* __construct
*
* @param bool $persistResults Whether or not results should be stored to the SESSION
* @param int $maxResults The max number of results to be stored to the session
* @param string $timezone The timezone to use for the results timestamp
* @return void
*/
public function __construct(bool $persistResults = false, int $maxResults = 5, string $timezone = 'America/New_York')
{
session_start();

date_default_timezone_set($timezone);

if ($persistResults) {
$this->maxResults = $maxResults;
$this->persistResults = $persistResults;
} else {
$this->clearResults();
}
}


/**
* play
* Invoke with a move: rock, paper or scissors
* Returns an array of results with player, computer, outcome, timestamp
*
* @param string $move
* @return array
*/
public function play(string $move)
{
if (!$this->validate($move)) {
Expand All @@ -17,30 +51,86 @@ public function play(string $move)
if ($computer == $move) {
$outcome = 'tie';
} elseif ($move == 'rock' and $computer == 'scissors' or $move == 'paper' and $computer == 'rock' or $move == 'scissors' and $computer == 'paper') {
$outcome = 'win';
$outcome = 'won';
} else {
$outcome = 'loose';
$outcome = 'lost';
}

$this->results[] = [
$results = [
'player' => $move,
'computer' => $computer,
'outcome' => $outcome
'outcome' => $outcome,
'timestamp' => date('F j, Y, g:i:s a')
];

return $outcome;
if ($this->persistResults) {
$this->persistResults($results);
}

return $results;
}

/**
* getResults
* Retrieve the results from the session
* @return void
*/
public function getResults()
{
return $this->results;
return $_SESSION[$this->sessionKey] ?? null;
}

private function validate($move)

/**
* clearResults
* Clear the results from the session
*
* @return void
*/
public function clearResults()
{
$_SESSION[$this->sessionKey] = null;
}


/**
* persistResults
* Persist the results to the session
*
* @param array $results
* @return void
*/
private function persistResults(array $results)
{

# Get the existing session data, defaulting to an empty array if none exists
$_SESSION[$this->sessionKey] = $_SESSION[$this->sessionKey] ?: [];

# Add these results to the start
array_unshift($_SESSION[$this->sessionKey], $results);

# Use array slice to make sure we're only includeing 0 up to maxResults
$_SESSION[$this->sessionKey] = array_slice($_SESSION[$this->sessionKey], 0, $this->maxResults);
}


/**
* validate
* Confirm the given move is valid
* @param string $move
* @return void
*/
private function validate(string $move)
{
return in_array($move, ['rock', 'paper', 'scissors']);
}


/**
* getRandomMove
* Get a random move of rock, paper, or scissors
* @return void
*/
private function getRandomMove()
{
return ['rock','paper','scissors'][rand(0, 1)];
Expand Down
11 changes: 11 additions & 0 deletions tests/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require $_SERVER['DOCUMENT_ROOT'].'/src/Game.php';

$game = new RPS\Game(true);

$game->play('rock');



var_dump($game->getResults());

0 comments on commit ce40aed

Please sign in to comment.