Symulator zakładów Martingale

Sep 16 2020

Skrypt symuluje strategię zakładów Martingale polegającą na obstawianiu określonej kwoty, aż do wystąpienia przegranej, po czym obstawiający podwaja stawkę, aby odrobić stratę. Trwa to do momentu wygranej. Po wygranej zakład jest resetowany do pierwotnej wartości zakładu. Ustawiłem kursy tak, aby naśladować blackjacka (49% szans na wygraną). Dla uproszczenia kwota wygrana lub przegrana w rundzie jest równa zakładowi. Symulacja kończy się, gdy upłynie określona liczba rund, wielkość następnego zakładu jest większa niż aktualnie dostępne środki, dostępne środki osiągną 0 lub osiągnięty zostanie cel.

Jestem zupełnie nowy w Pythonie i kodowaniu w ogóle, więc każda opinia będzie mile widziana.

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)
```

Odpowiedzi

2 user985366 Sep 16 2020 at 03:51

Klasy są przydatne, gdy chcesz utworzyć więcej niż jeden obiekt z tej samej klasy lub tworzyć i niszczyć obiekty podczas działania. W tym przypadku masz tylko jeden obiekt.

To nie jest rodzaj programu, w którym potrzebujesz zajęć. Jeśli nie użyłeś klasy, możesz mieć dokładnie ten sam program, ale usunąć wszystkie self.w swoim kodzie, co sprawi, że będzie krótszy i łatwiejszy do czytania i pisania.

To wszystko też nie jest potrzebne (jeśli nie używasz klasy), ponieważ zmienne będą po prostu globalne w skrypcie lub lokalne w twojej main()funkcji, jeśli taką masz.

    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

Poniższa funkcja jest również bez znaczenia, niezależnie od tego, czy używasz klasy, czy nie. Jest używany tylko w jednym miejscu w kodzie, więc oszczędziłoby to linii kodu i możliwości programisty, aby po prostu inkrementować zmienną bezpośrednio w tym jednym miejscu, a nie robić tego za pomocą funkcji.

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

Poniższa funkcja jest jeszcze bardziej bezużyteczna. Nie jest nawet wywoływana, ponieważ (poprawnie) odwołujesz się do zmiennej bezpośrednio tam, gdzie jest używana. Ma również taką samą nazwę jak zmienna current_funds, co może powodować problemy.

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

Poniższe wygląda dziwnie. Dlaczego po prostu nie drukujesz 'RUNNING_PROFIT'?

'running_profit'.upper()