Skip to content

Commit

Permalink
WORKING FIRST VERSION
Browse files Browse the repository at this point in the history
  • Loading branch information
georgelid06 committed Apr 17, 2023
1 parent e88a6ab commit 5247687
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 77 deletions.
72 changes: 10 additions & 62 deletions ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
13 changes: 2 additions & 11 deletions card.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"

Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)


Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
all_cards = generate_all_cards()
all_locations = generate_all_locations()
game = Game()
game.play_game()
game.play_game()

0 comments on commit 5247687

Please sign in to comment.