コード例 #1
0
def start_game2(game):
    """
    Experimental game loop function

    :param game:
    :return:
    """
    cli.draw_map(game.stars)
    display_report(game)
    cli.say(assets.ADVICE)

    for ship in game.ships:
        cli.ask_for_destination(game.fleets[ship.player_index].name, ship.name)
コード例 #2
0
def next_eta(game, ship):
    """
    Function for user interraction.

    User car either type
    - MAP to display the map
    - REPORT to display the report
    - any planet name to get there.
    :param game:
    :param ship: the active ship
    :return:
    """
    targets = get_names(game.stars)

    while True:
        cli.say(assets.INSTRUCTIONS)
        answer = cli.get_text()
        if answer == "MAP":
            cli.draw_map(game.stars)

        elif answer == "REPORT":
            display_report(game)  # TODO check maybe not needed to update data

        elif answer == ship.star.name:
            cli.say("Already in port, choose another star system to visit")

        elif answer in targets:
            scheduled_arrival = ship.travel_to(
                game.stars[targets.index(answer)], evaluate_all_delay)
            return answer, scheduled_arrival

        else:
            cli.say("{} is not a valid order.\n"
                    "Please, type MAP, REPORT or a star name".format(answer))
        cli.say("\n")
コード例 #3
0
def setup():
    number_of_players, player_prefs = cli.setup_game()

    fleets_info = cli.name_ships2(number_of_players,
                                  player_prefs[0] if player_prefs else 2)

    game = initiate_game(fleets_info, player_prefs)

    cli.say("Display instructions ? (Y/N) ")
    if cli.get_text() == "Y":
        cli.say(textwrap.fill(assets.INTRO, width=50))

    cli.name_ships(game.fleets)

    return game
コード例 #4
0
def trade_phase(game):
    ship = game.ship

    cli.say("\nWE ARE BUYING:\n")

    # New prototype
    for good_name, ship_units, star_units in zip(assets.GOODS_NAMES,
                                                 ship.goods, ship.star.goods):
        star_units = round(star_units)
        if star_units < 0 < ship_units:
            cli.say("     {} WE NEED {} UNITS.\n".format(
                good_name, -star_units))

            while True:
                units = cli.ask("HOW MANY ARE YOU SELLING ? ",
                                lambda n: n >= 0)
                if units == 0:
                    break
                elif units <= ship_units:
                    buy_rounds(game, ship.goods.index(ship_units), units)
                    break
                else:  # Beware, case also for negative values.
                    cli.say(
                        "     YOU ONLY HAVE {}  UNITS IN YOUR HOLD\n".format(
                            ship_units))

    # old legacy
    for good_index in range(6):
        star_units = round(ship.star.goods[good_index])
        if star_units < 0 < ship.goods[good_index]:
            cli.say("     {} WE NEED {} UNITS.\n".format(
                assets.GOODS_NAMES[good_index], -star_units))
            while True:
                units = cli.ask("HOW MANY ARE YOU SELLING ? ",
                                lambda n: n >= 0)
                if units == 0:
                    break
                elif units <= ship.goods[good_index]:
                    buy_rounds(game, good_index, units)
                    break
                else:  # Beware, case also for negative values.
                    cli.say(
                        "     YOU ONLY HAVE {}  UNITS IN YOUR HOLD\n".format(
                            ship.goods[good_index]))
コード例 #5
0
def start_game(game):
    """
    Main loop for the game. Actions are:
    - Displaying the map
    - Displaying the report
    - Initiate game. Players set course for every ship, whatever their order is.
    - begin a loop:
      - pick the next ship to activate (lowest date)
      - set game date to ship's date
      - enter trade phase
      - set next course

    :param game:
    :return:
    """
    cli.draw_map(game.stars)
    display_report(game)
    cli.say(assets.ADVICE)

    for ship in game.ships:  # This is the first round of setting destinations
        cli.say("\nCaptain {}, which star will {} travel to ?\n".format(
            game.fleets[ship.player_index].name, ship.name))

        game.ship = ship  # sets the active ship

        destination, scheduled_arrival = next_eta(game, ship)
        cli.display_eta(destination, scheduled_arrival)

    while landing(game):
        active_ship = game.ship
        star = active_ship.star
        fleet = game.fleets[active_ship.player_index]
        update_star_prices(star, game.year, game.day, game.margin)
        trade_phase(game)
        sell(game)
        if star.level >= model.DEVELOPED and active_ship.sum + fleet.sum != 0:
            bank_call(game)
        cli.say("\nWHAT IS YOUR NEXT PORT OF CALL ")
        next_eta(game, ship)
        if star.level_increment(game.level_inc):
            cli.display_star_class_upgrade(star)
            new_star = game.add_star()
            if new_star:
                cli.display_new_star(new_star, game.stars)
    cli.display_ga()
    cli.say("GAME OVER\n")
コード例 #6
0
def bank_call(game):
    cli.say("DO YOU WISH TO VISIT THE LOCAL BANK ")
    if cli.get_text() != "Y":
        return

    player = game.ship.player_index
    account = game.fleets[player]
    update_account(account, game.stardate)

    cli.say("     You have $ {:6} in the bank\n".format(account.sum))
    cli.say("     and      $ {:6} on your ship\n".format(game.ship.sum))
    if account.sum >= 0:
        value = cli.ask("     How much do you wish to transfer to ship ",
                        in_range(0, account.sum))
        eco.transfer_credit(account, game.ship, value)

    if game.ship.sum >= 0:
        value = cli.ask("     How much do you wish to collect from your ship ",
                        in_range(0, game.ship.sum))
        eco.transfer_credit(game.ship, account, value)
コード例 #7
0
def sell(game):
    """
    Selling is between a fleet, ship and star.
    :param game:
    :return:
    """
    cli.say("\nWe are selling:\n")
    for merchandise_index in range(6):
        star_units = rint(game.ship.star.goods[merchandise_index])
        if game.ship.star.prods[merchandise_index] <= 0 or game.ship.star.goods[
                merchandise_index] < 1:
            pass
        elif merchandise_index <= 3 and game.ship.cargo_weight >= game.max_weight:  # TODO: fix cargo weight check
            pass
        else:
            cli.say("     {} up to {} units ".format(
                assets.GOODS_NAMES[merchandise_index], star_units))
            if merchandise_index <= 3:
                cli.say("     Your max capacity limit you to {} units.".format(
                    game.ship.max_weight - game.ship.cargo_weight))

            while True:
                units = cli.ask("HOW MANY ARE YOU BUYING ",
                                in_range(0, star_units))
                if units == 0:
                    break
                elif merchandise_index > 3 or units + game.ship.cargo_weight <= game.max_weight:  # TODO: fix cargo weight check
                    sell_rounds(game, merchandise_index, units)
                    break
                else:
                    cli.say("     YOU HAVE {} TONS ABOARD, SO {}".format(
                        game.ship.cargo_weight, units))
                    cli.say(" TONS PUTS YOU OVER\n")
                    cli.say("     THE {} TON LIMIT.\n".format(game.max_weight))
                    cli.say("     ")
コード例 #8
0
def sell_rounds(game, index, units):
    star = game.ship.star
    for r in range(game.number_of_rounds):
        if r != max(game.number_of_rounds - 1, 2):
            cli.say("     WE WANT ABOUT ")
        else:
            cli.say("     OUR FINAL OFFER:")
        cli.say(100 * rint(0.011 * star.prices[index] * units + 0.5))
        price = cli.ask(
            " YOUR OFFER ",
            in_range(star.prices[index] * units / 10,
                     star.prices[index] * units * 10))
        if price >= star.prices[index] * units:
            if price <= game.ship.sum:
                cli.say("     SOLD!\n")
                eco.sold(game.ship, index, units, price)
                return
            else:
                cli.say("     YOU BID $ %d BUT YOU HAVE ONLY $ %d" %
                        (price, game.ship.sum))
                p = game.ship.player_index
                if star.level >= model.DEVELOPED and game.ship.sum + \
                        game.fleets[
                            p].sum >= price:
                    cli.say("     ")
                    bank_call(game)
                    if price <= game.ship.sum:
                        cli.say("     SOLD!\n")
                        eco.sold(game.ship, index, units, price)
                        return
                break
        elif price < (
                1 - eco.price_window(game.ship.star.goods[index], units, r)) * \
                star.prices[index] * units:
            break
        star.prices[index] = 0.8 * star.prices[index] + 0.2 * price / units
    cli.say("     THAT'S TOO LOW\n")
コード例 #9
0
def buy_rounds(game, index, units):
    star = game.ship.star
    star_units = rint(star.goods[index])

    if units > 2 * -star_units:
        units = 2 * -star_units
        cli.say("     WE'LL BID ON {} UNITS.\n".format(units))
    for bid_round in range(game.number_of_rounds):
        if bid_round != max(game.number_of_rounds - 1, 2):
            cli.say("     WE OFFER ")
        else:
            cli.say("     OUR FINAL OFFER:")

        cli.say(100 * rint(0.009 * star.prices[index] * units + 0.5))

        price = cli.ask(
            " WHAT DO YOU BID ",
            in_range(star.prices[index] * units / 10,
                     star.prices[index] * units * 10))

        if price <= star.prices[index] * units:
            cli.say("     WE'LL BUY!\n")
            game.ship.goods[index] -= units
            game.ship.sum += price
            star.goods[index] += units
            return

        elif price > (
                1 + eco.price_window(game.ship.star.goods[index], units,
                                     bid_round)) * star.prices[index] * units:
            break

        else:
            star.prices[index] = 0.8 * star.prices[index] + 0.2 * price / units
    cli.say("     WE'LL PASS THIS ONE\n")