ticket-to-campaign/data-generator.py

159 lines
4.8 KiB
Python

# Generation of data for a campaign of Ticket to Ride Legacy Legends of the West
# Imports
import os
import random
import jsonpickle
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
self.state = 0 # 0 means Circus is not used in game yet, 1 means next wagon in list is #1, and so on
def enable(self):
self.state = 1
def getNextColor(self):
return self.wagons[self.state - 1]
def takeWagon(self):
if self.state == 0:
print("Circus is not or no longer in game.")
return False
print("Player obtained a " + self.wagons[self.state - 1] + "circus sticker!")
self.state = self.state + 1
if self.state == len(self.wagons) - 1:
print("Circus stickers are now depleted.")
self.state = 0 # effectively disable circus
class ConcessionCard: # name may be different in English
# Each card contains:
# 6 cities to connect to the player's concession. There is no need to implement those.
# 7 gold "pepites", as rewards when the player succefully connects a city to their concession. The player can choose
# which "pepite" to earn, and then discovers the associated reward (between 11 and 17 coins).
# A ConcessionCard is then composed of 7 pepites, with the rewards randomly distributed.
def __init__(self):
availableValues = [11, 12, 13, 14, 15, 16, 17]
pepitesNb = len(availableValues) - 1
self.pepites = []
for n in range(0, 7):
choice = random.randint(0, pepitesNb - n)
self.pepites.append(availableValues[choice])
availableValues.pop(choice)
self.taken = set()
def getRemainingPepites(self):
return {1, 2, 3, 4, 5, 6, 7} - self.taken
def takeReward(self, choice):
if choice in self.taken:
print(str(choice) + " has already been obtained.")
return False
self.taken.add(choice)
print("Congrats! You obtained $" + str(self.pepites[choice]))
class Player:
def __init__(self, color):
self.color = color
self.concession = ConcessionCard()
class Game:
# initGame() is used to generate all data for a campaign and initialize status variables
def initGame(self, playersNb):
# Game status
self.year = 1858
self.concession = False
# General data to generate
self.circus = Circus(playersNb)
# Mama O'Connell
# Treasures
# Players
self.players = []
for n in range(0, playersNb):
color = input("Enter player " + str(n + 1) + "'s color: ")
self.players.append(Player(color))
def printStatus(self):
print("Ticket to Ride Legacy: Legends of the West")
print("")
print("Campaign - Year: " + str(self.year))
print("")
print("Players:")
for player in self.players:
print(" " + player.color)
if self.concession:
print(" Concession card remaining pepites: " + str(player.concession.getRemainingPepites()))
print("")
if self.circus.state > 0:
print("Circus next sticker color: " + self.circus.getNextColor())
# TODO: treasures remaining to find)
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
# TESTS
playersNb = 2
# Test of data generation
# myGame = Game()
# myGame.initGame(playersNb)
# myGame.saveData()
# Test of data loading
# myGame = loadData()
# print(myGame.year)
# print(myGame.circus.wagons)
# Test of ConcessionCard
# cc = ConcessionCard()
# userChoice = 3
# print(cc.pepites)
# cc.takeReward(userChoice - 1)
# print(cc.pepites)
# print(cc.taken)
# print("Remaining: " + str(cc.getRemainingPepites()))
# cc.takeReward(userChoice - 1)
# print(cc.pepites)
# print(cc.taken)
# print("Remaining: " + str(cc.getRemainingPepites()))
# Test of Player
myGame = Game()
myGame.initGame(playersNb)
myGame.printStatus()
myGame.circus.enable()
myGame.printStatus()
print(myGame.circus.wagons)