def choose_what_to_do_util(player, board, region, rp): ''' if computer is scottish, adss utility to bump up a troop or add reinforcement takes a player object,board object, region object, and replacement points integer for that region returns utility dictionary ''' util_dict = {'b': 0.01, 'r': 0.01} if len(region.blocks_present) > rp: util_dict['r'] += 1 if search.block_name_to_object( board, 'WALLACE' ) in region.blocks_present and search.block_name_to_object( board, 'WALLACE').current_strength < search.block_name_to_object( board, 'WALLACE').attack_strength: util_dict['b'] += 1 if search.block_name_to_object( board, 'KING') in region.blocks_present and search.block_name_to_object( board, 'KING').current_strength < search.block_name_to_object( board, 'KING').attack_strength: util_dict['b'] += 1 if search.block_name_to_object( board, 'EDWARD') in region.blocks_present and search.block_name_to_object( board, 'EDWARD').current_strength < search.block_name_to_object( board, 'EDWARD').attack_strength: util_dict['b'] += 1 return util_dict
def noble_home_to_object(board, regionID): ''' This function is meant to change a region based on its ID to the object of its noble in which the region is the home to. It return a noble block object ''' if regionID == 0: return search.block_name_to_object(board.all_blocks, 'ROSS') elif regionID == 2: return search.block_name_to_object(board.all_blocks, 'MORAY') elif regionID == 4: return search.block_name_to_object(board.all_blocks, 'BUCHAN') elif regionID == 5: return search.block_name_to_object(board.all_blocks, 'COMYN') elif regionID == 6: return search.block_name_to_object(board.all_blocks, 'COMYN') elif regionID == 7: return serach.block_name_to_object(board.all_blocks, 'MAR') elif regionID == 8: return search.block_name_to_object(board.all_blocks, 'ANGUS') elif regionID == 9: return search.block_name_to_object(board.all_blocks, 'ARGYLL') elif regionID == 10: return search.block_name_to_object(board.all_blocks, 'ATHOLL') elif regionID == 12: return search.block_name_to_object(board.all_blocks, 'LENNOX') elif regionID == 13: return search.block_name_to_object(board.all_blocks, 'MENTIETH') elif regionID == 14: return search.block_name_to_object(board.all_blocks, 'CARRICK') elif regionID == 15: return search.block_name_to_object(board.all_blocks, 'STEWARD') elif regionID == 17: return search.block_name_to_object(board.all_blocks, 'DUNBAR') elif regionID == 19: return search.block_name_to_object(board.all_blocks, 'GALLOWAY') elif regionID == 20: return search.block_name_to_object(board.all_blocks, 'BRUCE') return False
def distribute_rp(board, rp, region, eng_type, scot_type): ''' takes a board object, a given rp, a region object, and the side the computer plays helper function for winter_builds assigns rp randomly based on side for computer asks for appropriate inputs from the user based on side ''' if scot_type == 'comp': if region.is_friendly('SCOTLAND'): points = rp update_count = 0 while points > 0: update_count += 1 if update_count > 25: break choice_utilities = choose_what_to_do_util(board, region, rp) if choice_utilities == dict(): break computer_choice = weighted_prob.weighted_prob(choice_utilities) if computer_choice == 'r': if len(region.blocks_present) < rp and board.scot_pool: if not (len(board.scot_pool) == 2 and board.all_blocks[14] in board.scot_pool and board.all_blocks[16] in board.scot_pool ) or not (len(board.scot_pool) == 1 and board.all_blocks[16] in board.scot_pool): valid_block = False MAX_DRAWINGS = 25 num_drawings = 0 while not valid_block and num_drawings <= MAX_DRAWINGS: num_drawings += 1 draw_block = random.choice(board.scot_pool) #print('Trying to add ' + draw_block.name + ' to' + region.name) if draw_block.type == 'NORSE': if region.coast: valid_block = True elif draw_block.name == 'KING': pass elif draw_block.name == 'FRENCH': scottish_nobles = 0 for scot in board.scot_roster: if type(scot) == blocks.Noble: scottish_nobles += 1 if region.coast and scottish_nobles >= 8: valid_block = True else: valid_block = True if num_drawings <= MAX_DRAWINGS: draw_block.current_strength = 1 add_to_location(board, draw_block, board.regions[region.regionID]) points -= 1 elif computer_choice == 'b': potential_blocks = [] for block in region.blocks_present: if block.current_strength < block.attack_strength: potential_blocks.append(block) if potential_blocks: bump_block_utilities = choose_what_to_bump_util( potential_blocks) bump_choice = weighted_prob.weighted_prob( bump_block_utilities) bump_block = search.block_name_to_object( board.all_blocks, bump_choice) bump_block.heal_until_full(1) print("Bumped " + bump_block.name + " up!") points -= 1 elif not potential_blocks and len( region.blocks_present) >= rp: points = 0 else: print("Not a correct input! Please type 'r' or 'b'!") elif eng_type == 'opp': potential_blocks = [] for block in region.blocks_present: if block.current_strength < block.attack_strength and ( type(block) == blocks.Noble or block.type == 'INFANTRY'): potential_blocks.append(block) points = rp print(region.name + " has " + str(points)) while points > 0 or potential_blocks: try: for index, name in enumerate(potential_blocks): print(str(index) + ":" + " " * 5 + str(name)) user_choice = input( "Which block would you like to bump in " + region.name + "? \n Type 'done' if you are finished ") if user_choice.lower() == 'done': points = 0 else: potential_blocks[int(user_choice)].heal_until_full(1) points -= 1 except (IndexError, ValueError): print("Not a valid option!") if eng_type == 'comp': potential_blocks = [] if region.is_friendly('ENGLAND'): points = rp potential_blocks = [] while points > 0 or (potential_blocks != list()): potential_blocks = [] for block in region.blocks_present: if block.current_strength < block.attack_strength and ( type(block) == blocks.Noble or block.type == 'INFANTRY'): potential_blocks.append(block) if potential_blocks: bump_block_utilities = choose_what_to_bump_util( potential_blocks) bump_choice = weighted_prob.weighted_prob( bump_block_utilities) bump_block = search.block_name_to_object( board.all_blocks, bump_choice) if bump_block.current_strength < bump_block.attack_strength: bump_block.heal_until_full(1) print("Bumped " + bump_block.name + " up!") points -= 1 else: print(region.name + " has nothing to build!") points = 0 potential_blocks = list() break points = 0 potential_blocks = [] elif scot_type == 'opp': points = rp print(region.name + " has " + str(points)) while points > 0: user_choice = input( "Strengthen a troop (t) or bring in reinforcements (r)? type 'd' for done " ) if user_choice.lower() == "d": points = 0 elif user_choice.lower() == "r": if board.scot_pool: if len(region.blocks_present) < rp: valid_block = False while not valid_block: draw_block = random.choice(board.scot_pool) if draw_block.type == 'NORSE': if region.coast: valid_block = True elif draw_block.name == 'KING': pass elif draw_block.name == 'FRENCH': scottish_nobles = 0 for scot in board.scot_roster: if type(scot) == blocks.Noble: scottish_nobles += 1 if region.coast and scottish_nobles >= 8: valid_block = True else: valid_block = True draw_block.current_strength = 1 board.scot_pool.remove(draw_block) board.regions[ region.regionID].blocks_present.append( draw_block) board.scot_roster.append(draw_block) print(draw_block.name + " added to reinforcements of " + region.name) points -= 1 else: print("Can't put reinforcements here!") else: print("There are no blocks in the scottish pool!") elif user_choice.lower() == "t": potential_blocks = [] for block in region.blocks_present: if block.current_strength < block.attack_strength: potential_blocks.append(block) if potential_blocks: try: for index, name in enumerate(potential_blocks): print( str(index) + ":" + " " * 5 + str(name.name)) user_choice2 = input( "Which block would you like to bump in " + region.name + "? ") potential_blocks[int( user_choice2)].heal_until_full(1) points -= 1 except (IndexError, ValueError): print("Not a valid option!") else: print("All blocks at full strength.") else: print("Please type a valid choice!")
def initialize_winter(board, block_list, eng_type, scot_type, edward_prev_winter=[False]): ''' takes a board object, list of all blocks in game, and which side the computer is playing moves every block to correct location -- random for computer, user input for human ''' eng_nobles = [] scot_nobles = [] eng_king = [] scot_king = [] eng_edward = [] # print('SCOT ROSTER') # print(board.scot_roster) # print('SCOT POOL') # print(board.scot_pool) # print('ENG ROSTER') # print(board.eng_roster) # print('ENG POOL') # print(board.eng_pool) for region in board.regions: for block in region.blocks_present: #for block in block_list: if block in board.eng_roster or block in board.scot_roster: #print(block.current_strength) #print(block.is_dead()) #print(block in board.scot_roster) #print(block in board.scot_pool) if block.name != 'MORAY' and not block.is_dead( ) and find_location(board, block).regionID == 22 and not type( block) == blocks.Noble: #print(board.regions[22]) #print(block) disband(board, block) elif block.type == "KING": if block.allegiance == "ENGLAND": eng_king.append(block) if block.allegiance == "SCOTLAND": scot_king.append(block) elif block.type == "EDWARD": eng_edward.append(block) for noble in board.all_nobles: if noble.allegiance == 'SCOTLAND' and not noble.is_dead( ) and noble in board.scot_roster: scot_nobles.append(noble) elif not noble.is_dead() and noble in board.eng_roster: eng_nobles.append(noble) #print('ENG NOBLES:') #print(eng_nobles) #print('SCOT NOBLES:') #print(scot_nobles) for noble in eng_nobles: #print(noble.name) noble = go_home(board, noble, eng_type, scot_type) print("Sent " + noble.name + " home!") for noble in scot_nobles: if noble.name == "MORAY": if scot_type == 'comp': moray_utilities = moray_util(board, noble) comp_choice_moray = weighted_prob.weighted_prob( moray_utilities) if comp_choice_moray == 'stay': print("Moray stayed!") elif comp_choice_moray == 'home': go_home(board, noble, eng_type, scot_type) print("Sent Moray Home!") elif comp_choice_moray == 'disband': disband(board, noble) else: bool21 = True while bool21: user_decision = input( "Where do you want Moray to go? 's' for stay, 'h' for home, 'd' for disband " ) if user_decision == 's': if len(board.regions[find_location(board, noble).regionID]. blocks_present) <= board.regions[find_location( board, noble).regionID].castle_points: print("Moray stayed!") bool21 = False else: print("Moray cannot stay!") elif user_decision == 'h': go_home(board, noble, eng_type, scot_type) print("Sent Moray Home!") bool21 = False elif user_decision == 'd': disband(board, noble) bool21 = False else: print("invalid input!") else: go_home(board, noble, eng_type, scot_type) print("Sent " + noble.name + " home!") if eng_edward: if eng_type == 'comp': edward_utilities = edward_util(board, eng_edward[0], edward_prev_winter[0]) edward_choice = weighted_prob.weighted_prob(edward_utilities) if edward_choice == 'disband': disband(board, eng_edward[0]) elif edward_choice == 'stay': print("Edward I stayed!") else: if not edward_prev_winter[0]: add_to_location( board, eng_edward[0], choose_location( [find_location(board, eng_edward[0]), 'english pool'], 'ENGLAND', eng_type, scot_type, block)) edward_prev_winter[0] = True else: disband(board, eng_king[0]) if eng_king: disband(board, eng_king[0]) if scot_king: block = scot_king[0] if scot_type == 'comp': possible_regions_dict = dict() for region in board.regions: if (region.is_friendly('SCOTLAND') and region.cathedral ) or board.all_blocks[28] in region.blocks_present: possible_regions_dict[region.regionID] = 0.8 possible_regions_dict['disband'] = 0.01 scot_king_choice = weighted_prob.weighted_prob( possible_regions_dict) if scot_king_choice == 'disband': disband(board, block) else: add_to_location(board, block, board.regions[scot_king_choice]) else: possible_locations = [] for region in board.regions: if (region.is_friendly('SCOTLAND') ) and region.cathedral and len( region.blocks_present) <= region.castle_points: possible_locations.append(board.regions[region.regionID]) place = choose_location(possible_locations, block.allegiance, eng_type, scot_type, block) add_to_location(board, block, place) #print('ENGLAND ROSTER: ', end = ' ') #for block in board.eng_roster: #print(block.name, end = ' ') for brit in board.eng_roster: if brit.type == 'ARCHER' or brit.type == 'KNIGHT' and find_location( board, search.block_name_to_object( board.all_blocks, 'EDWARD')) != find_location(board, brit): disband(board, brit) for region in board.regions: computer_role = [] if eng_type == 'comp': computer_role.append('ENGLAND') if scot_type == 'comp': computer_role.append('SCOTLAND') if region.blocks_present and region.blocks_present[ 0].allegiance not in computer_role: display_blocks = [] if region.is_friendly('SCOTLAND'): if region.cathedral: castle_points = region.castle_points + 1 else: castle_points = region.castle_points else: if find_location( board, search.block_name_to_object(board.all_blocks, 'EDWARD')) == region: castle_points = 100 else: castle_points = region.castle_points for region_block in region.blocks_present: if type(region_block) != blocks.Noble: display_blocks.append(region_block) user_inputting = True while user_inputting: if display_blocks: have_to_move = len(region.blocks_present) - castle_points if have_to_move < 0: have_to_move = 0 print("\n\nCurrent region is " + region.name + ":\n") for i, blck in enumerate(display_blocks): print(blck.name, "(" + str(i) + ")") print("\n") user_input = input( "Choose a block to disband or move! \n You have to move at least " + str(have_to_move) + " blocks! 'Done' for done. \n>") try: if user_input.lower() == 'done': if have_to_move == 0: user_inputting = False else: print( "You are over castle limits! Continue disbanding!" ) else: user_block = display_blocks[int(user_input)] if user_block.type == "WALLACE": wallace_possible_locations = [ 'scottish pool', board.regions[18] ] add_to_location( board, block, choose_location(wallace_possible_locations, block.allegiance, eng_type, scot_type, block)) display_blocks.remove(user_block) else: disband(board, user_block) display_blocks.remove(user_block) except (ValueError, IndexError): print("That is not a valid option!") else: print("No blocks to move from " + region.name) user_inputting = False elif region.blocks_present and region.blocks_present[ 0].allegiance in computer_role: display_blocks = [] for region_block in region.blocks_present: if type(region_block) != blocks.Noble: display_blocks.append(region_block) if display_blocks: disbanding_utilities, have_to_move = disband_block_util( board, region) computer_choices = [] for i in range(have_to_move): computer_choice = weighted_prob.weighted_prob( disbanding_utilities) del disbanding_utilities[computer_choice] computer_choices.append(computer_choice) for computer_choice in computer_choices: computer_block = search.block_id_to_object( board.all_blocks, computer_choice) if computer_block.type == "WALLACE": wallace_possible_locations = [ 'scottish pool', board.regions[18] ] add_to_location( board, computer_block, choose_location(wallace_possible_locations, block.allegiance, eng_type, scot_type, block)) else: disband(board, computer_block) levy(board)
def disband_block_util(board, region): ''' used when all important pieces have moved for winter checks to see if castle limits are infringed takes a player object,board object, and a region object returns a utility dictionary where more utility means higher chance of disbanding. also returns the number of keys that need to be returned by the chancing function. ''' util_dict = {} display_blocks = [] if region.is_friendly('SCOTLAND'): if region.cathedral: castle_points = region.castle_points + 1 else: castle_points = region.castle_points else: if find_location( board, search.block_name_to_object(board.all_blocks, 'EDWARD')) == region: castle_points = 100 else: castle_points = region.castle_points for region_block in region.blocks_present: if type(region_block) != blocks.Noble: display_blocks.append(region_block) have_to_move = len(region.blocks_present) - castle_points if have_to_move < 0: have_to_move = 0 if have_to_move > 0: for block in display_blocks: util_dict[block.blockID] = 0 for i in range(block.attack_strength - block.current_strength): util_dict[block.blockID] += 1 if block.attack_letter == 'A': util_dict[block.blockID] += 1 elif block.attack_letter == 'B': util_dict[block.blockID] += 2 elif block.attack_letter == 'C': util_dict[block.blockID] += 3 return util_dict, have_to_move
def go_home(board, noble, eng_type, scot_type): ''' takes a noble from the location that they are at and then transports them home. also takes board object and the side that the computer is playing if there is more than 1 home location, it randomly picks one. changes allegiance based on who controls home area ''' if type(noble.home_location) == int: print(noble.name) if noble.home_location == find_location(board, noble).regionID: print(noble.name + " is already home.") elif not board.regions[ noble.home_location].blocks_present or board.regions[ noble.home_location].blocks_present[ 0].allegiance == noble.allegiance: print(noble.name) start = find_location(board, noble).regionID board.remove_from_region(noble, start) board.regions[noble.home_location].blocks_present.append(noble) else: noble.allegiance = board.regions[ noble.home_location].blocks_present[0].allegiance print(noble.name + '\'s allegiance was changed to ' + board.regions[ noble.home_location].blocks_present[0].allegiance) start = find_location(board, noble).regionID board.remove_from_region(noble, start) board.regions[noble.home_location].blocks_present.append(noble) else: possible_locations = [] new_locations = [] for home in noble.home_location: new_locations.append(home) if not board.regions[home].blocks_present or board.regions[ home].blocks_present[0].allegiance == noble.allegiance: possible_locations.append(board.regions[home]) else: if not possible_locations: print(noble.name + ' changes their allegiance') if noble.allegiance == "SCOTLAND": noble.allegiance == "ENGLAND" board.scot_roster.remove(noble) board.eng_roster.append(noble) else: noble.allegiance == "SCOTLAND" board.eng_roster.remove(noble) board.scot_roster.append(noble) print( 'IN THE BOARD NOBLE ALLEGIANCE IS: ', search.block_name_to_object(board.all_blocks, noble.name)) noble_choice = search.region_id_to_object( board, choose_location(new_locations, noble.allegiance, eng_type, scot_type, noble)) print(noble.name + ' went home to ' + board.regions[noble_choice.regionID].name) add_to_location(board, noble, noble_choice) else: noble_new_home = choose_location(possible_locations, noble.allegiance, eng_type, scot_type, noble) #add_to_location(board,noble,noble_new_home) current_loca = find_location(board, noble) board.remove_from_region(noble, current_loca.regionID) board.regions[noble_new_home.regionID].blocks_present.append( noble) return noble
def pil_execution(board, position, role, pil_data): print(role) if position == 'comp': """ #loop through regions to make sure there is a region that it works in for region_controlled in board.get_controlled_regions(role): new_list = [] new_list.append(region_controlled.regionID) for neighbor_region in board.find_all_borders(new_list): if not neighbor_region.is_friendly(role) and not neighbor_region.is_neutral(): possible = True if possible: #make a list of possible opponent regions to be pillaged possible_pill_lst = [] for region in board.get_controlled_regions(role): new_list = [] new_list.append(region.regionID) for neighbor_region in board.find_all_borders(new_list): if not neighbor_region.is_friendly(role) and not neighbor_region.is_neutral() and neighbor_region not in possible_pill_lst: possible_pill_lst.append(neighbor_region) chosen_subtract_region = possible_pill_lst[random.randint(0, len(possible_pill_lst) - 1)] # pillage combat-style points_pillaged = 0 for x in range (0,2): highest_block_lst = combat.find_max_strength(chosen_subtract_region.blocks_present) if highest_block_lst: block = highest_block_lst[0] #strike once block.get_hurt(1) print(block.name + ' took one hit.') points_pillaged+=1 if block.is_dead(): print(block.name, 'goes to the pool') if role == 'SCOTLAND': board.eng_pool.append(block) board.eng_roster.remove(block) board.remove_from_region(block, chosen_subtract_region.regionID) elif role == 'ENGLAND': board.scot_pool.append(block) board.scot_roster.remove(block) board.remove_from_region(block, chosen_subtract_region.regionID) #make a list of possible owned regions to gain points possible_add_lst = [] new_list = [] new_list.append(chosen_subtract_region.regionID) for neighbor_region in board.find_all_borders(new_list): if neighbor_region.is_friendly(role): possible_add_lst.append(neighbor_region) #choose randomly from the list chosen_add_region = possible_add_lst[random.randint(0, len(possible_add_lst) - 1)] health_points = points_pillaged possible_add_block_list = [] #list for possible blocks to heal in chosen_add_region for block in chosen_add_region.blocks_present: possible_add_block_list.append(block) while health_points > 0: block = possible_add_block_list[random.randint(0, len(possible_add_block_list) - 1)] healing_points = random.randint(0, health_points) block.heal_until_full(healing_points) health_points -= healing_points print(block.name + ' was healed ' + str(healing_points) + ' points.') else: print('There are no possible regions in which to play this card.') """ #this is strategized, receives chosen region IDs from utility: region_to_pil_id = pil_data[0] region_to_heal_id = pil_data[1] region_to_pil = search.region_id_to_object(board, region_to_pil_id) region_to_heal = search.region_id_to_object(board, region_to_heal_id) #pillage combat style points_pillaged = 0 good_list = True for block in region_to_pil.blocks_present: if block.current_strength > 0: good_list = True else: good_list = False for x in range (0,2): if len(region_to_pil.blocks_present) != 0 and good_list: highest_block_lst = combat.find_max_strength(region_to_pil.blocks_present) else: highest_block_lst = False if highest_block_lst: block = highest_block_lst[0] #strike once block.get_hurt(1) print(block.name + ' took one hit.') points_pillaged+=1 if block.is_dead(): print(block.name + ' has been pillaged to death') board.kill_block(block, role) #use weighted prob to choose blocks to add pts to #for block in blocks_present block_val_dict = dict() #list for possible blocks to heal in chosen_add_region for block in region_to_heal.blocks_present: utility_value = 0.0000000001 if block.name == 'KING' and block.current_strength < block.attack_strength: utility_value += .5 #if block is wallace and he needs it elif block.name == 'WALLACE' and block.current_strength < block.attack_strength: utility_value += .4 #for england: #if block is edward and he needs it if block.name == 'EDWARD' and block.current_strength < block.attack_strength: utility_value += .45 #if block is hobelars and he needs it elif block.name == 'HOBELARS' and block.current_strength < block.attack_strength: utility_value += .3 #if block is type noble and he needs it elif type(block) == blocks.Noble and block.current_strength < block.attack_strength: utility_value += .25 #if block is below full health elif block.current_strength < block.attack_strength: utility_value += .1 block_val_dict[block.name] = utility_value #possible healing pts = # pts taken from other region health_points = points_pillaged while health_points > 0 and len(region_to_heal.blocks_present) > 0: #should i add something here so it doesn't choose the same blocks??? block_name = weighted_prob.weighted_prob(block_val_dict) healing_points = random.randint(0, health_points) block.heal_until_full(healing_points) health_points -= healing_points print(block.name + ' was healed ' + str(healing_points) + ' points.') elif position == 'opp': quitt = False #loop through regions to make sure there is a region that it works in for region_controlled in board.get_controlled_regions(role): new_list = [] new_list.append(region_controlled.regionID) for neighbor_region in board.find_all_borders(new_list): if not neighbor_region.is_friendly(role) and not neighbor_region.is_neutral(): possible = True if possible: #make a list of possible opponent regions to be pillaged possible_pill_lst = [] for region in board.get_controlled_regions(role): new_list = [] new_list.append(region.regionID) for neighbor_region in board.find_all_borders(new_list): if not neighbor_region.is_friendly(role) and not neighbor_region.is_neutral(): if not neighbor_region in possible_pill_lst: possible_pill_lst.append(neighbor_region) print('Possible pillaging regions: ') for region in possible_pill_lst: print(region.name) valid_region = False while not valid_region: chosen_subtract_region_name = input('Which of your opponent\'s regions would you like to remove points from? Enter a name or \'none\'\n>').upper() if chosen_subtract_region_name.lower() == 'none': quitt = True valid_region = True if quitt: valid_region = True else: chosen_subtract_region = search.region_name_to_object(board, chosen_subtract_region_name) if chosen_subtract_region in possible_pill_lst: valid_region = True else: print('Invalid region.') continue # pillage combat-style points_pillaged = 0 for x in range (0,2): highest_block_lst = combat.find_max_strength(chosen_subtract_region.blocks_present) if highest_block_lst: block = highest_block_lst[0] #strike once block.get_hurt(1) print(block.name + ' took one hit.') points_pillaged+=1 if block.is_dead(): board.kill_block(block, role) #make a list of possible owned regions to gain points possible_add_lst = [] new_list = [] new_list.append(chosen_subtract_region.regionID) for neighbor_region in board.find_all_borders(new_list): if neighbor_region.is_friendly(role): possible_add_lst.append(neighbor_region) print('Possible regions to add pillaged points to: ') for region in possible_add_lst: print(region.name) valid_region = False while not valid_region: chosen_add_region_name = input('Which of your regions would you like to add pillaged points to? Enter a name or \'none\'\n>').upper() if chosen_add_region_name.lower() == 'none': quitt = True valid_region = True if not quitt: chosen_add_region = search.region_name_to_object(board, chosen_add_region_name) if chosen_add_region in possible_add_lst: valid_region = True else: print('Invalid region.') continue health_points = points_pillaged possible_add_block_list = [] while health_points > 0: #list for possible blocks to heal in chosen_add_region for block in chosen_add_region.blocks_present: possible_add_block_list.append(block) print('Possible blocks to heal: ') for block in possible_add_block_list: print(block.name) print() valid_input = False while not valid_input: print('You have ', health_points, ' health points.') block_name = input('Which block would you like to heal? Enter a name or \'none\'\n>').upper() if block_name.lower() == 'none': quitt = True valid_input = True health_points = 0 #if player doesnt enter 'none' if not quitt: block = search.block_name_to_object(chosen_add_region.blocks_present, block_name) if block in possible_add_block_list: valid_input = True else: print('Invalid block.') continue valid_in = False while not valid_in: healing_points = input('How many points would you like to heal it? Enter an integer or \'none\'\n>') if healing_points.lower() == 'none': quitt = True valid_in = True if not quitt: if healing_points.isdigit(): healing_points = int(healing_points) if healing_points <= 0 or healing_points > health_points: print('You do not have that many healing points.') else: block.heal_until_full(healing_points) health_points -= healing_points print(block.name + ' was healed ' + str(healing_points) + ' points.') valid_in = True else: print('Invalid input.') else: print('There are no possible regions in which to play this card.')
def sea_execution(board, position, role): """ Receives current board, opp/comp, and scotland/england For computer: Finds a block in a friendly, coastal region and moves to a randomized friendly, coastal region For human: Receives input name of block, loops until valid block (in friendly, coastal territory) Receives input name of region, loops until valid region (friendly, coastal territory that block is not already in) Prints executed action """ quitt = False if position == 'comp': #print(1) #temporary for dumb AI #create and print a list of coastal, friendly regions where norse is not the ONLY one possible_region_list = [] #loops through list of friendly, coastal, not just Norse regions to append to a possible_region_list for region in board.get_controlled_regions(role): #print(2) coastal = False just_norse = False if region.coast: coastal = True if len(region.blocks_present) == 1 and region.blocks_present[0].name.upper() == 'NORSE': just_norse = True if coastal and not just_norse: possible_region_list.append(region) #loops through list of friendly, coastal regions to append to a possible_final_region_list possible_final_region_list = [] for region in board.get_controlled_regions(role): #print(3) if region.coast: possible_final_region_list.append(region) if len(possible_final_region_list) >= 2: #if you want to add in last-min strategy, do it here #random region from possible list england = board.regions[22] if england in possible_region_list: original_region = england else: original_region = possible_region_list[random.randint(0, len(possible_region_list) - 1)] #remove the original region from the possible end regions possible_final_region_list.remove(original_region) #possible_block_list #list of possible blocks to move (present in region) and not norse possible_block_list = [] for block in original_region.blocks_present: if block.name != 'NORSE': possible_block_list.append(block) move_block_list = [] blocks_moved = 0 #print(4) while blocks_moved < 2: #print(5) block = possible_block_list[random.randint(0, len(possible_block_list)-1)] #if it's not already on the list,append to move_block_list if block not in move_block_list: move_block_list.append(block) blocks_moved+=1 elif block in move_block_list and len(possible_block_list) == 1: blocks_moved+=1 else: print('neither condition was met so this is an infinite loop') #print(6) new_region = possible_final_region_list[random.randint(0, len(possible_final_region_list) - 1)] for block in move_block_list: board.add_to_location(block, new_region) print(block.name + ' moved from ' + original_region.name + ' to ' + new_region.name) else: print('There are not enough friendly regions with which to play this card.') #add in if it's not possible elif position == 'opp': possible_region_list = [] #loops through list of friendly, coastal, not just Norse regions to append to a possible_region_list for region in board.get_controlled_regions(role): coastal = False just_norse = False if region.coast: coastal = True if len(region.blocks_present) == 1 and region.blocks_present[0].name.upper() == 'NORSE': just_norse = True if coastal and not just_norse: possible_region_list.append(region) #loops through list of friendly, coastal regions to append to a possible_final_region_list possible_final_region_list = [] for region in board.get_controlled_regions(role): if region.coast: possible_final_region_list.append(region) if len(possible_final_region_list) >= 2: print('Possible origin regions:') for region in possible_region_list: print(region.name) #user input region, check if in possible list valid_region = False while not valid_region: original_region_name = input('What region would you like to move block(s) from? Enter a name or \'none\'.\n>').upper() if original_region_name != 'NONE': original_region = search.region_name_to_object(board, original_region_name) if original_region and original_region in possible_region_list: valid_region = True else: print('Invalid region.') else: quitt = True if not quitt: #remove the original region from the possible end regions possible_final_region_list.remove(original_region) #possible_block_list #list of possible blocks to move (present in region) and not norse possible_block_list = [] for block in original_region.blocks_present: if block.name != 'NORSE': possible_block_list.append(block) print('Possible blocks:') for block in possible_block_list: print(block.name) move_block_list = [] blocks_moved = 0 quittt = False block_name = '' while blocks_moved < 2 and not quittt: if block_name != 'NONE': valid_block = False while not valid_block: block_name = input('Which block would you like to move? Enter a name or \'none\'.\n>').upper() if block_name != 'NONE': block_to_move = search.block_name_to_object(possible_block_list, block_name) if block_to_move and block_to_move not in move_block_list: valid_block = True move_block_list.append(block_to_move) blocks_moved+=1 elif block in move_block_list and len(possible_block_list) == 1: blocks_moved=1 else: print('Invalid block.') continue else: valid_block = True if len(move_block_list) == 1: quittt = True quitt = False if len(move_block_list) > 0: print('Possible final regions:') for region in possible_final_region_list: print(region.name) #user input region, check if in possible list valid_region = False while not valid_region: new_region_name = input('What region would you like to move block(s) to? Enter a name or \'none\'.\n>').upper() if new_region_name != 'NONE': new_region = search.region_name_to_object(board, new_region_name) if new_region and new_region in possible_final_region_list: valid_region = True else: print('Invalid region.') continue else: valid_region = True quitt = True if not quitt: for block in move_block_list: board.add_to_location(block, new_region) print(block.name + ' moved from ' + original_region.name + ' to ' + new_region.name) else: print('There are not enough friendly coastal regions with which to play this card.')
def movement_execution(board, position, role, num_moves, truce=False): ''' ''' #this is where I started implementing move_utility (david) blocks_moved = [] if position == 'comp': i = 0 move_found = False while not move_found: i += 1 for j in range(num_moves): board = move_utility.good_move(board, num_moves, role, board.turn, truce, blocks_moved) move_found = True return None #input() #this is where I ended implementing move_utility (david) blocks_moved = [] picked_regions = [] move_pt = 1 #Pick n regions to while move_pt <= num_moves: #print("LOOPING AGAIN", move_pt, num_moves) #print(move_pt) #print (blocks_moved) focus_region = None try: if type(prev_paths) != tuple: prev_paths = [] else: prev_paths = list(prev_paths) except UnboundLocalError: prev_paths = list() passed = False #FIND A FOCUS REGION AND PATH if position == 'opp': user_region_input = '' #Loop until valid input for a focus region. valid_region_input = False while not valid_region_input: #Take a region name input, then try to convert it into a region object user_region_input = input('Which region would you like to focus your movement (or pass)?\n>').strip().upper() if user_region_input.lower() == 'pass': passed = True break focus_region = search.region_name_to_object(board, user_region_input) #If it's actually a region - valid region name if focus_region: #Inputted region is friendly and unique if focus_region in board.get_controlled_regions(role) and focus_region not in picked_regions: valid_region_input = True #Not friendly or neutral else: print('Invalid region. Please select a region you control that hasn\'t been moved.\n>') #Invalid region name else: print('Invalid input. Please input a valid region name.\n>') elif position == 'comp': if move_pt == 1: print('First move:') elif move_pt == 2: print('Second move:') elif move_pt == 3: print('Third move:') unique_region = False while not unique_region: friendly_regions = board.get_controlled_regions(role) if len(friendly_regions) > 1: rand_startID = random.randint(0, len(friendly_regions) - 1) else: rand_startID = 0 focus_region = friendly_regions[rand_startID] if focus_region not in picked_regions: unique_region = True print(role + ' selected ' + focus_region.name + ' to move from.') if passed: move_pt += 1 print(role + ' passed a movement point.') continue if focus_region.name != 'ENGLAND': picked_regions.append(focus_region) #assigns moveable count for contested regions if focus_region.is_contested(): num_enemy = len(focus_region.combat_dict['Attacking']) num_friends = len(focus_region.combat_dict['Defending']) moveable_count = num_friends - num_enemy #assigns moveable count for else: moveable_count = len(focus_region.blocks_present) if focus_region.name == 'ENGLAND' and num_moves > move_pt: print('Blocks in ENGLAND: each block requires one movement point.') for block in focus_region.blocks_present: print(block) if position == 'opp': valid_block = False while not valid_block and num_moves > move_pt: user_block_name = input("Choose a block to move (type 'done' if done): ").strip().upper() if user_block_name == "DONE": print (role + ' chose not to move a block and passed a movement point.') valid_block = True user_block = search.block_name_to_object(board.all_blocks, user_block_name) if user_block: if user_block in focus_region.blocks_present: if user_block not in blocks_moved: if board.move_block(user_block,focus_region.regionID,position='opp',prev_paths=prev_paths,is_truce=truce) == False: print ("That path was not valid!") else: #move_pt +=1 prev_paths = tuple(prev_paths) blocks_moved.append(user_block) valid_block = True else: print ("You have already moved that block this turn!") else: print ("That block is not in the region!") else: print ("Please input a valid block name!") elif position == 'comp': computer_choice = random.randint(0,100) if computer_choice == 0: print (role + " passes a Movement Point") else: for block in focus_region.blocks_present: if num_moves > move_pt: possible_paths = board.check_all_paths(block.movement_points,focus_region.regionID,block,all_paths = [], truce=truce) if possible_paths: print(role + ' may take the following paths from its selected region:') print(possible_paths) computer_path1 = random.choice(possible_paths) end = computer_path1[-1] board.move_block(block,focus_region.regionID,end=end,position='comp',prev_paths=prev_paths,is_truce=truce, path = computer_path1) move_pt +=1 else: print("Computer chosen region has no moves from its selected region.") else: count = 0 can_go_again = True for i in range(moveable_count): if position == 'opp' and can_go_again: valid_block = False while not valid_block: user_block_name = input("Choose a block to move (type 'done' if done): ").strip().upper() if user_block_name == "DONE": print (role + ' chose not to move a block and passed a movement point.') valid_block = True user_block = search.block_name_to_object(board.all_blocks, user_block_name) if user_block: if user_block in focus_region.blocks_present: if user_block not in blocks_moved: if board.move_block(user_block,focus_region.regionID,position='opp',prev_paths=prev_paths,is_truce=truce) == False: print ("That path was not valid!") else: if combat.find_location(board, user_block).name == 'ENGLAND': can_go_again = False picked_regions.remove(focus_region) blocks_moved.append(user_block) valid_block = True else: print ("You have already moved that block this turn!") else: print ("That block is not in the region!") else: print ("Please input a valid block name!") elif position == 'comp' and can_go_again: #input('Computer Move Part 2') print('It is computer turn to make a move') computer_choice = random.randint(0,100) if computer_choice == 0: print ("Computer Passes a Movement Point") else: possible_paths_2 = list() block_index = i - count block = focus_region.blocks_present[block_index] if block not in blocks_moved: possible_paths_2 = board.check_all_paths(block.movement_points,focus_region.regionID,block,path=[], all_paths=[],truce=truce, role = role) #print("CHECK PATHS", board.check_all_paths(block.movement_points,focus_region.regionID,block,truce=truce)) if possible_paths_2: computer_paths_1 = copy.deepcopy(random.choice(possible_paths_2)) #print(possible_paths_2) end = computer_paths_1[-1] board.move_block(block,focus_region.regionID,end=end,position='comp',prev_paths=prev_paths,is_truce=truce, path = computer_paths_1) if board.regions[end].name == 'ENGLAND': prev_paths = tuple(prev_paths) can_go_again = False picked_regions.remove(focus_region) else: if type(prev_paths) == list: prev_paths.append(computer_paths_1) count+=1 blocks_moved.append(block) #print(can_go_again) move_pt+=1
def movement_execution(board, position, role, num_moves, truce=False): ''' ''' blocks_moved = [] picked_regions = [] computer_paths_1 = [] move_pt = 0 block_to_return = None #Pick n regions to while move_pt < num_moves: #add_to_total_string(move_pt) #add_to_total_string (blocks_moved) focus_region = None try: if type(prev_paths) != tuple: prev_paths = [] else: prev_paths = list(prev_paths) except UnboundLocalError: prev_paths = list() passed = False #FIND A FOCUS REGION AND PATH if position == 'opp': user_region_skip_input = '' #Loop until valid skip_input for a focus region. valid_region_skip_input = False while not valid_region_skip_input: #Take a region name skip_input, then try to convert it into a region object user_region_skip_input = skip_input('Which region would you like to focus your movement (or pass)?\n>').strip().upper() if user_region_skip_input.lower() == 'pass': passed = True break focus_region = search.region_name_to_object(board, user_region_skip_input) #If it's actually a region - valid region name if focus_region: #skip_inputted region is friendly and unique if focus_region in board.get_controlled_regions(role) and focus_region not in picked_regions: valid_region_skip_input = True #Not friendly or neutral else: add_to_total_string('Invalid region. Please select a region you control that hasn\'t been moved') #Invalid region name else: add_to_total_string('Invalid skip_input. Please skip_input a valid region name.') elif position == 'comp' and board.get_controlled_regions(role): skip_input('Computer Move Part 1') ### ###TEMPORARY ### #Get a random starting region unique_region = False while not unique_region: friendly_regions = board.get_controlled_regions(role) if len(friendly_regions) > 1: rand_startID = random.randint(0, len(friendly_regions) - 1) else: rand_startID = 0 focus_region = friendly_regions[rand_startID] if focus_region not in picked_regions: unique_region = True if focus_region == None: passed = True if passed: move_pt += 1 continue if focus_region.name != 'ENGLAND': picked_regions.append(focus_region) #assigns moveable count for contested regions #print(focus_region.is_contested()) if focus_region.is_contested(): num_enemy = len(focus_region.combat_dict['Attacking']) num_friends = len(focus_region.combat_dict['Defending']) moveable_count = num_friends - num_enemy #assigns moveable count for else: moveable_count = len(focus_region.blocks_present) if focus_region.name == 'ENGLAND' and num_moves > move_pt: add_to_total_string(focus_region.blocks_present) if position == 'opp': valid_block = False while not valid_block and num_moves > move_pt: user_block_name = skip_input("Choose a block to move (type 'done' if done): ").strip().upper() if user_block_name.lower() == "done": add_to_total_string ("You passed one movement point!") valid_block = True board_blocks = board.eng_roster + board.scot_roster user_block = search.block_name_to_object(board_blocks, user_block_name) if user_block: if user_block in focus_region.blocks_present: if user_block not in blocks_moved: if move_block(board, user_block,focus_region.regionID,position='opp',prev_paths=prev_paths,is_truce=truce) == False: add_to_total_string ("That path was not valid!") else: #move_pt +=1 prev_paths = tuple(prev_paths) blocks_moved.append(user_block) valid_block = True else: add_to_total_string ("You have already moved that block this turn!") else: add_to_total_string ("That block is not in the region!") else: add_to_total_string ("Please skip_input a valid block name!") elif position == 'comp': computer_choice = random.randint(0,100) if computer_choice == 0: pass else: for block in focus_region.blocks_present: if num_moves > move_pt: possible_paths = board.check_all_paths(block.movement_points,focus_region.regionID,block,all_paths = [], truce=truce) if possible_paths: add_to_total_string(possible_paths) computer_paths_1 = random.choice(possible_paths) end = computer_paths_1[-1] move_block(board, block,focus_region.regionID,end=end,position='comp',prev_paths=prev_paths,is_truce=truce) block_to_return = block move_pt +=1 else: pass else: count = 0 can_go_again = True for i in range(moveable_count): if position == 'opp' and can_go_again: valid_block = False while not valid_block: user_block_name = skip_input("Choose a block to move (type 'done' if done): ").strip().upper() if user_block_name.lower() == "done": add_to_total_string ("You passed one movement point!") valid_block = True board_blocks = board.eng_roster + board.scot_roster user_block = search.block_name_to_object(board_blocks, user_block_name) if user_block: if user_block in focus_region.blocks_present: if user_block not in blocks_moved: if move_block(board, user_block,focus_region.regionID,position='opp',prev_paths=prev_paths,is_truce=truce) == False: add_to_total_string ("That path was not valid!") else: if combat.find_location(board, user_block).name == 'ENGLAND': can_go_again = False picked_regions.remove(focus_region) blocks_moved.append(user_block) valid_block = True else: add_to_total_string ("You have already moved that block this turn!") else: add_to_total_string ("That block is not in the region!") else: add_to_total_string ("Please skip_input a valid block name!") elif position == 'comp' and can_go_again: skip_input('Computer Move Part 2') computer_choice = random.randint(0,100) if computer_choice == 0: pass else: possible_paths_2 = list() block_index = i - count block = focus_region.blocks_present[block_index] if block not in blocks_moved: possible_paths_2 = board.check_all_paths(block.movement_points,focus_region.regionID,block,path=[], all_paths=[],truce=truce, role = role) #add_to_total_string("CHECK PATHS", board.check_all_paths(block.movement_points,focus_region.regionID,block,truce=truce)) if possible_paths_2: computer_paths_1 = copy.deepcopy(random.choice(possible_paths_2)) #add_to_total_string(possible_paths_2) end = computer_paths_1[-1] move_block(board, block,focus_region.regionID,end=end,position='comp',prev_paths=prev_paths,is_truce=truce) block_to_return = block if board.regions[end].name == 'ENGLAND': prev_paths = tuple(prev_paths) can_go_again = False picked_regions.remove(focus_region) else: if type(prev_paths) == list: prev_paths.append(computer_paths_1) count+=1 blocks_moved.append(block) #add_to_total_string(move_pt) else: pass #add_to_total_string(can_go_again) move_pt+=1 return computer_paths_1, block_to_return