Compare commits

...

7 Commits

6 changed files with 221 additions and 143 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.json
*__pycache__

248
main.py
View File

@ -2,108 +2,12 @@
# Imports
import os
import random
import jsonpickle
import time
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):
if self.state == 0:
print("Circus is not or no longer in game.")
return False
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):
print("Circus stickers are now depleted.")
self.state = 0 # effectively disable circus
class TreasureCard:
def __init__(self):
self.treasures = [36, 33, 29, 27, 24]
self.state = -1 # disabled
def enable(self):
self.state = 0 # 0 means 0 treasures have been found
def getFoundNumber(self):
if self.state == -1:
print("Treasure hunt has not started yet or is completed.")
return False
return self.state
def takeTreasure(self):
if self.state == -1:
print("Treasure hunt has not started yet or is completed.")
return False
self.state = self.state + 1
if self.state == 5:
print("All treasures have been found.")
self.state = -1 # effectively disable treasure card
class ConcessionCard:
# Each card contains:
# 6 cities to connect to the player's concession. There is no need to implement those.
# 7 gold nuggets, as rewards when the player succefully connects a city to their concession. The player can choose
# which nugget to earn, and then discovers the associated reward (between 11 and 17 coins).
# A ConcessionCard is then composed of 7 nuggets, with the rewards randomly distributed.
def __init__(self):
availableValues = [11, 12, 13, 14, 15, 16, 17]
nuggetsNb = len(availableValues) - 1
self.nuggets = []
for n in range(0, 7):
choice = random.randint(0, nuggetsNb - n)
self.nuggets.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.nuggets[choice]))
class Player:
def __init__(self, color):
self.color = color
self.concession = ConcessionCard()
from src.Circus import Circus
from src.TreasureCard import TreasureCard
from src.Player import Player, ConcessionCard
from src.MamaOConnell import MamaOConnell
class Game:
@ -122,8 +26,10 @@ class Game:
# General data to generate
self.circus = Circus(playersNb)
# Mama O'Connell
self.treasure = TreasureCard()
self.mamaoc = MamaOConnell()
while not self.mamaoc.solver():
self.mamaoc = MamaOConnell()
# Players
self.players = []
@ -131,7 +37,64 @@ class Game:
color = input("Enter player " + str(n + 1) + "'s color: ")
self.players.append(Player(color))
# Stickers pools
self.stickers = {'White': 0, 'Red': 0, 'Blue': 0, 'Yellow': 0, 'Green': 0, 'Black': 0}
def getPlayerByColor(self, color):
for player in self.players:
if player.color.lower() == color.lower():
return player
print("ERROR: player '" + color + "' is not defined")
return False
def enableConcession(self):
self.concession = True
def disableConcession(self):
self.concession = False
# if number = 0 print Mama O'Connell's location, else print hint #number
def printMamaHint(self, number):
if number not in range(0, 4):
return False
os.system('clear')
input("When you are ready, press Enter.")
print("")
match number:
case 0:
print("Mama O'Connell is at: " + self.mamaoc.location.name)
case 1:
print("Mama O'Connell is " + str(self.mamaoc.hint1[1]) + " cities from " + self.mamaoc.hint1[0].name)
case 2:
print("Mama O'Connell is " + str(self.mamaoc.hint2[1]) + " cities from " + self.mamaoc.hint2[0].name)
case 3:
print("Mama O'Connell is " + str(self.mamaoc.hint3[1]) + " cities from " + self.mamaoc.hint3[0].name)
print("")
input("When you are done, press Enter.")
os.system('clear')
def takeSticker(self, color, nb):
if not nb.isdigit() or int(nb) < 1:
print("ERROR: Not a positive number")
return False
nb = int(nb)
for c, n in self.stickers.items():
if c.lower() == color.lower():
if nb > n:
print("ERROR: not enough stickers remaining.")
return False
else:
self.stickers[c] = n - nb
return True
print("ERROR: color not found.")
return False
def newStickersBatch(self):
self.stickers = {x: self.stickers[x] + 7 for x in self.stickers}
def printStatus(self):
print("")
print("---------------------------------------------------------------------")
print("")
print("Ticket to Ride Legacy: Legends of the West")
print("")
@ -145,8 +108,13 @@ class Game:
print("")
if self.circus.state > 0:
print("Circus next sticker color: " + self.circus.getNextColor())
print("")
if self.treasure.state > -1:
print(f"Treasures found: {self.treasure.getFoundNumber()}")
print("")
print("Road stickers:")
for color, nb in self.stickers.items():
print(" " + color + ": " + str(nb))
def saveData(self, path):
savefile = open('./' + path, 'w+')
@ -174,21 +142,23 @@ elif choice == "load":
if path == "":
path = "savefile.json"
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'):
while command not in ('load', 'save', 'next year', 'exit', 'take sticker', 'batch sticker', 'enable circus',\
'take circus', 'enable concession', 'take concession', 'disable concession', 'enable treasure', 'take treasure',\
'mama hint 1', 'mama hint 2', 'mama hint 3', 'mama location'):
print("")
print("Available commands:")
print(" Main: 'load', 'save', 'next year', 'exit'")
print(" Stickers: 'take sticker', 'batch sticker'")
print(" Circus: 'enable circus', 'take circus'")
print(" Concessions: 'enable concessions', 'take concession'")
print(" Treasure: 'enable treasure', 'take treasure'")
print(" Concessions: 'enable concession', 'take concession', 'disable concession'")
print(" Mama O'Connell tracking: 'mama hint 1', 'mama hint 2', 'mama hint 3', 'mama location'")
command = input("==> ")
print("")
match command:
case 'load':
choice = input("WARNING! If you load, you will loose all unsaved changes. Are you sure? (type YES if you are) ")
@ -203,38 +173,42 @@ while True:
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 ConcessionCard
# cc = ConcessionCard()
# userChoice = 3
# print(cc.nuggets)
# cc.takeReward(userChoice - 1)
# print(cc.nuggets)
# print(cc.taken)
# print("Remaining: " + str(cc.getRemainingPepites()))
# cc.takeReward(userChoice - 1)
# print(cc.nuggets)
# print(cc.taken)
# print("Remaining: " + str(cc.getRemainingPepites()))
# Test of Player
# myGame = Game()
# myGame.initGame()
# myGame.printStatus()
# myGame.treasure.enable()
# myGame.printStatus()
# myGame.treasure.takeTreasure()
# myGame.treasure.takeTreasure()
# myGame.treasure.takeTreasure()
# myGame.treasure.takeTreasure()
# myGame.printStatus()
# myGame.treasure.takeTreasure()
# myGame.printStatus()
case 'take sticker':
choice = input("Color ? ==> ")
number = input("Nb ? ==> ")
myGame.takeSticker(choice, number)
case 'batch sticker':
myGame.newStickersBatch()
case 'enable circus':
myGame.circus.enable()
case 'take circus':
myGame.circus.takeWagon()
case 'enable treasure':
myGame.treasure.enable()
case 'take treasure':
myGame.treasure.takeTreasure()
case 'enable concession':
myGame.enableConcession()
case 'take concession':
choice = input("Player? ==> ")
player = myGame.getPlayerByColor(choice)
if player == False:
print("Not a valid player.")
else:
choice = input("Which nugget? ==> ")
print("")
player.concession.takeReward(choice)
case 'disable concession':
myGame.disableConcession()
case 'mama hint 1':
myGame.printMamaHint(1)
case 'mama hint 2':
myGame.printMamaHint(2)
case 'mama hint 3':
myGame.printMamaHint(3)
case 'mama location':
myGame.printMamaHint(0)

43
src/Circus.py Normal file
View File

@ -0,0 +1,43 @@
import random
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):
if self.state > 0:
print("Circus is already in game.")
return False
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):
print("Circus stickers are now depleted.")
self.state = 0 # effectively disable circus

View File

@ -1,4 +1,3 @@
import os
import random
class City:
@ -209,7 +208,7 @@ class Board:
i = i + 1
return result
class MamaOConnel:
class MamaOConnell:
def __init__(self):
board = Board() # TODO Board should be a "static" or "constant" class, see what we can do
@ -274,7 +273,3 @@ class MamaOConnel:
if len(cross) > 1: # multiple cities remaining after 3 hints
return False
return True
myMama = MamaOConnel()
while not myMama.solver():
myMama = MamaOConnel()

41
src/Player.py Normal file
View File

@ -0,0 +1,41 @@
import random
class ConcessionCard:
# Each card contains:
# 6 cities to connect to the player's concession. There is no need to implement those.
# 7 gold nuggets, as rewards when the player succefully connects a city to their concession. The player can choose
# which nugget to earn, and then discovers the associated reward (between 11 and 17 coins).
# A ConcessionCard is then composed of 7 nuggets, with the rewards randomly distributed.
def __init__(self):
availableValues = [11, 12, 13, 14, 15, 16, 17]
nuggetsNb = len(availableValues) - 1
self.nuggets = []
for n in range(0, 7):
choice = random.randint(0, nuggetsNb - n)
self.nuggets.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 not choice.isdigit():
print("Please choose a number.")
return False
choice = int(choice)
if choice not in range(1, 8):
print("This is not a valid number")
return False
if choice in self.taken:
print(str(choice) + " has already been obtained.")
return False
self.taken.add(choice)
print("Congrats! You obtained $" + str(self.nuggets[choice - 1]))
class Player:
def __init__(self, color):
self.color = color
self.concession = ConcessionCard()

24
src/TreasureCard.py Normal file
View File

@ -0,0 +1,24 @@
class TreasureCard:
def __init__(self):
self.treasures = [36, 33, 29, 27, 24]
self.state = -1 # disabled
def enable(self):
self.state = 0 # 0 means 0 treasures have been found
def getFoundNumber(self):
if self.state == -1:
print("Treasure hunt has not started yet or is completed.")
return False
return self.state
def takeTreasure(self):
if self.state == -1:
print("Treasure hunt has not started yet or is completed.")
return False
print("Congrats! You obtained $" + str(self.treasures[self.state]))
self.state = self.state + 1
if self.state == 5:
print("All treasures have been found.")
self.state = -1 # effectively disable treasure card