Created
September 20, 2020 17:24
-
-
Save xmichael446/6b557d818a198e53cf216c4fe463167a to your computer and use it in GitHub Desktop.
Fish emulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
import names | |
import sqlite3 | |
connection = sqlite3.connect('test.db') | |
cursor = connection.cursor() | |
class Fish: | |
def __init__(self, id, aquarium_id, name, dead, mother, father, lifetime, gender, age): | |
self.id = id | |
self.aquarium_id = aquarium_id | |
self.name = name | |
self.dead = dead | |
self.mother = mother | |
self.father = father | |
self.lifetime = lifetime | |
self.gender = gender | |
self.age = age | |
father_id = 'null' if self.father == None else self.father.id | |
mother_id = 'null' if self.father == None else self.mother.id | |
cursor.execute('''INSERT INTO Fish (aquarium_id, name, dead, mother_id, father_id, lifetime, gender, age) VALUES ({0}, '{1}', '{2}', {3}, {4}, {5}, {6}, {7})'''.format(self.aquarium_id, self.name, 'false', father_id, mother_id, self.lifetime, self.gender, self.age)) | |
def __str__(self): | |
return self.name | |
def get_gender(self): | |
return "Male" if self.gender == 1 else "Female" | |
class Aquarium: | |
def __init__(self, id): | |
self.id = id | |
self.fish = [] | |
def get_fish(self): | |
return list(cursor.execute('''SELECT * FROM Fish WHERE aquarium_id = {} AND dead = "false"'''.format(self.id))) | |
class Logger: | |
def log_collision(self, fish_1, fish_2): | |
print(""" | |
!!! COLLISION !!! | |
Fish 1. | |
ID: {0} | |
Name: {2} | |
Father: {4} | |
Mother: {6} | |
Age: {8} | |
Gender: {10} | |
Fish 2. | |
ID: {1} | |
Name: {3} | |
Father: {5} | |
Mother: {7} | |
Age: {9} | |
Gender: {11} | |
""".format(fish_1.id, fish_2.id, fish_1, fish_2, fish_1.father, | |
fish_2.father, fish_1.mother, fish_2.mother, | |
fish_1.age, fish_2.age, fish_1.get_gender(), fish_2.get_gender())) | |
def log_death(self, fish): | |
print(""" | |
!!! DEATH !!! | |
ID: {} | |
Name: {} | |
Father: {} | |
Mother: {} | |
Age: {} | |
Gender: {} | |
""".format(fish.id, fish.name, fish.father, fish.mother, | |
fish.age, fish.get_gender())) | |
def log_birth(self, fish): | |
print(""" | |
!!! BIRTH !!! | |
ID: {} | |
Name: {} | |
Father: {} | |
Mother: {} | |
Gender: {} | |
""".format(fish.id, fish.name, fish.father, fish.mother, fish.get_gender())) | |
def log_all(self, total, males, females, total_deaths): | |
print(""" | |
<!> TOTAL <!> | |
Total fish: {} | |
Males: {} | |
Females: {} | |
Total Deaths: {} | |
<!> TOTAL <!> | |
""".format(total, males, females, total_deaths)) | |
iteration = 1 | |
collisions = [] | |
fish = [] | |
aquarium = Aquarium(1) | |
for fish in aquarium.get_fish(): | |
new_fish = Fish(fish[0], fish[1], fish[2], fish[3], fish[4], fish[5], fish[6], fish[7], fish[8]) | |
aquarium.fish.append(new_fish) | |
for i in aquarium.fish: | |
print(i) | |
# logger = Logger() | |
# total_deaths = 0 | |
# while True: | |
# deaths = [] | |
# for i in range(random.randrange(1, 6)): | |
# collisions.append(random.randrange(1, 6) + iteration) | |
# for i in range(len(collisions)): | |
# if min(collisions) == iteration: | |
# collisions.remove(min(collisions)) | |
# fish_1 = random.choice(aquarium.get_fish()) | |
# fish_2 = random.choice(aquarium.get_fish()) | |
# logger.log_collision(fish_1, fish_2) | |
# time.sleep(1) | |
# if fish_1.gender != fish_2.gender and fish_1.id != fish_2.id: | |
# mother = father = None | |
# if fish_1.gender == 1: | |
# father = fish_1 | |
# mother = fish_2 | |
# else: | |
# father = fish_2 | |
# mother = fish_1 | |
# id = current_id | |
# gender = random.choice([0, 1]) | |
# name = names.get_first_name(gender=gender) | |
# lifetime = random.randrange(1, 50) | |
# current_id += 1 | |
# fish = Fish(id, name, mother, father, lifetime, gender) | |
# aquarium.get_fish().append(fish) | |
# logger.log_birth(fish) | |
# time.sleep(1) | |
# for i in range(len(aquarium.get_fish())): | |
# if aquarium.get_fish()[i].lifetime == 0: | |
# deaths.append(aquarium.get_fish()[i]) | |
# total_deaths += 1 | |
# aquarium.get_fish()[i].lifetime -= 1 | |
# aquarium.get_fish()[i].age += 1 | |
# for i in range(len(deaths)): | |
# logger.log_death(deaths[i]) | |
# aquarium.get_fish().remove(deaths[i]) | |
# time.sleep(1) | |
# total = aquarium.get_total_fish() | |
# males = aquarium.get_males() | |
# females = aquarium.get_females() | |
# if iteration % 5 == 0: | |
# logger.log_all(total, males, females, total_deaths) | |
# iteration += 1 | |
# time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment