import random
Codon table for traits
TRAIT_TABLE = { “ATG”: “Strong”, “TAC”: “Fast”, “GAA”: “Resilient”, “TGC”: “Stealthy”, “GGA”: “Aggressive”, “TAA”: “Friendly”, }
Function to translate DNA to traits
def translate_dna(dna_sequence): codons = [dna_sequence[i:i+3] for i in range(0, len(dna_sequence), 3)] traits = [TRAIT_TABLE.get(codon, “Unknown”) for codon in codons] return traits
Function to mutate DNA
def mutate_dna(dna_sequence, mutation_rate=0.1): bases = [“A”, “T”, “C”, “G”] dna_list = list(dna_sequence) for i in range(len(dna_list)): if random.random() < mutation_rate: current_base = dna_list[i] dna_list[i] = random.choice([b for b in bases if b != current_base]) return “”.join(dna_list)
Function to create random DNA
def generate_dna(length=12): bases = [“A”, “T”, “C”, “G”] return “”.join(random.choice(bases) for _ in range(length))
Game loop
def evolution_game(): generation = 1 population_size = 5 mutation_rate = 0.2 population = [generate_dna() for _ in range(population_size)]
print("Welcome to the Evolution Game!") print("Guide your creatures' evolution by selecting which ones survive.") while True: print(f"n--- Generation {generation} ---") # Display population and traits for i, dna in enumerate(population): traits = translate_dna(dna) print(f"Creature {i+1}: DNA: {dna}, Traits: {', '.join(traits)}") # Player selects survivors survivors = input("Select the creatures to survive (e.g., 1,3): ").split(",") survivors = [population[int(i)-1] for i in survivors] # Create next generation with mutations next_population = [] for dna in survivors: for _ in range(population_size // len(survivors)): mutated_dna = mutate_dna(dna, mutation_rate) next_population.append(mutated_dna) # Update population population = next_population[:population_size] generation += 1 # Check for end conditions if generation > 10: # Limit to 10 generations for simplicity print("nThe simulation has ended. Here's the final population:") for dna in population: traits = translate_dna(dna) print(f"DNA: {dna}, Traits: {', '.join(traits)}") break
Run the game
evolution_game()
__________________///////////___________________ .| /
How the Game Works Population Initialization: The game starts with 5 creatures, each with a random DNA sequence.
Player Choice: Players select which creatures survive based on their DNA and traits.
Mutation: Surviving creatures’ DNA mutates to create the next generation. Mutations occur randomly, introducing new traits or altering existing ones.
Evolution: Over 10 generations, players guide the creatures’ evolution toward traits they prefer.
submitted by /u/NEED-HW
[link] [comments]
r/learnpython import random Codon table for traits TRAIT_TABLE = { “ATG”: “Strong”, “TAC”: “Fast”, “GAA”: “Resilient”, “TGC”: “Stealthy”, “GGA”: “Aggressive”, “TAA”: “Friendly”, } Function to translate DNA to traits def translate_dna(dna_sequence): codons = [dna_sequence[i:i+3] for i in range(0, len(dna_sequence), 3)] traits = [TRAIT_TABLE.get(codon, “Unknown”) for codon in codons] return traits Function to mutate DNA def mutate_dna(dna_sequence, mutation_rate=0.1): bases = [“A”, “T”, “C”, “G”] dna_list = list(dna_sequence) for i in range(len(dna_list)): if random.random() < mutation_rate: current_base = dna_list[i] dna_list[i] = random.choice([b for b in bases if b != current_base]) return “”.join(dna_list) Function to create random DNA def generate_dna(length=12): bases = [“A”, “T”, “C”, “G”] return “”.join(random.choice(bases) for _ in range(length)) Game loop def evolution_game(): generation = 1 population_size = 5 mutation_rate = 0.2 population = [generate_dna() for _ in range(population_size)] print(“Welcome to the Evolution Game!”) print(“Guide your creatures’ evolution by selecting which ones survive.”) while True: print(f”n— Generation {generation} —“) # Display population and traits for i, dna in enumerate(population): traits = translate_dna(dna) print(f”Creature {i+1}: DNA: {dna}, Traits: {‘, ‘.join(traits)}”) # Player selects survivors survivors = input(“Select the creatures to survive (e.g., 1,3): “).split(“,”) survivors = [population[int(i)-1] for i in survivors] # Create next generation with mutations next_population = [] for dna in survivors: for _ in range(population_size // len(survivors)): mutated_dna = mutate_dna(dna, mutation_rate) next_population.append(mutated_dna) # Update population population = next_population[:population_size] generation += 1 # Check for end conditions if generation > 10: # Limit to 10 generations for simplicity print(“nThe simulation has ended. Here’s the final population:”) for dna in population: traits = translate_dna(dna) print(f”DNA: {dna}, Traits: {‘, ‘.join(traits)}”) break Run the game evolution_game() __________________///////////___________________ .| / How the Game Works Population Initialization: The game starts with 5 creatures, each with a random DNA sequence. Player Choice: Players select which creatures survive based on their DNA and traits. Mutation: Surviving creatures’ DNA mutates to create the next generation. Mutations occur randomly, introducing new traits or altering existing ones. Evolution: Over 10 generations, players guide the creatures’ evolution toward traits they prefer. submitted by /u/NEED-HW [link] [comments]
import random
Codon table for traits
TRAIT_TABLE = { “ATG”: “Strong”, “TAC”: “Fast”, “GAA”: “Resilient”, “TGC”: “Stealthy”, “GGA”: “Aggressive”, “TAA”: “Friendly”, }
Function to translate DNA to traits
def translate_dna(dna_sequence): codons = [dna_sequence[i:i+3] for i in range(0, len(dna_sequence), 3)] traits = [TRAIT_TABLE.get(codon, “Unknown”) for codon in codons] return traits
Function to mutate DNA
def mutate_dna(dna_sequence, mutation_rate=0.1): bases = [“A”, “T”, “C”, “G”] dna_list = list(dna_sequence) for i in range(len(dna_list)): if random.random() < mutation_rate: current_base = dna_list[i] dna_list[i] = random.choice([b for b in bases if b != current_base]) return “”.join(dna_list)
Function to create random DNA
def generate_dna(length=12): bases = [“A”, “T”, “C”, “G”] return “”.join(random.choice(bases) for _ in range(length))
Game loop
def evolution_game(): generation = 1 population_size = 5 mutation_rate = 0.2 population = [generate_dna() for _ in range(population_size)]
print("Welcome to the Evolution Game!") print("Guide your creatures' evolution by selecting which ones survive.") while True: print(f"n--- Generation {generation} ---") # Display population and traits for i, dna in enumerate(population): traits = translate_dna(dna) print(f"Creature {i+1}: DNA: {dna}, Traits: {', '.join(traits)}") # Player selects survivors survivors = input("Select the creatures to survive (e.g., 1,3): ").split(",") survivors = [population[int(i)-1] for i in survivors] # Create next generation with mutations next_population = [] for dna in survivors: for _ in range(population_size // len(survivors)): mutated_dna = mutate_dna(dna, mutation_rate) next_population.append(mutated_dna) # Update population population = next_population[:population_size] generation += 1 # Check for end conditions if generation > 10: # Limit to 10 generations for simplicity print("nThe simulation has ended. Here's the final population:") for dna in population: traits = translate_dna(dna) print(f"DNA: {dna}, Traits: {', '.join(traits)}") break
Run the game
evolution_game()
__________________///////////___________________ .| /
How the Game Works Population Initialization: The game starts with 5 creatures, each with a random DNA sequence.
Player Choice: Players select which creatures survive based on their DNA and traits.
Mutation: Surviving creatures’ DNA mutates to create the next generation. Mutations occur randomly, introducing new traits or altering existing ones.
Evolution: Over 10 generations, players guide the creatures’ evolution toward traits they prefer.
submitted by /u/NEED-HW
[link] [comments]