Skip to content

Commit c097ba4

Browse files
committed
first project/step sixth/ Added brain-calc, Engine, Calc and Even game files
1 parent 0a9b3fa commit c097ba4

File tree

10 files changed

+100
-58
lines changed

10 files changed

+100
-58
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
.idea/

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ brain-games:
77
brain-even:
88
bin/brain-even
99

10+
brain-calc:
11+
bin/brain-calc
12+
1013
validate:
1114
composer validate
1215

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=Eksi-tech_php-project-45&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=Eksi-tech_php-project-45)
1111
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Eksi-tech_php-project-45&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=Eksi-tech_php-project-45)
1212
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=Eksi-tech_php-project-45&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=Eksi-tech_php-project-45)
13-
https://asciinema.org/a/PZIB0AMPHDWAigwTO2UAGq9PI
13+
Even - https://asciinema.org/a/PZIB0AMPHDWAigwTO2UAGq9PI
14+
Calc - https://asciinema.org/a/5gDhJ6Taqf9GAgZvAtUY4W16l

bin/brain-calc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
require_once __DIR__ . '/../src/Engine.php';
7+
require_once __DIR__ . '/../src/Games/Calc.php';
8+
9+
runGame('Calc');

bin/brain-even

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,13 @@
33
<?php
44

55
require_once __DIR__ . '/../vendor/autoload.php';
6+
require_once __DIR__ . '/../src/Engine.php';
7+
require_once __DIR__ . '/../src/Games/Even.php';
8+
9+
runGame('Even');
10+
11+
612

7-
use function cli\line;
8-
use function cli\prompt;
9-
10-
line('Welcome to the Brain Game!');
11-
$name = prompt('May I have your name?');
12-
line("Hello, %s!", $name);
13-
14-
line('Answer "yes" if the number is even, otherwise answer "no".');
15-
$winningCounter = 0;
16-
function isEven($number)
17-
{
18-
return $number % 2 === 0;
19-
}
20-
21-
while ($winningCounter < 3) {
22-
$number = rand(1, 20);
23-
line('Question:' . "{$number}");
24-
$answer = prompt('Your answer');
25-
26-
if (isEven($number)) {
27-
28-
if ($answer === 'yes') {
29-
line("Correct!");
30-
$winningCounter++;
31-
}
32-
elseif ($answer === 'no' || $answer != "yes") {
33-
line("'$answer' is wrong answer ;(. Correct answer was 'yes'.
34-
Let's try again, $name!");
35-
break;
36-
}
37-
}else{
38-
if ($answer === 'yes'|| $answer != "no") {
39-
line("'$answer' is wrong answer ;(. Correct answer was 'no'.
40-
Let's try again, $name!");
41-
break;
42-
}
43-
elseif ($answer === 'no') {
44-
line("Correct!");
45-
$winningCounter++;
46-
}
47-
}
48-
if ($winningCounter === 3) {
49-
line("Congratulations, $name!");
50-
}
51-
}
5213

5314

5415

bin/brain-games

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<?php
44

55
require_once __DIR__ . '/../vendor/autoload.php';
6-
require_once __DIR__ . '/../src/Cli.php';
6+
require_once __DIR__ . '/../src/Engine.php';

composer.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
},
1515
"bin": [
1616
"bin/brain-games",
17-
"bin/brain-even"
17+
"bin/brain-even",
18+
"bin/brain-calc"
1819
],
19-
"autoload": {
20-
"psr-4": {
21-
"BrainGames\\": "src/"
22-
}
23-
},
20+
2421
"require-dev": {
2522
"squizlabs/php_codesniffer": "^4.0"
2623
},

src/Engine.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
<?php
22

3-
namespace BrainGames\Cli;
4-
53
use function cli\line;
64
use function cli\prompt;
75

8-
line('Welcome to the Brain Game!');
9-
$name = prompt('May I have your name?');
10-
line("Hello, %s!", $name);
6+
function runGame($gameName)
7+
{
8+
line('Welcome to the Brain Games!');
9+
$name = prompt('May I have your name?');
10+
line("Hello, %s!", $name);
11+
12+
require_once __DIR__ . "/Games/$gameName.php";
13+
14+
$rules = getRules();
15+
line($rules);
16+
17+
$winCount = 0;
18+
while ($winCount < 3) {
19+
$question = generateQuestion();
20+
line("Question: $question");
21+
$userAnswer = prompt('Your answer');
22+
$correctAnswer = getCorrectAnswer($question);
23+
24+
if ($userAnswer == $correctAnswer) {
25+
line('Correct!');
26+
$winCount++;
27+
} else {
28+
line("'$userAnswer' is wrong answer ;(. Correct answer was '$correctAnswer'");
29+
line("Let's try again, $name!");
30+
return;
31+
}
32+
}
33+
34+
line("Congratulations, $name!");
35+
}

src/Games/Calc.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
function getRules()
4+
{
5+
return 'What is the result of the expression?';
6+
}
7+
8+
function generateQuestion()
9+
{
10+
$firstOperand = rand(1, 20);
11+
$secondOperand = rand(1, 20);
12+
$actions = ["+", "-", "*"];
13+
$operation = $actions[rand(0, 2)];
14+
return "$firstOperand $operation $secondOperand";
15+
}
16+
17+
function getCorrectAnswer($question)
18+
{
19+
list($firstOperand, $operation, $secondOperand) = explode(" ", $question);
20+
switch ($operation) {
21+
case "+":
22+
return $firstOperand + $secondOperand;
23+
case "-":
24+
return $firstOperand - $secondOperand;
25+
case "*":
26+
return $firstOperand * $secondOperand;
27+
}
28+
return 0;
29+
}

src/Games/Even.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
function getRules()
4+
{
5+
return 'Answer "yes" if the number is even, otherwise answer "no".';
6+
}
7+
8+
function generateQuestion()
9+
{
10+
return rand(1, 20);
11+
}
12+
13+
function getCorrectAnswer($question)
14+
{
15+
return $question % 2 ? 'no' : 'yes';
16+
}

0 commit comments

Comments
 (0)