Sépération du code
This commit is contained in:
60
python/planets_db.py
Normal file
60
python/planets_db.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import random
|
||||
import pl_stat
|
||||
import sqlite3
|
||||
|
||||
# Connect to SQLite database (or create it if it doesn't exist)
|
||||
conn = sqlite3.connect('planets.db')
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create table for planets if it does not exist
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS planets (
|
||||
tile TEXT,
|
||||
name TEXT,
|
||||
type TEXT,
|
||||
population_number REAL,
|
||||
population_type TEXT
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
|
||||
def generate_planet(tile):
|
||||
planet_type_generation = random.choice(list(pl_stat.planet_type.keys()))
|
||||
planet_name = random.choice(pl_stat.planet_name_prefix) + random.choice(pl_stat.planet_name_sufix)
|
||||
planet_population_people = random.choice(pl_stat.planet_population_type)
|
||||
planet_population_number = pl_stat.planet_type[planet_type_generation]["population"] * random.randrange(5, 15, 1) / 10
|
||||
|
||||
# Insert the generated planet into the database
|
||||
cursor.execute('''
|
||||
INSERT INTO planets (tile, name, type, population_number, population_type)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (tile, planet_name, planet_type_generation, planet_population_number, planet_population_people))
|
||||
|
||||
conn.commit()
|
||||
|
||||
planet_description = (f"Tile: {tile}\nPlanète : {planet_name}\nType : {planet_type_generation}\nPopulation : \n {planet_population_number} milliards\n Majoritairement {planet_population_people}")
|
||||
return planet_description
|
||||
|
||||
def search_tile(tile):
|
||||
if random.random() < 1/25:
|
||||
return generate_planet(tile)
|
||||
else:
|
||||
return None
|
||||
|
||||
def search_board():
|
||||
# Define the grid of tiles
|
||||
tiles = [(x, y) for x in range(5) for y in range(5)]
|
||||
|
||||
# Simulate searching each tile
|
||||
results = []
|
||||
for tile in tiles:
|
||||
result = search_tile(tile)
|
||||
if result:
|
||||
results.append(result)
|
||||
|
||||
return results
|
||||
|
||||
# Search the board and print results
|
||||
results = search_board()
|
||||
for result in results:
|
||||
print("\n" + result + "\n\n------------------------------")
|
||||
Reference in New Issue
Block a user