Last active
December 22, 2015 10:48
Revisions
-
mgldev revised this gist
Sep 6, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 Wrappable; } } -
mgldev revised this gist
Sep 6, 2013 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,13 @@ class DrawException extends DomainException {} abstract class 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 { protected function assess(Hand $hand) { return $hand instanceof Bluntable; } } class Paper extends Hand implements Cuttable { protected function assess(Hand $hand) { return $hand instanceof Bluntable; } } class Scissors extends Hand implements Bluntable { protected function assess(Hand $hand) { return $hand instanceof Cuttable; } } -
mgldev revised this gist
Sep 6, 2013 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } } -
mgldev renamed this gist
Sep 6, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mgldev created this gist
Sep 6, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"; }