I am trying to create a fantasy cricket team of 11 players from a large list of players.
For the team, I have basic minimum and maximum requirements for each type of player. I have assigned values like this:
$min_wicket_keeper = 1;
$min_batsman = 3;
$min_all_rounders = 1;
$min_bowlers = 2;
$max_wicket_keeper = 3;
$max_batsman = 5;
$max_all_rounders = 5;
$max_bowlers = 4;
I have total players like this:
$total_wicket_keepers = 2;
$total_batsman = 6;
$total_all_rounders = 5;
$total_bowlers = 5;
You can see I have total 18 players. Now I want to create a team of 11 players assigning random type players — keep in mind the minimum and maximum requirements of players.
Example 1
$wicket_keeper_limit = 1;
$batsman_limit = 4;
$all_rounder_limit = 4;
$bowler_limit = 2;
//so total 11
Example 2
$wicket_keeper_limit = 2;
$batsman_limit = 3;
$all_rounder_limit = 3;
$bowler_limit = 3;
//so total 11
I want a random number assigned for each type but total should be 11 and should not below minimum requirements and should not greater then total available players for each type followed by maximum limit.
I have tried code like this
$min_wicket_keeper = 1;
$min_batsman = 3;
$min_all_rounders = 1;
$min_bowlers = 2;
$max_wicket_keeper = 3;
$max_batsman = 5;
$max_all_rounders = 5;
$max_bowlers = 4;
$total_wicket_keepers = 2;
$total_batsman = 6;
$total_all_rounders = 5;
$total_bowlers = 5;
$total_players_match = $total_wicket_keepers+$total_batsman+$total_all_rounders+$total_bowlers;
echo $total_players_match;
if ($total_players_match > 11) {
$remain_players = 11;
if ($total_wicket_keepers > $min_wicket_keeper) {
$wicket_keeper_limit = rand($min_wicket_keeper,$total_wicket_keepers);
$remain_players = $remain_players-$wicket_keeper_limit;
} else {
$wicket_keeper_limit = $total_wicket_keepers;
$remain_players = $remain_players-$wicket_keeper_limit;
}
echo "WK: ".$wicket_keeper_limit." REMAIN: ".$remain_players."<br>";
if ($total_batsman>$min_batsman) {
$batsman_limit = rand($min_batsman,$total_batsman);
$remain_players = $remain_players-$batsman_limit;
if ($remain_players > ($total_bowlers + $total_all_rounders)) {
$batsman_limit = ($min_batsman + $remain_players) - ($total_bowlers+$total_all_rounders);
$remain_players = $remain_players-$batsman_limit;
}
} else {
$batsman_limit = $total_batsman;
$remain_players = $remain_players-$batsman_limit;
}
echo "BT: " . $batsman_limit . " REMAIN: " . $remain_players . "<br>";
if ($total_bowlers > $min_bowlers) {
$bowler_limit = rand($min_bowlers, $total_bowlers);
$remain_players = $remain_players - $bowler_limit;
if ($remain_players > $total_all_rounders) {
$bowler_limit = $total_bowlers;
$remain_players = $remain_players - $bowler_limit;
}
} else {
$bowler_limit = $total_bowlers;
$remain_players = $remain_players - $bowler_limit;
}
echo "BOL:" . $bowler_limit . " REMAIN:" . $remain_players . "<br>";
$all_rounder_limit = $remain_players;
echo "ALL: " . $all_rounder_limit . " REMAIN: " . $remain_players . "<br>";
} else {
$wicket_keeper_limit = $total_wicket_keepers;
$batsman_limit = $total_batsman;
$all_rounder_limit = $total_all_rounders;
$bowler_limit = $total_bowlers;
}
echo "WK:" . $wicket_keeper_limit . "<br>";
echo "BT:" . $batsman_limit . "<br>";
echo "BO:" . $bowler_limit . "<br>";
echo "AL:" . $all_rounder_limit . "<br>";
but it’s failing to follow my minimum and maximum requirements. Sometimes I am getting bowler as 0 and sometimes I am getting all rounder as high as 6.
This is Online Fiddle Link
Edit: According to comments, I should clear that, I will generate multiple teams with put above code in while loops, Like 50 teams which will have different/random players in each type like some team have 5 batsman, some team will have only 3 batsman.
When I will get above code working, I will later get players from my database according to limit, I have got for each type.
2
Answers
I’m sorry but your code to me was too much to handle. Based on your conditions I wrote following code:
I hope the comments are clear enough.
Live example
You might build this script as a single function/method or better would be to practice the single-responsibility principle and have multiple functions/methods which perform exactly one task. To avoid expanding the snippet too much, I’ll only show my advice as a multi-responsibility function.
Code: (Demo)
Potential output: