LoginSignup
1
1

More than 3 years have passed since last update.

pythonで書くガチャ-おまけ付きレアリティ確定-

Last updated at Posted at 2020-05-04

内容

「11連SR以上1枚確定ガチャ」
実際のソーシャルゲームでは、このような10回分のコストを一度に使うことにより、一定レアリティ以上のおまけが付くガチャが定番です。
前回のガチャロジックをベースに、実現する方法を考えてみます。
Pythonで書くシンプルなガチャロジック

# 抽選対象のアイテムIDと重みのdictionary
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}

id_1〜4までをおまけレアリティ確定の対象と考えてみます。

2回に分けて実行してマージする

# 抽選対象のアイテムIDと重みのdictionary
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
# 抽選回数
times = 10

item_list = gacha(item_dic,times)

# おまけ抽選対象のアイテムIDと重みのdictionary
sp_item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30}
# おまけ抽選回数
sp_times = 1

item_list.extend(gacha(sp_item_dic,sp_times))

作成したgacha関数をそのまま使った、簡単な実現方法です。
ただ、コードの通り呼び出し側に冗長な処理が記述されることになります。
そこで、処理のgacha関数への内包を試してみます。

gacha関数の改修を試してみる

gacha関数にパラメータとして、どのような情報が必要かを考えてみます。
前提として、今回のガチャ対象アイテムの一部がおまけ抽選の対象とします。
また、おまけは一つと固定にします。

おまけ抽選に必要な情報は、その対象となるIDのリストと抽選回数になります。
まずは単純にパラメータを追加してみます。

  • sp_item_ids:おまけガチャの対象となるIDのリスト

また、おまけがないガチャも実行できるように、
追加したパラメータには初期値をセットしてみます。

def gacha(item_dic, times, sp_item_ids=[]):
    total_weight = 0
    sp_total_weight = 0
    sp_item_dic = {}
    for item_key,value in item_dic.items():
        total_weight += value
        if len(sp_item_ids) > 0:
            if item_key in sp_item_ids:
                sp_total_weight += value
                sp_item_dic[item_key] = value

    results = []
    for i in range(times):
        results.append(lottery(item_dic,total_weight))

    #おまけガチャ
    if len(sp_item_dic) > 0:
        results.append(lottery(sp_item_dic, sp_total_weight))
    return results

改修したガチャを実行

# 抽選対象のアイテムIDと重みのdictionary
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
# 抽選回数
times = 10

# おまけ抽選対象のアイテムIDのリスト
sp_item_list = ["id_1","id_2","id_3","id_4"]

item_list = gacha(item_dic, times, sp_item_list)

同じような結果が得られました。
しかし、gacha関数のロジックがとても煩雑化しました。
これでは、元々の別々にガチャを実行したほうがシンプルですっきりしています。

改修の方向性が間違っていたと言えます

おまけ付きガチャ関数を作成

gacha関数を元に戻して、
新しくgacha_omakeという関数を作ります。

# おまけ付きガチャ
def gacha_omake(item_dic, times, sp_item_ids=[]):
    item_list = gacha(item_dic, times)
    #sp_item_idsが存在したらおまけ付きガチャとする
    if len(sp_item_ids) > 0:
        sp_item_dic = {}
        for item_key,value in item_dic.items():
            if item_key in sp_item_ids:
                sp_item_dic[item_key] = value
        if len(sp_item_dic) > 0:
            item_list.extend(gacha(sp_item_dic, 1))

    return item_list

def gacha(item_dic, times):
    total_weight = 0
    for value in item_dic.values():
        total_weight += value

    results = []
    for i in range(times):
        results.append(lottery(item_dic,total_weight))

    return results
# 抽選対象のアイテムIDと重みのdictionary
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
# 抽選回数
times = 10

# おまけ抽選対象のアイテムIDのリスト
sp_item_list = ["id_1","id_2","id_3","id_4"]

item_list = gacha_omake(item_dic, times, sp_item_list)

ガチャの処理そのものはgacha関数に任せて
通常ガチャと、おまけガチャを実行する処理のみを、切り出してまとめることにより
関数の持つ機能が明確になりました。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1