Skip to content

Instantly share code, notes, and snippets.

@mgldev
Last active December 22, 2015 10:48

Revisions

  1. mgldev revised this gist Sep 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ class Paper extends Hand implements Cuttable {

    protected function assess(Hand $hand) {

    return $hand instanceof Bluntable;
    return $hand instanceof Wrappable;
    }
    }

  2. mgldev revised this gist Sep 6, 2013. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,13 @@ class DrawException extends DomainException {}

    abstract class Hand {

    public abstract function beats(Hand $hand);
    public function beats(Hand $hand) {

    $this->rejectDraw($hand);
    $this->assess($hand);
    }

    protected abstract function assess(Hand $hand);

    public function __toString() {

    @@ -25,27 +31,24 @@ interface Bluntable {}

    class Rock extends Hand implements Wrappable {

    public function beats(Hand $hand) {
    protected function assess(Hand $hand) {

    $this->rejectDraw($hand);
    return $hand instanceof Bluntable;
    }
    }

    class Paper extends Hand implements Cuttable {

    public function beats(Hand $hand) {
    protected function assess(Hand $hand) {

    $this->rejectDraw($hand);
    return $hand instanceof Bluntable;
    }
    }

    class Scissors extends Hand implements Bluntable {

    public function beats(Hand $hand) {
    protected function assess(Hand $hand) {

    $this->rejectDraw($hand);
    return $hand instanceof Cuttable;
    }
    }
  3. mgldev revised this gist Sep 6, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ class Rock extends Hand implements Wrappable {

    public function beats(Hand $hand) {

    $this->rejectDraw($hand);
    return $hand instanceof Bluntable;
    }
    }
    @@ -35,6 +36,7 @@ class Paper extends Hand implements Cuttable {

    public function beats(Hand $hand) {

    $this->rejectDraw($hand);
    return $hand instanceof Bluntable;
    }
    }
    @@ -43,6 +45,7 @@ class Scissors extends Hand implements Bluntable {

    public function beats(Hand $hand) {

    $this->rejectDraw($hand);
    return $hand instanceof Cuttable;
    }
    }
  4. mgldev renamed this gist Sep 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. mgldev created this gist Sep 6, 2013.
    73 changes: 73 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?php

    class DrawException extends DomainException {}

    abstract class Hand {

    public abstract function beats(Hand $hand);

    public function __toString() {

    return get_class($this);
    }

    public function rejectDraw(Hand $hand) {

    if ($hand === $this) {
    throw new DrawException('No player wins, round is a draw');
    }
    }
    }

    interface Wrappable {}
    interface Cuttable {}
    interface Bluntable {}

    class Rock extends Hand implements Wrappable {

    public function beats(Hand $hand) {

    return $hand instanceof Bluntable;
    }
    }

    class Paper extends Hand implements Cuttable {

    public function beats(Hand $hand) {

    return $hand instanceof Bluntable;
    }
    }

    class Scissors extends Hand implements Bluntable {

    public function beats(Hand $hand) {

    return $hand instanceof Cuttable;
    }
    }

    $hands = ['r' => new Rock, 'p' => new Paper, 's' => new Scissors];

    echo "Choose hand [r, p, s]: ";

    $choices = [
    'computer' => array_keys($hands)[rand(0, 2)],
    'player' => strtolower(trim(fgets(STDIN)))
    ];

    $computerHand = $hands[$choices['computer']];
    $playerHand = $hands[$choices['player']];

    echo "Player chose $playerHand\n";
    echo "Computer chose $computerHand\n";

    try {
    if ($playerHand->beats($computerHand)) {
    echo sprintf("Player wins, %s beats %s\n", $playerHand, $computerHand);
    } else {
    echo sprintf("Computer wins, %s beats %s\n", $computerHand, $playerHand);
    }
    } catch (DrawException $ex) {
    echo $ex->getMessage() . "\n";
    }