我认为这不仅仅是一个逻辑问题,而是一个关于处理表格的技术问题.如果您先处理逻辑,那么您可以轻松地在桌面上执行所需的操作.我们只需要知道随机选择将使用(销毁)每个项目的数量.这是一个随机选择方法部分sudo-code / part php:
1. Query database for available items and their relative values.
2. Store information as a multi-dimensional Array
3. Shuffle the Array
//in php
bool shuffle ( array $itemsArray() )
4. Iterate through each item in the array and add 1
to a variable for that item if, we have not reached our
limiting factors (amount available and cost vs remaining points).
Do this until all available points are allotted to a variable.
//in php
$i = 0;
do {
if ($itemsArray[$i][numAvail] > 0 &&
($availiblePoints - $itemsArray[$i][cost] >= $itemsArray[$i][cost]){
$$itemsArray[$i]++
//use of '$$' for variable variable
$availiblePoints-=$itemsArray[$i][cost];
}
else {
countSkips++
//need to keep track of how many items we skip
//if $availiblePoints is not zero yet but skips is size of array then
//we are done and have leftover points that cant be used.
}
$i++;
if ($i > count($itemsArray)) { $i=0; };
//start over if we have gone past the end of our Array
} while ($availiblePoints > 0 && $countSkips < count($itemsArray) );
5. Logic Done. Now use the new variables to perform action on tables
因为数组是随机洗牌的,所以无论我们有多少分数,我们的结果都是随机的.如果我们有100分,我们的随机数组中的第一项成本为100分,或前4分为25分;随机性将以任何方式完成其工作.
这只是概念.代码可以改进一堆,例如我们保存的变量实际上应该在一个数组中,这样我们就可以在它对表执行操作时循环遍历它们.