我试图从一个大的球员名单中创建一个由11名球员组成的梦幻板球队。
对于球队,我对每种类型的球员都有基本的最低和最高要求。我给他们分配了这样的值:
$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;
型
你可以看到我总共有18个玩家,现在我想创建一个11人的团队,分配随机类型的玩家--记住玩家的最小和最大要求。
实施例1
$wicket_keeper_limit = 1;
$batsman_limit = 4;
$all_rounder_limit = 4;
$bowler_limit = 2;
//so total 11
型
实施例2
$wicket_keeper_limit = 2;
$batsman_limit = 3;
$all_rounder_limit = 3;
$bowler_limit = 3;
//so total 11
型
我想为每种类型分配一个随机数,但总数应该是11,不应该低于最低要求,不应该大于然后为每种类型的最大限制后可用的球员总数。
我试过这样的代码
$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>";
型
但它没有遵循我的最低和最高要求。有时我得到保龄球为0,有时我得到所有圆高达6。
这是Online Fiddle Link
**编辑:**根据评论,我应该清楚,我将生成多个团队,将上述代码放入while循环中,就像50个团队一样,每个类型都有不同/随机的球员,比如有些团队有5个击球手,有些团队只有3个击球手。
当我将得到上述代码的工作,我会稍后从我的数据库中得到球员根据限制,我已经为每种类型。
2条答案
按热度按时间ozxc1zmp1#
我很抱歉,但你的代码对我来说太多了,无法处理。根据你的条件,我写了以下代码:
字符串
我希望评论足够清楚。
Live example
tcbh2hod2#
您可以将此脚本构建为单个函数/方法,或者更好的方法是实践单一责任原则,并使用多个函数/方法执行一个任务。为了避免过多地扩展代码片段,我将仅将我的建议显示为多责任函数。
产品编号:(Demo)
字符串
潜在输出:
型
一些补充说明: