Skip to content

Commit

Permalink
(#5) Completely refactored the Game into various Classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeseekr committed Jun 30, 2020
1 parent a78ff30 commit 4aec36a
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 223 deletions.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ return PhpCsFixer\Config::create()
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('bootstrap')
->exclude('vendor')
->in(__DIR__)
);
72 changes: 25 additions & 47 deletions app/Commands/PlayDilemmaGameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace App\Commands;

use HopeSeekr\PrisonersDilemma\Adjudicator;
use HopeSeekr\PrisonersDilemma\Interrogator;
use HopeSeekr\PrisonersDilemma\SuspectDecision;
use Illuminate\Console\Scheduling\Schedule;
use InvalidArgumentException;
use LaravelZero\Framework\Commands\Command;
Expand Down Expand Up @@ -73,69 +76,44 @@ public function handle()
}
$this->line('');

while (true) {
try {
$decision = $this->ask($cp->onBlue()->bold()->white(' What is your decision? '));
if (filter_var($decision, FILTER_VALIDATE_INT) === false || ($decision < 0 || $decision > 2)) {
throw new InvalidArgumentException();
}
$getPlayersChoice = function () use ($cp): int {
while (true) {
try {
$decision = $this->ask($cp->onBlue()->bold()->white(' What is your decision? '));
if (filter_var($decision, FILTER_VALIDATE_INT) === false || ($decision < 0 || $decision > 2)) {
throw new InvalidArgumentException();
}

break;
} catch (InvalidArgumentException $e) {
return (int) $decision;
} catch (InvalidArgumentException $e) {
}
}
}
};
$yourChoice = $getPlayersChoice();

$play = function ($yourChoice) use ($choices) {
$interrogator = new Interrogator();

$play = function ($decision) use ($choices) {
$RANDOMNESS = 100; // 100 = 0 chance.
$sentences = [
'loitering' => 1,
'single' => 3,
'team' => 2,
];

$partnerChoice = random_int(1, 2);
$partnerDecisions = [
self::CONFESS => 'They confessed to everything',
self::TESTIFY => 'They testified against you',
self::SILENCE => 'They said nothing',
];
$partnerDecision = $partnerDecisions[$partnerChoice];
$partnerChoice = $interrogator->interrogatePartner();
$partnerDecision = SuspectDecision::getThirdPartyResponse($partnerChoice);
dump($partnerChoice);

$this->line((new ConsolePainter())->onRed()->white()->bold(" Your partner's decision: $partnerDecision. "));
$choices = [
'Confess',
'Testify against your partner',
'Say nothing',
];

switch ($decision) {
case self::CONFESS:
$convictions['you'] = $sentences['single'];
$convictions['partner'] = $partnerChoice === 0 ? $sentences['single'] : 0;
break;
case self::TESTIFY:
if ($partnerChoice === self::CONFESS) {
$convictions['you'] = 0;
$convictions['partner'] = $sentences['single'];
} elseif ($partnerChoice === self::TESTIFY) {
$convictions['partner'] = $convictions['you'] = $sentences['team'];
} elseif ($partnerChoice === self::SILENCE) {
$convictions['you'] = 0;
$convictions['partner'] = $sentences['single'];
}
break;
case self::SILENCE:
if ($partnerChoice === self::CONFESS) {
$convictions['you'] = 0;
$convictions['partner'] = $sentences['single'];
} elseif ($partnerChoice === self::TESTIFY) {
$convictions['you'] = $sentences['single'];
$convictions['partner'] = 0;
} elseif ($partnerChoice === self::SILENCE) {
$convictions['partner'] = $convictions['you'] = $sentences['loitering'];
}
}
$judge = new Adjudicator();

return $convictions;
return $judge->issueSentence($yourChoice, $partnerChoice);
};

$sentences = [
Expand All @@ -144,7 +122,7 @@ public function handle()
'team' => 2,
];

$convictions = $play($decision);
$convictions = $play($yourChoice);
dump($convictions);

$calculateIncome = function ($hourlyWage, $years): float {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"php": "^7.4",
"ext-json": "*",
"laravel-zero/framework": "^7.0",
"phpexperts/console-painter": "^1.0"
"phpexperts/console-painter": "^1.0",
"phpexperts/php-evolver": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "8.*|9.*",
Expand Down
Loading

0 comments on commit 4aec36a

Please sign in to comment.