对具有不确定数据的数组进行排序

时间:2015-12-30 19:25:51

标签: php arrays sorting laravel random

我正在为一个浏览器游戏制作一个脚本,该游戏将生成一个随机动物供玩家与0-5标记的任何地方作战。该动物上的标记是随机生成的,并被输入到自定义的imagick函数中,该函数将按照它们出现在数组中的顺序添加它们。

虽然标记是随机决定的,但是它们应该如何出现在动物身上有很多规则,例如"全身"中的标记。区域显示在"腹部"区域。为了更好地解释,我到目前为止附上测试仪的图像:

enter image description here

因此,为了分解这个随机生成的动物的5个标记,eyeshadow标记属于眼睛区域,undertail属于尾部,streaks属于全体,{{1} }属于back,而appaloosa属于leg。现在的顺序只是在脚本循环通过数据库和随机选择的标记时添加,所以okapi(腿上的条纹)位于顶部,因为它是数组中的最后一个,并且添加了最后一个。但遵循订单规则,数组中的最后一个应该是条纹(身体上的水平条纹),因为全身标记在顶部。

以下是选择标记的代码,这是使用Laravel引擎完成的:

okapi

我认为我可以通过许多复杂的if语句来实现这一点,但它对我来说似乎并不是一个优雅的解决方案。此外,还有一个例外:' Gradient&#39 ;,一个全身标记,即使它是一个全身标记,也会在所有内容下面。到目前为止,它是唯一有这种例外的标记。

我尝试过使用PHP提供的各种 // Determine number of markings $num = mt_rand(1,10); if ($num == 1) { $markingNum = 0; } elseif ($num > 1 && $num < 4) { $markingNum = 1; } elseif ($num > 4 && $num < 6) { $markingNum = 2; } elseif ($num > 6 && $num < 8) { $markingNum = 3; } elseif ($num > 8 && $num < 10) { $markingNum = 4; } else { $markingNum = 5; } // Calculate Marking type and color $markings = array(); if ($markingNum > 0) { for ($m = 0 ; $m < $markingNum; $m++) { // Set color values (pulls from the "pallet" selected earlier in the code, which will determine the range of color that marking can be) if ($m == 1) { $pal = $pallet->marking1; } elseif ($m == 2) { $pal = $pallet->marking2; } elseif ($m == 3) { $pal = $pallet->marking3; } elseif ($m == 4) { $pal = $pallet->marking4; } else { $pal = $pallet->marking5; } // Pull previous marking info if (count($markings) != 0) { $previous = DataMarking::whereIn('name', array_keys($markings))->get(); // This pulls the regions of the current markings in the array so it won't select a region that already has a marking. foreach ($previous as $p) { $regions[$p->region] = $p->name; } // Uncommon marking (10% chance) $r = mt_rand(1, 10); if ($r == 10) { $marking = DataMarking::where('rarity', 1) ->where('public', 1) ->whereNotIn('name', array_keys($markings)) ->whereNotIn('region', array_keys($regions)) ->orderByRaw("RAND()") ->first(); // Common markings } else { $marking = DataMarking::where('rarity', 0) ->where('public', 1) ->whereNotIn('name', array_keys($markings)) ->whereNotIn('region', array_keys($regions)) ->orderByRaw("RAND()") ->first(); } // Colors marking if ($pal == 0) { $markingColor = rand_color(); } else { $range = ColorRange::where('id', $pal)->firstOrFail(); $markingColor = "#" . mixRange(substr($range->start_hex, 1), substr($range->end_hex, 1)); } $markings[$marking->name] = $markingColor; } else { // Uncommon marking (10% chance) $r = mt_rand(1, 10); if ($r == 10) { $marking = DataMarking::where('rarity', 1) ->where('public', 1) ->orderByRaw("RAND()")->first(); // Common marking } else { $marking = DataMarking::where('rarity', 0) ->where('public', 1) ->orderByRaw("RAND()")->first(); } // Colors marking if ($pal == 0) { $markingColor = rand_color(); } else { $range = ColorRange::where('id', $pal)->firstOrFail(); $markingColor = "#" . mixRange(substr($range->start_hex, 1), substr($range->end_hex, 1)); } $markings[$marking->name] = $markingColor; } } } 函数,但我没有太多运气。 sort似乎是最有希望的,但由于我们排序的值存在于数据库中,而不是数组中我们正在排序(想象函数必须以标记=&gt;颜色数组格式),它&# 39;证明难以合作。

Tl; dr:我需要根据数据库中存在的键(标记区域)中存在的不确定数据量对数组进行重新排序。什么是实现这一目标的最优雅方式?

2 个答案:

答案 0 :(得分:4)

以下是对代码的一些优化,内联的注释用于描述已完成的操作。这显然没有完成,因为Marcin在他的回答中指出了一些更好的东西。

 // Determine number of markings
    $num = mt_rand(1,10);

    // Removed redundent $num > X as the conditions were already meet that it was > X by the previous if statement
    if ($num == 1) {
        $markingNum = 0;
    } else if ($num < 4) {
        $markingNum = 1;
    } else if ($num < 6) {
        $markingNum = 2;
    } else if ($num < 8) {
        $markingNum = 3;
    } else if ($num < 10) {
        $markingNum = 4;
    } else {
        $markingNum = 5;
    }

    // Calculate Marking type and color
    $markings = array();

    if ($markingNum > 0) {
        for ($m = 1 ; $m <= $markingNum; $m++) { // incrimented to 1 and <= so we can dynamically select elements
            // Set color values (pulls from the "pallet" selected earlier in the code, which will determine the range of color that marking can be)
            $pal = $pallet->{'marking' . $m}; // Removed if/else and replaced with a dynamic variable


            // Uncommon marking (10% chance)
            $r = mt_rand(1, 10);

            // removed duplicate database selections for a simple $rarity variable that accomplishes the same task
            if ($r == 10) {
                $rarity = 1;
            } else {
                $rarity = 0;
            }

            $marking = DataMarking::where('rarity', $rarity)
                                  ->where('public', 1)
                                  ->whereNotIn('name', array_keys($markings))
                                  ->whereNotIn('region', $regions)
                                  ->orderByRaw("RAND()")
                                  ->first();

            // Colors marking
            if ($pal == 0) {
                $markingColor = rand_color();
            } else {
                $range = ColorRange::where('id', $pal)->firstOrFail();
                $markingColor = "#" . mixRange(substr($range->start_hex, 1), substr($range->end_hex, 1));
            }

            $markings[$marking->name] = $marking; // adds all of the marking data, this is where you could have a z-index in the database
            $markings[$marking->name] = $markingColor; // add your color to your marking data
            $regions[] = $marking->region;

        }
    }

答案 1 :(得分:2)

我不会回答你的问题,但是看看你的代码还有很多可以改进的地方。

考虑一下:

if ($m == 1) {    
    $pal = $pallet->marking1;
} elseif ($m == 2) {
    $pal = $pallet->marking2;
} elseif ($m == 3) {
    $pal = $pallet->marking3;
} elseif ($m == 4) {
    $pal = $pallet->marking4;
} else {
    $pal = $pallet->marking5;
}

可以通过更简单的方式进行更改:

$pa1 = (in_array($m,range(1,4))) ? $pallet->marking{$m} : $pallet->marking5;

同样的:

if ($r == 10) {

    $marking = DataMarking::where('rarity', 1)
                          ->where('public', 1)
                          ->whereNotIn('name', array_keys($markings))
                          ->whereNotIn('region', array_keys($regions))
                          ->orderByRaw("RAND()")
                          ->first();
// Common markings
} else {
    $marking = DataMarking::where('rarity', 0)
                          ->where('public', 1)
                          ->whereNotIn('name', array_keys($markings))
                          ->whereNotIn('region', array_keys($regions))
                          ->orderByRaw("RAND()")
                          ->first();
}

如果可以改写为:

$marking = DataMarking::where('rarity', ($r == 10) ? 1 : 0)
                      ->where('public', 1)
                      ->whereNotIn('name', array_keys($markings))
                      ->whereNotIn('region', array_keys($regions))
                      ->orderByRaw("RAND()")
                      ->first();

当然,由于性能原因,上述ORDER BY RAND()可能不是最佳解决方案。

你应该非常关心你的代码和重复数量,否则你很快会迷失在你正在做的事情中