Skip to content

Commit 2cd5a1a

Browse files
Merge branch 'master' into complaint
2 parents bfcef78 + fd2d8fe commit 2cd5a1a

File tree

133 files changed

+203481
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+203481
-0
lines changed

AI Driven Storytelling Game/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Interactive Storytelling Game
2+
3+
This is an interactive storytelling game written in Python. In this game, players make choices that influence the storyline and outcome of their adventure.
4+
5+
## Features
6+
7+
- Dynamic storytelling based on player choices.
8+
- Player can choose character name and traits at the beginning.
9+
- Random events with effects that modify the story or character traits.
10+
- Inventory system for collecting items during the adventure.
11+
- Multiple story nodes with unique scenarios and outcomes.
12+
- Ending variations based on player decisions.
13+
14+
## Usage
15+
16+
1. Make sure you have Python 3 installed.
17+
2. Clone or download this repository to your local machine.
18+
3. Open a terminal or command prompt and navigate to the game directory.
19+
4. Run the script by executing `python ai-storytelling-game.py`.
20+
21+
## How to Play
22+
23+
1. Enter your character's name when prompted.
24+
2. Follow the prompts to make choices at various decision points.
25+
3. Read the story narrative and explore different paths.
26+
4. Pay attention to your character's traits and inventory items, as they can affect the story.
27+
5. Reach different endings based on your choices throughout the game.
28+
29+
## Customization
30+
31+
- To create your own story, modify the `nodes_data` list in the `game.py` script. Each node represents a story point with text, options, and optional effects.
32+
- Customize the effects to add or modify character traits and inventory items.
33+
- Feel free to expand the game by adding more story nodes, events, and endings.
34+
35+
## Dependencies
36+
37+
This game script uses only Python's standard library and doesn't require any external packages.
38+
39+
## Contributing
40+
41+
If you have any ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request. We appreciate your contributions!
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import random
2+
3+
class StoryNode:
4+
def __init__(self, text, options=None, effects=None):
5+
self.text = text
6+
self.options = options or []
7+
self.effects = effects or {}
8+
9+
class Character:
10+
def __init__(self, name):
11+
self.name = name
12+
self.inventory = []
13+
self.traits = {}
14+
15+
class StoryGame:
16+
def __init__(self):
17+
self.story_map = {}
18+
self.current_node = None
19+
self.player = None
20+
21+
def add_node(self, node_id, text, options=None, effects=None):
22+
self.story_map[node_id] = StoryNode(text, options, effects)
23+
24+
def start(self, node_id):
25+
self.current_node = self.story_map.get(node_id)
26+
27+
def make_choice(self, choice_idx):
28+
if self.current_node and choice_idx < len(self.current_node.options):
29+
next_node_id = self.current_node.options[choice_idx]
30+
self.current_node = self.story_map.get(next_node_id)
31+
32+
def apply_effects(self, effects):
33+
for key, value in effects.items():
34+
if key == "add_item":
35+
self.player.inventory.append(value)
36+
elif key == "add_trait":
37+
self.player.traits[value[0]] = value[1]
38+
39+
def get_current_text(self):
40+
text = self.current_node.text if self.current_node else "Game Over"
41+
if self.current_node.effects:
42+
self.apply_effects(self.current_node.effects)
43+
return text.replace("{name}", self.player.name)
44+
45+
def is_game_over(self):
46+
return not self.current_node
47+
48+
# Create the game
49+
game = StoryGame()
50+
51+
# Create character
52+
player_name = input("Enter your character's name: ")
53+
player = Character(player_name)
54+
game.player = player
55+
56+
nodes_data = [
57+
("start", "You wake up in a mysterious land. Do you explore or rest?", ["explore", "rest"]),
58+
("explore", "You venture deep into the woods and find an old cabin. Do you enter or continue exploring?", ["enter_cabin", "continue_exploring"]),
59+
("rest", "You find a cozy spot and rest for a while. When you wake up, you see a map nearby. Will you follow the map or ignore it?", ["follow_map", "ignore_map"]),
60+
("enter_cabin", "Inside the cabin, you discover a hidden passage. Do you enter the passage or stay in the cabin?", ["enter_passage", "stay_in_cabin"]),
61+
("continue_exploring", "You stumble upon a magical waterfall. Do you drink from it or move on?", ["drink_water", "move_on"]),
62+
("follow_map", "Following the map, you find a hidden treasure. Do you take it or leave it?", ["take_treasure", "leave_treasure"]),
63+
("enter_passage", "The passage leads to an ancient temple. Do you explore further or leave?", ["explore_temple", "leave_temple"]),
64+
("stay_in_cabin", "You find a journal that reveals the history of the land. Do you keep reading or close the journal?", ["keep_reading", "close_journal"]),
65+
("drink_water", "Drinking from the waterfall grants you enhanced senses. Will you use them to uncover secrets or continue your journey?", ["uncover_secrets", "continue_journey"]),
66+
("move_on", "As you move on, you encounter a talking animal. Do you listen to its advice or ignore it?", ["listen_to_animal", "ignore_animal"]),
67+
("enter_passage", "The passage leads to an underground city. Do you explore the city or return to the surface?", ["explore_city", "return_to_surface"]),
68+
("uncover_secrets", "Using your enhanced senses, you find a hidden cave. Do you enter the cave or keep moving?", ["enter_cave", "keep_moving"]),
69+
("listen_to_animal", "The animal warns you of danger ahead. Do you heed its warning or take your chances?", ["heed_warning", "take_chances"]),
70+
("node88", "You come across a portal that leads to another realm. Do you step through or stay?", ["step_through", "stay"]),
71+
("explore_city", "You discover an ancient artifact that can grant a wish. What do you wish for?", ["wish_for_power", "wish_for_wisdom"]),
72+
("return_to_surface", "You emerge from the passage and find yourself in a bustling marketplace. Do you explore or leave?", ["explore_marketplace", "leave_marketplace"]),
73+
("enter_cave", "Inside the cave, you find a friendly spirit. Do you converse with it or leave?", ["converse_with_spirit", "leave_cave"]),
74+
("keep_moving", "You continue your journey and encounter a group of fellow travelers. Do you join them or go your own way?", ["join_travelers", "go_own_way"]),
75+
("heed_warning", "You avoid the danger and find a hidden treasure. What will you do with the treasure?", ["share_treasure", "keep_treasure"]),
76+
("take_chances", "Your gamble pays off, and you uncover a secret passage. Will you enter it or continue?", ["enter_secret_passage", "continue_journey"]),
77+
("step_through", "You step through the portal and find yourself in a futuristic city. How do you navigate this new world?", ["explore_futuristic_city", "find_a_way_back"]),
78+
("stay", "You decide to stay in the current realm, building a new life for yourself.", []),
79+
("explore_marketplace", "In the marketplace, you meet a mysterious merchant. Will you buy a rare item or ignore the merchant?", ["buy_item", "ignore_merchant"]),
80+
("leave_marketplace", "You leave the marketplace and find yourself in a tranquil garden. Do you explore further or rest?", ["explore_garden", "rest_in_garden"]),
81+
("node89", "You face a final challenge. Do you confront it head-on or seek help?", ["confront", "seek_help"]),
82+
("confront", "With courage, you conquer the challenge and become a legend.", []),
83+
("seek_help", "You gather allies and together you overcome the final obstacle.", []),
84+
]
85+
86+
87+
for node_id, text, options, effects in nodes_data:
88+
game.add_node(node_id, text, options, effects)
89+
90+
# Start the game
91+
game.start("start")
92+
93+
# Game loop
94+
while not game.is_game_over():
95+
print(game.get_current_text())
96+
if game.current_node.options:
97+
choice = input("Enter your choice (0 or 1): ")
98+
game.make_choice(int(choice))
99+
else:
100+
break
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Algorithmic Trading Simulation
2+
3+
This Python script simulates an algorithmic trading platform where users can test trading strategies using historical price data. It's designed to provide a basic understanding of how automated trading works. Please note that this is a simplified educational example and not suitable for actual trading.
4+
5+
## Features
6+
7+
- Generates simulated historical price data for testing.
8+
- Implements a simple moving average strategy for trading signals.
9+
- Includes risk management based on a specified percentage of portfolio value.
10+
- Visualizes price data, moving averages, and portfolio value over time.
11+
12+
## Prerequisites
13+
14+
- Python
15+
- Required libraries: pandas, numpy, matplotlib
16+
17+
## Usage
18+
19+
1. Ensure you have Python installed.
20+
2. Install the required libraries: `pip install pandas numpy matplotlib`
21+
3. Modify the script's parameters, such as strategy settings and risk management parameters.
22+
4. Run the script: `python algorithmic_trading_simulation.py`
23+
24+
## Disclaimer
25+
26+
This script is meant for educational purposes only and is not intended for actual trading. It lacks important features and considerations necessary for a real-world trading system, such as accurate data, transaction costs, slippage, risk management, and more. Always exercise caution and consult with professionals before engaging in algorithmic trading.
27+
28+
## Contributing
29+
30+
Contributions to the project are welcome! If you have any improvements, bug fixes, or new features to add, feel free to submit a pull request.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import pandas as pd
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
# Simulated historical price data (more data points)
6+
data = {
7+
'Date': pd.date_range(start='2010-01-01', periods=1000),
8+
'Open': np.random.uniform(100, 200, 1000),
9+
'High': np.random.uniform(200, 250, 1000),
10+
'Low': np.random.uniform(80, 150, 1000),
11+
'Close': np.random.uniform(120, 180, 1000),
12+
'Volume': np.random.randint(10000, 50000, 1000)
13+
}
14+
15+
df = pd.DataFrame(data)
16+
df.set_index('Date', inplace=True)
17+
18+
# Example trading strategy
19+
def simple_moving_average_strategy(data, short_window, long_window):
20+
signals = pd.DataFrame(index=data.index)
21+
signals['Signal'] = 0.0
22+
23+
signals['Short_MA'] = data['Close'].rolling(window=short_window, min_periods=1, center=False).mean()
24+
signals['Long_MA'] = data['Close'].rolling(window=long_window, min_periods=1, center=False).mean()
25+
26+
signals['Signal'][short_window:] = np.where(signals['Short_MA'][short_window:] > signals['Long_MA'][short_window:], 1.0, 0.0)
27+
28+
signals['Positions'] = signals['Signal'].diff()
29+
30+
return signals
31+
32+
# Define strategy parameters
33+
short_window = 10
34+
long_window = 50
35+
36+
# Apply strategy
37+
signals = simple_moving_average_strategy(df, short_window, long_window)
38+
39+
# Risk management parameters
40+
risk_per_trade = 0.02 # 2% risk per trade
41+
initial_portfolio_value = 100000
42+
43+
# Simulate trading
44+
position = 0
45+
portfolio_value = []
46+
for index, row in signals.iterrows():
47+
if row['Positions'] == 1:
48+
max_position_size = (risk_per_trade * initial_portfolio_value) / (row['Close'] - row['Low'])
49+
position = min(max_position_size, initial_portfolio_value / row['Close'])
50+
initial_portfolio_value -= position * row['Close']
51+
elif row['Positions'] == -1:
52+
initial_portfolio_value += position * row['Close']
53+
position = 0
54+
portfolio_value.append(initial_portfolio_value + position * row['Close'])
55+
56+
signals['PortfolioValue'] = portfolio_value
57+
58+
# Visualization improvements
59+
plt.figure(figsize=(12, 8))
60+
61+
plt.subplot(2, 1, 1)
62+
plt.plot(signals.index, signals['Close'], label='Close Price', alpha=0.5)
63+
plt.plot(signals.index, signals['Short_MA'], label=f'Short {short_window} MA')
64+
plt.plot(signals.index, signals['Long_MA'], label=f'Long {long_window} MA')
65+
plt.legend()
66+
plt.title('Price Data and Moving Averages')
67+
68+
plt.subplot(2, 1, 2)
69+
plt.plot(signals.index, signals['PortfolioValue'], label='Portfolio Value', linestyle='--', color='green')
70+
plt.legend()
71+
plt.title('Portfolio Value')
72+
73+
plt.tight_layout()
74+
plt.show()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pandas
2+
numpy
3+
matplotlib

0 commit comments

Comments
 (0)