goinaction / code

Source Code for Go In Action examples
4.13k stars 2.37k forks source link

Lodu cash #166

Open Lolmind opened 8 months ago

Lolmind commented 8 months ago

import random import time

class Wallet: def init(self, initial_balance=0.0): self.balance = initial_balance

def deposit(self, amount):
    if amount > 0:  # Ensure positive deposit
        self.balance += amount
        return True
    return False

def withdraw(self, amount):
    if amount > 0 and amount <= self.balance:  # Ensure positive withdrawal within balance
        self.balance -= amount
        return True
    return False

class LudoGame: def init(self, match_players=4): self.players = [] self.entry_fee = 1.0 self.match_players = match_players self.prizes = [2.5, 1.0] self.game_time = 6 self.cooldown_time = 2 self.lottery_prizes = {'1st': 4, '2nd': 3, '3rd': 2}

def join_game(self, player):
    if len(self.players) < self.match_players:
        entry_successful = player.wallet.withdraw(self.entry_fee)
        if entry_successful:
            self.players.append(player)
            return f"{player.name} joined the game. Entry fee deducted. Remaining balance: ${player.wallet.balance}"
        return f"{player.name} doesn't have enough funds to join the game."
    return "Not enough slots available for the match."

def start_match(self):
    if len(self.players) < self.match_players:
        return "Not enough players for the match."

    time.sleep(self.cooldown_time)  # Cooldown time after a match

    # Shuffle players for randomness
    random.shuffle(self.players)

    # Simulate spinning the circle
    spin_result = sum(random.randint(1, 6) for _ in range(self.match_players))

    # Distribute prizes
    winners = self.players[:3]
    for i, winner in enumerate(winners):
        cash_prize = self.prizes[i]
        winner.wallet.deposit(cash_prize)

    # Simulate the lottery
    lottery_winner = random.choice(list(self.lottery_prizes.keys()))
    lottery_prize = self.lottery_prizes[lottery_winner] * random.randint(1, 5)
    winners[0].wallet.deposit(lottery_prize)

    return f"Match result: Circle Spin - {spin_result}, Lottery Winner - {lottery_winner}, Lottery Prize - ${lottery_prize}"

class Player: def init(self, name, initial_balance=0.0): self.name = name self.wallet = Wallet(initial_balance)

def deposit_funds(self, amount):
    deposit_successful = self.wallet.deposit(amount)
    if deposit_successful:
        return f"Deposit of ${amount} successful. New balance: ${self.wallet.balance}"
    return "Invalid deposit amount."

Example usage:

player1 = Player("Player1", 10.0) player2 = Player("Player2", 5.0) player3 = Player("Player3", 8.0) player4 = Player("Player4", 12.0)

game = LudoGame(match_players=4) print(game.join_game(player1)) print(game.join_game(player2)) print(game.join_game(player3)) print(game.join_game(player4))

Example deposit

deposit_amount = 5.0 print(player1.deposit_funds(deposit_amount))

result = game.start_match() print(result)

Example withdrawal

withdrawal_amount = 3.0 if player1.wallet.withdraw(withdrawal_amount): print(f"{player1.name} withdrew ${withdrawal_amount}. Remaining balance: ${player1.wallet.balance}") else: print(f"{player1.name} has insufficient funds for withdrawal.")