Last active
August 24, 2023 14:22
-
-
Save mhsenpc/e046166348f283538a1372134442d35c to your computer and use it in GitHub Desktop.
A game for practising words with their meaning. it is implemented as a laravel command. you just need to choose the right answer through the 3 items
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 characters
<?php | |
class LanguageGameCommand extends BaseCommand { | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'game:play'; | |
protected array $words = [ | |
'nehmen' => 'take', | |
'verstehen' => 'understand', | |
'treffen' => 'meet', | |
'einladen' => 'invite', | |
'flieBend' => 'fluently', | |
'nachste cafe' => 'nearest cafe', | |
'weiB' => 'know', | |
'wann' => 'when', | |
'wirst' => 'will', | |
'besuchen' => 'visit', | |
'will' => 'want', | |
'reisen' => 'travel', | |
'schild' => 'sign', | |
'stehn' => 'stand', | |
'regnen' => 'rain' | |
]; | |
/** | |
* Execute the console command. | |
*/ | |
public function handle(): void { | |
do{ | |
$currentWord = array_rand($this->words); | |
$realAnswer = $this->words[$currentWord]; | |
$twoMoreAnswerKeys = array_rand($this->words, 2); | |
$totalAnswers = [$realAnswer, $this->words[$twoMoreAnswerKeys[0]], $this->words[$twoMoreAnswerKeys[1]] ]; | |
$totalAnswers = array_unique($totalAnswers); | |
shuffle($totalAnswers); | |
$userInput = $this->choice( | |
sprintf("What does %s mean?", $currentWord), | |
$totalAnswers, | |
); | |
if($userInput == $realAnswer){ | |
$this->info("Correct!"); | |
} else { | |
$this->error("Wrong!"); | |
} | |
} while($userInput); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment