def choose(index): """Lets the user choose between random and manual input of parameters""" is_correct_input = False print() while not is_correct_input: print(constants.INSTRUCTIONS) print(constants.GAME_PARAMETERS[index]) echo.choice() try: user_input = input("Ihre Wahl:\n>> ") if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue elif user_input == "0": return 0 elif user_input == "1": return 1 else: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except KeyboardInterrupt: print("\nKeyboardInterrupt!") print("Abbruch...\n") sys.exit()
def get_managers(towns): """Returns tuple (hometown, number of managers) - user or random output""" choice = choose(1) if choice == 0: echo.clear() hometown_index = random.randint(0, len(towns) - 1) num_managers = random.randint(5, 20) print("\n\nZufällig gewählt...") print("\nHeimatstadt: " + towns[hometown_index]) print("Anzahl Manager: " + str(num_managers)) input() echo.clear() elif choice == 1: echo.clear() hometown_index = choose_hometown(towns) num_managers = choose_num_managers(towns[hometown_index]) print("\n\nIhre Eingaben...") print("\nHeimatstadt: " + towns[hometown_index]) print("Anzahl Manager: " + str(num_managers)) input() echo.clear() else: pass return (towns[hometown_index], num_managers)
def get_towns(): """Returns tuple of pairwise different towns (user or random output)""" choice = choose(0) if choice == 0: num_towns = random.randint(5, 20) towns = tuple(random.sample(constants.LIST_OF_TOWNS, num_towns)) echo.clear() print("\n\nZufällig gewählt...\n") for town in towns: print(("[" + str(towns.index(town) + 1) + "]").rjust(4) + " " + \ town) input() echo.clear() return towns elif choice == 1: echo.clear() towns = choose_towns() print("\n\nIhre Eingaben...\n") for town in towns: print(("[" + str(towns.index(town) + 1) + "]").rjust(4) + " " + \ town) input() echo.clear() return towns else: pass
def special_input(input_index): """Controls user actions during initialization of game parameters""" # quit! if input_index == 0: print("\nAbbruch...\n") sys.exit() # new game! elif input_index == 1: echo.clear() print("\n\nNeues Spiel...") time.sleep(1.5) hotelmanagement.main() # help! elif input_index == 2: print(constants.INPUTS) else: pass
def choose_num_managers(hometown): """Lets the user choose the number of managers in his hometown""" is_correct_input = False while not is_correct_input: echo.clear() print(constants.INSTRUCTIONS) print("\nAnzahl der Manager (Min: 5, Max: 20) in Ihrer Heimatstadt" + \ " " + hometown + " festlegen\n") try: user_input = input("Ihre Wahl:\n>> ") if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue else: num_managers = int(user_input) assert (num_managers <= 20 and num_managers >= 5) echo.clear() is_correct_input = True return num_managers except ValueError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except AssertionError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except KeyboardInterrupt: print("\nKeyboardInterrupt!") print("Abbruch...\n") sys.exit()
def special_move(move_index, state, days_left): """Controls user actions not concerning real game moves""" # quit! if move_index == 0: print("\nAbbruch...\n") sys.exit() # new game! elif move_index == 1: echo.clear() print("\nNeues Spiel...") time.sleep(1) hotelmanagement.main() # help! elif move_index == 2: print(constants.MOVES) else: pass
def choose_towns(): """Lets the user choose its own towns for the game""" is_correct_input = False while not is_correct_input: print() print(constants.INSTRUCTIONS) print("\nNamensliste der Städte angeben\n\n" + \ "Geben Sie eine [Leerzeichen + Komma]-separierte Liste von\n" + \ "mindestens 5 und maximal 20 verschiedenen Namen an, z.B.\n" + \ "'Stadt1, Stadt2, Stadt3, ..., Stadt16'\n") try: user_input = input("Ihre Wahl:\n>> ") if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue else: towns = user_input.split(sep=", ") if (len(towns) != len(set(towns))): print("\nStädte müssen verschieden benannt sein!") print("Neuer Versuch ...\n") input() echo.clear() continue if not (5 <= len(towns) <= 20): print("\nAnzahl der Städte nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue is_correct_input = True return tuple(towns) except ValueError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except KeyboardInterrupt: print("\nKeyboardInterrupt!") print("Abbruch...\n") sys.exit()
def get_timeframe(): """Returns the number of days to be played (user or random output)""" choice = choose(2) if choice == 0: timeframe = random.randint(5, 40) print("\nZufällig gewählt: " + str(timeframe) + " Tage") input() echo.clear() return timeframe elif choice == 1: timeframe = choose_timeframe() print("\nIhre Eingabe: " + str(timeframe) + " Tage") input() echo.clear() return timeframe else: pass
def get_potentials(towns): """Returns tpotential wins for each town (user or randomto output)""" choice = choose(3) # generate random potentials if choice == 0: echo.clear() potentials = tuple([random.randint(-20, 90) for i in \ range(len(towns))]) print("\n\nZufällig gewählt...\n") max_length_town_name = max([len(town) for town in towns]) for i in range(len(towns)): print(("[" + str(i + 1) + "]").rjust(4) + " " + \ towns[i].ljust(max_length_town_name + 1) + \ str(potentials[i]).rjust(3)) input() echo.clear() return potentials # user defined potentials elif choice == 1: potentials = tuple(choose_potentials(towns)) print("\n\nIhre Eingaben...\n") max_length_town_name = max([len(town) for town in towns]) for i in range(len(towns)): print(("[" + str(i + 1) + "]").rjust(4) + " " + \ towns[i].ljust(max_length_town_name + 1) + \ str(potentials[i]).rjust(3)) input() echo.clear() return potentials else: pass
def get_name(): """Returns name of the player - used later on for the highscore list""" is_real_name = False while not is_real_name: print("Bitte geben Sie Ihren Namen für die Aufnahme in\n" + \ "die Highscore-Liste an:") user_input = input(">> ") # user performs special input (quit!, help!, new game!) if user_input in constants.SPECIAL_INPUT: game.special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue # input is interpreted as a name else: name = user_input is_real_name = True return name
def choose_hometown(towns): """Lets the user choose the hometown of his managers""" is_correct_input = False while not is_correct_input: print() print(constants.INSTRUCTIONS) print("\nHeimatstadt der Manager festlegen\n\n" + \ "Geben Sie dazu den Index der festzulegenden Heimatstadt an.\n") print("Liste der Städte:") for town in towns: print(("[" + str(towns.index(town) + 1) + "]").rjust(4) + " " + \ town) print() try: user_input = input("Ihre Wahl:\n>> ") if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear continue else: hometown_index = int(user_input) - 1 assert (hometown_index < len(towns) and hometown_index >= 0) echo.clear() is_correct_input = True return hometown_index except ValueError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except AssertionError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except KeyboardInterrupt: print("\nKeyboardInterrupt!") print("Abbruch...\n") sys.exit()
def get_network(towns): """Returns adjacency tuple for the towns (user or random output)""" choice = choose(4) # random choice for adjacency matrix if choice == 0: network = [[random.randint(0, 1) for i in range(len(towns))] \ for j in range(len(towns))] # assures adjacency matrix to be symmetric for i in range(len(towns)): for j in range(i + 1): if i == j: network[i][j] = 1 else: network[i][j] = network[j][i] j += 1 i += 1 network = tuple(network) echo.clear() print("\n\nZufälliges Straßennetz:\n") # define artificial pre_state to reuse echo.infrastructure for printing pre_state = dict([(towns.index(town), [town, None, None, None, None, \ network[towns.index(town)]]) for town in towns]) echo.infrastructure(pre_state) input() echo.clear() return network # user defined adjacency matrix elif choice == 1: network = choose_network(towns) print("\n\nIhr Straßennetz:\n") # define artificial pre_state to reuse echo.infrastructure for printing pre_state = dict([(towns.index(town), [town, None, None, None, None, \ network[towns.index(town)]]) for town in towns]) echo.infrastructure(pre_state) input() echo.clear() return network else: pass
def choose_timeframe(): """Lets the user choose the timeframe of the game (number of days)""" is_correct_input = False while not is_correct_input: echo.clear() print() print(constants.INSTRUCTIONS) print("\nAnzahl der Spieltage (Min: 5, Max: 40) festlegen\n") user_input = input("Ihre Wahl:\n>> ") if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue else: try: timeframe = int(user_input) assert (timeframe <= 40 and timeframe >= 5) is_correct_input = True return timeframe except ValueError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except AssertionError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue
def main(): """Controlling the game flow""" echo.clear() print(constants.WELCOME_MESSAGE) # getting towns towns = init.get_towns() num_towns = len(towns) rng_towns = range(num_towns) # getting hometown and number of managers in hometown managers = [0 for town in rng_towns] (hometown, num_managers) = init.get_managers(towns) managers[towns.index(hometown)] = num_managers # getting the number of days to be played period = init.get_timeframe() # getting possible wins for each city potentials = init.get_potentials(towns) # getting the network (adjacency) network = init.get_network(towns) hotels = [0 for town in rng_towns] score_of_today = [0 for town in rng_towns] score = 0 # mapping cities to integers (inverse of towns) cities = dict((town, towns.index(town)) for town in towns) # mapping integers to towns (inverse of cities) towns = dict((towns.index(town), town) for town in towns) # state represents the most important data structure of the whole program # it contains the essential attributes of the game state = dict((town,[towns[town], managers[town], hotels[town], \ potentials[town], score_of_today[town], network[town]]) \ for town in rng_towns) day = 1 # main loop over the number of days to be played while day < period + 1: echo.clear() echo.headline(day) days_left = period - day [state, day_shift, city] = game.play_round(state, period, days_left, \ towns, cities) # special case: hire -> day_shift > 1 if day_shift > 1: # new manager is active with the beginning of day d + 2 # where d is the day he was hired (in the morning) for shift in range(day_shift): # state enters as state at the end of day + shift profit_today = sum([state[town][4] for town in rng_towns]) if shift > 0: print("Automatische Berechnung ...\n") time.sleep(.5) print("\nStatus am Ende von Tag " + str(day + shift)) echo.status(state) print("Gewinn an Tag " + str(day + shift) + ": " + \ str(profit_today)) # adding the profit of the current day to the overall score score += profit_today print("Gesamtgewinn mit Ende von Tag " + str(day + shift) + \ ": " + str(score) + "\n") # resetting the score of the day for town in rng_towns: state[town][4] = 0 # we want the manager to be effective with beginning of # day + (shift = 2) if shift == 1: state[city][1] += 1 # turning state into evening mode state = game.calculate_profit(state) shift += 1 # special case: resetting state to morning mode at the end # of the final iteration if shift == day_shift: for town in rng_towns: state[town][4] = 0 input() echo.clear() # every other case else: profit_today = sum([state[town][4] for town in rng_towns]) print("\nStatus am Ende von Tag " + str(day) + \ " (" + str(period) + "):") echo.status(state) print("Gewinn an Tag " + str(day) + ": " + str(profit_today)) score += profit_today print("Gesamtgewinn mit Ende von Tag " + str(day) + ": " + \ str(score) + "\n") # setting the score of the day to 0 for beginning of next day for town in rng_towns: state[town][4] = 0 input() echo.clear() day += day_shift print("Spielende!\n") print("Gesamtgewinn im Spiel: " + str(score) + "\n") name = get_name() save_score(score, name) print(constants.GOODBYE_MESSAGE)
def play_round(state, period, days_left, towns, cities): """Controls main user actions concerning game moves""" rng_towns = range(len(state)) # set profit at beginning of day to 0 for town in rng_towns: state[town][4] = 0 is_correct_move = False while not is_correct_move: print(constants.INSTRUCTIONS) # print network, status and number of remaining days help the user plan # the next move echo.infrastructure(state) echo.status(state) print("Verbleibende Tage: " + str(days_left + 1) + \ " (" + str(period) + ")\n") user_input = input("Ihr Spielzug: \n>> ") # special move if user_input in constants.SPECIAL_MOVES: special_move(constants.SPECIAL_MOVES.index(user_input), state, \ days_left) input() echo.clear() continue # pass if user_input == "pass": state = evaluate_pass(state) is_correct_move = True echo.clear() return [state, constants.DAYSHIFT_PASS, None] # build elif user_input[0:7] == "build: " and user_input[7:] in cities.keys(): city = user_input[7:] if state[cities[city]][2] == 1: print("\nEs existiert bereits ein Hotel in " + city + ".\n" + \ "Sie können dort kein weiteres errichten.\n" + \ "Wählen Sie einen anderen Zug.") print("Neuer Versuch...\n") input() echo.clear() continue else: state = evaluate_build(state, cities[city]) is_correct_move = True echo.clear() return [state, constants.DAYSHIFT_BUILD, None] # move elif user_input[0:6] == "move: ": move_parameters = user_input[6:].split(sep=", ") try: num_managers = int(move_parameters[0]) if num_managers < 1: print("\nAnzahl der Manager muss nicht negativ sein.") print("Neuer Versuch...\n") input() echo.clear() continue assert (move_parameters[1] in cities.keys() and \ move_parameters[2] in cities.keys()) city1 = move_parameters[1] if num_managers > state[cities[city1]][1]: print("\nAnzahl der Manager zu groß für " + city1 + ".") print("Neuer Versuch...\n") input() echo.clear() continue city2 = move_parameters[2] if state[cities[city1]][5][cities[city2]] == 0: print("\nKeine Verbindung zwischen " + city1 + " und " + \ city2 + ".") print("Neuer Versuch...\n") input() echo.clear() continue state = evaluate_move(state, num_managers, \ cities[city1], cities[city2]) is_correct_move = True echo.clear() return [state, constants.DAYSHIFT_MOVE, None] except ValueError: print("\nIhre Angabe zur Anzahl der Manager ist nicht " + \ "korrekt.\n") print("Neuer Versuch...\n") input() echo.clear() continue except AssertionError: print("\nIhre Angabe zu den Städten ist nicht korrekt.\n") print("Neuer Versuch...\n") input() echo.clear() continue except IndexError: print("\nNicht genügend Parameter angegeben.\n") print("Neuer Versuch...\n") input() echo.clear() continue # hire elif user_input[0:6] == "hire: " and user_input[6:] in cities.keys(): city = user_input[6:] if days_left < constants.DAYSHIFT_HIRE - 1: print("\nEs sind nur noch " + str(days_left) + " Runden " + \ "zu spielen.\n" + "Dieser Zug benötigt mehr Zeit.\n" + \ "Wählen Sie einen anderen Zug.\n") input() echo.clear() continue state = calculate_profit(state) is_correct_move = True echo.clear() return [state, constants.DAYSHIFT_HIRE, cities[city]] else: print("\nSpielzug nicht gemäß Vorgaben!") print("Neuer Versuch...\n") input() echo.clear() continue
def choose_network(towns): """Lets the user choose the network parameters""" is_correct_input = False # pre-define the adjacency matrix as fully connected network = [[1 for i in range(len(towns))] \ for j in range(len(towns))] # define artificial pre_state to reuse echo.infrastructure for printing pre_state = dict([(towns.index(town), [town, None, None, None, None, \ [1 for town in towns]]) for town in towns]) # let the user change the adjacency matrix entry by entry while not is_correct_input: echo.clear() print() print(constants.INSTRUCTIONS) echo.infrastructure(pre_state) print("Geben Sie exakt '<Zeile>, <Spalte>' des zu ändernden " + \ "Eintrags an.\n" + "Der Wert wird dann 'invertiert'.\n" + \ "Gleichzeitig wird der Eintrag 'Spalte, Zeile' invertiert " + \ "(-> Symmetrie).\n" "Eine Eingabe mit <Zeile> == <Spalte> wird nicht akzeptiert.\n" "Beenden Sie Ihre Modifikationen mit 'end!'.\n") try: user_input = input("Ihre Wahl:\n>> ") # user performs special input (quit!, help!, new game!) if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue # user terminates modifications elif user_input == "end!": print("\nÄnderungen abgeschlossen...\n") input() echo.clear() is_correct_input = True return tuple(network) # user enters coordinates for the entry to be changed else: index = user_input.split(sep=", ") # check whether two 'coordinate-like' like literals are passed if (len(index) != 2): print("\nEingabe hat nicht richtige Anzahl an Werten!") print("Neuer Versuch ...\n") input() echo.clear() continue index = [int(i) for i in index] # check whether the two 'coordinates' lie in range of matrix if not ((1 <= index[0] <= len(towns)) and \ (1 <= index[1] <= len(towns))): print("\nKoordinaten außerhalb gültigen Bereichs!") print("Neuer Versuch ...\n") input() echo.clear() continue # coordinates are different and within range if not index[0] == index[1]: value = network[index[0] - 1][index[1] - 1] + 1 value %= 2 network[index[0] - 1][index[1] - 1] = value network[index[1] - 1][index[0] - 1] = value pre_state[index[0] - 1][5][index[1] - 1] = value pre_state[index[1] - 1][5][index[0] - 1] = value else: print("\Eintrag auf Hauptdiagonale nicht veränderbar!") print("Neuer Versuch ...\n") input() echo.clear() continue except ValueError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except KeyboardInterrupt: print("\nKeyboardInterrupt!") print("Abbruch...\n") sys.exit()
def choose_potentials(towns): """Lets the user choose the potential win to be made in each town""" is_correct_input = False while not is_correct_input: echo.clear() print(constants.INSTRUCTIONS) print("\nPotentiellen Gewinn je Stadt angeben\n\n" + \ "Geben Sie eine [Leerzeichen + Komma]-separierte Liste von\n" + \ str(len(towns)) + " ganzen Zahlen (Min: -20, Max: 90) an, " + \ "z.B.\n0, -2, 88, ..., -17\n") print("Städte:\n") for town in towns: print(("[" + str(towns.index(town) + 1) + "]").rjust(4) + " " + \ town) try: user_input = input("\nIhre Wahl:\n>> ") if user_input in constants.SPECIAL_INPUT: special_input(constants.SPECIAL_INPUT.index(user_input)) input() echo.clear() continue else: potentials = user_input.split(sep=", ") if (len(potentials) != len(towns)): print("\nEingabe hat nicht richtige Anzahl an Werten!") print("Neuer Versuch ...\n") input() echo.clear() continue potentials = [int(potential) for potential in potentials] if not (min(potentials) >= -20 and max(potentials) <= 90): print("\nMindestens ein Wert liegt nicht im " + \ "vorgegebenen Bereich.") print("Neuer Versuch ...\n") input() echo.clear() continue echo.clear() is_correct_input = True return tuple(potentials) except ValueError: print("\nEingabe nicht gemäß Vorgabe!") print("Neuer Versuch ...\n") input() echo.clear() continue except KeyboardInterrupt: print("\nKeyboardInterrupt!") print("Abbruch...\n") sys.exit()