From 5247687659bef07724053b75186110b23dce2d41 Mon Sep 17 00:00:00 2001 From: georgelid06 Date: Mon, 17 Apr 2023 20:48:36 +0100 Subject: [PATCH] WORKING FIRST VERSION --- ai.py | 72 ++++++++--------------------------------------------- card.py | 13 ++-------- game.py | 5 ++-- location.py | 1 - main.py | 2 +- 5 files changed, 16 insertions(+), 77 deletions(-) diff --git a/ai.py b/ai.py index 74bc74b..f7a85ed 100644 --- a/ai.py +++ b/ai.py @@ -7,73 +7,21 @@ def __init__(self, game, player_number, all_cards): self.player_number = player_number + 1 self.location_powers = [0, 0, 0] self.turn_energy_spent = 0 - self.energy = 1 # Add this line to initialize energy + self.energy = 1 self.deck = self.draw_starting_deck(all_cards) self.hand = self.draw_starting_hand(self.deck) - def calculate_hawkeye_effect(self, card, location, turn): - hawkeye_cards = [c for c in location.cards if c.name == "Hawkeye" and c.turn_played == turn - 1 and not c.hawkeye_effect_applied and c.location == location.location_number] - if hawkeye_cards: - for hawkeye_card in hawkeye_cards: - if hawkeye_card.owner == card.owner: - return 2 - return 0 - - def evaluate_card_location_score(self, card, location, location_index): - if location is None: - return 0 - - score = card.power - - # Consider the Hawkeye effect when evaluating the score - hawkeye_power_bonus = self.calculate_hawkeye_effect(card, location, self.game.current_turn) - score += hawkeye_power_bonus - - # Consider other card abilities that affect power - if card.ability is not None: - if card.ability.ability_type == "On Play" or card.ability.ability_type == "On Reveal": - power_bonus = card.ability.effect(card, self.game, card.owner, location_index) - if power_bonus is not None and power_bonus > 0: - score += power_bonus - - # Calculate the opponent's total power at the location - opponent_total_power = location.calculate_total_power(1 - self.player_number) - - if opponent_total_power > 0: - score += (score - opponent_total_power) / opponent_total_power - - return score - - def choose_card_and_location(self): - def evaluate_combinations(remaining_energy, current_cards, current_locations, current_score, card_index): - if card_index >= len(self.hand): - return current_cards, current_locations, current_score - - card = self.hand[card_index] - card.owner = self.player_number # Add this line to assign the card owner before evaluation - - # Without the current card - best_cards, best_locations, best_score = evaluate_combinations(remaining_energy, current_cards, current_locations, current_score, card_index + 1) - - # With the current card (if there's enough energy) - if card.energy_cost <= remaining_energy: + valid_plays = [] + for card_index, card in enumerate(self.hand): + if card.energy_cost <= self.energy: for location_index, location in enumerate(self.game.locations): - score = self.evaluate_card_location_score(card, location, location_index) - new_total_score = current_score + score - new_cards, new_locations, new_score = evaluate_combinations(remaining_energy - card.energy_cost, current_cards + [card_index], current_locations + [location_index], new_total_score, card_index + 1) - - if new_score > best_score: - best_score = new_score - best_cards = new_cards - best_locations = new_locations - - return best_cards, best_locations, best_score - - best_card_indices, best_location_indices, _ = evaluate_combinations(self.energy, [], [], 0, 0) + if Location.can_play_card_at_location(card, location, self.game.current_turn, self.energy): + valid_plays.append((card_index, location_index)) - if best_card_indices and best_location_indices: - return best_card_indices, best_location_indices + if valid_plays: + chosen_card_index, chosen_location_index = random.choice(valid_plays) + return [chosen_card_index], [chosen_location_index] else: return None, None @@ -103,4 +51,4 @@ def draw_card(self): new_card = random.choice(self.deck) if len(self.hand) < 7: self.hand.append(new_card) - self.deck.remove(new_card) + self.deck.remove(new_card) \ No newline at end of file diff --git a/card.py b/card.py index 2c48839..a6e150d 100644 --- a/card.py +++ b/card.py @@ -14,8 +14,6 @@ def __init__(self, name, energy_cost, power, ability_description, ability=None): self.location_effect_applied = False # Add this flag self.hawkeye_effect_applied = False # Add this flag for Hawkeye cards only - - def __repr__(self): return f"{self.name} (Energy: {self.energy_cost}, Power: {self.power}, Ability: {self.ability_description})" @@ -24,7 +22,6 @@ def __init__(self, effect, ability_type): self.effect = effect self.ability_type = ability_type - def generate_all_cards(): # Define the card abilities/effects here def medusa_effect(card, game, card_owner, location_index): # Add location_index parameter @@ -40,20 +37,14 @@ def punisher_effect(card, game, card_owner, location_index): def sentinel_effect(card, game, card_owner, location_index): if card_owner is not None: - player = game.players[card_owner] # Get the player from the game object - sentinel_card = None - for c in player.deck: - if c.name == "Sentinel": - sentinel_card = c - break - + player = game.players[card.owner] if sentinel_card is not None: new_sentinel = Card(sentinel_card.name, sentinel_card.energy_cost, sentinel_card.power, sentinel_card.ability_description, sentinel_card.ability) player.hand.append(new_sentinel) def star_lord_effect(card, game, card_owner, location_index): - location = game.locations[card.location] + location = location_index opponent = 1 if card_owner == 0 else 0 if any(c.owner == opponent and c.turn_played == game.current_turn for c in location.cards): return 3 diff --git a/game.py b/game.py index adc5740..8a6e97a 100644 --- a/game.py +++ b/game.py @@ -129,7 +129,7 @@ def reveal_cards(self, player_number): location = self.locations[card.location] location_card = next((c for c in location.cards if c.owner == card.owner and c.name == card.name and c.location == card.location), None) if card.ability is not None and card.ability.ability_type == "On Reveal": - power_bonus = card.ability.effect(card, self, player_number - 1) + power_bonus = card.ability.effect(card, self, player_number - 1, location) if power_bonus is not None and power_bonus > 0: card.power += power_bonus # Update the location card's power value as well @@ -143,7 +143,7 @@ def apply_ongoing_abilities(self): for location in self.locations: for card in location.cards: if card.owner == player_number - 1 and card.ability is not None and card.ability.ability_type == "Ongoing": - card.ability.effect(card, self, player_number - 1) + card.ability.effect(card, self, player_number - 1, location) self.apply_location_effects(player_number) @@ -245,6 +245,7 @@ def play_game(self): for turn in range(6): # Loop through the 6 turns self.current_turn = turn + 1 print(f"Turn {self.current_turn}") + self.play_turn() self.apply_ongoing_abilities() self.end_of_turn() diff --git a/location.py b/location.py index 3c0467f..3a1e116 100644 --- a/location.py +++ b/location.py @@ -109,7 +109,6 @@ def murderworld_end_of_turn_three(location_index, game, current_turn): location = game.locations[location_index] location.cards.clear() - # Generate all locations all_locations = [ Location("Xandar", "Cards here have +1 Power.", xandar_effect), diff --git a/main.py b/main.py index e4e0462..4a22429 100644 --- a/main.py +++ b/main.py @@ -5,4 +5,4 @@ all_cards = generate_all_cards() all_locations = generate_all_locations() game = Game() -game.play_game() \ No newline at end of file +game.play_game()