Add ability to save data to file and load it back
This commit is contained in:
parent
737915cb3b
commit
c18209402c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.json
|
@ -4,3 +4,6 @@ Tooling for running a campaign of Ticket to Ride Legacy: Legends of the West wit
|
|||||||
### Components
|
### Components
|
||||||
* `data-generator.py` contains models to generate data for a campaign
|
* `data-generator.py` contains models to generate data for a campaign
|
||||||
* currently, only generates a Circus instance
|
* currently, only generates a Circus instance
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
* Python library `jsonpickle`
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
# Generation of data for a campaign of Ticket to Ride Legacy Legends of the West
|
# Generation of data for a campaign of Ticket to Ride Legacy Legends of the West
|
||||||
|
|
||||||
# Imports
|
# Imports
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
|
import jsonpickle
|
||||||
|
|
||||||
# Class for Circus: list of <number> wagons, of the 5 colors
|
# Class for Circus: list of <number> wagons, of the 5 colors
|
||||||
class Circus:
|
class Circus:
|
||||||
@ -25,8 +27,33 @@ class Circus:
|
|||||||
self.wagons.append(color)
|
self.wagons.append(color)
|
||||||
remaining[color] = remaining[color] - 1
|
remaining[color] = remaining[color] - 1
|
||||||
|
|
||||||
#def next(): # returns next color (i.e., color that the player will get it they use circus)
|
class Game:
|
||||||
|
|
||||||
myCircus = Circus(3)
|
# 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
|
||||||
|
|
||||||
print(myCircus.wagons)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user