beursspel-sjo/crypto.py
2025-06-22 22:58:40 +02:00

57 lines
1.8 KiB
Python

import random
import matplotlib.pyplot as plt
# Game parameters
coin_price = 100.0
price_sensitivity = 0.5
volatility = 0.3
price_history = [coin_price]
trade_history = [0] # User's buy/sell behavior per turn
cumulative_trades = [0] # Net amount of coins user holds over time
def save_graph():
plt.clf()
plt.plot(price_history, label="Coin Price", color='blue')
plt.plot(trade_history, label="Trade Behavior", color='orange')
plt.plot(cumulative_trades, label="Net Coins Held", color='green')
plt.title("Coin Price, Trade Behavior & Net Holdings Over Time")
plt.xlabel("Turn")
plt.ylabel("Value")
plt.legend()
plt.grid(True)
plt.savefig("coin_price.png")
print("📊 Graph saved as 'coin_price.png'")
print("💰 Welcome to Crypto Kids! Type +10 to buy or -10 to sell.")
print("Type 'exit' to stop the game.\n")
while True:
print(f"\nCurrent Coin Price: {round(coin_price, 2)}")
user_input = input("Enter buy/sell amount (e.g., +10 or -10): ").strip()
if user_input.lower() == "exit":
print("👋 Game Over. Thanks for playing!")
break
try:
trade_amount = int(user_input)
except ValueError:
print("⚠️ Please enter a valid number like +10 or -5.")
continue
trade_history.append(trade_amount)
new_total = cumulative_trades[-1] + trade_amount
cumulative_trades.append(new_total)
buys = trade_amount if trade_amount > 0 else 0
sells = -trade_amount if trade_amount < 0 else 0
net_demand = buys - sells
price_change_from_demand = net_demand * price_sensitivity
random_factor = random.uniform(-volatility, volatility) * coin_price
coin_price += price_change_from_demand + random_factor
coin_price = max(1, coin_price)
price_history.append(coin_price)
save_graph()