def deal_initial_countries(game): """ :param game: :return: """ countries_per_player = int(len(game.countries) / len(game.players)) countries_raffle = len(game.countries) % len(game.players) print(f"\nDealing countries to players now...\n") print(f"Total countries: {len(game.countries)}") print(f"Total players: {len(game.players)}") print(f"Countries per player: {countries_per_player}") print( f"Remaining countries after first round of dealing countries: {countries_raffle}" ) helpers.press_any_key() game.DealInitialCountriesEqually() for p in game.players: p_c = game.GetCountries(p) print(f'\n{p} got {len(p_c)}:') for c in game.GetCountries(p): print(c) helpers.press_any_key()
def deal_rest_countries_dice(game): free_countries = game.GetUnassignedCountries() players_dices = [] winner = None print( "\nWe still have to deal {} countries, but we'll use dices for that:". format(len(free_countries))) for c in free_countries: print(f'Dealing country {c.name}:') max_value = 0 for p in game.players: input(f"{p.name}, press any key to roll one dice.") dice = throw_dice(1)[0] players_dices.append((p, dice)) print(f"{p.name} got {dice}.") if dice > max_value: max_value = dice winner = p print(f'Player {winner.name} receives {c.name}.') c.SetPlayer(winner) ### What happens if two people get the same? or three? helpers.press_any_key()
def prompt_players(game): """ Prompts for players and sets name and color. :return: A list of objects of type Player """ names_and_colors = [] min_num = 2 max_num = 6 # while not (number_players >= min_num and number_players <= max_num): # number_players = int(input('How many players will be playing ({}-{})? '.format(min_num, max_num))) number_players = helpers.prompt_int_range( 'How many players will be playing? ', None, 2, 6) print(f"\nAlright, we have {number_players} today!\n") for x in range(number_players): name = input("Please enter player {}'s name: ".format(x + 1)) p_color = colors.pop() print("Player {} is {} with color {}.\n".format(x + 1, name, p_color)) names_and_colors.append((name, p_color)) print("Because you cannot have people pick colors." "You'll have a bunch of guys fighting over who's Mr. Black.\n") game.AssignPlayers(names_and_colors) helpers.press_any_key()
def play(): ''' Main orchestration function. Call this to play. ´Will call for the user interaction functions. :return: Nothing ''' # Initialize a game object game = risk.Game() # Flag to interrupt the game if desired keep_playing = True # We'll use winner as an identifier to finish the game winner = None # We shuffle colors once before playing, then they get assigned to players in create_players() random.shuffle(colors) show_banner() # Loading map data from files print('Loading map data from files...') game.LoadMapFromFile() game.LoadCards() game.InitializeCountriesDeck() # for c in game.countries: # print(f'Country {c.name} loaded.') # press_any_key() prompt_players(game) # demo_load_players(game,4) # Deal countries to players, first round demo_deal_initial_countries(game) # Raffle for the rest of remaining cards if needed demo_deal_rest_countries_dice(game) game.LoadWorldDominationObjective(0.6) print( f'\nWorld domination set to {game.world_objective.amount_countries}.\n' ) initialize_objectives(game) game.AdvanceNextPlayer() game.AddTroopsTooAllCountries(1) for n in (2, 1): for p in game.players: # print(f'{p} adding now {n} armies.') for x in range(n): p_cs = game.GetCountries(p) c = p_cs[random.randint(0, len(p_cs) - 1)] # print(f'Adding randomly one army to: {c}') c.armies += 1 show_countries_and_players(game) helpers.press_any_key() while keep_playing: print( '\n------------------------------ 1. ATTACK AND RELOCATION ------------------------------\n' ) for p in game.players: if keep_playing: print(f'{p.name} plays!\n\nATTACK ROUND\n') attack_round(p, game) if check_if_winner(game): keep_playing = False break if not ask_keep_playing(): keep_playing = False break else: break if keep_playing: print( f'\n{p.name} has finished attacking.\n\nRELOCATION ROUND\n' ) movement_round(p, game) print(f'\n{p.name} has finished relocating troops.') if check_if_winner(game): keep_playing = False break if not ask_keep_playing(): keep_playing = False break else: break if keep_playing: print( '\n------------------------------ 2. DEPLOYMENT ROUND ------------------------------\n' ) for p in game.players: print(f'{p.name} plays!\n\nTroop deployment round\n') deployment_round(p, game) print(f'\n{p.name} has finished deploying troops.') if not ask_keep_playing(): keep_playing = False break
def attack_round(player, game): print(f'Current player: {game.current_player}') finished_attacking = False while not finished_attacking: show_player_countries_which_can_attack(player, game) player_cs = game.GetCountries(player, True) if len(player_cs) > 0: attacking_country_no = helpers.prompt_int_range( 'Which country is attacking? (Enter number or 0 to pass) ', None, 0, len(player_cs)) if attacking_country_no > 0: attacker_c = player_cs[attacking_country_no - 1] enemy_countries = [] for c in attacker_c.neighbours: if c.player != attacker_c.player: enemy_countries.append(c) if len(enemy_countries) > 0: print( f'Country {attacker_c.name} can attack these enemy countries:' ) x = 0 for n in enemy_countries: x += 1 print(f'{x} - {n}') attacked_country_no = helpers.prompt_int_range( 'Which country do you wish to attack? (Enter number) ', None, 1, len(enemy_countries)) attacked_country_no -= 1 attacked_c = enemy_countries[attacked_country_no] max_attack_troops = attacker_c.armies - 1 # You cannot attack with more than three armies no matter how many the country has. if max_attack_troops > 3: max_attack_troops = 3 if max_attack_troops == 1: print('Only one army available to attack.') troops_no = 1 else: troops_no = helpers.prompt_int_range( f'How many troops are attacking? (1 to {max_attack_troops}) ', None, 1, max_attack_troops) # game.Attack(attacker_c, attacked_c, dices_attacker, dices_defender) battle = game.CallAttack(attacker_c, attacked_c, troops_no) print(f"\nFollowing battle will take place:\n{battle}") input( f'\n{player.name}, press any key to throw {troops_no} dices...' ) battle.RollDicesAttacker() print(f'{player.name} got dices {battle.dices_attacker}') attacker_loses_before_fighting = True for dice in battle.dices_attacker: if dice > 1: attacker_loses_before_fighting = False if attacker_loses_before_fighting: print( f'Ones means that the attacker cannot win, defender does not need to throw dices.' ) battle.RollDicesDefender( ) # Because the object still needs dices to calculate else: input( f'\n{attacked_c.player.name}, press any key to throw {attacked_c.armies} dices...' ) battle.RollDicesDefender() print( f'{attacked_c.player.name} got dices {battle.dices_defender}' ) battle.Calculate() if battle.defender_lost_country: game.UpdatePlayerCountries(battle.defending_player) game.UpdatePlayerCountries(battle.attacking_player) print(f"\nBattle results:\n{battle}") print( f'\nStatus after battle:\n - Attacker {attacker_c}\n - Defender {attacked_c}' ) else: print(f'{attacker_c} has no enemy neighbour countries.') helpers.press_any_key() else: finished_attacking = True else: print( f'{player.name} has no countries with more than one army that could attack.' ) finished_attacking = True helpers.press_any_key()