Martingale Betting Simulator

Sep 16 2020

Script mô phỏng chiến lược cá cược Martingale đặt cược một số tiền cố định cho đến khi xảy ra thua lỗ, lúc này người đặt cược tăng gấp đôi tiền cược để bù lại số tiền thua lỗ. Điều này tiếp tục cho đến khi chiến thắng xảy ra. Sau khi thắng, tiền cược được đặt lại về giá trị đặt cược ban đầu. Tôi đặt tỷ lệ cược để bắt chước trò Blackjack (49% cơ hội thắng). Vì đơn giản, số tiền thắng hoặc thua trong một vòng bằng tiền cược. Mô phỏng kết thúc khi số vòng được chỉ định trôi qua, quy mô của lần đặt cược tiếp theo lớn hơn số tiền hiện có, số tiền khả dụng đạt 0 hoặc đạt được lợi nhuận mục tiêu.

Tôi là thương hiệu mới đối với Python và viết mã nói chung, vì vậy mọi phản hồi sẽ được đánh giá cao.

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

Trả lời

2 user985366 Sep 16 2020 at 03:51

Các lớp rất hữu ích khi bạn muốn tạo nhiều hơn một đối tượng từ cùng một lớp hoặc tạo và hủy các đối tượng trong khi chạy. Trong trường hợp này, bạn chỉ có một đối tượng.

Đây không phải là loại chương trình mà bạn cần một lớp học. Nếu bạn không sử dụng một lớp, bạn có thể có cùng một chương trình nhưng loại bỏ mọi thứ self.trong mã của bạn, điều này làm cho nó ngắn hơn và dễ đọc và dễ viết hơn.

Tất cả những điều này cũng không cần thiết (khi bạn không sử dụng một lớp) vì các biến sẽ chỉ là toàn cục trong tập lệnh hoặc cục bộ trong main()hàm của bạn nếu bạn có.

    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

Hàm bên dưới cũng vô nghĩa, cho dù bạn có sử dụng một lớp hay không. Nó chỉ được sử dụng ở một nơi trong mã, vì vậy nó sẽ tiết kiệm các dòng mã và chất xám cho nhà phát triển để chỉ tăng biến trực tiếp tại một nơi đó, không thực hiện nó thông qua một hàm.

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

Chức năng bên dưới thậm chí còn vô dụng hơn. Nó thậm chí không được gọi vì bạn đang (chính xác) tham chiếu trực tiếp đến biến khi được sử dụng. Nó cũng trùng tên với biến current_funds, điều này có thể dẫn đến sự cố.

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

Dưới đây trông kỳ lạ. Tại sao bạn không chỉ cần in 'RUNNING_PROFIT'?

'running_profit'.upper()