doubingling4706 2014-10-05 07:40
浏览 20
已采纳

从4个表中随机挑选

I had a bit of a problem to come up with a good title, but I'll explain now.

I'm building an online game. I'm trying to build option to destroy weapons.

I have 4 kinds of weapons - attack,defence,patrol and spy. patrol and spy weapons have 3 ranks of weapons, and attack and defence have 15 ranks.

I have a table for each of the categories with col. named w1,w2,w3,w4... and ID of the user ofcourse.

I gave each rank points, so w15 for example worth 15 points, and w2 worth 2 points, and I built a function who calculate how much points the attacker destroyed to the defender.

Where I'm stuck is how to pick randomly weapons?

let's say the attacker destroyed 100 points worth of weapons. so it can be 100 weapons of rank 1 of patrol, or 25 weapons rank 1 of each category, or 10 weapons ranked 10. I need it to be randomly between categories (attack,defence,patrol and spy) and between weapons (w1,w2,w3..). In addition I need it to be in the limit of the number of weapons the defender have, he can't lose more then he have.

Thank you very much !! I know I wrote a long question

  • 写回答

6条回答 默认 最新

  • dongque1646 2014-10-16 18:58
    关注

    I think this is more of a logic question than it is a technical question about dealing with tables. If you handle the logic first then you can easily perform needed actions on the table however you'd like. We just need to know how many of each item would be used (destroyed) with a random selection. Here is a random selection method in part 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
    

    Because the array was randomly shuffled, our results are random no matter how many points we have. If we have 100 points and the first item in our randomized array cost 100 points, or the first 4 cost 25 points; randomness will have done its job either way.

    This is just the concept. The code can be improved a bunch, for instance the variables we saved should actually be in an array so we can loop through them when its time to perform actions on the table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误