マルチンゲールベッティングシミュレーター

Sep 16 2020

スクリプトは、損失が発生するまで一定額を賭けるマルチンゲールベッティング戦略をシミュレートします。損失が発生すると、ベッターは損失を補うために賭けを2倍にします。これは勝利が起こるまで続きます。勝った後、賭けは元の賭けの値にリセットされます。ブラックジャックを模倣するオッズを設定しました(49%の確率で勝利)。簡単にするために、ラウンドで勝ち負けた金額は賭けと同じです。指定されたラウンド数が経過したとき、次の賭けのサイズが現在利用可能な資金よりも大きいとき、利用可能な資金が0に達したとき、または目標利益が達成されたときに、シミュレーションは終了します。

私はPythonとコーディング全般にまったく慣れていないので、フィードバックをいただければ幸いです。

import random


class MartGame:
    def __init__(self, bet=25, starting_funds=5000, goal_profit=1000, round_n=0, rng_v=0.00,
                 wins=0, losses=0, loss_run=0, win_run=0):
        self.bet = bet
        self.starting_funds = starting_funds
        self.goal_profit = goal_profit
        self.round_n = round_n
        self.rng_v = rng_v
        self.wins = wins
        self.losses = losses
        self.los_run = loss_run
        self.win_run = win_run
        self.original_bet = bet
        self.current_funds = self.starting_funds

    def rng(self):
        """Generates random number"""
        self.rng_v = random.random()

    def winloss_generator(self):
        """Generates win/loss condition"""
        if self.rng_v <= .49:
            return 'win'
        else:
            return 'loss'

    def increase_winloss(self):
        """Increases wins or losses variable based on winloss_generator output"""
        if self.winloss_generator() == 'win':
            self.wins += 1
            return 'win'
        elif self.winloss_generator() == 'loss':
            self.losses += 1
            return 'loss'
        else:
            print('error')

    def increase_round(self):
        """Increases round number by 1"""
        self.round_n += 1

    def change_current_funds(self):
        """Increases or decreases current_funds based on winloss_generator output"""
        if self.winloss_generator() == 'win':
            self.current_funds += self.bet
        elif self.winloss_generator() == 'loss':
            self.current_funds -= self.bet
        else:
            print('error')

    def change_bet(self):
        """If outcome is a win, bet is reset to original value. If outcome is a loss bet is doubled"""
        if self.winloss_generator() == 'win':
            self.bet = self.original_bet
        elif self.winloss_generator() == 'loss':
            self.bet = self.bet * 2

    def running_profit(self):
        """Returns running profit"""
        return self.current_funds - self.starting_funds

    def current_funds(self):
        """Returns running total of funds"""
        return self.current_funds

    def print_current_record(self):
        """Prints current win/loss record to screen"""
        print('Current Record: ', self.wins, 'wins, ', self.losses, 'losses')

    def print_winloss(self):
        """Prints win/loss condition to screen"""
        print(self.winloss_generator())

    def print_profit(self):
        """Prints running profit to screen"""
        print(self.running_profit(), 'running_profit'.upper())

    def print_running_total(self):
        """Prints running total to the screen"""
        print(self.current_funds, 'current funds'.upper())

    def print_round(self):
        """Prints current round"""
        print('Round:', self.round_n)

    def print_current_bet(self):
        """Prints current bet"""
        print('Bet:', self.bet)

    def run(self, rounds, each=False):
        """Runs simulation for specified number of rounds. 'each' argument indicates
        whether each round should be displayed. False will only display the final results"""
        while self.running_profit() < self.goal_profit and \
                self.round_n < rounds and self.current_funds > 0 \
                and self.current_funds > self.bet:
            self.increase_round()
            self.rng()
            self.winloss_generator()
            self.increase_winloss()
            self.change_current_funds()
            if each is True:
                self.print_round()
                self.print_current_bet()
                self.print_winloss()
                self.print_running_total()
                self.print_profit()
                self.print_current_record()
                print()
            self.change_bet()
        print()
        self.end_script()

    def end_script(self):
        """Prints final outcome of the simulation and summary of the results"""
        if self.running_profit() == self.goal_profit:
            print('YOU WIN!')
        else:
            print('YOU LOSE')
        print("Total Rounds:", self.round_n)
        print('Win/Loss Record: ', self.wins, 'Wins', self.losses, 'Losses')
        print('Ending Funds:', self.current_funds)
        print('Goal Profit:', self.goal_profit)
        print("Ending Profit:", self.running_profit())


inst = MartGame(25, 5000, 1000)

inst.run(1000, True)
```

回答

2 user985366 Sep 16 2020 at 03:51

クラスは、同じクラスから複数のオブジェクトを作成する場合、または実行中にオブジェクトを作成および破棄する場合に役立ちます。この場合、オブジェクトは1つだけです。

これは、クラスが必要な種類のプログラムではありません。クラスを使用しなかった場合は、まったく同じプログラムを使用できますがself.、コード内のすべてを削除することで、読み取りと書き込みが短くなり、簡単になります。

変数はスクリプト内でグローバルであるか、main()関数内でローカルであるため(クラスを使用しない場合)、これらすべては必要ありません。

    self.bet = bet
    self.starting_funds = starting_funds
    self.goal_profit = goal_profit
    self.round_n = round_n
    self.rng_v = rng_v
    self.wins = wins
    self.losses = losses
    self.los_run = loss_run
    self.win_run = win_run
    self.original_bet = bet
    self.current_funds = self.starting_funds

以下の関数も、クラスを使用するかどうかに関係なく、意味がありません。これはコード内の1つの場所でのみ使用されるため、開発者が関数を介してではなく、その1つの場所で変数を直接インクリメントするだけでコード行と頭脳を節約できます。

def increase_round(self):
    """Increases round number by 1"""
    self.round_n += 1

以下の機能はさらに役に立たない。使用されている場所で変数を直接(正しく)参照しているため、呼び出されません。また、変数と同じ名前であるため、current_funds問題が発生する可能性があります。

def current_funds(self):
    """Returns running total of funds"""
    return self.current_funds

以下は奇妙に見えます。印刷してみません'RUNNING_PROFIT'か?

'running_profit'.upper()