forked from georgelid06/Marvel-Snap-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
67 lines (57 loc) · 2.26 KB
/
ai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import random
from location import Location
import copy
from loguru import logger
class AIPlayer:
def __init__(self, game, player_id, all_cards):
self.game = game
self.player_id = player_id
self.turn_energy_spent = 0
self.energy = 1
self.deck = self.draw_starting_deck(all_cards)
self.starting_deck = copy.deepcopy(self.deck)
self.discard_stack = []
self.hand = self.draw_starting_hand(self.deck)
self.played_cards = []
def choose_plays(self):
valid_plays = []
hand_ordered_by_energy_cost = sorted(
self.hand, key=lambda card: card.energy_cost
)
for card in hand_ordered_by_energy_cost:
if card.energy_cost <= self.energy:
for location in self.game.locations:
if location.can_play_card_at_location(
card, location, self.game.current_turn, self.energy
):
valid_plays.append((card, location.position))
self.energy -= card.energy_cost
return valid_plays
def draw_starting_deck(self, all_cards):
deck = random.sample(all_cards, 12)
for card in deck:
card.owner_id = self.player_id
return deck
def draw_starting_hand(self, deck):
quicksilver_card = next(
(card for card in deck if card.name == "Quicksilver"), None
)
if quicksilver_card:
deck.remove(quicksilver_card) # Remove Quicksilver from the deck
hand = random.sample(deck, 3) # Draw only 3 cards
for card in hand: # Add this loop to remove the 3 cards from the deck
deck.remove(card)
hand.append(quicksilver_card) # Add Quicksilver to the hand
logger.debug(f"Player {self.player_id} starts with Quicksilver")
else:
hand = random.sample(deck, 4) # Draw 4 cards
for card in hand:
deck.remove(card) # Remove drawn cards from the deck
return hand
def draw_card(self):
if not self.deck:
return
new_card = random.choice(self.deck)
if len(self.hand) < 7:
self.hand.append(new_card)
self.deck.remove(new_card)