-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
135 lines (121 loc) · 4.19 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
require_once 'vendor/autoload.php';
use App\Animal;
use App\Food;
use Codedungeon\PHPCliColors\Color;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\ConsoleOutput;
$animals = [
new Animal('horse', 55, 'plant'),
new Animal('rabbit', 75, 'vegetable'),
new Animal('panda', 60, 'plant'),
new Animal('parrot', 80, 'grain'),
];
$foods = [
new Food('fruit', 840),
new Food('vegetable', 220),
new Food('grain', 390),
new Food('plant', 555),
];
function displayAnimals($animals): void
{
echo Color::bold_purple(), "Select an animal:\n", Color::reset();
$outputAnimal = new ConsoleOutput();
$tableAnimal = new Table($outputAnimal);
$tableAnimal
->setHeaders(['Index', 'Animal', 'Favorite food', 'Happiness', 'Food reserves', 'Last meal'])
->setRows(array_map(function ($index, Animal $animal): array {
return [
$index,
$animal->getName(),
$animal->getFavoriteFood(),
$animal->getHappinessLevel(),
$animal->getFoodReserves(),
$animal->getLastFed()
];
}, array_keys($animals), $animals))
->render();
}
function displayFoods($foods, $selectedAnimal): void
{
echo Color::bold_purple(), "Select food to feed {$selectedAnimal->getname()}:\n", Color::reset();
$outputFood = new ConsoleOutput();
$tableFood = new Table($outputFood);
$tableFood
->setHeaders(['Index', 'Food', 'Amount'])
->setRows(array_map(function ($index, $food) {
return [$index, $food->getName(), $food->getAmount()];
}, array_keys($foods), $foods))
->render();
}
while (true) {
displayAnimals($animals);
$animalIndex = (int)readline("Enter the number of the animal you want to interact with: ");
if (!isset($animals[$animalIndex])) {
echo Color::blue(), "Invalid selection. Please try again.\n", Color::reset();
continue;
}
$selectedAnimal = $animals[$animalIndex];
echo Color::bold_cyan(), "You selected {$selectedAnimal->getName()}.\n", Color::reset();
echo "What do you want to do?\n";
$outputActivities = new ConsoleOutput();
$tableActivities = new Table($outputActivities);
$tableActivities
->setHeaders(['Index', 'Activity'])
->setRows([
['1', 'Play'],
['2', 'Work'],
['3', 'Feed'],
['4', 'Pet'],
['0', 'Exit'],
])
->render();
$action = (int)readline("Enter the number of the action: ");
if ($action === 0) {
break;
}
switch ($action) {
case 1:
$selectedAnimal->play($foods);
echo
Color::bold_cyan(),
"You played with {$selectedAnimal->getName()}.\n",
Color::reset();
break;
case 2:
$selectedAnimal->work();
echo
Color::bold_cyan(),
"You made {$selectedAnimal->getName()} work.\n",
Color::reset();
break;
case 3:
displayFoods($foods, $selectedAnimal);
$foodIndex = (int)readline("Enter the number of the food you want to give: ");
if (!isset($foods[$foodIndex])) {
echo Color::blue(), "Invalid selection. Please try again.\n", Color::reset();
break;
}
$selectedFood = $foods[$foodIndex];
$selectedAnimal->feed($selectedFood);
echo
Color::bold_cyan(),
"You fed {$selectedAnimal->getName()} with {$selectedFood->getName()}.\n",
Color::reset();
break;
case 4:
$selectedAnimal->pet();
echo
Color::bold_cyan(),
"You petted {$selectedAnimal->getName()}.\n",
Color::reset();
break;
default:
echo Color::blue(), "Invalid action. Please try again.\n", Color::reset();
break;
}
echo "Current status of {$selectedAnimal->getName()}:
Happiness: {$selectedAnimal->getHappinessLevel()},
Food Reserves: {$selectedAnimal->getFoodReserves()}.\n";
}
echo Color::bold_cyan(), "Goodbye!\n", Color::reset();