Skip to content

Commit fc8efb9

Browse files
committed
first project/step 7/ added Gcd Game, brain-gcd bin.
1 parent c097ba4 commit fc8efb9

File tree

6 files changed

+188
-2
lines changed

6 files changed

+188
-2
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ brain-even:
1010
brain-calc:
1111
bin/brain-calc
1212

13+
brain-gcd:
14+
bin/brain-gcd
15+
1316
validate:
1417
composer validate
1518

1619
lint:
1720
composer exec --verbose phpcs -- --standard=PSR12 src bin
21+
22+
correct:
23+
composer exec -- phpcbf --standard=PSR12 src bin

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
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)
1313
Even - https://asciinema.org/a/PZIB0AMPHDWAigwTO2UAGq9PI
14-
Calc - https://asciinema.org/a/5gDhJ6Taqf9GAgZvAtUY4W16l
14+
Calc - https://asciinema.org/a/5gDhJ6Taqf9GAgZvAtUY4W16l
15+
Gcd - https://asciinema.org/a/bBiNGAePf6ZXq4p0odlB5zFD6

bin/brain-gcd

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/Gcd.php';
8+
9+
runGame('Gcd');

brain-gcd.cast

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{"version": 2, "width": 120, "height": 30, "timestamp": 1762692133, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.020509, "o", "\u001b[?2004h\u001b]0;wasex@DESKTOP-G9HIGVK: ~/php-project-45\u0007\u001b[01;32mwasex@DESKTOP-G9HIGVK\u001b[00m:\u001b[01;34m~/php-project-45\u001b[00m$ "]
3+
[2.378912, "o", "m"]
4+
[2.458766, "o", "a"]
5+
[2.616335, "o", "k"]
6+
[2.730709, "o", "e"]
7+
[3.073927, "o", " "]
8+
[4.059469, "o", "b"]
9+
[4.165117, "o", "r"]
10+
[4.446778, "o", "a"]
11+
[4.56114, "o", "i"]
12+
[4.78113, "o", "n"]
13+
[5.766798, "o", "-"]
14+
[6.066033, "o", "g"]
15+
[6.373452, "o", "c"]
16+
[6.435198, "o", "d"]
17+
[6.972303, "o", "\r\n\u001b[?2004l\r"]
18+
[6.974413, "o", "bin/brain-gcd\r\n"]
19+
[6.999131, "o", "\r\n"]
20+
[7.000995, "o", "Welcome to the Brain Games!\r\n"]
21+
[7.00123, "o", "May I have your name?: "]
22+
[7.693641, "o", "V"]
23+
[8.195215, "o", "i"]
24+
[8.380079, "o", "k"]
25+
[8.46802, "o", "t"]
26+
[8.63529, "o", "o"]
27+
[8.776036, "o", "r"]
28+
[9.128015, "o", "\r\n"]
29+
[9.128463, "o", "Hello, Viktor!\r\n"]
30+
[9.128707, "o", "Find the greatest common divisor of given numbers.\r\n"]
31+
[9.128831, "o", "Question: 33 19\r\n"]
32+
[9.129002, "o", "Your answer: "]
33+
[29.11289, "o", "1"]
34+
[29.456083, "o", "\r\n"]
35+
[29.456413, "o", "Correct!\r\n"]
36+
[29.456595, "o", "Question: 42 17\r\nYour answer: "]
37+
[49.388125, "o", "1"]
38+
[49.652021, "o", "\r\n"]
39+
[49.652302, "o", "Correct!\r\nQuestion: 28 77\r\nYour answer: "]
40+
[84.174285, "o", "7"]
41+
[86.207402, "o", "\r\n"]
42+
[86.207681, "o", "Correct!\r\nCongratulations, Viktor!\r\n"]
43+
[86.213302, "o", "\u001b[?2004h\u001b]0;wasex@DESKTOP-G9HIGVK: ~/php-project-45\u0007\u001b[01;32mwasex@DESKTOP-G9HIGVK\u001b[00m:\u001b[01;34m~/php-project-45\u001b[00m$ "]
44+
[89.260781, "o", "make brain-gcd"]
45+
[90.237544, "o", "\r\n\u001b[?2004l\r"]
46+
[90.239577, "o", "bin/brain-gcd\r\n"]
47+
[90.263845, "o", "\r\n"]
48+
[90.266011, "o", "Welcome to the Brain Games!\r\n"]
49+
[90.266219, "o", "May I have your name?: "]
50+
[91.830356, "o", "V"]
51+
[92.067959, "o", "i"]
52+
[92.349515, "o", "k"]
53+
[92.516645, "o", "t"]
54+
[93.035697, "o", "o"]
55+
[93.185252, "o", "r"]
56+
[93.502203, "o", "\r\n"]
57+
[93.502609, "o", "Hello, Viktor!\r\n"]
58+
[93.50269, "o", "Find the greatest common divisor of given numbers.\r\n"]
59+
[93.502894, "o", "Question: 90 27\r\n"]
60+
[93.503148, "o", "Your answer: "]
61+
[94.910203, "o", "1"]
62+
[95.350155, "o", "\b \b"]
63+
[99.653316, "o", "2"]
64+
[99.785366, "o", "7"]
65+
[100.190316, "o", "\r\n"]
66+
[100.190676, "o", "'27' is wrong answer ;(. Correct answer was '9'\r\n"]
67+
[100.190766, "o", "Let's try again, Viktor!\r\n"]
68+
[100.196152, "o", "\u001b[?2004h\u001b]0;wasex@DESKTOP-G9HIGVK: ~/php-project-45\u0007\u001b[01;32mwasex@DESKTOP-G9HIGVK\u001b[00m:\u001b[01;34m~/php-project-45\u001b[00m$ "]
69+
[102.619136, "o", "make brain-gcd"]
70+
[103.015041, "o", "\r\n\u001b[?2004l\r"]
71+
[103.017119, "o", "bin/brain-gcd\r\n"]
72+
[103.040898, "o", "\r\n"]
73+
[103.042719, "o", "Welcome to the Brain Games!\r\n"]
74+
[103.04296, "o", "May I have your name?: "]
75+
[105.989374, "o", "V"]
76+
[106.200503, "o", "i"]
77+
[106.446958, "o", "j"]
78+
[107.124397, "o", "\b \b"]
79+
[107.854868, "o", "j"]
80+
[107.995735, "o", "t"]
81+
[108.840529, "o", "\b \b"]
82+
[109.007773, "o", "\b \b"]
83+
[109.518131, "o", "k"]
84+
[110.160553, "o", "t"]
85+
[110.301513, "o", "o"]
86+
[110.424491, "o", "r"]
87+
[110.662191, "o", "\r\n"]
88+
[110.662611, "o", "Hello, Viktor!\r\n"]
89+
[110.662855, "o", "Find the greatest common divisor of given numbers.\r\n"]
90+
[110.663052, "o", "Question: 36 90\r\nYour answer: "]
91+
[138.566873, "o", "5"]
92+
[139.341276, "o", "\r\n"]
93+
[139.34155, "o", "'5' is wrong answer ;(. Correct answer was '18'\r\n"]
94+
[139.3417, "o", "Let's try again, Viktor!\r\n"]
95+
[139.347356, "o", "\u001b[?2004h\u001b]0;wasex@DESKTOP-G9HIGVK: ~/php-project-45\u0007\u001b[01;32mwasex@DESKTOP-G9HIGVK\u001b[00m:\u001b[01;34m~/php-project-45\u001b[00m$ "]
96+
[144.850279, "o", "make brain-gcd"]
97+
[145.439886, "o", "\r\n\u001b[?2004l\r"]
98+
[145.442021, "o", "bin/brain-gcd\r\n"]
99+
[145.466249, "o", "\r\n"]
100+
[145.468027, "o", "Welcome to the Brain Games!\r\n"]
101+
[145.46818, "o", "May I have your name?: "]
102+
[146.64549, "o", "V"]
103+
[146.91804, "o", "i"]
104+
[147.111722, "o", "k"]
105+
[147.199713, "o", "t"]
106+
[147.366835, "o", "o"]
107+
[147.490151, "o", "r"]
108+
[147.771717, "o", "\r\n"]
109+
[147.772148, "o", "Hello, Viktor!\r\n"]
110+
[147.772364, "o", "Find the greatest common divisor of given numbers.\r\n"]
111+
[147.772491, "o", "Question: 2 30\r\n"]
112+
[147.772728, "o", "Your answer: "]
113+
[151.441231, "o", "2"]
114+
[151.590903, "o", "\r\n"]
115+
[151.591203, "o", "Correct!\r\n"]
116+
[151.591431, "o", "Question: 72 54\r\nYour answer: "]
117+
[171.58442, "o", "1"]
118+
[171.751628, "o", "8"]
119+
[172.033319, "o", "\r\n"]
120+
[172.033611, "o", "Correct!\r\nQuestion: 100 120\r\nYour answer: "]
121+
[191.516273, "o", "2"]
122+
[191.639567, "o", "9"]
123+
[192.299439, "o", "\b \b"]
124+
[192.792193, "o", "0"]
125+
[193.100412, "o", "\r\n"]
126+
[193.10065, "o", "Correct!\r\n"]
127+
[193.100855, "o", "Congratulations, Viktor!\r\n"]
128+
[193.106341, "o", "\u001b[?2004h\u001b]0;wasex@DESKTOP-G9HIGVK: ~/php-project-45\u0007\u001b[01;32mwasex@DESKTOP-G9HIGVK\u001b[00m:\u001b[01;34m~/php-project-45\u001b[00m$ "]
129+
[195.414841, "o", "\u001b[?2004l\r\r\nexit\r\n"]

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"bin": [
1616
"bin/brain-games",
1717
"bin/brain-even",
18-
"bin/brain-calc"
18+
"bin/brain-calc",
19+
"bin/brain-gcd"
1920
],
2021

2122
"require-dev": {

src/Games/Gcd.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
function getRules()
4+
{
5+
return 'Find the greatest common divisor of given numbers.';
6+
}
7+
8+
function generateQuestion()
9+
{
10+
if (rand(1, 3) === 1) {
11+
// 33% случаев - гарантированный нетривиальный НОД
12+
$base = rand(2, 25);
13+
$firstNumber = $base * rand(2, 10);
14+
$secondNumber = $base * rand(2, 10);
15+
} else {
16+
// 67% случаев - полностью случайные
17+
$firstNumber = rand(1, 100);
18+
$secondNumber = rand(1, 100);
19+
}
20+
return "$firstNumber $secondNumber";
21+
}
22+
23+
function getCorrectAnswer($question)
24+
{
25+
list($secondNumber, $firstNumber ) = explode(" ", $question);
26+
27+
$firstNumber = (int)$firstNumber;
28+
$secondNumber = (int)$secondNumber;
29+
30+
if ($secondNumber == 0) {
31+
return $firstNumber;
32+
}
33+
34+
while ($secondNumber != 0) {
35+
$temp = $secondNumber;
36+
$secondNumber = $firstNumber % $secondNumber;
37+
$firstNumber = $temp;
38+
}
39+
return $firstNumber;
40+
}

0 commit comments

Comments
 (0)