Skip to content

Commit a2c737c

Browse files
committed
Add brain-calc game
1 parent f58d99b commit a2c737c

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ brain-games:
88
./bin/brain-games
99
brain-even:
1010
./bin/brain-even
11-
11+
brain-calc:
12+
./bin/brain-calc

bin/brain-calc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
require __DIR__."/../vendor/autoload.php";
6+
7+
\BrainGames\CalcGame\startCalcGame();

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
"autoload": {
77
"files": [
88
"src/Cli.php",
9-
"src/EvenGame.php"
9+
"src/EvenGame.php",
10+
"src/CalcGame.php"
1011
]
1112
},
1213
"bin": [
13-
"bin/brain-games"
14+
"bin/brain-games",
15+
"bin/brain-even",
16+
"bin/brain-calc"
1417
],
1518
"authors": [
1619
{

src/CalcGame.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace BrainGames\CalcGame;
4+
5+
use function BrainGames\Cli\playGame;
6+
7+
function startCalcGame(): void
8+
{
9+
$questions = [];
10+
$answers = [];
11+
for ($i = 0; $i < 3; $i++) {
12+
$number1 = rand(1, 100);
13+
$number2 = rand(1, 100);
14+
$operationNumber = rand(1, 3);
15+
$operation = '';
16+
$expressionResult = 0;
17+
switch ($operationNumber) {
18+
case 1:
19+
$operation = '+';
20+
$expressionResult = $number1 + $number2;
21+
break;
22+
case 2:
23+
$operation = '-';
24+
$expressionResult = $number1 - $number2;
25+
break;
26+
case 3:
27+
$operation = '*';
28+
$expressionResult = $number1 * $number2;
29+
break;
30+
}
31+
$expression="{$number1}{$operation}{$number2}";
32+
$questions[$i] = $expression ;
33+
$answers[$i] = $expressionResult;
34+
}
35+
playGame($questions, $answers, 'What is the result of the expression?');
36+
}

0 commit comments

Comments
 (0)