Improve main interface

This commit is contained in:
Alexandre CATTEAU 2024-05-19 23:09:08 +02:00
parent 0000c38cee
commit 31dee57c01

51
main.py
View File

@ -4,6 +4,7 @@
import os
import random
import jsonpickle
import time
class Circus:
# There are:
@ -17,7 +18,8 @@ class Circus:
def __init__(self, playersNb):
wagonsPerColors = 2 * playersNb
colors = "blue", "green", "black", "red", "yellow"
remaining = {"blue": wagonsPerColors, "green": wagonsPerColors, "black": wagonsPerColors, "red": wagonsPerColors, "yellow": wagonsPerColors}
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)]
@ -146,8 +148,8 @@ class Game:
if self.treasure.state > -1:
print(f"Treasures found: {self.treasure.getFoundNumber()}")
def saveData(self):
savefile = open('./savefile.json', 'w+')
def saveData(self, path):
savefile = open('./' + path, 'w+')
savefile.write(jsonpickle.encode(self))
savefile.close()
@ -174,17 +176,42 @@ elif choice == "load":
myGame = loadData(path)
myGame.printStatus()
while True:
os.system('clear')
myGame.printStatus()
command = input("==> ")
while command not in ('load', 'save', 'next year', 'exit', 'enable circus', 'take circus', 'enable concessions',\
'take concession', 'enable treasure', 'take treasure'):
print("")
print("Available commands:")
print(" Main: 'load', 'save', 'next year', 'exit'")
print(" Circus: 'enable circus', 'take circus'")
print(" Concessions: 'enable concessions', 'take concession'")
print(" Treasure: 'enable treasure', 'take treasure'")
command = input("==> ")
match command:
case 'load':
choice = input("WARNING! If you load, you will loose all unsaved changes. Are you sure? (type YES if you are) ")
if choice == "YES":
path = input("Path to save file (defaults to savefile.json)? ")
if path == "":
path = "savefile.json"
myGame = loadData(path)
case 'save':
path = input("Path to save file (defaults to savefile.json)? ")
if path == "":
path = "savefile.json"
myGame.saveData(path)
print("Game saved!")
time.sleep(3)
case 'next year':
myGame.yearId = myGame.yearId + 1
case 'exit':
exit()
# TESTS
# Test of data generation
# myGame = Game()
# myGame.initGame()
# myGame.saveData()
# Test of data loading
# myGame = loadData('savefile.json')
# myGame.printStatus()
# Test of ConcessionCard
# cc = ConcessionCard()
# userChoice = 3