我试图从一个大的球员名单中创建一个由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,不应低于最低要求,不应大于每种类型的可用球员总数,然后是最大限制.

我try 过这样的代码

$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>";

但它不能达到follow my minimum and maximum requirements. Sometimes I am getting bowler as 0 and sometimes I am getting all rounder as high as 6.

这是Online Fiddle Link

Edit:根据 comments ,我应该明确的是,我将生成多支球队与放在上面的代码在While循环,像50支球队将有不同的/随机的球员在每种类型像一些球队有5名击球手,有些球队将只有3名击球手.

当我会得到上面的代码工作,我会在稍后从我的数据库根据限制,我已经为每种类型得到球员.

推荐答案

您可以将此脚本构建为单个函数/方法,或者更好的做法是实践单一责任原则,并拥有多个函数/方法,这些函数/方法正好执行一个任务.为了避免过多地扩展代码片段,我将只将我的建议显示为一个多责任函数.

  • 传入您的团队创建设置.
  • 实施逻辑保护条件,以便在设置阻止填充整个团队时向您发出alert .
  • 对你的玩家总数施加最大限度的限制.
  • 作为一种捷径,用所有指定的最小值为您的团队提供种子.
  • 在循环中一次随机 Select 一个位置,并在池中"消耗"该位置,这样位置就永远不会从池中透支.
  • 当队伍满了或泳池空了时,结束循环.
  • 返回填充的团队,或者如果团队无法完全填充,则向用户发出alert .

代码:(Demo)

function randomizeTeam(
    int $teamSize,
    array $positionPool,
    array $positionMins = [],
    array $positionMaxs = []
): array {
    // guard conditions
    foreach ($positionMins as $pos => $min) {
        if (!key_exists($pos, $positionPool) || $positionPool[$pos] < $min) {
            throw new Exception('Position pool cannot accommodate minimum required positions');
        }
    }

    // input preparation
    foreach ($positionMaxs as $pos => $max) {
        if (key_exists($pos, $positionPool) && $positionPool[$pos] > $max) {
            $positionPool[$pos] = $max;
        }
    }
    
    // seed team
    $team = [];
    $sum = 0;
    foreach ($positionPool as $pos => &$pool) {
        $team[$pos] = $positionMins[$pos] ?? 0;
        $pool -= $team[$pos];
        if (!$pool) {
            unset($positionPool[$pos]); // remove exhausted pool
        }
        $sum += $team[$pos];
    }
    
    // add random positions
    while ($sum < $teamSize && $positionPool) {
        $pos = array_rand($positionPool);
        ++$team[$pos];
        --$positionPool[$pos];
        if (!$positionPool[$pos]) {
            unset($positionPool[$pos]); // remove exhausted pool
        }
        ++$sum;
    }

    // another guard condition
    if (array_sum($team) < $teamSize) {
        throw new Exception('Position pool was exhausted before filling team');
    }

    return $team;
}

var_export(
    randomizeTeam(
        11,
        [
            'wicket_keeper' => 2,
            'batsman'       => 6,
            'all_rounders'  => 5,
            'bowlers'       => 5,
        ],
        [
            'wicket_keeper' => 1,
            'batsman'       => 3,
            'all_rounders'  => 1,
            'bowlers'       => 2,
        ],
        [
            'wicket_keeper' => 3,
            'batsman'       => 5,
            'all_rounders'  => 5,
            'bowlers'       => 4,
        ]
    )
);

潜在输出:

array (
  'wicket_keeper' => 2,
  'batsman' => 4,
  'all_rounders' => 2,
  'bowlers' => 3,
)

以下是一些附加说明:

  • 理想的做法是以数组形式声明输入数据,这样您就可以享受循环和数组函数的动态好处,同时避免变量变量.
  • 上述脚本被设计成允许可扩展位置池,并允许不需要提及主池中所有位置的最小和/或最大位置限制.

Php相关问答推荐

在不刷新页面的情况下无法使用JQuery更新id

如何在Livewire中验证多个 Select 字段

PHP邮件表单无法识别重音/特殊字符

在WooCommerce管理订单页面中显示区域名称

自定义WooCommerce帐户下载列表

PHP中的圆生成器在国际日期变更线上失败

在WooCommerce中禁用客户用户角色的纳税计算

将购买限制在WooCommerce中具有相同产品属性的项目

中继器内置中继器的ACF WordPress组

带Redhat php curl的http_code 0

按列值将二维数组排序为不超过N个的递增组

如何用Ubuntu 22.04在VSCode中启用XDebug

Woocommerce API - 图像问题

.htaccess 重写规则问题

使用 phpseclib 验证 RSA-PSS

我的功能以舒适的方式显示数组 struct

Laravel的Storage::exists返回false,而file_exists返回true.

来自 GoDaddy 的邮箱在 Gmail 中显示为via

PHP for each 函数打印 td

mysql / sql修复多对多数据库中的行