Reputation: 11
I need to modify my code so that it becomes a model-view-controller. Since I'm a total noob when it comes to programming my head aches if I'm honest. Any help how to do this would be appreciated.
For a better understanding I will paste the whole task so you can see what my code is for.
*In one pasture there are 200 bustling sheep. The flock consists of 95 white sheep, 60 black sheep and 45 white-black sheep. The shepherd now wants to divide them up so that the corresponding wool can be sheared by color. Help him write a small script so that he can better order his sheep. Please implement the first three points with variables, arrays, mathematical operators and functions. Tip: For a better overview, you can also define your own functions.
Here's my code sample, as I said the following part has to become a model-view-controller. How do i do this?
Here is my code:
<?php
$stableForEachColor = array("stableForWhite", "stableForBlack", "stableForWhiteBlack");
// Function to get the full amount of all sheep
function legs($all)
{
$legs = 4;
return $all * $legs;
}
$whiteSheep = 95;
$blackSheep = 60;
$whiteBlackSheep = 45;
$all = $whiteSheep + $blackSheep + $whiteBlackSheep;
echo "At the pasture there are $all sheep in total.\n\n";
$legs = legs($all);
echo "All sheep combined have $legs legs in total.\n\n";
// Names of the white sheep
for ($colorWhite = 1; $colorWhite <= $whiteSheep; $colorWhite++) {
echo "Wooly $colorWhite is white.\n";
}
// Names of the black sheep
for ($colorBlack = 1; $colorBlack <= $blackSheep; $colorBlack++) {
echo "Wooly $colorBlack is black.\n";
}
// Names of the white-black sheep
for ($colorWhiteBlack = 1; $colorWhiteBlack <= $whiteBlackSheep; $colorWhiteBlack++) {
echo "Wooly $colorWhiteBlack is white-black.\n";
}
$stable = array('white_sheep' => $colorWhite, 'black_sheep' => $colorBlack, 'white-black_sheep' => $colorWhiteBlack);
echo '<pre>';
print_r($stable);
$sheep = 200;
$stablesC = 4;
for ($i = $sheep; $i > 0; $i--) {
$stables[rand(0, ($stablesC - 1))] += 1;
}
echo '<pre>';
print_r($stables);
Upvotes: 0
Views: 90
Reputation: 2860
Most of that task involves just restructuring your code, to separate...well, the M, V and C bits.
There is no one true answer, but based on a quick glance i suspect the model part may look roughly like:
and your view layer can likely be very slim (just a couple methods in total).
anyway, maybe this gives you a little of an idea on where to start.
Upvotes: 1