从4个表中随机挑选

Ofi*_*irH 6 php mysql sql random

我想出一个好头衔有点问题,但我现在就解释一下.

我正在建立一个在线游戏.我正在努力建立销毁武器的选择.

我有4种武器 - 攻击,防御,巡逻和间谍.巡逻和间谍武器有3级武器,攻防有15级.

我有一个表格,每个类别与col.命名为w1,w2,w3,w4 ...和课程用户的ID.

我给了每个等级点,所以w15例如值15分,w2值2分,我建立了一个函数来计算攻击者摧毁了多少分给防守者.

我被困在哪里是如何随机挑选武器?

让我们说攻击者摧毁了100点武器.所以它可以是100种巡逻等级1的武器,或者每种类别中的25种武器,或者10种武器排名10.我需要它在类别(攻击,防御,巡逻和间谍)之间以及武器之间随机(w1, W2,W3 ..).另外我需要它在防守者拥有的武器数量的极限,他不能失去更多,然后他有.

非常感谢你 !!我知道我写了一个很长的问题

Eri*_*nes 1

我认为这更多的是一个逻辑问题,而不是一个关于处理表的技术问题。如果您首先处理逻辑,那么您可以轻松地在表格上执行您想要的所需操作。我们只需要知道随机选择的每个项目有多少会被使用(销毁)。这是 sudo-code / php 部分中的随机选择方法:

1. Query database for available items and their relative values.
Run Code Online (Sandbox Code Playgroud)
2. Store information as a multi-dimensional Array
Run Code Online (Sandbox Code Playgroud)
3. Shuffle the Array
Run Code Online (Sandbox Code Playgroud)
 //in php 
    bool shuffle ( array $itemsArray() )
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
 //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) );
Run Code Online (Sandbox Code Playgroud)
5.  Logic Done. Now use the new variables to perform action on tables
Run Code Online (Sandbox Code Playgroud)

因为数组是随机洗牌的,所以无论我们有多少点,我们的结果都是随机的。如果我们有 100 点,并且随机数组中的第一项花费 100 点,或者前 4 项花费 25 点;不管怎样,随机性都会发挥作用。

这只是概念。代码可以改进很多,例如我们保存的变量实际上应该在一个数组中,这样我们就可以在对表执行操作时循环遍历它们。