项目生成器由概率控制

时间:2013-01-23 05:07:57

标签: javascript arrays random probability

我对JS很新,但我想制作一个由概率控制的非常基本的RPG项目生成器。这就是我想出的。

我的itemtype部分有问题。在对itemtype进行概率滚动之后,我想在武器,配件和盔甲下随机生成一组元素。

它应该是稀有剑,罕见的工作人员,普通手套或普通胸部。

我该怎么做?

var rarityNum = Math.floor( 1 + Math.random() * 100 );
var itemNum = Math.floor( 1 + Math.random() * 100 );

var rarity;
if ( rarityNum > 75 ) { rarity = "rare "; }
else if ( rarityNum > 65 ) { rarity = "uncommon "; }
else { rarity = "common "; }

var weapons =["sword","bow","staff"];
var armors =["chest","leggings","gloves"];

var itemtype;
if ( itemNum > 51 ) { itemtype = "weapons"; }
else if ( itemNum > 40 ) { itemtype = "accessories"; }
else { itemtype = "armors"; }

document.write (rarity);
document.write (itemtype);

1 个答案:

答案 0 :(得分:1)

var item = {
  "Weapon" : ["Sword", "Bow"],
  "Armor"  : ["Chest", "Leggings", "Gloves", "Helmet"],
  "Access" : ["Ring", "Bracelet", "Amulet"]
};

var rareness   = Math.floor( 1 + Math.random() * 100 )
  , item_type  = Math.floor( 1 + Math.random() * 100 )
;//randoms

var rarity =
    rareness > 75? "Rare"     :
    rareness > 65? "Uncommon" :
                   "Common"   ;

var type =
    item_type > 51? "Weapon" :
    item_type > 40? "Access" :
                    "Armor"  ;

var item_list  = item[type]
  , item_index = Math.round( Math.random() * (item_list.length - 1) )
;//items

document.write( rarity + " " + item_list[item_index] );