def process_build_turn(): gv.N_REINFORCE_ARMIES = count_army_allowance() while gv.N_REINFORCE_ARMIES > 0: save.checkpoint('during_build_turn') gv.prnt("You have " + str(gv.N_REINFORCE_ARMIES) + " armies to place.") gv.N_REINFORCE_ARMIES = reinforce_a_territory(gv.N_REINFORCE_ARMIES)
def process_round(): # Slightly complicated way of iterating over players to allow restoring saved games. # Must maintain a list of players yet to go and pop the next players off the list. gv.PLAYERS_YET_TO_GO = [] for player in gv.PLAYERS: gv.PLAYERS_YET_TO_GO.append(player) while gv.PLAYERS_YET_TO_GO: gv.CURR_PLAYER = gv.PLAYERS_YET_TO_GO.pop() if gv.DEBUG: print gv.CURR_PLAYER, 'PLAYER' save.checkpoint('beginning_of_player_turn') gv.UI.clear_inv_choices() gv.UI.set_inv_choice({ 'You can only choose to (b)uild or (a)ttack!': lambda x: x not in ['a', 'attack', 'b', 'build'] }) action_choice = gv.UI.user_input_check_choices( gv.PLAYERS[gv.CURR_PLAYER].colour + ' ' + gv.CURR_PLAYER + ", (b)uild or (a)ttack?") if action_choice in ['b', 'build']: if gv.DEBUG: print 'build selected' risk.process_build_turn() elif action_choice in ['a', 'attack']: risk.process_attack_turn() if gv.DEBUG: print 'attack selected'
def pre_round(): gv.ROUND_NUMBER += 1 save.checkpoint('round_start') gv.prnt("------------------------") gv.prnt("- Round: " + str(gv.ROUND_NUMBER) + "-") gv.prnt("------------------------") gv.BOARD.print_board()
def process_attack_turn(): save.checkpoint('start_of_attack_turn') if not attackable_neighbours_exist() and not gv.RESTORING: gv.prnt("Not possible to attack! Build instead.") process_build_turn() while attackable_neighbours_exist() or gv.RESTORING: if not gv.RESTORING: gv.ATTACK_FROM = choose_attack_from() gv.ATTACK_TO = choose_attack_to() gv.N_ATTACK_ARMIES = choose_narmies_to_attack_with() gv.N_DEFEND_ARMIES = gv.TERRITORIES[gv.ATTACK_TO].narmies gv.DEFENDER = gv.TERRITORIES[gv.ATTACK_TO].owner else: gv.ATTACK_FROM = gv.SAVED_GLOBALS['ATTACK_FROM'] gv.ATTACK_TO = gv.SAVED_GLOBALS['ATTACK_TO'] gv.N_ATTACK_ARMIES = gv.SAVED_GLOBALS['N_ATTACK_ARMIES'] gv.N_DEFEND_ARMIES = gv.SAVED_GLOBALS['N_DEFEND_ARMIES'] gv.DEFENDER = gv.SAVED_GLOBALS['DEFENDER'] gv.CURR_PLAYER = gv.SAVED_GLOBALS['CURR_PLAYER'] gv.prnt.clear_output() gv.prnt('\n===================================\n' '{} is attacking {} at {} with {} armies from {}!\n' 'The defender has {} armies.\n' '===================================\n\n'.format( gv.CURR_PLAYER, gv.DEFENDER, gv.ATTACK_TO, gv.N_ATTACK_ARMIES, gv.ATTACK_FROM, gv.N_DEFEND_ARMIES, )) narmies_attacker, narmies_defender = chess.play_chess( gv.N_ATTACK_ARMIES, gv.N_DEFEND_ARMIES, gv.CURR_PLAYER, gv.DEFENDER) if narmies_defender == 0: gv.prnt("{0} defeated!".format(gv.TERRITORIES[gv.ATTACK_TO].owner)) gv.TERRITORIES[gv.ATTACK_FROM].narmies -= gv.N_ATTACK_ARMIES gv.TERRITORIES[gv.ATTACK_TO].narmies = narmies_attacker gv.TERRITORIES[gv.ATTACK_TO].reassign_owner(gv.CURR_PLAYER) else: gv.prnt("{0} repelled!".format(gv.CURR_PLAYER)) gv.TERRITORIES[gv.ATTACK_FROM].narmies -= \ gv.N_ATTACK_ARMIES - narmies_attacker gv.TERRITORIES[gv.ATTACK_TO].narmies = narmies_defender gv.UI.handle_user_input('<press enter>') save.checkpoint('after_battle') gv.BOARD.print_board() gv.prnt("Continue to attack, (y)es or (n)o?") attack_some_more = False while attack_some_more is False: response = gv.UI.handle_user_input('continue: ') if response in ['y', 'yes']: attack_some_more = True if response in ['n', 'no']: return 'Chickened out'
def play_chess(n_attack_armies, n_defend_armies, attacker, defender): save.checkpoint('before_battle') gv.UI.clear_inv_choices() gv.UI.set_inv_choice({ "Please enter (r) or (c).": lambda x: x.lower() not in ['r', 'c', 'risk', 'chess'] }) style = gv.UI.handle_user_input("Resolve (R)isk style, or (c)hess style?") if style in ['r', 'risk']: result = risk_style(n_attack_armies, n_defend_armies, attacker, defender) if style in ['c', 'chess']: result = chess_style(n_attack_armies, n_defend_armies, attacker, defender) return result
def chess_style(n_attack_armies, n_defend_armies, attacker, defender): gv.CHECK = {'attacker': False, 'defender': False} hide_message = '---{}, hide screen from {} and press enter---' fight_on_message = '---{} ({}), press enter to fight on---' if gv.RESTORING: gv.CHESSBOARD = gv.SAVED_GLOBALS['CHESSBOARD'] attacker = gv.SAVED_GLOBALS['CURR_PLAYER'] defender = gv.SAVED_GLOBALS['DEFENDER'] def_pieces, att_pieces = get_pieces_from_board() else: # Defender chooses board layout choose_board_layout(defender) # Defender chooses pieces first, then attacker gv.UI.handle_user_input(hide_message.format(defender, attacker), clear=True, player=defender) def_pieces = choose_pieces(defender, n_defend_armies) gv.UI.handle_user_input(hide_message.format(attacker, defender), clear=True, player=attacker) att_pieces = choose_pieces(attacker, n_attack_armies) # Defender chooses first # Board layout: bottom half of board, defender's # unplaced pieces arranged at top gv.UI.handle_user_input(hide_message.format(defender, attacker), clear=True, player=defender) choose_placement('defender', defender, def_pieces) # Attacker chooses placement # Board layout: top half of board, attacker's # unplaced pieces arranged at top gv.UI.handle_user_input(hide_message.format(attacker, defender), clear=True, player=attacker) choose_placement('attacker', attacker, att_pieces) # Fight! # Attacker makes first move withdraw = False while def_pieces and att_pieces and not withdraw: save.checkpoint('during_battle') # gv.UI.handle_user_input(fight_on_message.format(attacker, 'attacker'), clear=True, player=attacker) #### # Attacker's move ### result = process_move('attacker', attacker, def_pieces) # Recalc the pieces lists. There may have been promotions. # This is a lazy programmer's way of ensuring accuracy. def_pieces, att_pieces = get_pieces_from_board() if result == 'withdraw': withdraw = True elif result == 'checkmate': def_pieces = [] break if stalemate(att_pieces, def_pieces): break #### # Defender's move ### # gv.UI.handle_user_input(fight_on_message.format(defender, 'defender'), clear=True, player=defender) result = process_move('defender', defender, att_pieces) # Recalc the pieces lists. There may have been promotions. # This is a lazy programmer's way of ensuring accuracy. def_pieces, att_pieces = get_pieces_from_board() if result == 'checkmate': att_pieces = [] break if stalemate(att_pieces, def_pieces): break # Board layout: Entire board. Pieces captured by # attacker are placed at top, defender bottom. # 'attacker' written at top, 'DEFENDER' at bottom n_attack_armies, n_defend_armies = \ count_remaining_armies(att_pieces, def_pieces) return n_attack_armies, n_defend_armies
def setup_game(): print_welcome_message() players.choose_number_of_players() players.setup_players() risk.setup_board() save.checkpoint('initial')