ticket-to-campaign/data-generator.py

60 lines
1.9 KiB
Python
Raw Normal View History

2024-04-28 18:56:21 +02:00
# Generation of data for a campaign of Ticket to Ride Legacy Legends of the West
# Imports
import os
2024-04-28 18:56:21 +02:00
import random
import jsonpickle
2024-04-28 18:56:21 +02:00
# Class for Circus: list of <number> wagons, of the 5 colors
class Circus:
# There are:
# 4 wagons of each colors (until 2 players' stop)
# 2 wagons of each colors (until 3 players' stop)
# 2 wagons of each colors (until 4 players' stop)
# 2 wagons of each colors (until 5 players' stop)
# NOTE: In the original distribution, the same color never appears twice in a row
# We chose to remove this constraint, for the sake of simplicity
def __init__(self, playersNb):
wagonsPerColors = 2 * playersNb
colors = "blue", "green", "black", "red", "yellow"
remaining = {"blue": wagonsPerColors, "green": wagonsPerColors, "black": wagonsPerColors, "red": wagonsPerColors, "yellow": wagonsPerColors}
self.wagons = []
for n in range(0, 5 * wagonsPerColors):
color = colors[random.randint(0, 4)]
while remaining[color] == 0:
color = colors[random.randint(0, 4)]
self.wagons.append(color)
remaining[color] = remaining[color] - 1
class Game:
2024-04-28 18:56:21 +02:00
# initGame() is used to generate all data for a campaign and initialize status variables
def initGame(self, playersNb):
self.circusList = Circus(playersNb)
# Mama O'Connell
# Treasures
# Concessions
self.circusState = 0 # 0 means Circus is not used in game yet, 1 means next wagon in list is #1, and so on
self.year = 1858
2024-04-28 18:56:21 +02:00
def saveData(self):
savefile = open('./savefile.json', 'w+')
savefile.write(jsonpickle.encode(self))
savefile.close()
def loadData():
with open('./savefile.json', 'r') as savefile:
content = savefile.read()
data = jsonpickle.decode(content)
return data
# Test of data generation
playersNb = 3
# myGame = Game()
# myGame.initGame(playersNb)
# myGame.saveData()
myGame = loadData()
print(myGame.year)
print(myGame.circusList.wagons)